Make WordPress Core

Changeset 27213


Ignore:
Timestamp:
02/20/2014 06:12:05 PM (12 years ago)
Author:
wonderboymusic
Message:

Replace the current wp.media.gallery instance in media-editor.js with one that leverages wp.media.collection.

See #26631.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/media-editor.js

    r27212 r27213  
    99         * @static
    1010         */
    11         var workflows = {};
     11        var workflows = {}, cache = {};
    1212
    1313        /**
     
    538538        };
    539539
    540         /**
    541          * wp.media.gallery
    542          * @namespace
    543          */
    544         wp.media.gallery = (function() {
    545                 /**
    546                  *
    547                  * @static
    548                  * @type object
    549                  */
    550                 var galleries = {};
    551 
    552                 return {
    553                         /**
    554                          * Default gallery properties
    555                          *
    556                          * @global wp.media.view.settings
    557                          * @readonly
    558                          */
    559                         defaults: {
    560                                 order:      'ASC',
    561                                 id:         wp.media.view.settings.post.id,
    562                                 itemtag:    'dl',
    563                                 icontag:    'dt',
     540        wp.media.gallery = (function() {
     541                var gallery = {
     542                        defaults : {
     543                                itemtag: 'dl',
     544                                icontag: 'dt',
    564545                                captiontag: 'dd',
    565                                 columns:    '3',
    566                                 link:       'post',
    567                                 size:       'thumbnail',
    568                                 orderby:    'menu_order ID'
    569                         },
    570                         /**
    571                          * Retrieve attachments based on the properties of the passed shortcode
    572                          *
    573                          * @global wp.media.query
    574                          *
    575                          * @param {wp.shortcode} shortcode An instance of wp.shortcode().
    576                          * @returns {wp.media.model.Attachments} A Backbone.Collection containing
    577                          *      the images belonging to a gallery. The 'gallery' prop is a Backbone.Model
    578                          *      containing the 'props' for the gallery.
    579                          */
    580                         attachments: function( shortcode ) {
    581                                 var shortcodeString = shortcode.string(),
    582                                         result = galleries[ shortcodeString ],
    583                                         attrs, args, query, others;
    584 
    585                                 delete galleries[ shortcodeString ];
    586 
    587                                 if ( result ) {
    588                                         return result;
    589                                 }
    590 
    591                                 // Fill the default shortcode attributes.
    592                                 attrs = _.defaults( shortcode.attrs.named, wp.media.gallery.defaults );
    593                                 args  = _.pick( attrs, 'orderby', 'order' );
    594 
    595                                 args.type    = 'image';
    596                                 args.perPage = -1;
    597 
    598                                 // Mark the `orderby` override attribute.
    599                                 if( undefined !== attrs.orderby ) {
    600                                         attrs._orderByField = attrs.orderby;
    601                                 }
    602                                 if ( 'rand' === attrs.orderby ) {
    603                                         attrs._orderbyRandom = true;
    604                                 }
    605 
    606                                 // Map the `orderby` attribute to the corresponding model property.
    607                                 if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) ) {
    608                                         args.orderby = 'menuOrder';
    609                                 }
    610 
    611                                 // Map the `ids` param to the correct query args.
    612                                 if ( attrs.ids ) {
    613                                         args.post__in = attrs.ids.split(',');
    614                                         args.orderby  = 'post__in';
    615                                 } else if ( attrs.include ) {
    616                                         args.post__in = attrs.include.split(',');
    617                                 }
    618 
    619                                 if ( attrs.exclude ) {
    620                                         args.post__not_in = attrs.exclude.split(',');
    621                                 }
    622 
    623                                 if ( ! args.post__in ) {
    624                                         args.uploadedTo = attrs.id;
    625                                 }
    626 
    627                                 // Collect the attributes that were not included in `args`.
    628                                 others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' );
    629 
    630                                 query = wp.media.query( args );
    631                                 query.gallery = new Backbone.Model( others );
    632                                 return query;
    633                         },
    634                         /**
    635                          * Triggered when clicking 'Insert Gallery' or 'Update Gallery'
    636                          *
    637                          * @global wp.shortcode
    638                          * @global wp.media.model.Attachments
    639                          *
    640                          * @param {wp.media.model.Attachments} attachments A Backbone.Collection containing
    641                          *      the images belonging to a gallery. The 'gallery' prop is a Backbone.Model
    642                          *      containing the 'props' for the gallery.
    643                          * @returns {wp.shortcode}
    644                          */
    645                         shortcode: function( attachments ) {
    646                                 var props = attachments.props.toJSON(),
    647                                         attrs = _.pick( props, 'orderby', 'order' ),
    648                                         shortcode, clone;
    649 
    650                                 if ( attachments.gallery ) {
    651                                         _.extend( attrs, attachments.gallery.toJSON() );
    652                                 }
    653 
    654                                 // Convert all gallery shortcodes to use the `ids` property.
    655                                 // Ignore `post__in` and `post__not_in`; the attachments in
    656                                 // the collection will already reflect those properties.
    657                                 attrs.ids = attachments.pluck('id');
    658 
    659                                 // Copy the `uploadedTo` post ID.
    660                                 if ( props.uploadedTo ) {
    661                                         attrs.id = props.uploadedTo;
    662                                 }
    663 
    664                                 // Check if the gallery is randomly ordered.
    665                                 delete attrs.orderby;
    666 
    667                                 if ( attrs._orderbyRandom ) {
    668                                         attrs.orderby = 'rand';
    669                                 } else if ( attrs._orderByField && attrs._orderByField != 'rand' ) {
    670                                         attrs.orderby = attrs._orderByField;
    671                                 }
    672 
    673                                 delete attrs._orderbyRandom;
    674                                 delete attrs._orderByField;
    675 
    676                                 // If the `ids` attribute is set and `orderby` attribute
    677                                 // is the default value, clear it for cleaner output.
    678                                 if ( attrs.ids && 'post__in' === attrs.orderby ) {
    679                                         delete attrs.orderby;
    680                                 }
    681 
    682                                 // Remove default attributes from the shortcode.
    683                                 _.each( wp.media.gallery.defaults, function( value, key ) {
    684                                         if ( value === attrs[ key ] )
    685                                                 delete attrs[ key ];
    686                                 });
    687 
    688                                 shortcode = new wp.shortcode({
    689                                         tag:    'gallery',
    690                                         attrs:  attrs,
    691                                         type:   'single'
    692                                 });
    693 
    694                                 // Use a cloned version of the gallery.
    695                                 clone = new wp.media.model.Attachments( attachments.models, {
    696                                         props: props
    697                                 });
    698                                 clone.gallery = attachments.gallery;
    699                                 galleries[ shortcode.string() ] = clone;
    700 
    701                                 return shortcode;
    702                         },
    703                         /**
    704                          * Triggered when double-clicking a Gallery shortcode placeholder
    705                          *   in the editor
    706                          *
    707                          * @global wp.shortcode
    708                          * @global wp.media.model.Selection
    709                          * @global wp.media.view.l10n
    710                          *
    711                          * @param {string} content Content that is searched for possible
    712                          *    shortcode markup matching the passed tag name,
    713                          *
    714                          * @this wp.media.gallery
    715                          *
    716                          * @returns {wp.media.view.MediaFrame.Select} A media workflow.
    717                          */
    718                         edit: function( content ) {
    719                                 var shortcode = wp.shortcode.next( 'gallery', content ),
    720                                         defaultPostId = wp.media.gallery.defaults.id,
    721                                         attachments, selection;
    722 
    723                                 // Bail if we didn't match the shortcode or all of the content.
    724                                 if ( ! shortcode || shortcode.content !== content ) {
    725                                         return;
    726                                 }
    727 
    728                                 // Ignore the rest of the match object.
    729                                 shortcode = shortcode.shortcode;
    730 
    731                                 if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) ) {
    732                                         shortcode.set( 'id', defaultPostId );
    733                                 }
    734 
    735                                 attachments = wp.media.gallery.attachments( shortcode );
    736 
    737                                 selection = new wp.media.model.Selection( attachments.models, {
    738                                         props:    attachments.props.toJSON(),
    739                                         multiple: true
    740                                 });
    741 
    742                                 selection.gallery = attachments.gallery;
    743 
    744                                 // Fetch the query's attachments, and then break ties from the
    745                                 // query to allow for sorting.
    746                                 selection.more().done( function() {
    747                                         // Break ties with the query.
    748                                         selection.props.set({ query: false });
    749                                         selection.unmirror();
    750                                         selection.props.unset('orderby');
    751                                 });
    752 
    753                                 // Destroy the previous gallery frame.
    754                                 if ( this.frame ) {
    755                                         this.frame.dispose();
    756                                 }
    757 
    758                                 // Store the current gallery frame.
    759                                 this.frame = wp.media({
    760                                         frame:     'post',
    761                                         state:     'gallery-edit',
    762                                         title:     wp.media.view.l10n.editGalleryTitle,
    763                                         editing:   true,
    764                                         multiple:  true,
    765                                         selection: selection
    766                                 }).open();
    767 
    768                                 return this.frame;
    769                         }
    770                 };
    771         }());
     546                                columns: '3',
     547                                link: 'post',
     548                                size: 'thumbnail',
     549                                order: 'ASC',
     550                                id: wp.media.view.settings.post.id,
     551                                orderby : 'menu_order ID'
     552                        }
     553                };
     554
     555                return _.extend(gallery, wp.media.collection.instance( 'gallery', {
     556                        type : 'image',
     557                        title : wp.media.view.l10n.editGalleryTitle
     558                }));
     559        }());
    772560
    773561        /**
Note: See TracChangeset for help on using the changeset viewer.