1 /* global _wpMediaViewsL10n, MediaElementPlayer, _wpMediaGridSettings */
2 (function($, _, Backbone, wp) {
3 // Local reference to the WordPress media namespace.
4 var media = wp.media, l10n;
6 // Link localized strings and settings.
7 if ( media.view.l10n ) {
8 l10n = media.view.l10n;
10 l10n = media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n;
15 * wp.media.controller.EditAttachmentMetadata
17 * A state for editing an attachment's metadata.
20 * @augments wp.media.controller.State
21 * @augments Backbone.Model
23 media.controller.EditAttachmentMetadata = media.controller.State.extend({
25 id: 'edit-attachment',
26 // Title string passed to the frame's title region view.
27 title: l10n.attachmentDetails,
28 // Region mode defaults.
29 content: 'edit-metadata',
37 * wp.media.view.MediaFrame.Manage
39 * A generic management frame workflow.
41 * Used in the media grid view.
44 * @augments wp.media.view.MediaFrame
45 * @augments wp.media.view.Frame
46 * @augments wp.media.View
47 * @augments wp.Backbone.View
48 * @augments Backbone.View
49 * @mixes wp.media.controller.StateMachine
51 media.view.MediaFrame.Manage = media.view.MediaFrame.extend({
55 initialize: function() {
57 _.defaults( this.options, {
61 library: {}, // Options hash for the query to the media library.
65 mode: [ 'grid', 'edit' ]
68 this.$body = $( document.body );
69 this.$window = $( window );
70 this.$adminBar = $( '#wpadminbar' );
71 this.$window.on( 'scroll resize', _.debounce( _.bind( this.fixPosition, this ), 15 ) );
72 $( document ).on( 'click', '.add-new-h2', _.bind( this.addNewClickHandler, this ) );
74 // Ensure core and media grid view UI is enabled.
75 this.$el.addClass('wp-core-ui');
77 // Force the uploader off if the upload limit has been exceeded or
78 // if the browser isn't supported.
79 if ( wp.Uploader.limitExceeded || ! wp.Uploader.browser.supported ) {
80 this.options.uploader = false;
83 // Initialize a window-wide uploader.
84 if ( this.options.uploader ) {
85 this.uploader = new media.view.UploaderWindow({
88 dropzone: document.body,
89 container: document.body
92 this.uploader.ready();
93 $('body').append( this.uploader.el );
95 this.options.uploader = false;
98 this.gridRouter = new media.view.MediaFrame.Manage.Router();
100 // Call 'initialize' directly on the parent class.
101 media.view.MediaFrame.prototype.initialize.apply( this, arguments );
103 // Append the frame view directly the supplied container.
104 this.$el.appendTo( this.options.container );
107 this.bindRegionModeHandlers();
110 // Update the URL when entering search string (at most once per second)
111 $( '#media-search-input' ).on( 'input', _.debounce( function(e) {
112 var val = $( e.currentTarget ).val(), url = '';
114 url += '?search=' + val;
116 self.gridRouter.navigate( self.gridRouter.baseUrl( url ) );
121 * Create the default states for the frame.
123 createStates: function() {
124 var options = this.options;
126 if ( this.options.states ) {
130 // Add the default states.
132 new media.controller.Library({
133 library: media.query( options.library ),
134 multiple: options.multiple,
135 title: options.title,
138 contentUserSetting: false,
146 * Bind region mode activation events to proper handlers.
148 bindRegionModeHandlers: function() {
149 this.on( 'content:create:browse', this.browseContent, this );
151 // Handle a frame-level event for editing an attachment.
152 this.on( 'edit:attachment', this.openEditAttachmentModal, this );
154 this.on( 'select:activate', this.bindKeydown, this );
155 this.on( 'select:deactivate', this.unbindKeydown, this );
158 handleKeydown: function( e ) {
159 if ( 27 === e.which ) {
161 this.deactivateMode( 'select' ).activateMode( 'edit' );
165 bindKeydown: function() {
166 this.$body.on( 'keydown.select', _.bind( this.handleKeydown, this ) );
169 unbindKeydown: function() {
170 this.$body.off( 'keydown.select' );
173 fixPosition: function() {
174 var $browser, $toolbar;
175 if ( ! this.isModeActive( 'select' ) ) {
179 $browser = this.$('.attachments-browser');
180 $toolbar = $browser.find('.media-toolbar');
182 // Offset doesn't appear to take top margin into account, hence +16
183 if ( ( $browser.offset().top + 16 ) < this.$window.scrollTop() + this.$adminBar.height() ) {
184 $browser.addClass( 'fixed' );
185 $toolbar.css('width', $browser.width() + 'px');
187 $browser.removeClass( 'fixed' );
188 $toolbar.css('width', '');
193 * Click handler for the `Add New` button.
195 addNewClickHandler: function( event ) {
196 event.preventDefault();
197 this.trigger( 'toggle:upload:attachment' );
201 * Open the Edit Attachment modal.
203 openEditAttachmentModal: function( model ) {
204 // Create a new EditAttachment frame, passing along the library and the attachment model.
206 frame: 'edit-attachments',
208 library: this.state().get('library'),
214 * Create an attachments browser view within the content region.
216 * @param {Object} contentRegion Basic object with a `view` property, which
217 * should be set with the proper region view.
218 * @this wp.media.controller.Region
220 browseContent: function( contentRegion ) {
221 var state = this.state();
223 // Browse our library of attachments.
224 this.browserView = contentRegion.view = new media.view.AttachmentsBrowser({
226 collection: state.get('library'),
227 selection: state.get('selection'),
229 sortable: state.get('sortable'),
230 search: state.get('searchable'),
231 filters: state.get('filterable'),
232 display: state.get('displaySettings'),
233 dragInfo: state.get('dragInfo'),
236 suggestedWidth: state.get('suggestedWidth'),
237 suggestedHeight: state.get('suggestedHeight'),
239 AttachmentView: state.get('AttachmentView'),
241 scrollElement: document
243 this.browserView.on( 'ready', _.bind( this.bindDeferred, this ) );
245 this.errors = wp.Uploader.errors;
246 this.errors.on( 'add remove reset', this.sidebarVisibility, this );
249 sidebarVisibility: function() {
250 this.browserView.$( '.media-sidebar' ).toggle( !! this.errors.length );
253 bindDeferred: function() {
254 if ( ! this.browserView.dfd ) {
257 this.browserView.dfd.done( _.bind( this.startHistory, this ) );
260 startHistory: function() {
261 // Verify pushState support and activate
262 if ( window.history && window.history.pushState ) {
263 Backbone.history.start( {
264 root: _wpMediaGridSettings.adminUrl,
272 * A similar view to media.view.Attachment.Details
273 * for use in the Edit Attachment modal.
276 * @augments wp.media.view.Attachment.Details
277 * @augments wp.media.view.Attachment
278 * @augments wp.media.View
279 * @augments wp.Backbone.View
280 * @augments Backbone.View
282 media.view.Attachment.Details.TwoColumn = media.view.Attachment.Details.extend({
283 template: media.template( 'attachment-details-two-column' ),
285 editAttachment: function( event ) {
286 event.preventDefault();
287 this.controller.content.mode( 'edit-image' );
291 * Noop this from parent class, doesn't apply here.
293 toggleSelectionHandler: function() {},
296 media.view.Attachment.Details.prototype.render.apply( this, arguments );
298 media.mixin.removeAllPlayers();
299 this.$( 'audio, video' ).each( function (i, elem) {
300 var el = media.view.MediaDetails.prepareSrc( elem );
301 setTimeout( function() {
302 new MediaElementPlayer( el, media.mixin.mejsSettings );
309 * A router for handling the browser history and application state.
312 * @augments Backbone.Router
314 media.view.MediaFrame.Manage.Router = Backbone.Router.extend({
316 'upload.php?item=:slug': 'showItem',
317 'upload.php?search=:query': 'search'
320 // Map routes against the page URL
321 baseUrl: function( url ) {
322 return 'upload.php' + url;
325 // Respond to the search route by filling the search field and trigggering the input event
326 search: function( query ) {
327 $( '#media-search-input' ).val( query ).trigger( 'input' );
330 // Show the modal with a specific item
331 showItem: function( query ) {
332 var library = media.frame.state().get('library'), item;
334 // Trigger the media frame to open the correct item
335 item = library.findWhere( { id: parseInt( query, 10 ) } );
337 media.frame.trigger( 'edit:attachment', item );
339 item = media.attachment( query );
340 media.frame.listenTo( item, 'change', function( model ) {
341 media.frame.stopListening( item );
342 media.frame.trigger( 'edit:attachment', model );
349 media.view.EditImage.Details = media.view.EditImage.extend({
350 initialize: function( options ) {
351 this.editor = window.imageEdit;
352 this.frame = options.frame;
353 this.controller = options.controller;
354 media.View.prototype.initialize.apply( this, arguments );
358 this.frame.content.mode( 'edit-metadata' );
364 this.model.fetch().done( function() {
365 self.frame.content.mode( 'edit-metadata' );
371 * A frame for editing the details of a specific media item.
373 * Opens in a modal by default.
375 * Requires an attachment model to be passed in the options hash under `model`.
378 * @augments wp.media.view.Frame
379 * @augments wp.media.View
380 * @augments wp.Backbone.View
381 * @augments Backbone.View
382 * @mixes wp.media.controller.StateMachine
384 media.view.MediaFrame.EditAttachments = media.view.MediaFrame.extend({
386 className: 'edit-attachment-frame',
387 template: media.template( 'edit-attachment-frame' ),
388 regions: [ 'title', 'content' ],
391 'click .left': 'previousMediaItem',
392 'click .right': 'nextMediaItem'
395 initialize: function() {
396 media.view.Frame.prototype.initialize.apply( this, arguments );
398 _.defaults( this.options, {
400 state: 'edit-attachment'
403 this.controller = this.options.controller;
404 this.gridRouter = this.controller.gridRouter;
405 this.library = this.options.library;
407 if ( this.options.model ) {
408 this.model = this.options.model;
415 this.title.mode( 'default' );
419 bindHandlers: function() {
420 // Bind default title creation.
421 this.on( 'title:create:default', this.createTitle, this );
423 // Close the modal if the attachment is deleted.
424 this.listenTo( this.model, 'change:status destroy', this.close, this );
426 this.on( 'content:create:edit-metadata', this.editMetadataMode, this );
427 this.on( 'content:create:edit-image', this.editImageMode, this );
428 this.on( 'content:render:edit-image', this.editImageModeRender, this );
429 this.on( 'close', this.detach );
432 createModal: function() {
435 // Initialize modal container view.
436 if ( this.options.modal ) {
437 this.modal = new media.view.Modal({
439 title: this.options.title
442 this.modal.on( 'open', function () {
443 $( 'body' ).on( 'keydown.media-modal', _.bind( self.keyEvent, self ) );
446 // Completely destroy the modal DOM element when closing it.
447 this.modal.on( 'close', function() {
449 $( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
450 // Restore the original focus item if possible
451 $( 'li.attachment[data-id="' + self.model.get( 'id' ) +'"]' ).focus();
455 // Set this frame as the modal's content.
456 this.modal.content( this );
462 * Add the default states to the frame.
464 createStates: function() {
466 new media.controller.EditAttachmentMetadata( { model: this.model } )
471 * Content region rendering callback for the `edit-metadata` mode.
473 * @param {Object} contentRegion Basic object with a `view` property, which
474 * should be set with the proper region view.
476 editMetadataMode: function( contentRegion ) {
477 contentRegion.view = new media.view.Attachment.Details.TwoColumn({
483 * Attach a subview to display fields added via the
484 * `attachment_fields_to_edit` filter.
486 contentRegion.view.views.set( '.attachment-compat', new media.view.AttachmentCompat({
491 // Update browser url when navigating media details
493 this.gridRouter.navigate( this.gridRouter.baseUrl( '?item=' + this.model.id ) );
498 * Render the EditImage view into the frame's content region.
500 * @param {Object} contentRegion Basic object with a `view` property, which
501 * should be set with the proper region view.
503 editImageMode: function( contentRegion ) {
504 var editImageController = new media.controller.EditImage( {
508 // Noop some methods.
509 editImageController._toolbar = function() {};
510 editImageController._router = function() {};
511 editImageController._menu = function() {};
513 contentRegion.view = new media.view.EditImage.Details( {
516 controller: editImageController
520 editImageModeRender: function( view ) {
521 view.on( 'ready', view.loadEditor );
524 toggleNav: function() {
525 this.$('.left').toggleClass( 'disabled', ! this.hasPrevious() );
526 this.$('.right').toggleClass( 'disabled', ! this.hasNext() );
532 rerender: function() {
533 // Only rerender the `content` region.
534 if ( this.content.mode() !== 'edit-metadata' ) {
535 this.content.mode( 'edit-metadata' );
537 this.content.render();
544 * Click handler to switch to the previous media item.
546 previousMediaItem: function() {
547 if ( ! this.hasPrevious() ) {
548 this.$( '.left' ).blur();
551 this.model = this.library.at( this.getCurrentIndex() - 1 );
553 this.$( '.left' ).focus();
557 * Click handler to switch to the next media item.
559 nextMediaItem: function() {
560 if ( ! this.hasNext() ) {
561 this.$( '.right' ).blur();
564 this.model = this.library.at( this.getCurrentIndex() + 1 );
566 this.$( '.right' ).focus();
569 getCurrentIndex: function() {
570 return this.library.indexOf( this.model );
573 hasNext: function() {
574 return ( this.getCurrentIndex() + 1 ) < this.library.length;
577 hasPrevious: function() {
578 return ( this.getCurrentIndex() - 1 ) > -1;
581 * Respond to the keyboard events: right arrow, left arrow, except when
582 * focus is in a textarea or input field.
584 keyEvent: function( event ) {
585 if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName ) && ! ( event.target.readOnly || event.target.disabled ) ) {
589 // The right arrow key
590 if ( 39 === event.keyCode ) {
591 this.nextMediaItem();
593 // The left arrow key
594 if ( 37 === event.keyCode ) {
595 this.previousMediaItem();
599 resetRoute: function() {
600 this.gridRouter.navigate( this.gridRouter.baseUrl( '' ) );
604 media.view.SelectModeToggleButton = media.view.Button.extend({
605 initialize: function() {
606 media.view.Button.prototype.initialize.apply( this, arguments );
607 this.listenTo( this.controller, 'select:activate select:deactivate', this.toggleBulkEditHandler );
608 this.listenTo( this.controller, 'selection:action:done', this.back );
612 this.controller.deactivateMode( 'select' ).activateMode( 'edit' );
616 media.view.Button.prototype.click.apply( this, arguments );
617 if ( this.controller.isModeActive( 'select' ) ) {
620 this.controller.deactivateMode( 'edit' ).activateMode( 'select' );
625 media.view.Button.prototype.render.apply( this, arguments );
626 this.$el.addClass( 'select-mode-toggle-button' );
630 toggleBulkEditHandler: function() {
631 var toolbar = this.controller.content.get().toolbar, children;
633 children = toolbar.$( '.media-toolbar-secondary > *, .media-toolbar-primary > *');
635 // TODO: the Frame should be doing all of this.
636 if ( this.controller.isModeActive( 'select' ) ) {
637 this.model.set( 'text', l10n.cancelSelection );
638 children.not( '.media-button' ).hide();
640 toolbar.$( '.delete-selected-button' ).removeClass( 'hidden' );
642 this.model.set( 'text', l10n.bulkSelect );
643 this.controller.content.get().$el.removeClass( 'fixed' );
644 toolbar.$el.css( 'width', '' );
645 toolbar.$( '.delete-selected-button' ).addClass( 'hidden' );
646 children.not( '.spinner, .media-button' ).show();
647 this.controller.state().get( 'selection' ).reset();
653 * A button that handles bulk Delete/Trash logic
656 * @augments wp.media.view.Button
657 * @augments wp.media.View
658 * @augments wp.Backbone.View
659 * @augments Backbone.View
661 media.view.DeleteSelectedButton = media.view.Button.extend({
662 initialize: function() {
663 media.view.Button.prototype.initialize.apply( this, arguments );
664 if ( this.options.filters ) {
665 this.listenTo( this.options.filters.model, 'change', this.filterChange );
667 this.listenTo( this.controller, 'selection:toggle', this.toggleDisabled );
670 filterChange: function( model ) {
671 if ( 'trash' === model.get( 'status' ) ) {
672 this.model.set( 'text', l10n.untrashSelected );
673 } else if ( media.view.settings.mediaTrash ) {
674 this.model.set( 'text', l10n.trashSelected );
676 this.model.set( 'text', l10n.deleteSelected );
680 toggleDisabled: function() {
681 this.model.set( 'disabled', ! this.controller.state().get( 'selection' ).length );
685 media.view.Button.prototype.render.apply( this, arguments );
686 if ( this.controller.isModeActive( 'select' ) ) {
687 this.$el.addClass( 'delete-selected-button' );
689 this.$el.addClass( 'delete-selected-button hidden' );
691 this.toggleDisabled();
697 * When MEDIA_TRASH is true, a button that handles bulk Delete Permanently logic
700 * @augments wp.media.view.DeleteSelectedButton
701 * @augments wp.media.view.Button
702 * @augments wp.media.View
703 * @augments wp.Backbone.View
704 * @augments Backbone.View
706 media.view.DeleteSelectedPermanentlyButton = media.view.DeleteSelectedButton.extend({
707 initialize: function() {
708 media.view.DeleteSelectedButton.prototype.initialize.apply( this, arguments );
709 this.listenTo( this.controller, 'select:activate', this.selectActivate );
710 this.listenTo( this.controller, 'select:deactivate', this.selectDeactivate );
713 filterChange: function( model ) {
714 this.canShow = ( 'trash' === model.get( 'status' ) );
717 selectActivate: function() {
718 this.toggleDisabled();
719 this.$el.toggleClass( 'hidden', ! this.canShow );
722 selectDeactivate: function() {
723 this.toggleDisabled();
724 this.$el.addClass( 'hidden' );
728 media.view.Button.prototype.render.apply( this, arguments );
729 this.selectActivate();
734 }(jQuery, _, Backbone, wp));