You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

451 line
10 KiB

  1. /* global YT */
  2. (function( window, settings ) {
  3. var NativeHandler, YouTubeHandler;
  4. window.wp = window.wp || {};
  5. // Fail gracefully in unsupported browsers.
  6. if ( ! ( 'addEventListener' in window ) ) {
  7. return;
  8. }
  9. /**
  10. * Trigger an event.
  11. *
  12. * @param {Element} target HTML element to dispatch the event on.
  13. * @param {string} name Event name.
  14. */
  15. function trigger( target, name ) {
  16. var evt;
  17. if ( 'function' === typeof window.Event ) {
  18. evt = new Event( name );
  19. } else {
  20. evt = document.createEvent( 'Event' );
  21. evt.initEvent( name, true, true );
  22. }
  23. target.dispatchEvent( evt );
  24. }
  25. /**
  26. * Create a custom header instance.
  27. *
  28. * @class CustomHeader
  29. */
  30. function CustomHeader() {
  31. this.handlers = {
  32. nativeVideo: new NativeHandler(),
  33. youtube: new YouTubeHandler()
  34. };
  35. }
  36. CustomHeader.prototype = {
  37. /**
  38. * Initalize the custom header.
  39. *
  40. * If the environment supports video, loops through registered handlers
  41. * until one is found that can handle the video.
  42. */
  43. initialize: function() {
  44. if ( this.supportsVideo() ) {
  45. for ( var id in this.handlers ) {
  46. var handler = this.handlers[ id ];
  47. if ( 'test' in handler && handler.test( settings ) ) {
  48. this.activeHandler = handler.initialize.call( handler, settings );
  49. // Dispatch custom event when the video is loaded.
  50. trigger( document, 'wp-custom-header-video-loaded' );
  51. break;
  52. }
  53. }
  54. }
  55. },
  56. /**
  57. * Determines if the current environment supports video.
  58. *
  59. * Themes and plugins can override this method to change the criteria.
  60. *
  61. * @return {boolean}
  62. */
  63. supportsVideo: function() {
  64. // Don't load video on small screens. @todo: consider bandwidth and other factors.
  65. if ( window.innerWidth < settings.minWidth || window.innerHeight < settings.minHeight ) {
  66. return false;
  67. }
  68. return true;
  69. },
  70. /**
  71. * Base handler for custom handlers to extend.
  72. *
  73. * @type {BaseHandler}
  74. */
  75. BaseVideoHandler: BaseHandler
  76. };
  77. /**
  78. * Create a video handler instance.
  79. *
  80. * @class BaseHandler
  81. */
  82. function BaseHandler() {}
  83. BaseHandler.prototype = {
  84. /**
  85. * Initialize the video handler.
  86. *
  87. * @param {object} settings Video settings.
  88. */
  89. initialize: function( settings ) {
  90. var handler = this,
  91. button = document.createElement( 'button' );
  92. this.settings = settings;
  93. this.container = document.getElementById( 'wp-custom-header' );
  94. this.button = button;
  95. button.setAttribute( 'type', 'button' );
  96. button.setAttribute( 'id', 'wp-custom-header-video-button' );
  97. button.setAttribute( 'class', 'wp-custom-header-video-button wp-custom-header-video-play' );
  98. button.innerHTML = settings.l10n.play;
  99. // Toggle video playback when the button is clicked.
  100. button.addEventListener( 'click', function() {
  101. if ( handler.isPaused() ) {
  102. handler.play();
  103. } else {
  104. handler.pause();
  105. }
  106. });
  107. // Update the button class and text when the video state changes.
  108. this.container.addEventListener( 'play', function() {
  109. button.className = 'wp-custom-header-video-button wp-custom-header-video-play';
  110. button.innerHTML = settings.l10n.pause;
  111. if ( 'a11y' in window.wp ) {
  112. window.wp.a11y.speak( settings.l10n.playSpeak);
  113. }
  114. });
  115. this.container.addEventListener( 'pause', function() {
  116. button.className = 'wp-custom-header-video-button wp-custom-header-video-pause';
  117. button.innerHTML = settings.l10n.play;
  118. if ( 'a11y' in window.wp ) {
  119. window.wp.a11y.speak( settings.l10n.pauseSpeak);
  120. }
  121. });
  122. this.ready();
  123. },
  124. /**
  125. * Ready method called after a handler is initialized.
  126. *
  127. * @abstract
  128. */
  129. ready: function() {},
  130. /**
  131. * Whether the video is paused.
  132. *
  133. * @abstract
  134. * @return {boolean}
  135. */
  136. isPaused: function() {},
  137. /**
  138. * Pause the video.
  139. *
  140. * @abstract
  141. */
  142. pause: function() {},
  143. /**
  144. * Play the video.
  145. *
  146. * @abstract
  147. */
  148. play: function() {},
  149. /**
  150. * Append a video node to the header container.
  151. *
  152. * @param {Element} node HTML element.
  153. */
  154. setVideo: function( node ) {
  155. var editShortcutNode,
  156. editShortcut = this.container.getElementsByClassName( 'customize-partial-edit-shortcut' );
  157. if ( editShortcut.length ) {
  158. editShortcutNode = this.container.removeChild( editShortcut[0] );
  159. }
  160. this.container.innerHTML = '';
  161. this.container.appendChild( node );
  162. if ( editShortcutNode ) {
  163. this.container.appendChild( editShortcutNode );
  164. }
  165. },
  166. /**
  167. * Show the video controls.
  168. *
  169. * Appends a play/pause button to header container.
  170. */
  171. showControls: function() {
  172. if ( ! this.container.contains( this.button ) ) {
  173. this.container.appendChild( this.button );
  174. }
  175. },
  176. /**
  177. * Whether the handler can process a video.
  178. *
  179. * @abstract
  180. * @param {object} settings Video settings.
  181. * @return {boolean}
  182. */
  183. test: function() {
  184. return false;
  185. },
  186. /**
  187. * Trigger an event on the header container.
  188. *
  189. * @param {string} name Event name.
  190. */
  191. trigger: function( name ) {
  192. trigger( this.container, name );
  193. }
  194. };
  195. /**
  196. * Create a custom handler.
  197. *
  198. * @param {object} protoProps Properties to apply to the prototype.
  199. * @return CustomHandler The subclass.
  200. */
  201. BaseHandler.extend = function( protoProps ) {
  202. var prop;
  203. function CustomHandler() {
  204. var result = BaseHandler.apply( this, arguments );
  205. return result;
  206. }
  207. CustomHandler.prototype = Object.create( BaseHandler.prototype );
  208. CustomHandler.prototype.constructor = CustomHandler;
  209. for ( prop in protoProps ) {
  210. CustomHandler.prototype[ prop ] = protoProps[ prop ];
  211. }
  212. return CustomHandler;
  213. };
  214. /**
  215. * Native video handler.
  216. *
  217. * @class NativeHandler
  218. */
  219. NativeHandler = BaseHandler.extend({
  220. /**
  221. * Whether the native handler supports a video.
  222. *
  223. * @param {object} settings Video settings.
  224. * @return {boolean}
  225. */
  226. test: function( settings ) {
  227. var video = document.createElement( 'video' );
  228. return video.canPlayType( settings.mimeType );
  229. },
  230. /**
  231. * Set up a native video element.
  232. */
  233. ready: function() {
  234. var handler = this,
  235. video = document.createElement( 'video' );
  236. video.id = 'wp-custom-header-video';
  237. video.autoplay = 'autoplay';
  238. video.loop = 'loop';
  239. video.muted = 'muted';
  240. video.width = this.settings.width;
  241. video.height = this.settings.height;
  242. video.addEventListener( 'play', function() {
  243. handler.trigger( 'play' );
  244. });
  245. video.addEventListener( 'pause', function() {
  246. handler.trigger( 'pause' );
  247. });
  248. video.addEventListener( 'canplay', function() {
  249. handler.showControls();
  250. });
  251. this.video = video;
  252. handler.setVideo( video );
  253. video.src = this.settings.videoUrl;
  254. },
  255. /**
  256. * Whether the video is paused.
  257. *
  258. * @return {boolean}
  259. */
  260. isPaused: function() {
  261. return this.video.paused;
  262. },
  263. /**
  264. * Pause the video.
  265. */
  266. pause: function() {
  267. this.video.pause();
  268. },
  269. /**
  270. * Play the video.
  271. */
  272. play: function() {
  273. this.video.play();
  274. }
  275. });
  276. /**
  277. * YouTube video handler.
  278. *
  279. * @class YouTubeHandler
  280. */
  281. YouTubeHandler = BaseHandler.extend({
  282. /**
  283. * Whether the handler supports a video.
  284. *
  285. * @param {object} settings Video settings.
  286. * @return {boolean}
  287. */
  288. test: function( settings ) {
  289. return 'video/x-youtube' === settings.mimeType;
  290. },
  291. /**
  292. * Set up a YouTube iframe.
  293. *
  294. * Loads the YouTube IFrame API if the 'YT' global doesn't exist.
  295. */
  296. ready: function() {
  297. var handler = this;
  298. if ( 'YT' in window ) {
  299. YT.ready( handler.loadVideo.bind( handler ) );
  300. } else {
  301. var tag = document.createElement( 'script' );
  302. tag.src = 'https://www.youtube.com/iframe_api';
  303. tag.onload = function () {
  304. YT.ready( handler.loadVideo.bind( handler ) );
  305. };
  306. document.getElementsByTagName( 'head' )[0].appendChild( tag );
  307. }
  308. },
  309. /**
  310. * Load a YouTube video.
  311. */
  312. loadVideo: function() {
  313. var handler = this,
  314. video = document.createElement( 'div' ),
  315. // @link http://stackoverflow.com/a/27728417
  316. VIDEO_ID_REGEX = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
  317. video.id = 'wp-custom-header-video';
  318. handler.setVideo( video );
  319. handler.player = new YT.Player( video, {
  320. height: this.settings.height,
  321. width: this.settings.width,
  322. videoId: this.settings.videoUrl.match( VIDEO_ID_REGEX )[1],
  323. events: {
  324. onReady: function( e ) {
  325. e.target.mute();
  326. handler.showControls();
  327. },
  328. onStateChange: function( e ) {
  329. if ( YT.PlayerState.PLAYING === e.data ) {
  330. handler.trigger( 'play' );
  331. } else if ( YT.PlayerState.PAUSED === e.data ) {
  332. handler.trigger( 'pause' );
  333. } else if ( YT.PlayerState.ENDED === e.data ) {
  334. e.target.playVideo();
  335. }
  336. }
  337. },
  338. playerVars: {
  339. autoplay: 1,
  340. controls: 0,
  341. disablekb: 1,
  342. fs: 0,
  343. iv_load_policy: 3,
  344. loop: 1,
  345. modestbranding: 1,
  346. playsinline: 1,
  347. rel: 0,
  348. showinfo: 0
  349. }
  350. });
  351. },
  352. /**
  353. * Whether the video is paused.
  354. *
  355. * @return {boolean}
  356. */
  357. isPaused: function() {
  358. return YT.PlayerState.PAUSED === this.player.getPlayerState();
  359. },
  360. /**
  361. * Pause the video.
  362. */
  363. pause: function() {
  364. this.player.pauseVideo();
  365. },
  366. /**
  367. * Play the video.
  368. */
  369. play: function() {
  370. this.player.playVideo();
  371. }
  372. });
  373. // Initialize the custom header when the DOM is ready.
  374. window.wp.customHeader = new CustomHeader();
  375. document.addEventListener( 'DOMContentLoaded', window.wp.customHeader.initialize.bind( window.wp.customHeader ), false );
  376. // Selective refresh support in the Customizer.
  377. if ( 'customize' in window.wp ) {
  378. window.wp.customize.selectiveRefresh.bind( 'render-partials-response', function( response ) {
  379. if ( 'custom_header_settings' in response ) {
  380. settings = response.custom_header_settings;
  381. }
  382. });
  383. window.wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
  384. if ( 'custom_header' === placement.partial.id ) {
  385. window.wp.customHeader.initialize();
  386. }
  387. });
  388. }
  389. })( window, window._wpCustomHeaderSettings || {} );