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