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