Make WordPress Core

Changeset 27214


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

In media-views.js, add wp.media.controller.CollectionAdd and wp.media.controller.CollectionEdit to provide an abstraction for Add and Edit screens in the media modal for collection-type things.

There are currently no instances of this. Those will be forthcoming.

See #26631.

File:
1 edited

Legend:

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

    r27171 r27214  
    759759                }
    760760        });
     761
     762        /**
     763         * wp.media.controller.CollectionEdit
     764         *
     765         * @static
     766         * @param {string} prop The shortcode slug
     767         * @param {object} args
     768         * @returns {wp.media.controller.Library}
     769         */
     770        media.controller.CollectionEdit = function ( prop, args ) {
     771                /**
     772                 * @constructor
     773                 * @augments wp.media.controller.Library
     774                 * @augments wp.media.controller.State
     775                 * @augments Backbone.Model
     776                 */
     777                return media.controller.Library.extend({
     778                        defaults : _.defaults(args.defaults || {}, {
     779                                id:         prop + '-edit',
     780                                toolbar:    prop + '-edit',
     781                                multiple:   false,
     782                                describe:   true,
     783                                edge:       199,
     784                                editing:    false,
     785                                sortable:   true,
     786                                searchable: false,
     787                                content:    'browse',
     788                                priority:   60,
     789                                dragInfo:   true,
     790
     791                                // Don't sync the selection, as the Edit {Collection} library
     792                                // *is* the selection.
     793                                syncSelection: false
     794                        }),
     795
     796                        initialize: function() {
     797                                // If we haven't been provided a `library`, create a `Selection`.
     798                                if ( ! this.get('library') ) {
     799                                        this.set( 'library', new media.model.Selection() );
     800                                }
     801                                // The single `Attachment` view to be used in the `Attachments` view.
     802                                if ( ! this.get('AttachmentView') ) {
     803                                        this.set( 'AttachmentView', media.view.Attachment.EditLibrary );
     804                                }
     805                                media.controller.Library.prototype.initialize.apply( this, arguments );
     806                        },
     807
     808                        activate: function() {
     809                                var library = this.get('library');
     810
     811                                // Limit the library to images only.
     812                                library.props.set( 'type', args.type );
     813
     814                                // Watch for uploaded attachments.
     815                                this.get('library').observe( wp.Uploader.queue );
     816
     817                                this.frame.on( 'content:render:browse', this.settings, this );
     818
     819                                media.controller.Library.prototype.activate.apply( this, arguments );
     820                        },
     821
     822                        deactivate: function() {
     823                                // Stop watching for uploaded attachments.
     824                                this.get('library').unobserve( wp.Uploader.queue );
     825
     826                                this.frame.off( 'content:render:browse', this.settings, this );
     827
     828                                media.controller.Library.prototype.deactivate.apply( this, arguments );
     829                        },
     830
     831                        settings: function( browser ) {
     832                                var library = this.get('library'), obj = {};
     833
     834                                if ( ! library || ! browser ) {
     835                                        return;
     836                                }
     837
     838                                library[ prop ] = library[ prop ] || new Backbone.Model();
     839
     840                                obj[ prop ] = new media.view.Settings[ args.settings ]({
     841                                        controller: this,
     842                                        model:      library[ prop ],
     843                                        priority:   40
     844                                });
     845
     846                                browser.sidebar.set( obj );
     847
     848                                if ( args.dragInfoText ) {
     849                                        browser.toolbar.set( 'dragInfo', new media.View({
     850                                                el: $( '<div class="instructions">' + args.dragInfoText + '</div>' )[0],
     851                                                priority: -40
     852                                        }) );
     853                                }
     854
     855                                browser.toolbar.set( 'reverse', {
     856                                        text:     l10n.reverseOrder,
     857                                        priority: 80,
     858
     859                                        click: function() {
     860                                                library.reset( library.toArray().reverse() );
     861                                        }
     862                                });
     863                        }
     864                });
     865        };
     866
     867        /**
     868         * wp.media.controller.CollectionAdd
     869         *
     870         * @static
     871         * @param {string} prop The shortcode slug
     872         * @param {object} args
     873         * @returns {wp.media.controller.Library}
     874         */
     875        media.controller.CollectionAdd = function ( prop, args ) {
     876                /**
     877                 * @constructor
     878                 * @augments wp.media.controller.Library
     879                 * @augments wp.media.controller.State
     880                 * @augments Backbone.Model
     881                 */
     882                return media.controller.Library.extend({
     883                        defaults: _.defaults({
     884                                id:           prop + '-library',
     885                                filterable:   'uploaded',
     886                                multiple:     'add',
     887                                menu:         prop,
     888                                toolbar:      prop + '-add',
     889                                priority:     100,
     890                                syncSelection: false
     891                        }, args.defaults || {}, media.controller.Library.prototype.defaults ),
     892                        initialize: function() {
     893                                // If we haven't been provided a `library`, create a `Selection`.
     894                                if ( ! this.get('library') ) {
     895                                        this.set( 'library', media.query({ type: args.type }) );
     896                                }
     897                                media.controller.Library.prototype.initialize.apply( this, arguments );
     898                        },
     899
     900                        activate: function() {
     901                                var library = this.get('library'),
     902                                        edit    = this.frame.state(prop + '-edit').get('library');
     903
     904                                if ( this.editLibrary && this.editLibrary !== edit ) {
     905                                        library.unobserve( this.editLibrary );
     906                                }
     907
     908                                // Accepts attachments that exist in the original library and
     909                                // that do not exist in gallery's library.
     910                                library.validator = function( attachment ) {
     911                                        return !! this.mirroring.get( attachment.cid ) && ! edit.get( attachment.cid ) && media.model.Selection.prototype.validator.apply( this, arguments );
     912                                };
     913
     914                                // Reset the library to ensure that all attachments are re-added
     915                                // to the collection. Do so silently, as calling `observe` will
     916                                // trigger the `reset` event.
     917                                library.reset( library.mirroring.models, { silent: true });
     918                                library.observe( edit );
     919                                this.editLibrary = edit;
     920
     921                                media.controller.Library.prototype.activate.apply( this, arguments );
     922                        }
     923                });
     924        };
    761925
    762926        /**
Note: See TracChangeset for help on using the changeset viewer.