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 new MediaElementPlayer( el, media.mixin.mejsSettings );
307 * A router for handling the browser history and application state.
310 * @augments Backbone.Router
312 media.view.MediaFrame.Manage.Router = Backbone.Router.extend({
314 'upload.php?item=:slug': 'showItem',
315 'upload.php?search=:query': 'search'
318 // Map routes against the page URL
319 baseUrl: function( url ) {
320 return 'upload.php' + url;
323 // Respond to the search route by filling the search field and trigggering the input event
324 search: function( query ) {
325 $( '#media-search-input' ).val( query ).trigger( 'input' );
328 // Show the modal with a specific item
329 showItem: function( query ) {
330 var library = media.frame.state().get('library'), item;
332 // Trigger the media frame to open the correct item
333 item = library.findWhere( { id: parseInt( query, 10 ) } );
335 media.frame.trigger( 'edit:attachment', item );
337 item = media.attachment( query );
338 media.frame.listenTo( item, 'change', function( model ) {
339 media.frame.stopListening( item );
340 media.frame.trigger( 'edit:attachment', model );
347 media.view.EditImage.Details = media.view.EditImage.extend({
348 initialize: function( options ) {
349 this.editor = window.imageEdit;
350 this.frame = options.frame;
351 this.controller = options.controller;
352 media.View.prototype.initialize.apply( this, arguments );
356 this.frame.content.mode( 'edit-metadata' );
362 this.model.fetch().done( function() {
363 self.frame.content.mode( 'edit-metadata' );
369 * A frame for editing the details of a specific media item.
371 * Opens in a modal by default.
373 * Requires an attachment model to be passed in the options hash under `model`.
376 * @augments wp.media.view.Frame
377 * @augments wp.media.View
378 * @augments wp.Backbone.View
379 * @augments Backbone.View
380 * @mixes wp.media.controller.StateMachine
382 media.view.MediaFrame.EditAttachments = media.view.MediaFrame.extend({
384 className: 'edit-attachment-frame',
385 template: media.template( 'edit-attachment-frame' ),
386 regions: [ 'title', 'content' ],
389 'click .left': 'previousMediaItem',
390 'click .right': 'nextMediaItem'
393 initialize: function() {
394 media.view.Frame.prototype.initialize.apply( this, arguments );
396 _.defaults( this.options, {
398 state: 'edit-attachment'
401 this.controller = this.options.controller;
402 this.gridRouter = this.controller.gridRouter;
403 this.library = this.options.library;
405 if ( this.options.model ) {
406 this.model = this.options.model;
413 this.title.mode( 'default' );
417 bindHandlers: function() {
418 // Bind default title creation.
419 this.on( 'title:create:default', this.createTitle, this );
421 // Close the modal if the attachment is deleted.
422 this.listenTo( this.model, 'change:status destroy', this.close, this );
424 this.on( 'content:create:edit-metadata', this.editMetadataMode, this );
425 this.on( 'content:create:edit-image', this.editImageMode, this );
426 this.on( 'content:render:edit-image', this.editImageModeRender, this );
427 this.on( 'close', this.detach );
430 createModal: function() {
433 // Initialize modal container view.
434 if ( this.options.modal ) {
435 this.modal = new media.view.Modal({
437 title: this.options.title
440 this.modal.on( 'open', function () {
441 $( 'body' ).on( 'keydown.media-modal', _.bind( self.keyEvent, self ) );
444 // Completely destroy the modal DOM element when closing it.
445 this.modal.on( 'close', function() {
447 $( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
448 // Restore the original focus item if possible
449 $( 'li.attachment[data-id="' + self.model.get( 'id' ) +'"]' ).focus();
453 // Set this frame as the modal's content.
454 this.modal.content( this );
460 * Add the default states to the frame.
462 createStates: function() {
464 new media.controller.EditAttachmentMetadata( { model: this.model } )
469 * Content region rendering callback for the `edit-metadata` mode.
471 * @param {Object} contentRegion Basic object with a `view` property, which
472 * should be set with the proper region view.
474 editMetadataMode: function( contentRegion ) {
475 contentRegion.view = new media.view.Attachment.Details.TwoColumn({
481 * Attach a subview to display fields added via the
482 * `attachment_fields_to_edit` filter.
484 contentRegion.view.views.set( '.attachment-compat', new media.view.AttachmentCompat({
489 // Update browser url when navigating media details
491 this.gridRouter.navigate( this.gridRouter.baseUrl( '?item=' + this.model.id ) );
496 * Render the EditImage view into the frame's content region.
498 * @param {Object} contentRegion Basic object with a `view` property, which
499 * should be set with the proper region view.
501 editImageMode: function( contentRegion ) {
502 var editImageController = new media.controller.EditImage( {
506 // Noop some methods.
507 editImageController._toolbar = function() {};
508 editImageController._router = function() {};
509 editImageController._menu = function() {};
511 contentRegion.view = new media.view.EditImage.Details( {
514 controller: editImageController
518 editImageModeRender: function( view ) {
519 view.on( 'ready', view.loadEditor );
522 toggleNav: function() {
523 this.$('.left').toggleClass( 'disabled', ! this.hasPrevious() );
524 this.$('.right').toggleClass( 'disabled', ! this.hasNext() );
530 rerender: function() {
531 // Only rerender the `content` region.
532 if ( this.content.mode() !== 'edit-metadata' ) {
533 this.content.mode( 'edit-metadata' );
535 this.content.render();
542 * Click handler to switch to the previous media item.
544 previousMediaItem: function() {
545 if ( ! this.hasPrevious() ) {
546 this.$( '.left' ).blur();
549 this.model = this.library.at( this.getCurrentIndex() - 1 );
551 this.$( '.left' ).focus();
555 * Click handler to switch to the next media item.
557 nextMediaItem: function() {
558 if ( ! this.hasNext() ) {
559 this.$( '.right' ).blur();
562 this.model = this.library.at( this.getCurrentIndex() + 1 );
564 this.$( '.right' ).focus();
567 getCurrentIndex: function() {
568 return this.library.indexOf( this.model );
571 hasNext: function() {
572 return ( this.getCurrentIndex() + 1 ) < this.library.length;
575 hasPrevious: function() {
576 return ( this.getCurrentIndex() - 1 ) > -1;
579 * Respond to the keyboard events: right arrow, left arrow, except when
580 * focus is in a textarea or input field.
582 keyEvent: function( event ) {
583 if ( ( 'INPUT' === event.target.nodeName || 'TEXTAREA' === event.target.nodeName ) && ! ( event.target.readOnly || event.target.disabled ) ) {
587 // The right arrow key
588 if ( 39 === event.keyCode ) {
589 this.nextMediaItem();
591 // The left arrow key
592 if ( 37 === event.keyCode ) {
593 this.previousMediaItem();
597 resetRoute: function() {
598 this.gridRouter.navigate( this.gridRouter.baseUrl( '' ) );
602 media.view.SelectModeToggleButton = media.view.Button.extend({
603 initialize: function() {
604 media.view.Button.prototype.initialize.apply( this, arguments );
605 this.listenTo( this.controller, 'select:activate select:deactivate', this.toggleBulkEditHandler );
606 this.listenTo( this.controller, 'selection:action:done', this.back );
610 this.controller.deactivateMode( 'select' ).activateMode( 'edit' );
614 media.view.Button.prototype.click.apply( this, arguments );
615 if ( this.controller.isModeActive( 'select' ) ) {
618 this.controller.deactivateMode( 'edit' ).activateMode( 'select' );
623 media.view.Button.prototype.render.apply( this, arguments );
624 this.$el.addClass( 'select-mode-toggle-button' );
628 toggleBulkEditHandler: function() {
629 var toolbar = this.controller.content.get().toolbar, children;
631 children = toolbar.$( '.media-toolbar-secondary > *, .media-toolbar-primary > *');
633 // TODO: the Frame should be doing all of this.
634 if ( this.controller.isModeActive( 'select' ) ) {
635 this.model.set( 'text', l10n.cancelSelection );
636 children.not( '.media-button' ).hide();
638 toolbar.$( '.delete-selected-button' ).removeClass( 'hidden' );
640 this.model.set( 'text', l10n.bulkSelect );
641 this.controller.content.get().$el.removeClass( 'fixed' );
642 toolbar.$el.css( 'width', '' );
643 toolbar.$( '.delete-selected-button' ).addClass( 'hidden' );
644 children.not( '.spinner, .media-button' ).show();
645 this.controller.state().get( 'selection' ).reset();
651 * A button that handles bulk Delete/Trash logic
654 * @augments wp.media.view.Button
655 * @augments wp.media.View
656 * @augments wp.Backbone.View
657 * @augments Backbone.View
659 media.view.DeleteSelectedButton = media.view.Button.extend({
660 initialize: function() {
661 media.view.Button.prototype.initialize.apply( this, arguments );
662 if ( this.options.filters ) {
663 this.listenTo( this.options.filters.model, 'change', this.filterChange );
665 this.listenTo( this.controller, 'selection:toggle', this.toggleDisabled );
668 filterChange: function( model ) {
669 if ( 'trash' === model.get( 'status' ) ) {
670 this.model.set( 'text', l10n.untrashSelected );
671 } else if ( media.view.settings.mediaTrash ) {
672 this.model.set( 'text', l10n.trashSelected );
674 this.model.set( 'text', l10n.deleteSelected );
678 toggleDisabled: function() {
679 this.model.set( 'disabled', ! this.controller.state().get( 'selection' ).length );
683 media.view.Button.prototype.render.apply( this, arguments );
684 if ( this.controller.isModeActive( 'select' ) ) {
685 this.$el.addClass( 'delete-selected-button' );
687 this.$el.addClass( 'delete-selected-button hidden' );
689 this.toggleDisabled();
695 * When MEDIA_TRASH is true, a button that handles bulk Delete Permanently logic
698 * @augments wp.media.view.DeleteSelectedButton
699 * @augments wp.media.view.Button
700 * @augments wp.media.View
701 * @augments wp.Backbone.View
702 * @augments Backbone.View
704 media.view.DeleteSelectedPermanentlyButton = media.view.DeleteSelectedButton.extend({
705 initialize: function() {
706 media.view.DeleteSelectedButton.prototype.initialize.apply( this, arguments );
707 this.listenTo( this.controller, 'select:activate', this.selectActivate );
708 this.listenTo( this.controller, 'select:deactivate', this.selectDeactivate );
711 filterChange: function( model ) {
712 this.canShow = ( 'trash' === model.get( 'status' ) );
715 selectActivate: function() {
716 this.toggleDisabled();
717 this.$el.toggleClass( 'hidden', ! this.canShow );
720 selectDeactivate: function() {
721 this.toggleDisabled();
722 this.$el.addClass( 'hidden' );
726 media.view.Button.prototype.render.apply( this, arguments );
727 this.selectActivate();
732 }(jQuery, _, Backbone, wp));