- if ( typeof checkCounts !== 'undefined' ) {
- this.listenTo( media.model.Attachments.all, 'change:type', this.mediaTypeCounts );
- }
-
- this.on( 'menu:create:gallery', this.createMenu, this );
- this.on( 'menu:create:playlist', this.createMenu, this );
- this.on( 'menu:create:video-playlist', this.createMenu, this );
- this.on( 'toolbar:create:main-insert', this.createToolbar, this );
- this.on( 'toolbar:create:main-gallery', this.createToolbar, this );
- this.on( 'toolbar:create:main-playlist', this.createToolbar, this );
- this.on( 'toolbar:create:main-video-playlist', this.createToolbar, this );
- this.on( 'toolbar:create:featured-image', this.featuredImageToolbar, this );
- this.on( 'toolbar:create:main-embed', this.mainEmbedToolbar, this );
-
- handlers = {
- menu: {
- 'default': 'mainMenu',
- 'gallery': 'galleryMenu',
- 'playlist': 'playlistMenu',
- 'video-playlist': 'videoPlaylistMenu'
- },
-
- content: {
- 'embed': 'embedContent',
- 'edit-image': 'editImageContent',
- 'edit-selection': 'editSelectionContent'
- },
-
- toolbar: {
- 'main-insert': 'mainInsertToolbar',
- 'main-gallery': 'mainGalleryToolbar',
- 'gallery-edit': 'galleryEditToolbar',
- 'gallery-add': 'galleryAddToolbar',
- 'main-playlist': 'mainPlaylistToolbar',
- 'playlist-edit': 'playlistEditToolbar',
- 'playlist-add': 'playlistAddToolbar',
- 'main-video-playlist': 'mainVideoPlaylistToolbar',
- 'video-playlist-edit': 'videoPlaylistEditToolbar',
- 'video-playlist-add': 'videoPlaylistAddToolbar'
- }
- };
-
- _.each( handlers, function( regionHandlers, region ) {
- _.each( regionHandlers, function( callback, handler ) {
- this.on( region + ':render:' + handler, this[ callback ], this );
- }, this );
- }, this );
- },
-
- activate: function() {
- // Hide menu items for states tied to particular media types if there are no items
- _.each( this.counts, function( type ) {
- if ( type.count < 1 ) {
- this.menuItemVisibility( type.state, 'hide' );
- }
- }, this );
- },
-
- mediaTypeCounts: function( model, attr ) {
- if ( typeof this.counts[ attr ] !== 'undefined' && this.counts[ attr ].count < 1 ) {
- this.counts[ attr ].count++;
- this.menuItemVisibility( this.counts[ attr ].state, 'show' );
- }
- },
-
- // Menus
- /**
- * @param {wp.Backbone.View} view
- */
- mainMenu: function( view ) {
- view.set({
- 'library-separator': new media.View({
- className: 'separator',
- priority: 100
- })
- });
- },
-
- menuItemVisibility: function( state, visibility ) {
- var menu = this.menu.get();
- if ( visibility === 'hide' ) {
- menu.hide( state );
- } else if ( visibility === 'show' ) {
- menu.show( state );
- }
- },
- /**
- * @param {wp.Backbone.View} view
- */
- galleryMenu: function( view ) {
- var lastState = this.lastState(),
- previous = lastState && lastState.id,
- frame = this;
-
- view.set({
- cancel: {
- text: l10n.cancelGalleryTitle,
- priority: 20,
- click: function() {
- if ( previous ) {
- frame.setState( previous );
- } else {
- frame.close();
- }
-
- // Keep focus inside media modal
- // after canceling a gallery
- this.controller.modal.focusManager.focus();
- }
- },
- separateCancel: new media.View({
- className: 'separator',
- priority: 40
- })
- });
- },
-
- playlistMenu: function( view ) {
- var lastState = this.lastState(),
- previous = lastState && lastState.id,
- frame = this;
-
- view.set({
- cancel: {
- text: l10n.cancelPlaylistTitle,
- priority: 20,
- click: function() {
- if ( previous ) {
- frame.setState( previous );
- } else {
- frame.close();
- }
- }
- },
- separateCancel: new media.View({
- className: 'separator',
- priority: 40
- })
- });
- },
-
- videoPlaylistMenu: function( view ) {
- var lastState = this.lastState(),
- previous = lastState && lastState.id,
- frame = this;
-
- view.set({
- cancel: {
- text: l10n.cancelVideoPlaylistTitle,
- priority: 20,
- click: function() {
- if ( previous ) {
- frame.setState( previous );
- } else {
- frame.close();
- }
- }
- },
- separateCancel: new media.View({
- className: 'separator',
- priority: 40
- })
- });
- },
-
- // Content
- embedContent: function() {
- var view = new media.view.Embed({
- controller: this,
- model: this.state()
- }).render();
-
- this.content.set( view );
-
- if ( ! isTouchDevice ) {
- view.url.focus();
- }
- },
-
- editSelectionContent: function() {
- var state = this.state(),
- selection = state.get('selection'),
- view;
-
- view = new media.view.AttachmentsBrowser({
- controller: this,
- collection: selection,
- selection: selection,
- model: state,
- sortable: true,
- search: false,
- dragInfo: true,
-
- AttachmentView: media.view.Attachment.EditSelection
- }).render();
-
- view.toolbar.set( 'backToLibrary', {
- text: l10n.returnToLibrary,
- priority: -100,
-
- click: function() {
- this.controller.content.mode('browse');
- }
- });
-
- // Browse our library of attachments.
- this.content.set( view );
- },
-
- editImageContent: function() {
- var image = this.state().get('image'),
- view = new media.view.EditImage( { model: image, controller: this } ).render();
-
- this.content.set( view );
-
- // after creating the wrapper view, load the actual editor via an ajax call
- view.loadEditor();
-
- },
-
- // Toolbars
-
- /**
- * @param {wp.Backbone.View} view
- */
- selectionStatusToolbar: function( view ) {
- var editable = this.state().get('editable');
-
- view.set( 'selection', new media.view.Selection({
- controller: this,
- collection: this.state().get('selection'),
- priority: -40,
-
- // If the selection is editable, pass the callback to
- // switch the content mode.
- editable: editable && function() {
- this.controller.content.mode('edit-selection');
- }
- }).render() );
- },
-
- /**
- * @param {wp.Backbone.View} view
- */
- mainInsertToolbar: function( view ) {
- var controller = this;
-
- this.selectionStatusToolbar( view );
-
- view.set( 'insert', {
- style: 'primary',
- priority: 80,
- text: l10n.insertIntoPost,
- requires: { selection: true },
-
- /**
- * @fires wp.media.controller.State#insert
- */
- click: function() {
- var state = controller.state(),
- selection = state.get('selection');
-
- controller.close();
- state.trigger( 'insert', selection ).reset();
- }
- });
- },
-
- /**
- * @param {wp.Backbone.View} view
- */
- mainGalleryToolbar: function( view ) {
- var controller = this;
-
- this.selectionStatusToolbar( view );
-
- view.set( 'gallery', {
- style: 'primary',
- text: l10n.createNewGallery,
- priority: 60,
- requires: { selection: true },
-
- click: function() {
- var selection = controller.state().get('selection'),
- edit = controller.state('gallery-edit'),
- models = selection.where({ type: 'image' });
-
- edit.set( 'library', new media.model.Selection( models, {
- props: selection.props.toJSON(),
- multiple: true
- }) );
-
- this.controller.setState('gallery-edit');
-
- // Keep focus inside media modal
- // after jumping to gallery view
- this.controller.modal.focusManager.focus();
- }
- });
- },
-
- mainPlaylistToolbar: function( view ) {
- var controller = this;
-
- this.selectionStatusToolbar( view );
-
- view.set( 'playlist', {
- style: 'primary',
- text: l10n.createNewPlaylist,
- priority: 100,
- requires: { selection: true },
-
- click: function() {
- var selection = controller.state().get('selection'),
- edit = controller.state('playlist-edit'),
- models = selection.where({ type: 'audio' });
-
- edit.set( 'library', new media.model.Selection( models, {
- props: selection.props.toJSON(),
- multiple: true
- }) );
-
- this.controller.setState('playlist-edit');
-
- // Keep focus inside media modal
- // after jumping to playlist view
- this.controller.modal.focusManager.focus();
- }
- });
- },
-
- mainVideoPlaylistToolbar: function( view ) {
- var controller = this;
-
- this.selectionStatusToolbar( view );
-
- view.set( 'video-playlist', {
- style: 'primary',
- text: l10n.createNewVideoPlaylist,
- priority: 100,
- requires: { selection: true },
-
- click: function() {
- var selection = controller.state().get('selection'),
- edit = controller.state('video-playlist-edit'),
- models = selection.where({ type: 'video' });
-
- edit.set( 'library', new media.model.Selection( models, {
- props: selection.props.toJSON(),
- multiple: true
- }) );
-
- this.controller.setState('video-playlist-edit');
-
- // Keep focus inside media modal
- // after jumping to video playlist view
- this.controller.modal.focusManager.focus();
- }
- });
- },
-
- featuredImageToolbar: function( toolbar ) {
- this.createSelectToolbar( toolbar, {
- text: l10n.setFeaturedImage,
- state: this.options.state
- });
- },
-
- mainEmbedToolbar: function( toolbar ) {
- toolbar.view = new media.view.Toolbar.Embed({
- controller: this
- });
- },
-
- galleryEditToolbar: function() {
- var editing = this.state().get('editing');
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: editing ? l10n.updateGallery : l10n.insertGallery,
- priority: 80,
- requires: { library: true },
-
- /**
- * @fires wp.media.controller.State#update
- */
- click: function() {
- var controller = this.controller,
- state = controller.state();
-
- controller.close();
- state.trigger( 'update', state.get('library') );
-
- // Restore and reset the default state.
- controller.setState( controller.options.state );
- controller.reset();
- }
- }
- }
- }) );
- },
-
- galleryAddToolbar: function() {
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: l10n.addToGallery,
- priority: 80,
- requires: { selection: true },
-
- /**
- * @fires wp.media.controller.State#reset
- */
- click: function() {
- var controller = this.controller,
- state = controller.state(),
- edit = controller.state('gallery-edit');
-
- edit.get('library').add( state.get('selection').models );
- state.trigger('reset');
- controller.setState('gallery-edit');
- }
- }
- }
- }) );
- },
-
- playlistEditToolbar: function() {
- var editing = this.state().get('editing');
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: editing ? l10n.updatePlaylist : l10n.insertPlaylist,
- priority: 80,
- requires: { library: true },
-
- /**
- * @fires wp.media.controller.State#update
- */
- click: function() {
- var controller = this.controller,
- state = controller.state();
-
- controller.close();
- state.trigger( 'update', state.get('library') );
-
- // Restore and reset the default state.
- controller.setState( controller.options.state );
- controller.reset();
- }
- }
- }
- }) );
- },
-
- playlistAddToolbar: function() {
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: l10n.addToPlaylist,
- priority: 80,
- requires: { selection: true },
-
- /**
- * @fires wp.media.controller.State#reset
- */
- click: function() {
- var controller = this.controller,
- state = controller.state(),
- edit = controller.state('playlist-edit');
-
- edit.get('library').add( state.get('selection').models );
- state.trigger('reset');
- controller.setState('playlist-edit');
- }
- }
- }
- }) );
- },
-
- videoPlaylistEditToolbar: function() {
- var editing = this.state().get('editing');
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: editing ? l10n.updateVideoPlaylist : l10n.insertVideoPlaylist,
- priority: 140,
- requires: { library: true },
-
- click: function() {
- var controller = this.controller,
- state = controller.state(),
- library = state.get('library');
-
- library.type = 'video';
-
- controller.close();
- state.trigger( 'update', library );
-
- // Restore and reset the default state.
- controller.setState( controller.options.state );
- controller.reset();
- }
- }
- }
- }) );
- },
-
- videoPlaylistAddToolbar: function() {
- this.toolbar.set( new media.view.Toolbar({
- controller: this,
- items: {
- insert: {
- style: 'primary',
- text: l10n.addToVideoPlaylist,
- priority: 140,
- requires: { selection: true },
-
- click: function() {
- var controller = this.controller,
- state = controller.state(),
- edit = controller.state('video-playlist-edit');
-
- edit.get('library').add( state.get('selection').models );
- state.trigger('reset');
- controller.setState('video-playlist-edit');
- }
- }
- }
- }) );