/*globals _, wp, jQuery */ /** * wp.media.view.AttachmentsBrowser * * @class * @augments wp.media.View * @augments wp.Backbone.View * @augments Backbone.View * * @param {object} options * @param {object} [options.filters=false] Which filters to show in the browser's toolbar. * Accepts 'uploaded' and 'all'. * @param {object} [options.search=true] Whether to show the search interface in the * browser's toolbar. * @param {object} [options.date=true] Whether to show the date filter in the * browser's toolbar. * @param {object} [options.display=false] Whether to show the attachments display settings * view in the sidebar. * @param {bool|string} [options.sidebar=true] Whether to create a sidebar for the browser. * Accepts true, false, and 'errors'. */ var View = require( '../view.js' ), Library = require( '../attachment/library.js' ), Toolbar = require( '../toolbar.js' ), Spinner = require( '../spinner.js' ), Search = require( '../search.js' ), Label = require( '../label.js' ), Uploaded = require( '../attachment-filters/uploaded.js' ), All = require( '../attachment-filters/all.js' ), DateFilter = require( '../attachment-filters/date.js' ), UploaderInline = require( '../uploader/inline.js' ), Attachments = require( '../attachments.js' ), Sidebar = require( '../sidebar.js' ), UploaderStatus = require( '../uploader/status.js' ), Details = require( '../attachment/details.js' ), AttachmentCompat = require( '../attachment-compat.js' ), AttachmentDisplay = require( '../settings/attachment-display.js' ), mediaTrash = wp.media.view.settings.mediaTrash, l10n = wp.media.view.l10n, $ = jQuery, AttachmentsBrowser; AttachmentsBrowser = View.extend({ tagName: 'div', className: 'attachments-browser', initialize: function() { _.defaults( this.options, { filters: false, search: true, date: true, display: false, sidebar: true, AttachmentView: Library }); this.listenTo( this.controller, 'toggle:upload:attachment', _.bind( this.toggleUploader, this ) ); this.controller.on( 'edit:selection', this.editSelection ); this.createToolbar(); if ( this.options.sidebar ) { this.createSidebar(); } this.createUploader(); this.createAttachments(); this.updateContent(); if ( ! this.options.sidebar || 'errors' === this.options.sidebar ) { this.$el.addClass( 'hide-sidebar' ); if ( 'errors' === this.options.sidebar ) { this.$el.addClass( 'sidebar-for-errors' ); } } this.collection.on( 'add remove reset', this.updateContent, this ); }, editSelection: function( modal ) { modal.$( '.media-button-backToLibrary' ).focus(); }, /** * @returns {wp.media.view.AttachmentsBrowser} Returns itself to allow chaining */ dispose: function() { this.options.selection.off( null, null, this ); View.prototype.dispose.apply( this, arguments ); return this; }, createToolbar: function() { var LibraryViewSwitcher, Filters, toolbarOptions; toolbarOptions = { controller: this.controller }; if ( this.controller.isModeActive( 'grid' ) ) { toolbarOptions.className = 'media-toolbar wp-filter'; } /** * @member {wp.media.view.Toolbar} */ this.toolbar = new Toolbar( toolbarOptions ); this.views.add( this.toolbar ); this.toolbar.set( 'spinner', new Spinner({ priority: -60 }) ); if ( -1 !== $.inArray( this.options.filters, [ 'uploaded', 'all' ] ) ) { // "Filters" will return a , screen reader text needs to be rendered before this.toolbar.set( 'dateFilterLabel', new Label({ value: l10n.filterByDate, attributes: { 'for': 'media-attachment-date-filters' }, priority: -75 }).render() ); this.toolbar.set( 'dateFilter', new DateFilter({ controller: this.controller, model: this.collection.props, priority: -75 }).render() ); // BulkSelection is a
with subviews, including screen reader text this.toolbar.set( 'selectModeToggleButton', new wp.media.view.SelectModeToggleButton({ text: l10n.bulkSelect, controller: this.controller, priority: -70 }).render() ); this.toolbar.set( 'deleteSelectedButton', new wp.media.view.DeleteSelectedButton({ filters: Filters, style: 'primary', disabled: true, text: mediaTrash ? l10n.trashSelected : l10n.deleteSelected, controller: this.controller, priority: -60, click: function() { var changed = [], removed = [], self = this, selection = this.controller.state().get( 'selection' ), library = this.controller.state().get( 'library' ); if ( ! selection.length ) { return; } if ( ! mediaTrash && ! confirm( l10n.warnBulkDelete ) ) { return; } if ( mediaTrash && 'trash' !== selection.at( 0 ).get( 'status' ) && ! confirm( l10n.warnBulkTrash ) ) { return; } selection.each( function( model ) { if ( ! model.get( 'nonces' )['delete'] ) { removed.push( model ); return; } if ( mediaTrash && 'trash' === model.get( 'status' ) ) { model.set( 'status', 'inherit' ); changed.push( model.save() ); removed.push( model ); } else if ( mediaTrash ) { model.set( 'status', 'trash' ); changed.push( model.save() ); removed.push( model ); } else { model.destroy({wait: true}); } } ); if ( changed.length ) { selection.remove( removed ); $.when.apply( null, changed ).then( function() { library._requery( true ); self.controller.trigger( 'selection:action:done' ); } ); } else { this.controller.trigger( 'selection:action:done' ); } } }).render() ); if ( mediaTrash ) { this.toolbar.set( 'deleteSelectedPermanentlyButton', new wp.media.view.DeleteSelectedPermanentlyButton({ filters: Filters, style: 'primary', disabled: true, text: l10n.deleteSelected, controller: this.controller, priority: -55, click: function() { var removed = [], selection = this.controller.state().get( 'selection' ); if ( ! selection.length || ! confirm( l10n.warnBulkDelete ) ) { return; } selection.each( function( model ) { if ( ! model.get( 'nonces' )['delete'] ) { removed.push( model ); return; } model.destroy(); } ); selection.remove( removed ); this.controller.trigger( 'selection:action:done' ); } }).render() ); } } else if ( this.options.date ) { // DateFilter is a