Edit File by line
/home/barbar84/www/wp-admin/js/widgets
File: media-image-widget.js
/**
[0] Fix | Delete
* @output wp-admin/js/widgets/media-image-widget.js
[1] Fix | Delete
*/
[2] Fix | Delete
[3] Fix | Delete
/* eslint consistent-this: [ "error", "control" ] */
[4] Fix | Delete
(function( component, $ ) {
[5] Fix | Delete
'use strict';
[6] Fix | Delete
[7] Fix | Delete
var ImageWidgetModel, ImageWidgetControl;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Image widget model.
[11] Fix | Delete
*
[12] Fix | Delete
* See WP_Widget_Media_Image::enqueue_admin_scripts() for amending prototype from PHP exports.
[13] Fix | Delete
*
[14] Fix | Delete
* @class wp.mediaWidgets.modelConstructors.media_image
[15] Fix | Delete
* @augments wp.mediaWidgets.MediaWidgetModel
[16] Fix | Delete
*/
[17] Fix | Delete
ImageWidgetModel = component.MediaWidgetModel.extend({});
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Image widget control.
[21] Fix | Delete
*
[22] Fix | Delete
* See WP_Widget_Media_Image::enqueue_admin_scripts() for amending prototype from PHP exports.
[23] Fix | Delete
*
[24] Fix | Delete
* @class wp.mediaWidgets.controlConstructors.media_audio
[25] Fix | Delete
* @augments wp.mediaWidgets.MediaWidgetControl
[26] Fix | Delete
*/
[27] Fix | Delete
ImageWidgetControl = component.MediaWidgetControl.extend(/** @lends wp.mediaWidgets.controlConstructors.media_image.prototype */{
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* View events.
[31] Fix | Delete
*
[32] Fix | Delete
* @type {object}
[33] Fix | Delete
*/
[34] Fix | Delete
events: _.extend( {}, component.MediaWidgetControl.prototype.events, {
[35] Fix | Delete
'click .media-widget-preview.populated': 'editMedia'
[36] Fix | Delete
} ),
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Render preview.
[40] Fix | Delete
*
[41] Fix | Delete
* @return {void}
[42] Fix | Delete
*/
[43] Fix | Delete
renderPreview: function renderPreview() {
[44] Fix | Delete
var control = this, previewContainer, previewTemplate, fieldsContainer, fieldsTemplate, linkInput;
[45] Fix | Delete
if ( ! control.model.get( 'attachment_id' ) && ! control.model.get( 'url' ) ) {
[46] Fix | Delete
return;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
previewContainer = control.$el.find( '.media-widget-preview' );
[50] Fix | Delete
previewTemplate = wp.template( 'wp-media-widget-image-preview' );
[51] Fix | Delete
previewContainer.html( previewTemplate( control.previewTemplateProps.toJSON() ) );
[52] Fix | Delete
previewContainer.addClass( 'populated' );
[53] Fix | Delete
[54] Fix | Delete
linkInput = control.$el.find( '.link' );
[55] Fix | Delete
if ( ! linkInput.is( document.activeElement ) ) {
[56] Fix | Delete
fieldsContainer = control.$el.find( '.media-widget-fields' );
[57] Fix | Delete
fieldsTemplate = wp.template( 'wp-media-widget-image-fields' );
[58] Fix | Delete
fieldsContainer.html( fieldsTemplate( control.previewTemplateProps.toJSON() ) );
[59] Fix | Delete
}
[60] Fix | Delete
},
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Open the media image-edit frame to modify the selected item.
[64] Fix | Delete
*
[65] Fix | Delete
* @return {void}
[66] Fix | Delete
*/
[67] Fix | Delete
editMedia: function editMedia() {
[68] Fix | Delete
var control = this, mediaFrame, updateCallback, defaultSync, metadata;
[69] Fix | Delete
[70] Fix | Delete
metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
[71] Fix | Delete
[72] Fix | Delete
// Needed or else none will not be selected if linkUrl is not also empty.
[73] Fix | Delete
if ( 'none' === metadata.link ) {
[74] Fix | Delete
metadata.linkUrl = '';
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
// Set up the media frame.
[78] Fix | Delete
mediaFrame = wp.media({
[79] Fix | Delete
frame: 'image',
[80] Fix | Delete
state: 'image-details',
[81] Fix | Delete
metadata: metadata
[82] Fix | Delete
});
[83] Fix | Delete
mediaFrame.$el.addClass( 'media-widget' );
[84] Fix | Delete
[85] Fix | Delete
updateCallback = function() {
[86] Fix | Delete
var mediaProps, linkType;
[87] Fix | Delete
[88] Fix | Delete
// Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
[89] Fix | Delete
mediaProps = mediaFrame.state().attributes.image.toJSON();
[90] Fix | Delete
linkType = mediaProps.link;
[91] Fix | Delete
mediaProps.link = mediaProps.linkUrl;
[92] Fix | Delete
control.selectedAttachment.set( mediaProps );
[93] Fix | Delete
control.displaySettings.set( 'link', linkType );
[94] Fix | Delete
[95] Fix | Delete
control.model.set( _.extend(
[96] Fix | Delete
control.mapMediaToModelProps( mediaProps ),
[97] Fix | Delete
{ error: false }
[98] Fix | Delete
) );
[99] Fix | Delete
};
[100] Fix | Delete
[101] Fix | Delete
mediaFrame.state( 'image-details' ).on( 'update', updateCallback );
[102] Fix | Delete
mediaFrame.state( 'replace-image' ).on( 'replace', updateCallback );
[103] Fix | Delete
[104] Fix | Delete
// Disable syncing of attachment changes back to server. See <https://core.trac.wordpress.org/ticket/40403>.
[105] Fix | Delete
defaultSync = wp.media.model.Attachment.prototype.sync;
[106] Fix | Delete
wp.media.model.Attachment.prototype.sync = function rejectedSync() {
[107] Fix | Delete
return $.Deferred().rejectWith( this ).promise();
[108] Fix | Delete
};
[109] Fix | Delete
mediaFrame.on( 'close', function onClose() {
[110] Fix | Delete
mediaFrame.detach();
[111] Fix | Delete
wp.media.model.Attachment.prototype.sync = defaultSync;
[112] Fix | Delete
});
[113] Fix | Delete
[114] Fix | Delete
mediaFrame.open();
[115] Fix | Delete
},
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Get props which are merged on top of the model when an embed is chosen (as opposed to an attachment).
[119] Fix | Delete
*
[120] Fix | Delete
* @return {Object} Reset/override props.
[121] Fix | Delete
*/
[122] Fix | Delete
getEmbedResetProps: function getEmbedResetProps() {
[123] Fix | Delete
return _.extend(
[124] Fix | Delete
component.MediaWidgetControl.prototype.getEmbedResetProps.call( this ),
[125] Fix | Delete
{
[126] Fix | Delete
size: 'full',
[127] Fix | Delete
width: 0,
[128] Fix | Delete
height: 0
[129] Fix | Delete
}
[130] Fix | Delete
);
[131] Fix | Delete
},
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Get the instance props from the media selection frame.
[135] Fix | Delete
*
[136] Fix | Delete
* Prevent the image_title attribute from being initially set when adding an image from the media library.
[137] Fix | Delete
*
[138] Fix | Delete
* @param {wp.media.view.MediaFrame.Select} mediaFrame - Select frame.
[139] Fix | Delete
* @return {Object} Props.
[140] Fix | Delete
*/
[141] Fix | Delete
getModelPropsFromMediaFrame: function getModelPropsFromMediaFrame( mediaFrame ) {
[142] Fix | Delete
var control = this;
[143] Fix | Delete
return _.omit(
[144] Fix | Delete
component.MediaWidgetControl.prototype.getModelPropsFromMediaFrame.call( control, mediaFrame ),
[145] Fix | Delete
'image_title'
[146] Fix | Delete
);
[147] Fix | Delete
},
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Map model props to preview template props.
[151] Fix | Delete
*
[152] Fix | Delete
* @return {Object} Preview template props.
[153] Fix | Delete
*/
[154] Fix | Delete
mapModelToPreviewTemplateProps: function mapModelToPreviewTemplateProps() {
[155] Fix | Delete
var control = this, previewTemplateProps, url;
[156] Fix | Delete
url = control.model.get( 'url' );
[157] Fix | Delete
previewTemplateProps = component.MediaWidgetControl.prototype.mapModelToPreviewTemplateProps.call( control );
[158] Fix | Delete
previewTemplateProps.currentFilename = url ? url.replace( /\?.*$/, '' ).replace( /^.+\//, '' ) : '';
[159] Fix | Delete
previewTemplateProps.link_url = control.model.get( 'link_url' );
[160] Fix | Delete
return previewTemplateProps;
[161] Fix | Delete
}
[162] Fix | Delete
});
[163] Fix | Delete
[164] Fix | Delete
// Exports.
[165] Fix | Delete
component.controlConstructors.media_image = ImageWidgetControl;
[166] Fix | Delete
component.modelConstructors.media_image = ImageWidgetModel;
[167] Fix | Delete
[168] Fix | Delete
})( wp.mediaWidgets, jQuery );
[169] Fix | Delete
[170] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function