|
| 1 | +/** |
| 2 | + * Copyright 2015 The AMP HTML Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {isLayoutSizeDefined} from '../../../src/layout'; |
| 18 | +import {loadPromise} from '../../../src/event-helper'; |
| 19 | + |
| 20 | + |
| 21 | +class AmpBrightcove extends AMP.BaseElement { |
| 22 | + |
| 23 | + /** @override */ |
| 24 | + createdCallback() { |
| 25 | + this.preconnect.url('https://players.brightcove.net'); |
| 26 | + } |
| 27 | + |
| 28 | + /** @override */ |
| 29 | + isLayoutSupported(layout) { |
| 30 | + return isLayoutSizeDefined(layout); |
| 31 | + } |
| 32 | + |
| 33 | + /** @override */ |
| 34 | + layoutCallback() { |
| 35 | + const width = this.element.getAttribute('width'); |
| 36 | + const height = this.element.getAttribute('height'); |
| 37 | + const account = AMP.assert( |
| 38 | + this.element.getAttribute('data-account'), |
| 39 | + 'The data-account attribute is required for <amp-brightcove> %s', |
| 40 | + this.element); |
| 41 | + const playerid = (this.element.getAttribute('data-player-id') || 'default'); |
| 42 | + const embed = (this.element.getAttribute('data-embed') || 'default'); |
| 43 | + const iframe = document.createElement('iframe'); |
| 44 | + let src = 'https://players.brightcove.net/' + encodeURIComponent(account) + '/' + encodeURIComponent(playerid) + '_' + encodeURIComponent(embed) + '/index.html'; |
| 45 | + if (this.element.getAttribute('data-playlist-id')) { |
| 46 | + src += '?playlistId='; |
| 47 | + src += this.encodeId_(this.element.getAttribute('data-playlist-id')); |
| 48 | + } else if (this.element.getAttribute('data-video-id')) { |
| 49 | + src += '?videoId='; |
| 50 | + src += this.encodeId_(this.element.getAttribute('data-video-id')); |
| 51 | + } |
| 52 | + iframe.setAttribute('frameborder', '0'); |
| 53 | + iframe.setAttribute('allowfullscreen', 'true'); |
| 54 | + iframe.src = src; |
| 55 | + this.applyFillContent(iframe); |
| 56 | + iframe.width = width; |
| 57 | + iframe.height = height; |
| 58 | + this.element.appendChild(iframe); |
| 59 | + /** @private {?Element} */ |
| 60 | + this.iframe_ = iframe; |
| 61 | + return loadPromise(iframe); |
| 62 | + } |
| 63 | + |
| 64 | + /** @private */ |
| 65 | + encodeId_(id) { |
| 66 | + /* id is either a Brightcove-assigned id, or a customer-generated reference id. |
| 67 | + reference ids are prefixed 'ref:' and the colon must be preserved unencoded */ |
| 68 | + if (id.substring(0,4) === 'ref:') { |
| 69 | + return 'ref:' + encodeURIComponent(id.substring(4)); |
| 70 | + } else { |
| 71 | + return encodeURIComponent(id); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** @override */ |
| 76 | + documentInactiveCallback() { |
| 77 | + /* |
| 78 | + This stops playback with the postMessage API. |
| 79 | + Add this script to the player in the player configuration in the Studio |
| 80 | + or via the Player Management API: |
| 81 | +
|
| 82 | + http://players.brightcove.net/906043040001/plugins/postmessage_pause.js |
| 83 | +
|
| 84 | + It's not a 'real' video.js plugin, just a plain script running in |
| 85 | + the iframe so needs no configuration options. |
| 86 | + */ |
| 87 | + this.iframe_.contentWindow./*OK*/postMessage('pause', 'https://players.brightcove.net'); |
| 88 | + return false; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +AMP.registerElement('amp-brightcove', AmpBrightcove); |
0 commit comments