1 /* global getUserSetting, tinymce, QTags */
3 // WordPress, TinyMCE, and Media
4 // -----------------------------
7 * Stores the editors' `wp.media.controller.Frame` instances.
14 * A helper mixin function to avoid truthy and falsey values being
15 * passed as an input that expects booleans. If key is undefined in the map,
16 * but has a default value, set it.
18 * @param {object} attrs Map of props from a shortcode or settings.
19 * @param {string} key The key within the passed map to check for a value.
20 * @returns {mixed|undefined} The original or coerced value of key within attrs
22 wp.media.coerce = function ( attrs, key ) {
23 if ( _.isUndefined( attrs[ key ] ) && ! _.isUndefined( this.defaults[ key ] ) ) {
24 attrs[ key ] = this.defaults[ key ];
25 } else if ( 'true' === attrs[ key ] ) {
27 } else if ( 'false' === attrs[ key ] ) {
39 * Joins the `props` and `attachment` objects,
40 * outputting the proper object format based on the
43 * @global wp.media.view.settings
44 * @global getUserSetting()
46 * @param {Object} [props={}] Attachment details (align, link, size, etc).
47 * @param {Object} attachment The attachment object, media version of Post.
48 * @returns {Object} Joined props
50 props: function( props, attachment ) {
51 var link, linkUrl, size, sizes, fallbacks,
52 defaultProps = wp.media.view.settings.defaultProps;
54 // Final fallbacks run after all processing has been completed.
55 fallbacks = function( props ) {
56 // Generate alt fallbacks and strip tags.
57 if ( 'image' === props.type && ! props.alt ) {
58 if ( props.caption ) {
59 props.alt = props.caption;
60 } else if ( props.title !== props.url ) {
61 props.alt = props.title;
66 props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
67 props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
73 props = props ? _.clone( props ) : {};
75 if ( attachment && attachment.type ) {
76 props.type = attachment.type;
79 if ( 'image' === props.type ) {
80 props = _.defaults( props || {}, {
81 align: defaultProps.align || getUserSetting( 'align', 'none' ),
82 size: defaultProps.size || getUserSetting( 'imgsize', 'medium' ),
88 // All attachment-specific settings follow.
90 return fallbacks( props );
93 props.title = props.title || attachment.title;
95 link = props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' );
96 if ( 'file' === link || 'embed' === link ) {
97 linkUrl = attachment.url;
98 } else if ( 'post' === link ) {
99 linkUrl = attachment.link;
100 } else if ( 'custom' === link ) {
101 linkUrl = props.linkUrl;
103 props.linkUrl = linkUrl || '';
105 // Format properties for images.
106 if ( 'image' === attachment.type ) {
107 props.classes.push( 'wp-image-' + attachment.id );
109 sizes = attachment.sizes;
110 size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
112 _.extend( props, _.pick( attachment, 'align', 'caption', 'alt' ), {
116 captionId: 'attachment_' + attachment.id
118 } else if ( 'video' === attachment.type || 'audio' === attachment.type ) {
119 _.extend( props, _.pick( attachment, 'title', 'type', 'icon', 'mime' ) );
120 // Format properties for non-images.
122 props.title = props.title || attachment.filename;
123 props.rel = props.rel || 'attachment wp-att-' + attachment.id;
126 return fallbacks( props );
129 * Create link markup that is suitable for passing to the editor
131 * @global wp.html.string
133 * @param {Object} props Attachment details (align, link, size, etc).
134 * @param {Object} attachment The attachment object, media version of Post.
135 * @returns {string} The link markup
137 link: function( props, attachment ) {
140 props = wp.media.string.props( props, attachment );
144 content: props.title,
151 options.attrs.rel = props.rel;
154 return wp.html.string( options );
157 * Create an Audio shortcode string that is suitable for passing to the editor
159 * @param {Object} props Attachment details (align, link, size, etc).
160 * @param {Object} attachment The attachment object, media version of Post.
161 * @returns {string} The audio shortcode
163 audio: function( props, attachment ) {
164 return wp.media.string._audioVideo( 'audio', props, attachment );
167 * Create a Video shortcode string that is suitable for passing to the editor
169 * @param {Object} props Attachment details (align, link, size, etc).
170 * @param {Object} attachment The attachment object, media version of Post.
171 * @returns {string} The video shortcode
173 video: function( props, attachment ) {
174 return wp.media.string._audioVideo( 'video', props, attachment );
177 * Helper function to create a media shortcode string
181 * @global wp.shortcode
182 * @global wp.media.view.settings
184 * @param {string} type The shortcode tag name: 'audio' or 'video'.
185 * @param {Object} props Attachment details (align, link, size, etc).
186 * @param {Object} attachment The attachment object, media version of Post.
187 * @returns {string} The media shortcode
189 _audioVideo: function( type, props, attachment ) {
190 var shortcode, html, extension;
192 props = wp.media.string.props( props, attachment );
193 if ( props.link !== 'embed' )
194 return wp.media.string.link( props );
198 if ( 'video' === type ) {
199 if ( attachment.image && -1 === attachment.image.src.indexOf( attachment.icon ) ) {
200 shortcode.poster = attachment.image.src;
203 if ( attachment.width ) {
204 shortcode.width = attachment.width;
207 if ( attachment.height ) {
208 shortcode.height = attachment.height;
212 extension = attachment.filename.split('.').pop();
214 if ( _.contains( wp.media.view.settings.embedExts, extension ) ) {
215 shortcode[extension] = attachment.url;
217 // Render unsupported audio and video files as links.
218 return wp.media.string.link( props );
221 html = wp.shortcode.string({
229 * Create image markup, optionally with a link and/or wrapped in a caption shortcode,
230 * that is suitable for passing to the editor
233 * @global wp.shortcode
235 * @param {Object} props Attachment details (align, link, size, etc).
236 * @param {Object} attachment The attachment object, media version of Post.
239 image: function( props, attachment ) {
241 options, classes, shortcode, html;
243 props.type = 'image';
244 props = wp.media.string.props( props, attachment );
245 classes = props.classes || [];
247 img.src = ! _.isUndefined( attachment ) ? attachment.url : props.url;
248 _.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
250 // Only assign the align class to the image if we're not printing
251 // a caption, since the alignment is sent to the shortcode.
252 if ( props.align && ! props.caption ) {
253 classes.push( 'align' + props.align );
257 classes.push( 'size-' + props.size );
260 img['class'] = _.compact( classes ).join(' ');
262 // Generate `img` tag options.
269 // Generate the `a` element options, if they exist.
270 if ( props.linkUrl ) {
280 html = wp.html.string( options );
282 // Generate the caption shortcode.
283 if ( props.caption ) {
287 shortcode.width = img.width;
290 if ( props.captionId ) {
291 shortcode.id = props.captionId;
295 shortcode.align = 'align' + props.align;
298 html = wp.shortcode.string({
301 content: html + ' ' + props.caption
310 coerce : wp.media.coerce,
318 edit : function( data, isURL ) {
319 var frame, props = {}, shortcode;
322 props.url = data.replace(/<[^>]+>/g, '');
324 shortcode = wp.shortcode.next( 'embed', data ).shortcode;
326 props = _.defaults( shortcode.attrs.named, this.defaults );
327 if ( shortcode.content ) {
328 props.url = shortcode.content;
341 shortcode : function( model ) {
342 var self = this, content;
344 _.each( this.defaults, function( value, key ) {
345 model[ key ] = self.coerce( model, key );
347 if ( value === model[ key ] ) {
355 return new wp.shortcode({
363 wp.media.collection = function(attributes) {
364 var collections = {};
367 coerce : wp.media.coerce,
369 * Retrieve attachments based on the properties of the passed shortcode
371 * @global wp.media.query
373 * @param {wp.shortcode} shortcode An instance of wp.shortcode().
374 * @returns {wp.media.model.Attachments} A Backbone.Collection containing
375 * the media items belonging to a collection.
376 * The query[ this.tag ] property is a Backbone.Model
377 * containing the 'props' for the collection.
379 attachments: function( shortcode ) {
380 var shortcodeString = shortcode.string(),
381 result = collections[ shortcodeString ],
382 attrs, args, query, others, self = this;
384 delete collections[ shortcodeString ];
388 // Fill the default shortcode attributes.
389 attrs = _.defaults( shortcode.attrs.named, this.defaults );
390 args = _.pick( attrs, 'orderby', 'order' );
392 args.type = this.type;
395 // Mark the `orderby` override attribute.
396 if ( undefined !== attrs.orderby ) {
397 attrs._orderByField = attrs.orderby;
400 if ( 'rand' === attrs.orderby ) {
401 attrs._orderbyRandom = true;
404 // Map the `orderby` attribute to the corresponding model property.
405 if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) ) {
406 args.orderby = 'menuOrder';
409 // Map the `ids` param to the correct query args.
411 args.post__in = attrs.ids.split(',');
412 args.orderby = 'post__in';
413 } else if ( attrs.include ) {
414 args.post__in = attrs.include.split(',');
417 if ( attrs.exclude ) {
418 args.post__not_in = attrs.exclude.split(',');
421 if ( ! args.post__in ) {
422 args.uploadedTo = attrs.id;
425 // Collect the attributes that were not included in `args`.
426 others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' );
428 _.each( this.defaults, function( value, key ) {
429 others[ key ] = self.coerce( others, key );
432 query = wp.media.query( args );
433 query[ this.tag ] = new Backbone.Model( others );
437 * Triggered when clicking 'Insert {label}' or 'Update {label}'
439 * @global wp.shortcode
440 * @global wp.media.model.Attachments
442 * @param {wp.media.model.Attachments} attachments A Backbone.Collection containing
443 * the media items belonging to a collection.
444 * The query[ this.tag ] property is a Backbone.Model
445 * containing the 'props' for the collection.
446 * @returns {wp.shortcode}
448 shortcode: function( attachments ) {
449 var props = attachments.props.toJSON(),
450 attrs = _.pick( props, 'orderby', 'order' ),
453 if ( attachments.type ) {
454 attrs.type = attachments.type;
455 delete attachments.type;
458 if ( attachments[this.tag] ) {
459 _.extend( attrs, attachments[this.tag].toJSON() );
462 // Convert all gallery shortcodes to use the `ids` property.
463 // Ignore `post__in` and `post__not_in`; the attachments in
464 // the collection will already reflect those properties.
465 attrs.ids = attachments.pluck('id');
467 // Copy the `uploadedTo` post ID.
468 if ( props.uploadedTo ) {
469 attrs.id = props.uploadedTo;
471 // Check if the gallery is randomly ordered.
472 delete attrs.orderby;
474 if ( attrs._orderbyRandom ) {
475 attrs.orderby = 'rand';
476 } else if ( attrs._orderByField && attrs._orderByField != 'rand' ) {
477 attrs.orderby = attrs._orderByField;
480 delete attrs._orderbyRandom;
481 delete attrs._orderByField;
483 // If the `ids` attribute is set and `orderby` attribute
484 // is the default value, clear it for cleaner output.
485 if ( attrs.ids && 'post__in' === attrs.orderby ) {
486 delete attrs.orderby;
489 attrs = this.setDefaults( attrs );
491 shortcode = new wp.shortcode({
497 // Use a cloned version of the gallery.
498 clone = new wp.media.model.Attachments( attachments.models, {
501 clone[ this.tag ] = attachments[ this.tag ];
502 collections[ shortcode.string() ] = clone;
507 * Triggered when double-clicking a collection shortcode placeholder
510 * @global wp.shortcode
511 * @global wp.media.model.Selection
512 * @global wp.media.view.l10n
514 * @param {string} content Content that is searched for possible
515 * shortcode markup matching the passed tag name,
517 * @this wp.media.{prop}
519 * @returns {wp.media.view.MediaFrame.Select} A media workflow.
521 edit: function( content ) {
522 var shortcode = wp.shortcode.next( this.tag, content ),
523 defaultPostId = this.defaults.id,
524 attachments, selection, state;
526 // Bail if we didn't match the shortcode or all of the content.
527 if ( ! shortcode || shortcode.content !== content ) {
531 // Ignore the rest of the match object.
532 shortcode = shortcode.shortcode;
534 if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) ) {
535 shortcode.set( 'id', defaultPostId );
538 attachments = this.attachments( shortcode );
540 selection = new wp.media.model.Selection( attachments.models, {
541 props: attachments.props.toJSON(),
545 selection[ this.tag ] = attachments[ this.tag ];
547 // Fetch the query's attachments, and then break ties from the
548 // query to allow for sorting.
549 selection.more().done( function() {
550 // Break ties with the query.
551 selection.props.set({ query: false });
552 selection.unmirror();
553 selection.props.unset('orderby');
556 // Destroy the previous gallery frame.
558 this.frame.dispose();
561 if ( shortcode.attrs.named.type && 'video' === shortcode.attrs.named.type ) {
562 state = 'video-' + this.tag + '-edit';
564 state = this.tag + '-edit';
567 // Store the current frame.
568 this.frame = wp.media({
571 title: this.editTitle,
580 setDefaults: function( attrs ) {
582 // Remove default attributes from the shortcode.
583 _.each( this.defaults, function( value, key ) {
584 attrs[ key ] = self.coerce( attrs, key );
585 if ( value === attrs[ key ] ) {
595 wp.media._galleryDefaults = {
603 id: wp.media.view.settings.post && wp.media.view.settings.post.id,
604 orderby : 'menu_order ID'
607 if ( wp.media.view.settings.galleryDefaults ) {
608 wp.media.galleryDefaults = _.extend( {}, wp.media._galleryDefaults, wp.media.view.settings.galleryDefaults );
610 wp.media.galleryDefaults = wp.media._galleryDefaults;
613 wp.media.gallery = new wp.media.collection({
616 editTitle : wp.media.view.l10n.editGalleryTitle,
617 defaults : wp.media.galleryDefaults,
619 setDefaults: function( attrs ) {
620 var self = this, changed = ! _.isEqual( wp.media.galleryDefaults, wp.media._galleryDefaults );
621 _.each( this.defaults, function( value, key ) {
622 attrs[ key ] = self.coerce( attrs, key );
623 if ( value === attrs[ key ] && ( ! changed || value === wp.media._galleryDefaults[ key ] ) ) {
632 * wp.media.featuredImage
635 wp.media.featuredImage = {
637 * Get the featured image post ID
639 * @global wp.media.view.settings
641 * @returns {wp.media.view.settings.post.featuredImageId|number}
644 return wp.media.view.settings.post.featuredImageId;
647 * Set the featured image id, save the post thumbnail data and
648 * set the HTML in the post meta box to the new featured image.
650 * @global wp.media.view.settings
651 * @global wp.media.post
653 * @param {number} id The post ID of the featured image, or -1 to unset it.
655 set: function( id ) {
656 var settings = wp.media.view.settings;
658 settings.post.featuredImageId = id;
660 wp.media.post( 'get-post-thumbnail-html', {
661 post_id: settings.post.id,
662 thumbnail_id: settings.post.featuredImageId,
663 _wpnonce: settings.post.nonce
664 }).done( function( html ) {
666 window.alert( window.setPostThumbnailL10n.error );
669 $( '.inside', '#postimagediv' ).html( html );
673 * Remove the featured image id, save the post thumbnail data and
674 * set the HTML in the post meta box to no featured image.
677 wp.media.featuredImage.set( -1 );
680 * The Featured Image workflow
682 * @global wp.media.controller.FeaturedImage
683 * @global wp.media.view.l10n
685 * @this wp.media.featuredImage
687 * @returns {wp.media.view.MediaFrame.Select} A media workflow.
691 wp.media.frame = this._frame;
695 this._frame = wp.media({
696 state: 'featured-image',
697 states: [ new wp.media.controller.FeaturedImage() , new wp.media.controller.EditImage() ]
700 this._frame.on( 'toolbar:create:featured-image', function( toolbar ) {
702 * @this wp.media.view.MediaFrame.Select
704 this.createSelectToolbar( toolbar, {
705 text: wp.media.view.l10n.setFeaturedImage
709 this._frame.on( 'content:render:edit-image', function() {
710 var selection = this.state('featured-image').get('selection'),
711 view = new wp.media.view.EditImage( { model: selection.single(), controller: this } ).render();
713 this.content.set( view );
715 // after bringing in the frame, load the actual editor via an ajax call
720 this._frame.state('featured-image').on( 'select', this.select );
724 * 'select' callback for Featured Image workflow, triggered when
725 * the 'Set Featured Image' button is clicked in the media modal.
727 * @global wp.media.view.settings
729 * @this wp.media.controller.FeaturedImage
732 var selection = this.get('selection').single();
734 if ( ! wp.media.view.settings.post.featuredImageId ) {
738 wp.media.featuredImage.set( selection ? selection.id : -1 );
741 * Open the content media manager to the 'featured image' tab when
742 * the post thumbnail is clicked.
744 * Update the featured image id when the 'remove' link is clicked.
746 * @global wp.media.view.settings
749 $('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
750 event.preventDefault();
751 // Stop propagation to prevent thickbox from activating.
752 event.stopPropagation();
754 wp.media.featuredImage.frame().open();
755 }).on( 'click', '#remove-post-thumbnail', function() {
756 wp.media.featuredImage.remove();
762 $( wp.media.featuredImage.init );
770 * Send content to the editor
774 * @global wpActiveEditor
775 * @global tb_remove() - Possibly overloaded by legacy plugins
777 * @param {string} html Content to send to the editor
779 insert: function( html ) {
780 var editor, wpActiveEditor,
781 hasTinymce = ! _.isUndefined( window.tinymce ),
782 hasQuicktags = ! _.isUndefined( window.QTags );
784 if ( this.activeEditor ) {
785 wpActiveEditor = window.wpActiveEditor = this.activeEditor;
787 wpActiveEditor = window.wpActiveEditor;
790 // Delegate to the global `send_to_editor` if it exists.
791 // This attempts to play nice with any themes/plugins that have
792 // overridden the insert functionality.
793 if ( window.send_to_editor ) {
794 return window.send_to_editor.apply( this, arguments );
797 if ( ! wpActiveEditor ) {
798 if ( hasTinymce && tinymce.activeEditor ) {
799 editor = tinymce.activeEditor;
800 wpActiveEditor = window.wpActiveEditor = editor.id;
801 } else if ( ! hasQuicktags ) {
804 } else if ( hasTinymce ) {
805 editor = tinymce.get( wpActiveEditor );
808 if ( editor && ! editor.isHidden() ) {
809 editor.execCommand( 'mceInsertContent', false, html );
810 } else if ( hasQuicktags ) {
811 QTags.insertContent( html );
813 document.getElementById( wpActiveEditor ).value += html;
816 // If the old thickbox remove function exists, call it in case
817 // a theme/plugin overloaded it.
818 if ( window.tb_remove ) {
819 try { window.tb_remove(); } catch( e ) {}
824 * Setup 'workflow' and add to the 'workflows' cache. 'open' can
825 * subsequently be called upon it.
827 * @global wp.media.view.l10n
829 * @param {string} id A slug used to identify the workflow.
830 * @param {Object} [options={}]
832 * @this wp.media.editor
834 * @returns {wp.media.view.MediaFrame.Select} A media workflow.
836 add: function( id, options ) {
837 var workflow = this.get( id );
839 // only add once: if exists return existing
844 workflow = workflows[ id ] = wp.media( _.defaults( options || {}, {
847 title: wp.media.view.l10n.addMedia,
851 workflow.on( 'insert', function( selection ) {
852 var state = workflow.state();
854 selection = selection || state.get('selection');
859 $.when.apply( $, selection.map( function( attachment ) {
860 var display = state.display( attachment ).toJSON();
862 * @this wp.media.editor
864 return this.send.attachment( display, attachment.toJSON() );
865 }, this ) ).done( function() {
866 wp.media.editor.insert( _.toArray( arguments ).join('\n\n') );
870 workflow.state('gallery-edit').on( 'update', function( selection ) {
872 * @this wp.media.editor
874 this.insert( wp.media.gallery.shortcode( selection ).string() );
877 workflow.state('playlist-edit').on( 'update', function( selection ) {
879 * @this wp.media.editor
881 this.insert( wp.media.playlist.shortcode( selection ).string() );
884 workflow.state('video-playlist-edit').on( 'update', function( selection ) {
886 * @this wp.media.editor
888 this.insert( wp.media.playlist.shortcode( selection ).string() );
891 workflow.state('embed').on( 'select', function() {
893 * @this wp.media.editor
895 var state = workflow.state(),
896 type = state.get('type'),
897 embed = state.props.toJSON();
899 embed.url = embed.url || '';
901 if ( 'link' === type ) {
907 this.send.link( embed ).done( function( resp ) {
908 wp.media.editor.insert( resp );
911 } else if ( 'image' === type ) {
919 if ( 'none' === embed.link ) {
921 } else if ( 'file' === embed.link ) {
922 embed.linkUrl = embed.url;
925 this.insert( wp.media.string.image( embed ) );
929 workflow.state('featured-image').on( 'select', wp.media.featuredImage.select );
930 workflow.setState( workflow.options.state );
934 * Determines the proper current workflow id
936 * @global wpActiveEditor
939 * @param {string} [id=''] A slug used to identify the workflow.
941 * @returns {wpActiveEditor|string|tinymce.activeEditor.id}
948 // If an empty `id` is provided, default to `wpActiveEditor`.
949 id = window.wpActiveEditor;
951 // If that doesn't work, fall back to `tinymce.activeEditor.id`.
952 if ( ! id && ! _.isUndefined( window.tinymce ) && tinymce.activeEditor ) {
953 id = tinymce.activeEditor.id;
956 // Last but not least, fall back to the empty string.
961 * Return the workflow specified by id
963 * @param {string} id A slug used to identify the workflow.
965 * @this wp.media.editor
967 * @returns {wp.media.view.MediaFrame} A media workflow.
969 get: function( id ) {
971 return workflows[ id ];
974 * Remove the workflow represented by id from the workflow cache
976 * @param {string} id A slug used to identify the workflow.
978 * @this wp.media.editor
980 remove: function( id ) {
982 delete workflows[ id ];
989 * Called when sending an attachment to the editor
990 * from the medial modal.
992 * @global wp.media.view.settings
993 * @global wp.media.post
995 * @param {Object} props Attachment details (align, link, size, etc).
996 * @param {Object} attachment The attachment object, media version of Post.
999 attachment: function( props, attachment ) {
1000 var caption = attachment.caption,
1003 // If captions are disabled, clear the caption.
1004 if ( ! wp.media.view.settings.captions ) {
1005 delete attachment.caption;
1008 props = wp.media.string.props( props, attachment );
1012 post_content: attachment.description,
1013 post_excerpt: caption
1016 if ( props.linkUrl ) {
1017 options.url = props.linkUrl;
1020 if ( 'image' === attachment.type ) {
1021 html = wp.media.string.image( props );
1027 }, function( option, prop ) {
1028 if ( props[ prop ] )
1029 options[ option ] = props[ prop ];
1031 } else if ( 'video' === attachment.type ) {
1032 html = wp.media.string.video( props, attachment );
1033 } else if ( 'audio' === attachment.type ) {
1034 html = wp.media.string.audio( props, attachment );
1036 html = wp.media.string.link( props );
1037 options.post_title = props.title;
1040 return wp.media.post( 'send-attachment-to-editor', {
1041 nonce: wp.media.view.settings.nonce.sendToEditor,
1042 attachment: options,
1044 post_id: wp.media.view.settings.post.id
1048 * Called when 'Insert From URL' source is not an image. Example: YouTube url.
1050 * @global wp.media.view.settings
1052 * @param {Object} embed
1053 * @returns {Promise}
1055 link: function( embed ) {
1056 return wp.media.post( 'send-link-to-editor', {
1057 nonce: wp.media.view.settings.nonce.sendToEditor,
1059 link_text: embed.linkText,
1060 html: wp.media.string.link( embed ),
1061 post_id: wp.media.view.settings.post.id
1068 * @param {string} [id=undefined] Optional. A slug used to identify the workflow.
1069 * @param {Object} [options={}]
1071 * @this wp.media.editor
1073 * @returns {wp.media.view.MediaFrame}
1075 open: function( id, options ) {
1078 options = options || {};
1081 this.activeEditor = id;
1083 workflow = this.get( id );
1085 // Redo workflow if state has changed
1086 if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) ) {
1087 workflow = this.add( id, options );
1090 wp.media.frame = workflow;
1092 return workflow.open();
1096 * Bind click event for .insert-media using event delegation
1098 * @global wp.media.view.l10n
1102 .on( 'click.add-media-button', '.insert-media', function( event ) {
1103 var elem = $( event.currentTarget ),
1104 editor = elem.data('editor'),
1108 title: wp.media.view.l10n.addMedia,
1112 event.preventDefault();
1114 // Remove focus from the `.insert-media` button.
1115 // Prevents Opera from showing the outline of the button
1118 // See: https://core.trac.wordpress.org/ticket/22445
1121 if ( elem.hasClass( 'gallery' ) ) {
1122 options.state = 'gallery';
1123 options.title = wp.media.view.l10n.createGalleryTitle;
1126 wp.media.editor.open( editor, options );
1129 // Initialize and render the Editor drag-and-drop uploader.
1130 new wp.media.view.EditorUploader().render();
1134 _.bindAll( wp.media.editor, 'open' );
1135 $( wp.media.editor.init );