mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-10 22:58:33 +02:00
* 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
122 lines
3.0 KiB
JavaScript
122 lines
3.0 KiB
JavaScript
/*globals _, Backbone */
|
|
|
|
/**
|
|
* wp.media.view.Settings
|
|
*
|
|
* @class
|
|
* @augments wp.media.View
|
|
* @augments wp.Backbone.View
|
|
* @augments Backbone.View
|
|
*/
|
|
var View = wp.media.View,
|
|
$ = Backbone.$,
|
|
Settings;
|
|
|
|
Settings = View.extend({
|
|
events: {
|
|
'click button': 'updateHandler',
|
|
'change input': 'updateHandler',
|
|
'change select': 'updateHandler',
|
|
'change textarea': 'updateHandler'
|
|
},
|
|
|
|
initialize: function() {
|
|
this.model = this.model || new Backbone.Model();
|
|
this.listenTo( this.model, 'change', this.updateChanges );
|
|
},
|
|
|
|
prepare: function() {
|
|
return _.defaults({
|
|
model: this.model.toJSON()
|
|
}, this.options );
|
|
},
|
|
/**
|
|
* @returns {wp.media.view.Settings} Returns itself to allow chaining
|
|
*/
|
|
render: function() {
|
|
View.prototype.render.apply( this, arguments );
|
|
// Select the correct values.
|
|
_( this.model.attributes ).chain().keys().each( this.update, this );
|
|
return this;
|
|
},
|
|
/**
|
|
* @param {string} key
|
|
*/
|
|
update: function( key ) {
|
|
var value = this.model.get( key ),
|
|
$setting = this.$('[data-setting="' + key + '"]'),
|
|
$buttons, $value;
|
|
|
|
// Bail if we didn't find a matching setting.
|
|
if ( ! $setting.length ) {
|
|
return;
|
|
}
|
|
|
|
// Attempt to determine how the setting is rendered and update
|
|
// the selected value.
|
|
|
|
// Handle dropdowns.
|
|
if ( $setting.is('select') ) {
|
|
$value = $setting.find('[value="' + value + '"]');
|
|
|
|
if ( $value.length ) {
|
|
$setting.find('option').prop( 'selected', false );
|
|
$value.prop( 'selected', true );
|
|
} else {
|
|
// If we can't find the desired value, record what *is* selected.
|
|
this.model.set( key, $setting.find(':selected').val() );
|
|
}
|
|
|
|
// Handle button groups.
|
|
} else if ( $setting.hasClass('button-group') ) {
|
|
$buttons = $setting.find('button').removeClass('active');
|
|
$buttons.filter( '[value="' + value + '"]' ).addClass('active');
|
|
|
|
// Handle text inputs and textareas.
|
|
} else if ( $setting.is('input[type="text"], textarea') ) {
|
|
if ( ! $setting.is(':focus') ) {
|
|
$setting.val( value );
|
|
}
|
|
// Handle checkboxes.
|
|
} else if ( $setting.is('input[type="checkbox"]') ) {
|
|
$setting.prop( 'checked', !! value && 'false' !== value );
|
|
}
|
|
},
|
|
/**
|
|
* @param {Object} event
|
|
*/
|
|
updateHandler: function( event ) {
|
|
var $setting = $( event.target ).closest('[data-setting]'),
|
|
value = event.target.value,
|
|
userSetting;
|
|
|
|
event.preventDefault();
|
|
|
|
if ( ! $setting.length ) {
|
|
return;
|
|
}
|
|
|
|
// Use the correct value for checkboxes.
|
|
if ( $setting.is('input[type="checkbox"]') ) {
|
|
value = $setting[0].checked;
|
|
}
|
|
|
|
// Update the corresponding setting.
|
|
this.model.set( $setting.data('setting'), value );
|
|
|
|
// If the setting has a corresponding user setting,
|
|
// update that as well.
|
|
if ( userSetting = $setting.data('userSetting') ) {
|
|
window.setUserSetting( userSetting, value );
|
|
}
|
|
},
|
|
|
|
updateChanges: function( model ) {
|
|
if ( model.hasChanged() ) {
|
|
_( model.changed ).chain().keys().each( this.update, this );
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = Settings;
|