WordPress/wp-includes/js/media/views/frame/image-details.js
Scott Taylor a5478d7adb Let us pray to the gods of backwards compatibility:
* 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
2015-03-31 02:03:29 +00:00

178 lines
4.4 KiB
JavaScript

/*globals wp */
/**
* wp.media.view.MediaFrame.ImageDetails
*
* A media frame for manipulating an image that's already been inserted
* into a post.
*
* @class
* @augments wp.media.view.MediaFrame.Select
* @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 Select = wp.media.view.MediaFrame.Select,
l10n = wp.media.view.l10n,
ImageDetails;
ImageDetails = Select.extend({
defaults: {
id: 'image',
url: '',
menu: 'image-details',
content: 'image-details',
toolbar: 'image-details',
type: 'link',
title: l10n.imageDetailsTitle,
priority: 120
},
initialize: function( options ) {
this.image = new wp.media.model.PostImage( options.metadata );
this.options.selection = new wp.media.model.Selection( this.image.attachment, { multiple: false } );
Select.prototype.initialize.apply( this, arguments );
},
bindHandlers: function() {
Select.prototype.bindHandlers.apply( this, arguments );
this.on( 'menu:create:image-details', this.createMenu, this );
this.on( 'content:create:image-details', this.imageDetailsContent, this );
this.on( 'content:render:edit-image', this.editImageContent, this );
this.on( 'toolbar:render:image-details', this.renderImageDetailsToolbar, this );
// override the select toolbar
this.on( 'toolbar:render:replace', this.renderReplaceImageToolbar, this );
},
createStates: function() {
this.states.add([
new wp.media.controller.ImageDetails({
image: this.image,
editable: false
}),
new wp.media.controller.ReplaceImage({
id: 'replace-image',
library: wp.media.query( { type: 'image' } ),
image: this.image,
multiple: false,
title: l10n.imageReplaceTitle,
toolbar: 'replace',
priority: 80,
displaySettings: true
}),
new wp.media.controller.EditImage( {
image: this.image,
selection: this.options.selection
} )
]);
},
imageDetailsContent: function( options ) {
options.view = new wp.media.view.ImageDetails({
controller: this,
model: this.state().image,
attachment: this.state().image.attachment
});
},
editImageContent: function() {
var state = this.state(),
model = state.get('image'),
view;
if ( ! model ) {
return;
}
view = new wp.media.view.EditImage( { model: model, controller: this } ).render();
this.content.set( view );
// after bringing in the frame, load the actual editor via an ajax call
view.loadEditor();
},
renderImageDetailsToolbar: function() {
this.toolbar.set( new wp.media.view.Toolbar({
controller: this,
items: {
select: {
style: 'primary',
text: l10n.update,
priority: 80,
click: function() {
var controller = this.controller,
state = controller.state();
controller.close();
// not sure if we want to use wp.media.string.image which will create a shortcode or
// perhaps wp.html.string to at least to build the <img />
state.trigger( 'update', controller.image.toJSON() );
// Restore and reset the default state.
controller.setState( controller.options.state );
controller.reset();
}
}
}
}) );
},
renderReplaceImageToolbar: function() {
var frame = this,
lastState = frame.lastState(),
previous = lastState && lastState.id;
this.toolbar.set( new wp.media.view.Toolbar({
controller: this,
items: {
back: {
text: l10n.back,
priority: 20,
click: function() {
if ( previous ) {
frame.setState( previous );
} else {
frame.close();
}
}
},
replace: {
style: 'primary',
text: l10n.replace,
priority: 80,
click: function() {
var controller = this.controller,
state = controller.state(),
selection = state.get( 'selection' ),
attachment = selection.single();
controller.close();
controller.image.changeAttachment( attachment, state.display( attachment ) );
// not sure if we want to use wp.media.string.image which will create a shortcode or
// perhaps wp.html.string to at least to build the <img />
state.trigger( 'replace', controller.image.toJSON() );
// Restore and reset the default state.
controller.setState( controller.options.state );
controller.reset();
}
}
}
}) );
}
});
module.exports = ImageDetails;