mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-04 09:49:37 +01:00
a5478d7adb
* The way that the JS modules for media are currently set up turns the existing global `wp.media` namespace into a read-only API, this is bad. * For the existing module implementation to work with plugins, those looking to override or extend a class would have to modify their own plugin to use `browserify` - we can't expect this to happen * Because the general way that plugins override media classes is via machete (resetting them to something else), we cannot use `require( 'module' )` in the internal code for media modules We CAN continue to use `require( 'fun/js' )` in the manifests for media. Future code/projects should carefully consider what is made to be public API. In 3.5, EVERYTHING was made public, so everything shall remain public. See #31684, #28510. Built from https://develop.svn.wordpress.org/trunk@31935 git-svn-id: http://core.svn.wordpress.org/trunk@31914 1a063a9b-81f0-0310-95a4-ce76da25c4cd
171 lines
4.0 KiB
JavaScript
171 lines
4.0 KiB
JavaScript
/*globals wp, _ */
|
|
|
|
/**
|
|
* wp.media.view.MediaFrame.Select
|
|
*
|
|
* A frame for selecting an item or items from the media library.
|
|
*
|
|
* @class
|
|
* @augments wp.media.view.MediaFrame
|
|
* @augments wp.media.view.Frame
|
|
* @augments wp.media.View
|
|
* @augments wp.Backbone.View
|
|
* @augments Backbone.View
|
|
* @mixes wp.media.controller.StateMachine
|
|
*/
|
|
|
|
var MediaFrame = wp.media.view.MediaFrame,
|
|
l10n = wp.media.view.l10n,
|
|
Select;
|
|
|
|
Select = MediaFrame.extend({
|
|
initialize: function() {
|
|
// Call 'initialize' directly on the parent class.
|
|
MediaFrame.prototype.initialize.apply( this, arguments );
|
|
|
|
_.defaults( this.options, {
|
|
selection: [],
|
|
library: {},
|
|
multiple: false,
|
|
state: 'library'
|
|
});
|
|
|
|
this.createSelection();
|
|
this.createStates();
|
|
this.bindHandlers();
|
|
},
|
|
|
|
/**
|
|
* Attach a selection collection to the frame.
|
|
*
|
|
* A selection is a collection of attachments used for a specific purpose
|
|
* by a media frame. e.g. Selecting an attachment (or many) to insert into
|
|
* post content.
|
|
*
|
|
* @see media.model.Selection
|
|
*/
|
|
createSelection: function() {
|
|
var selection = this.options.selection;
|
|
|
|
if ( ! (selection instanceof wp.media.model.Selection) ) {
|
|
this.options.selection = new wp.media.model.Selection( selection, {
|
|
multiple: this.options.multiple
|
|
});
|
|
}
|
|
|
|
this._selection = {
|
|
attachments: new wp.media.model.Attachments(),
|
|
difference: []
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Create the default states on the frame.
|
|
*/
|
|
createStates: function() {
|
|
var options = this.options;
|
|
|
|
if ( this.options.states ) {
|
|
return;
|
|
}
|
|
|
|
// Add the default states.
|
|
this.states.add([
|
|
// Main states.
|
|
new wp.media.controller.Library({
|
|
library: wp.media.query( options.library ),
|
|
multiple: options.multiple,
|
|
title: options.title,
|
|
priority: 20
|
|
})
|
|
]);
|
|
},
|
|
|
|
/**
|
|
* Bind region mode event callbacks.
|
|
*
|
|
* @see media.controller.Region.render
|
|
*/
|
|
bindHandlers: function() {
|
|
this.on( 'router:create:browse', this.createRouter, this );
|
|
this.on( 'router:render:browse', this.browseRouter, this );
|
|
this.on( 'content:create:browse', this.browseContent, this );
|
|
this.on( 'content:render:upload', this.uploadContent, this );
|
|
this.on( 'toolbar:create:select', this.createSelectToolbar, this );
|
|
},
|
|
|
|
/**
|
|
* Render callback for the router region in the `browse` mode.
|
|
*
|
|
* @param {wp.media.view.Router} routerView
|
|
*/
|
|
browseRouter: function( routerView ) {
|
|
routerView.set({
|
|
upload: {
|
|
text: l10n.uploadFilesTitle,
|
|
priority: 20
|
|
},
|
|
browse: {
|
|
text: l10n.mediaLibraryTitle,
|
|
priority: 40
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Render callback for the content region in the `browse` mode.
|
|
*
|
|
* @param {wp.media.controller.Region} contentRegion
|
|
*/
|
|
browseContent: function( contentRegion ) {
|
|
var state = this.state();
|
|
|
|
this.$el.removeClass('hide-toolbar');
|
|
|
|
// Browse our library of attachments.
|
|
contentRegion.view = new wp.media.view.AttachmentsBrowser({
|
|
controller: this,
|
|
collection: state.get('library'),
|
|
selection: state.get('selection'),
|
|
model: state,
|
|
sortable: state.get('sortable'),
|
|
search: state.get('searchable'),
|
|
filters: state.get('filterable'),
|
|
display: state.has('display') ? state.get('display') : state.get('displaySettings'),
|
|
dragInfo: state.get('dragInfo'),
|
|
|
|
idealColumnWidth: state.get('idealColumnWidth'),
|
|
suggestedWidth: state.get('suggestedWidth'),
|
|
suggestedHeight: state.get('suggestedHeight'),
|
|
|
|
AttachmentView: state.get('AttachmentView')
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Render callback for the content region in the `upload` mode.
|
|
*/
|
|
uploadContent: function() {
|
|
this.$el.removeClass( 'hide-toolbar' );
|
|
this.content.set( new wp.media.view.UploaderInline({
|
|
controller: this
|
|
}) );
|
|
},
|
|
|
|
/**
|
|
* Toolbars
|
|
*
|
|
* @param {Object} toolbar
|
|
* @param {Object} [options={}]
|
|
* @this wp.media.controller.Region
|
|
*/
|
|
createSelectToolbar: function( toolbar, options ) {
|
|
options = options || this.options.button || {};
|
|
options.controller = this;
|
|
|
|
toolbar.view = new wp.media.view.Toolbar.Select( options );
|
|
}
|
|
});
|
|
|
|
module.exports = Select;
|