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.
 
 
 
 
 

937 lines
24 KiB

  1. /* global tinymce */
  2. /*
  3. * The TinyMCE view API.
  4. *
  5. * Note: this API is "experimental" meaning that it will probably change
  6. * in the next few releases based on feedback from 3.9.0.
  7. * If you decide to use it, please follow the development closely.
  8. *
  9. * Diagram
  10. *
  11. * |- registered view constructor (type)
  12. * | |- view instance (unique text)
  13. * | | |- editor 1
  14. * | | | |- view node
  15. * | | | |- view node
  16. * | | | |- ...
  17. * | | |- editor 2
  18. * | | | |- ...
  19. * | |- view instance
  20. * | | |- ...
  21. * |- registered view
  22. * | |- ...
  23. */
  24. ( function( window, wp, shortcode, $ ) {
  25. 'use strict';
  26. var views = {},
  27. instances = {};
  28. wp.mce = wp.mce || {};
  29. /**
  30. * wp.mce.views
  31. *
  32. * A set of utilities that simplifies adding custom UI within a TinyMCE editor.
  33. * At its core, it serves as a series of converters, transforming text to a
  34. * custom UI, and back again.
  35. */
  36. wp.mce.views = {
  37. /**
  38. * Registers a new view type.
  39. *
  40. * @param {String} type The view type.
  41. * @param {Object} extend An object to extend wp.mce.View.prototype with.
  42. */
  43. register: function( type, extend ) {
  44. views[ type ] = wp.mce.View.extend( _.extend( extend, { type: type } ) );
  45. },
  46. /**
  47. * Unregisters a view type.
  48. *
  49. * @param {String} type The view type.
  50. */
  51. unregister: function( type ) {
  52. delete views[ type ];
  53. },
  54. /**
  55. * Returns the settings of a view type.
  56. *
  57. * @param {String} type The view type.
  58. *
  59. * @return {Function} The view constructor.
  60. */
  61. get: function( type ) {
  62. return views[ type ];
  63. },
  64. /**
  65. * Unbinds all view nodes.
  66. * Runs before removing all view nodes from the DOM.
  67. */
  68. unbind: function() {
  69. _.each( instances, function( instance ) {
  70. instance.unbind();
  71. } );
  72. },
  73. /**
  74. * Scans a given string for each view's pattern,
  75. * replacing any matches with markers,
  76. * and creates a new instance for every match.
  77. *
  78. * @param {String} content The string to scan.
  79. *
  80. * @return {String} The string with markers.
  81. */
  82. setMarkers: function( content ) {
  83. var pieces = [ { content: content } ],
  84. self = this,
  85. instance, current;
  86. _.each( views, function( view, type ) {
  87. current = pieces.slice();
  88. pieces = [];
  89. _.each( current, function( piece ) {
  90. var remaining = piece.content,
  91. result, text;
  92. // Ignore processed pieces, but retain their location.
  93. if ( piece.processed ) {
  94. pieces.push( piece );
  95. return;
  96. }
  97. // Iterate through the string progressively matching views
  98. // and slicing the string as we go.
  99. while ( remaining && ( result = view.prototype.match( remaining ) ) ) {
  100. // Any text before the match becomes an unprocessed piece.
  101. if ( result.index ) {
  102. pieces.push( { content: remaining.substring( 0, result.index ) } );
  103. }
  104. instance = self.createInstance( type, result.content, result.options );
  105. text = instance.loader ? '.' : instance.text;
  106. // Add the processed piece for the match.
  107. pieces.push( {
  108. content: instance.ignore ? text : '<p data-wpview-marker="' + instance.encodedText + '">' + text + '</p>',
  109. processed: true
  110. } );
  111. // Update the remaining content.
  112. remaining = remaining.slice( result.index + result.content.length );
  113. }
  114. // There are no additional matches.
  115. // If any content remains, add it as an unprocessed piece.
  116. if ( remaining ) {
  117. pieces.push( { content: remaining } );
  118. }
  119. } );
  120. } );
  121. content = _.pluck( pieces, 'content' ).join( '' );
  122. return content.replace( /<p>\s*<p data-wpview-marker=/g, '<p data-wpview-marker=' ).replace( /<\/p>\s*<\/p>/g, '</p>' );
  123. },
  124. /**
  125. * Create a view instance.
  126. *
  127. * @param {String} type The view type.
  128. * @param {String} text The textual representation of the view.
  129. * @param {Object} options Options.
  130. * @param {Boolean} force Recreate the instance. Optional.
  131. *
  132. * @return {wp.mce.View} The view instance.
  133. */
  134. createInstance: function( type, text, options, force ) {
  135. var View = this.get( type ),
  136. encodedText,
  137. instance;
  138. text = tinymce.DOM.decode( text );
  139. if ( text.indexOf( '[' ) !== -1 && text.indexOf( ']' ) !== -1 ) {
  140. // Looks like a shortcode? Remove any line breaks from inside of shortcodes
  141. // or autop will replace them with <p> and <br> later and the string won't match.
  142. text = text.replace( /\[[^\]]+\]/g, function( match ) {
  143. return match.replace( /[\r\n]/g, '' );
  144. });
  145. }
  146. if ( ! force ) {
  147. instance = this.getInstance( text );
  148. if ( instance ) {
  149. return instance;
  150. }
  151. }
  152. encodedText = encodeURIComponent( text );
  153. options = _.extend( options || {}, {
  154. text: text,
  155. encodedText: encodedText
  156. } );
  157. return instances[ encodedText ] = new View( options );
  158. },
  159. /**
  160. * Get a view instance.
  161. *
  162. * @param {(String|HTMLElement)} object The textual representation of the view or the view node.
  163. *
  164. * @return {wp.mce.View} The view instance or undefined.
  165. */
  166. getInstance: function( object ) {
  167. if ( typeof object === 'string' ) {
  168. return instances[ encodeURIComponent( object ) ];
  169. }
  170. return instances[ $( object ).attr( 'data-wpview-text' ) ];
  171. },
  172. /**
  173. * Given a view node, get the view's text.
  174. *
  175. * @param {HTMLElement} node The view node.
  176. *
  177. * @return {String} The textual representation of the view.
  178. */
  179. getText: function( node ) {
  180. return decodeURIComponent( $( node ).attr( 'data-wpview-text' ) || '' );
  181. },
  182. /**
  183. * Renders all view nodes that are not yet rendered.
  184. *
  185. * @param {Boolean} force Rerender all view nodes.
  186. */
  187. render: function( force ) {
  188. _.each( instances, function( instance ) {
  189. instance.render( null, force );
  190. } );
  191. },
  192. /**
  193. * Update the text of a given view node.
  194. *
  195. * @param {String} text The new text.
  196. * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
  197. * @param {HTMLElement} node The view node to update.
  198. * @param {Boolean} force Recreate the instance. Optional.
  199. */
  200. update: function( text, editor, node, force ) {
  201. var instance = this.getInstance( node );
  202. if ( instance ) {
  203. instance.update( text, editor, node, force );
  204. }
  205. },
  206. /**
  207. * Renders any editing interface based on the view type.
  208. *
  209. * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
  210. * @param {HTMLElement} node The view node to edit.
  211. */
  212. edit: function( editor, node ) {
  213. var instance = this.getInstance( node );
  214. if ( instance && instance.edit ) {
  215. instance.edit( instance.text, function( text, force ) {
  216. instance.update( text, editor, node, force );
  217. } );
  218. }
  219. },
  220. /**
  221. * Remove a given view node from the DOM.
  222. *
  223. * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
  224. * @param {HTMLElement} node The view node to remove.
  225. */
  226. remove: function( editor, node ) {
  227. var instance = this.getInstance( node );
  228. if ( instance ) {
  229. instance.remove( editor, node );
  230. }
  231. }
  232. };
  233. /**
  234. * A Backbone-like View constructor intended for use when rendering a TinyMCE View.
  235. * The main difference is that the TinyMCE View is not tied to a particular DOM node.
  236. *
  237. * @param {Object} options Options.
  238. */
  239. wp.mce.View = function( options ) {
  240. _.extend( this, options );
  241. this.initialize();
  242. };
  243. wp.mce.View.extend = Backbone.View.extend;
  244. _.extend( wp.mce.View.prototype, {
  245. /**
  246. * The content.
  247. *
  248. * @type {*}
  249. */
  250. content: null,
  251. /**
  252. * Whether or not to display a loader.
  253. *
  254. * @type {Boolean}
  255. */
  256. loader: true,
  257. /**
  258. * Runs after the view instance is created.
  259. */
  260. initialize: function() {},
  261. /**
  262. * Returns the content to render in the view node.
  263. *
  264. * @return {*}
  265. */
  266. getContent: function() {
  267. return this.content;
  268. },
  269. /**
  270. * Renders all view nodes tied to this view instance that are not yet rendered.
  271. *
  272. * @param {String} content The content to render. Optional.
  273. * @param {Boolean} force Rerender all view nodes tied to this view instance. Optional.
  274. */
  275. render: function( content, force ) {
  276. if ( content != null ) {
  277. this.content = content;
  278. }
  279. content = this.getContent();
  280. // If there's nothing to render an no loader needs to be shown, stop.
  281. if ( ! this.loader && ! content ) {
  282. return;
  283. }
  284. // We're about to rerender all views of this instance, so unbind rendered views.
  285. force && this.unbind();
  286. // Replace any left over markers.
  287. this.replaceMarkers();
  288. if ( content ) {
  289. this.setContent( content, function( editor, node ) {
  290. $( node ).data( 'rendered', true );
  291. this.bindNode.call( this, editor, node );
  292. }, force ? null : false );
  293. } else {
  294. this.setLoader();
  295. }
  296. },
  297. /**
  298. * Binds a given node after its content is added to the DOM.
  299. */
  300. bindNode: function() {},
  301. /**
  302. * Unbinds a given node before its content is removed from the DOM.
  303. */
  304. unbindNode: function() {},
  305. /**
  306. * Unbinds all view nodes tied to this view instance.
  307. * Runs before their content is removed from the DOM.
  308. */
  309. unbind: function() {
  310. this.getNodes( function( editor, node ) {
  311. this.unbindNode.call( this, editor, node );
  312. }, true );
  313. },
  314. /**
  315. * Gets all the TinyMCE editor instances that support views.
  316. *
  317. * @param {Function} callback A callback.
  318. */
  319. getEditors: function( callback ) {
  320. _.each( tinymce.editors, function( editor ) {
  321. if ( editor.plugins.wpview ) {
  322. callback.call( this, editor );
  323. }
  324. }, this );
  325. },
  326. /**
  327. * Gets all view nodes tied to this view instance.
  328. *
  329. * @param {Function} callback A callback.
  330. * @param {Boolean} rendered Get (un)rendered view nodes. Optional.
  331. */
  332. getNodes: function( callback, rendered ) {
  333. this.getEditors( function( editor ) {
  334. var self = this;
  335. $( editor.getBody() )
  336. .find( '[data-wpview-text="' + self.encodedText + '"]' )
  337. .filter( function() {
  338. var data;
  339. if ( rendered == null ) {
  340. return true;
  341. }
  342. data = $( this ).data( 'rendered' ) === true;
  343. return rendered ? data : ! data;
  344. } )
  345. .each( function() {
  346. callback.call( self, editor, this, this /* back compat */ );
  347. } );
  348. } );
  349. },
  350. /**
  351. * Gets all marker nodes tied to this view instance.
  352. *
  353. * @param {Function} callback A callback.
  354. */
  355. getMarkers: function( callback ) {
  356. this.getEditors( function( editor ) {
  357. var self = this;
  358. $( editor.getBody() )
  359. .find( '[data-wpview-marker="' + this.encodedText + '"]' )
  360. .each( function() {
  361. callback.call( self, editor, this );
  362. } );
  363. } );
  364. },
  365. /**
  366. * Replaces all marker nodes tied to this view instance.
  367. */
  368. replaceMarkers: function() {
  369. this.getMarkers( function( editor, node ) {
  370. var $viewNode;
  371. if ( ! this.loader && $( node ).text() !== this.text ) {
  372. editor.dom.setAttrib( node, 'data-wpview-marker', null );
  373. return;
  374. }
  375. $viewNode = editor.$(
  376. '<div class="wpview wpview-wrap" data-wpview-text="' + this.encodedText + '" data-wpview-type="' + this.type + '" contenteditable="false"></div>'
  377. );
  378. editor.$( node ).replaceWith( $viewNode );
  379. } );
  380. },
  381. /**
  382. * Removes all marker nodes tied to this view instance.
  383. */
  384. removeMarkers: function() {
  385. this.getMarkers( function( editor, node ) {
  386. editor.dom.setAttrib( node, 'data-wpview-marker', null );
  387. } );
  388. },
  389. /**
  390. * Sets the content for all view nodes tied to this view instance.
  391. *
  392. * @param {*} content The content to set.
  393. * @param {Function} callback A callback. Optional.
  394. * @param {Boolean} rendered Only set for (un)rendered nodes. Optional.
  395. */
  396. setContent: function( content, callback, rendered ) {
  397. if ( _.isObject( content ) && content.body.indexOf( '<script' ) !== -1 ) {
  398. this.setIframes( content.head || '', content.body, callback, rendered );
  399. } else if ( _.isString( content ) && content.indexOf( '<script' ) !== -1 ) {
  400. this.setIframes( '', content, callback, rendered );
  401. } else {
  402. this.getNodes( function( editor, node ) {
  403. content = content.body || content;
  404. if ( content.indexOf( '<iframe' ) !== -1 ) {
  405. content += '<span class="mce-shim"></span>';
  406. }
  407. editor.undoManager.transact( function() {
  408. node.innerHTML = '';
  409. node.appendChild( _.isString( content ) ? editor.dom.createFragment( content ) : content );
  410. editor.dom.add( node, 'span', { 'class': 'wpview-end' } );
  411. } );
  412. callback && callback.call( this, editor, node );
  413. }, rendered );
  414. }
  415. },
  416. /**
  417. * Sets the content in an iframe for all view nodes tied to this view instance.
  418. *
  419. * @param {String} head HTML string to be added to the head of the document.
  420. * @param {String} body HTML string to be added to the body of the document.
  421. * @param {Function} callback A callback. Optional.
  422. * @param {Boolean} rendered Only set for (un)rendered nodes. Optional.
  423. */
  424. setIframes: function( head, body, callback, rendered ) {
  425. var self = this;
  426. this.getNodes( function( editor, node ) {
  427. var dom = editor.dom,
  428. styles = '',
  429. bodyClasses = editor.getBody().className || '',
  430. editorHead = editor.getDoc().getElementsByTagName( 'head' )[0],
  431. iframe, iframeWin, iframeDoc, MutationObserver, observer, i, block;
  432. tinymce.each( dom.$( 'link[rel="stylesheet"]', editorHead ), function( link ) {
  433. if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 &&
  434. link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) {
  435. styles += dom.getOuterHTML( link );
  436. }
  437. } );
  438. if ( self.iframeHeight ) {
  439. dom.add( node, 'span', {
  440. 'data-mce-bogus': 1,
  441. style: {
  442. display: 'block',
  443. width: '100%',
  444. height: self.iframeHeight
  445. }
  446. }, '\u200B' );
  447. }
  448. editor.undoManager.transact( function() {
  449. node.innerHTML = '';
  450. iframe = dom.add( node, 'iframe', {
  451. /* jshint scripturl: true */
  452. src: tinymce.Env.ie ? 'javascript:""' : '',
  453. frameBorder: '0',
  454. allowTransparency: 'true',
  455. scrolling: 'no',
  456. 'class': 'wpview-sandbox',
  457. style: {
  458. width: '100%',
  459. display: 'block'
  460. },
  461. height: self.iframeHeight
  462. } );
  463. dom.add( node, 'span', { 'class': 'mce-shim' } );
  464. dom.add( node, 'span', { 'class': 'wpview-end' } );
  465. } );
  466. // Bail if the iframe node is not attached to the DOM.
  467. // Happens when the view is dragged in the editor.
  468. // There is a browser restriction when iframes are moved in the DOM. They get emptied.
  469. // The iframe will be rerendered after dropping the view node at the new location.
  470. if ( ! iframe.contentWindow ) {
  471. return;
  472. }
  473. iframeWin = iframe.contentWindow;
  474. iframeDoc = iframeWin.document;
  475. iframeDoc.open();
  476. iframeDoc.write(
  477. '<!DOCTYPE html>' +
  478. '<html>' +
  479. '<head>' +
  480. '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
  481. head +
  482. styles +
  483. '<style>' +
  484. 'html {' +
  485. 'background: transparent;' +
  486. 'padding: 0;' +
  487. 'margin: 0;' +
  488. '}' +
  489. 'body#wpview-iframe-sandbox {' +
  490. 'background: transparent;' +
  491. 'padding: 1px 0 !important;' +
  492. 'margin: -1px 0 0 !important;' +
  493. '}' +
  494. 'body#wpview-iframe-sandbox:before,' +
  495. 'body#wpview-iframe-sandbox:after {' +
  496. 'display: none;' +
  497. 'content: "";' +
  498. '}' +
  499. '</style>' +
  500. '</head>' +
  501. '<body id="wpview-iframe-sandbox" class="' + bodyClasses + '">' +
  502. body +
  503. '</body>' +
  504. '</html>'
  505. );
  506. iframeDoc.close();
  507. function resize() {
  508. var $iframe;
  509. if ( block ) {
  510. return;
  511. }
  512. // Make sure the iframe still exists.
  513. if ( iframe.contentWindow ) {
  514. $iframe = $( iframe );
  515. self.iframeHeight = $( iframeDoc.body ).height();
  516. if ( $iframe.height() !== self.iframeHeight ) {
  517. $iframe.height( self.iframeHeight );
  518. editor.nodeChanged();
  519. }
  520. }
  521. }
  522. if ( self.iframeHeight ) {
  523. block = true;
  524. setTimeout( function() {
  525. block = false;
  526. resize();
  527. }, 3000 );
  528. }
  529. function reload() {
  530. $( node ).data( 'rendered', null );
  531. setTimeout( function() {
  532. wp.mce.views.render();
  533. } );
  534. }
  535. $( iframeWin ).on( 'load', resize ).on( 'unload', reload );
  536. MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver;
  537. if ( MutationObserver ) {
  538. observer = new MutationObserver( _.debounce( resize, 100 ) );
  539. observer.observe( iframeDoc.body, {
  540. attributes: true,
  541. childList: true,
  542. subtree: true
  543. } );
  544. } else {
  545. for ( i = 1; i < 6; i++ ) {
  546. setTimeout( resize, i * 700 );
  547. }
  548. }
  549. callback && callback.call( self, editor, node );
  550. }, rendered );
  551. },
  552. /**
  553. * Sets a loader for all view nodes tied to this view instance.
  554. */
  555. setLoader: function( dashicon ) {
  556. this.setContent(
  557. '<div class="loading-placeholder">' +
  558. '<div class="dashicons dashicons-' + ( dashicon || 'admin-media' ) + '"></div>' +
  559. '<div class="wpview-loading"><ins></ins></div>' +
  560. '</div>'
  561. );
  562. },
  563. /**
  564. * Sets an error for all view nodes tied to this view instance.
  565. *
  566. * @param {String} message The error message to set.
  567. * @param {String} dashicon A dashicon ID. Optional. {@link https://developer.wordpress.org/resource/dashicons/}
  568. */
  569. setError: function( message, dashicon ) {
  570. this.setContent(
  571. '<div class="wpview-error">' +
  572. '<div class="dashicons dashicons-' + ( dashicon || 'no' ) + '"></div>' +
  573. '<p>' + message + '</p>' +
  574. '</div>'
  575. );
  576. },
  577. /**
  578. * Tries to find a text match in a given string.
  579. *
  580. * @param {String} content The string to scan.
  581. *
  582. * @return {Object}
  583. */
  584. match: function( content ) {
  585. var match = shortcode.next( this.type, content );
  586. if ( match ) {
  587. return {
  588. index: match.index,
  589. content: match.content,
  590. options: {
  591. shortcode: match.shortcode
  592. }
  593. };
  594. }
  595. },
  596. /**
  597. * Update the text of a given view node.
  598. *
  599. * @param {String} text The new text.
  600. * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
  601. * @param {HTMLElement} node The view node to update.
  602. * @param {Boolean} force Recreate the instance. Optional.
  603. */
  604. update: function( text, editor, node, force ) {
  605. _.find( views, function( view, type ) {
  606. var match = view.prototype.match( text );
  607. if ( match ) {
  608. $( node ).data( 'rendered', false );
  609. editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) );
  610. wp.mce.views.createInstance( type, text, match.options, force ).render();
  611. editor.focus();
  612. return true;
  613. }
  614. } );
  615. },
  616. /**
  617. * Remove a given view node from the DOM.
  618. *
  619. * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
  620. * @param {HTMLElement} node The view node to remove.
  621. */
  622. remove: function( editor, node ) {
  623. this.unbindNode.call( this, editor, node );
  624. editor.dom.remove( node );
  625. editor.focus();
  626. }
  627. } );
  628. } )( window, window.wp, window.wp.shortcode, window.jQuery );
  629. /*
  630. * The WordPress core TinyMCE views.
  631. * Views for the gallery, audio, video, playlist and embed shortcodes,
  632. * and a view for embeddable URLs.
  633. */
  634. ( function( window, views, media, $ ) {
  635. var base, gallery, av, embed,
  636. schema, parser, serializer;
  637. function verifyHTML( string ) {
  638. var settings = {};
  639. if ( ! window.tinymce ) {
  640. return string.replace( /<[^>]+>/g, '' );
  641. }
  642. if ( ! string || ( string.indexOf( '<' ) === -1 && string.indexOf( '>' ) === -1 ) ) {
  643. return string;
  644. }
  645. schema = schema || new window.tinymce.html.Schema( settings );
  646. parser = parser || new window.tinymce.html.DomParser( settings, schema );
  647. serializer = serializer || new window.tinymce.html.Serializer( settings, schema );
  648. return serializer.serialize( parser.parse( string, { forced_root_block: false } ) );
  649. }
  650. base = {
  651. state: [],
  652. edit: function( text, update ) {
  653. var type = this.type,
  654. frame = media[ type ].edit( text );
  655. this.pausePlayers && this.pausePlayers();
  656. _.each( this.state, function( state ) {
  657. frame.state( state ).on( 'update', function( selection ) {
  658. update( media[ type ].shortcode( selection ).string(), type === 'gallery' );
  659. } );
  660. } );
  661. frame.on( 'close', function() {
  662. frame.detach();
  663. } );
  664. frame.open();
  665. }
  666. };
  667. gallery = _.extend( {}, base, {
  668. state: [ 'gallery-edit' ],
  669. template: media.template( 'editor-gallery' ),
  670. initialize: function() {
  671. var attachments = media.gallery.attachments( this.shortcode, media.view.settings.post.id ),
  672. attrs = this.shortcode.attrs.named,
  673. self = this;
  674. attachments.more()
  675. .done( function() {
  676. attachments = attachments.toJSON();
  677. _.each( attachments, function( attachment ) {
  678. if ( attachment.sizes ) {
  679. if ( attrs.size && attachment.sizes[ attrs.size ] ) {
  680. attachment.thumbnail = attachment.sizes[ attrs.size ];
  681. } else if ( attachment.sizes.thumbnail ) {
  682. attachment.thumbnail = attachment.sizes.thumbnail;
  683. } else if ( attachment.sizes.full ) {
  684. attachment.thumbnail = attachment.sizes.full;
  685. }
  686. }
  687. } );
  688. self.render( self.template( {
  689. verifyHTML: verifyHTML,
  690. attachments: attachments,
  691. columns: attrs.columns ? parseInt( attrs.columns, 10 ) : media.galleryDefaults.columns
  692. } ) );
  693. } )
  694. .fail( function( jqXHR, textStatus ) {
  695. self.setError( textStatus );
  696. } );
  697. }
  698. } );
  699. av = _.extend( {}, base, {
  700. action: 'parse-media-shortcode',
  701. initialize: function() {
  702. var self = this;
  703. if ( this.url ) {
  704. this.loader = false;
  705. this.shortcode = media.embed.shortcode( {
  706. url: this.text
  707. } );
  708. }
  709. wp.ajax.post( this.action, {
  710. post_ID: media.view.settings.post.id,
  711. type: this.shortcode.tag,
  712. shortcode: this.shortcode.string()
  713. } )
  714. .done( function( response ) {
  715. self.render( response );
  716. } )
  717. .fail( function( response ) {
  718. if ( self.url ) {
  719. self.ignore = true;
  720. self.removeMarkers();
  721. } else {
  722. self.setError( response.message || response.statusText, 'admin-media' );
  723. }
  724. } );
  725. this.getEditors( function( editor ) {
  726. editor.on( 'wpview-selected', function() {
  727. self.pausePlayers();
  728. } );
  729. } );
  730. },
  731. pausePlayers: function() {
  732. this.getNodes( function( editor, node, content ) {
  733. var win = $( 'iframe.wpview-sandbox', content ).get( 0 );
  734. if ( win && ( win = win.contentWindow ) && win.mejs ) {
  735. _.each( win.mejs.players, function( player ) {
  736. try {
  737. player.pause();
  738. } catch ( e ) {}
  739. } );
  740. }
  741. } );
  742. }
  743. } );
  744. embed = _.extend( {}, av, {
  745. action: 'parse-embed',
  746. edit: function( text, update ) {
  747. var frame = media.embed.edit( text, this.url ),
  748. self = this;
  749. this.pausePlayers();
  750. frame.state( 'embed' ).props.on( 'change:url', function( model, url ) {
  751. if ( url && model.get( 'url' ) ) {
  752. frame.state( 'embed' ).metadata = model.toJSON();
  753. }
  754. } );
  755. frame.state( 'embed' ).on( 'select', function() {
  756. var data = frame.state( 'embed' ).metadata;
  757. if ( self.url ) {
  758. update( data.url );
  759. } else {
  760. update( media.embed.shortcode( data ).string() );
  761. }
  762. } );
  763. frame.on( 'close', function() {
  764. frame.detach();
  765. } );
  766. frame.open();
  767. }
  768. } );
  769. views.register( 'gallery', _.extend( {}, gallery ) );
  770. views.register( 'audio', _.extend( {}, av, {
  771. state: [ 'audio-details' ]
  772. } ) );
  773. views.register( 'video', _.extend( {}, av, {
  774. state: [ 'video-details' ]
  775. } ) );
  776. views.register( 'playlist', _.extend( {}, av, {
  777. state: [ 'playlist-edit', 'video-playlist-edit' ]
  778. } ) );
  779. views.register( 'embed', _.extend( {}, embed ) );
  780. views.register( 'embedURL', _.extend( {}, embed, {
  781. match: function( content ) {
  782. var re = /(^|<p>)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi,
  783. match = re.exec( content );
  784. if ( match ) {
  785. return {
  786. index: match.index + match[1].length,
  787. content: match[2],
  788. options: {
  789. url: true
  790. }
  791. };
  792. }
  793. }
  794. } ) );
  795. } )( window, window.wp.mce.views, window.wp.media, window.jQuery );