Edit File by line
/home/barbar84/www/wp-admin/js/widgets
File: media-gallery-widget.js
/**
[0] Fix | Delete
* @output wp-admin/js/widgets/media-gallery-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 GalleryWidgetModel, GalleryWidgetControl, GalleryDetailsMediaFrame;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Custom gallery details frame.
[11] Fix | Delete
*
[12] Fix | Delete
* @since 4.9.0
[13] Fix | Delete
* @class wp.mediaWidgets~GalleryDetailsMediaFrame
[14] Fix | Delete
* @augments wp.media.view.MediaFrame.Post
[15] Fix | Delete
*/
[16] Fix | Delete
GalleryDetailsMediaFrame = wp.media.view.MediaFrame.Post.extend(/** @lends wp.mediaWidgets~GalleryDetailsMediaFrame.prototype */{
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Create the default states.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 4.9.0
[22] Fix | Delete
* @return {void}
[23] Fix | Delete
*/
[24] Fix | Delete
createStates: function createStates() {
[25] Fix | Delete
this.states.add([
[26] Fix | Delete
new wp.media.controller.Library({
[27] Fix | Delete
id: 'gallery',
[28] Fix | Delete
title: wp.media.view.l10n.createGalleryTitle,
[29] Fix | Delete
priority: 40,
[30] Fix | Delete
toolbar: 'main-gallery',
[31] Fix | Delete
filterable: 'uploaded',
[32] Fix | Delete
multiple: 'add',
[33] Fix | Delete
editable: true,
[34] Fix | Delete
[35] Fix | Delete
library: wp.media.query( _.defaults({
[36] Fix | Delete
type: 'image'
[37] Fix | Delete
}, this.options.library ) )
[38] Fix | Delete
}),
[39] Fix | Delete
[40] Fix | Delete
// Gallery states.
[41] Fix | Delete
new wp.media.controller.GalleryEdit({
[42] Fix | Delete
library: this.options.selection,
[43] Fix | Delete
editing: this.options.editing,
[44] Fix | Delete
menu: 'gallery'
[45] Fix | Delete
}),
[46] Fix | Delete
[47] Fix | Delete
new wp.media.controller.GalleryAdd()
[48] Fix | Delete
]);
[49] Fix | Delete
}
[50] Fix | Delete
} );
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Gallery widget model.
[54] Fix | Delete
*
[55] Fix | Delete
* See WP_Widget_Gallery::enqueue_admin_scripts() for amending prototype from PHP exports.
[56] Fix | Delete
*
[57] Fix | Delete
* @since 4.9.0
[58] Fix | Delete
*
[59] Fix | Delete
* @class wp.mediaWidgets.modelConstructors.media_gallery
[60] Fix | Delete
* @augments wp.mediaWidgets.MediaWidgetModel
[61] Fix | Delete
*/
[62] Fix | Delete
GalleryWidgetModel = component.MediaWidgetModel.extend(/** @lends wp.mediaWidgets.modelConstructors.media_gallery.prototype */{} );
[63] Fix | Delete
[64] Fix | Delete
GalleryWidgetControl = component.MediaWidgetControl.extend(/** @lends wp.mediaWidgets.controlConstructors.media_gallery.prototype */{
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* View events.
[68] Fix | Delete
*
[69] Fix | Delete
* @since 4.9.0
[70] Fix | Delete
* @type {object}
[71] Fix | Delete
*/
[72] Fix | Delete
events: _.extend( {}, component.MediaWidgetControl.prototype.events, {
[73] Fix | Delete
'click .media-widget-gallery-preview': 'editMedia'
[74] Fix | Delete
} ),
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Gallery widget control.
[78] Fix | Delete
*
[79] Fix | Delete
* See WP_Widget_Gallery::enqueue_admin_scripts() for amending prototype from PHP exports.
[80] Fix | Delete
*
[81] Fix | Delete
* @constructs wp.mediaWidgets.controlConstructors.media_gallery
[82] Fix | Delete
* @augments wp.mediaWidgets.MediaWidgetControl
[83] Fix | Delete
*
[84] Fix | Delete
* @since 4.9.0
[85] Fix | Delete
* @param {Object} options - Options.
[86] Fix | Delete
* @param {Backbone.Model} options.model - Model.
[87] Fix | Delete
* @param {jQuery} options.el - Control field container element.
[88] Fix | Delete
* @param {jQuery} options.syncContainer - Container element where fields are synced for the server.
[89] Fix | Delete
* @return {void}
[90] Fix | Delete
*/
[91] Fix | Delete
initialize: function initialize( options ) {
[92] Fix | Delete
var control = this;
[93] Fix | Delete
[94] Fix | Delete
component.MediaWidgetControl.prototype.initialize.call( control, options );
[95] Fix | Delete
[96] Fix | Delete
_.bindAll( control, 'updateSelectedAttachments', 'handleAttachmentDestroy' );
[97] Fix | Delete
control.selectedAttachments = new wp.media.model.Attachments();
[98] Fix | Delete
control.model.on( 'change:ids', control.updateSelectedAttachments );
[99] Fix | Delete
control.selectedAttachments.on( 'change', control.renderPreview );
[100] Fix | Delete
control.selectedAttachments.on( 'reset', control.renderPreview );
[101] Fix | Delete
control.updateSelectedAttachments();
[102] Fix | Delete
[103] Fix | Delete
/*
[104] Fix | Delete
* Refresh a Gallery widget partial when the user modifies one of the selected attachments.
[105] Fix | Delete
* This ensures that when an attachment's caption is updated in the media modal the Gallery
[106] Fix | Delete
* widget in the preview will then be refreshed to show the change. Normally doing this
[107] Fix | Delete
* would not be necessary because all of the state should be contained inside the changeset,
[108] Fix | Delete
* as everything done in the Customizer should not make a change to the site unless the
[109] Fix | Delete
* changeset itself is published. Attachments are a current exception to this rule.
[110] Fix | Delete
* For a proposal to include attachments in the customized state, see #37887.
[111] Fix | Delete
*/
[112] Fix | Delete
if ( wp.customize && wp.customize.previewer ) {
[113] Fix | Delete
control.selectedAttachments.on( 'change', function() {
[114] Fix | Delete
wp.customize.previewer.send( 'refresh-widget-partial', control.model.get( 'widget_id' ) );
[115] Fix | Delete
} );
[116] Fix | Delete
}
[117] Fix | Delete
},
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Update the selected attachments if necessary.
[121] Fix | Delete
*
[122] Fix | Delete
* @since 4.9.0
[123] Fix | Delete
* @return {void}
[124] Fix | Delete
*/
[125] Fix | Delete
updateSelectedAttachments: function updateSelectedAttachments() {
[126] Fix | Delete
var control = this, newIds, oldIds, removedIds, addedIds, addedQuery;
[127] Fix | Delete
[128] Fix | Delete
newIds = control.model.get( 'ids' );
[129] Fix | Delete
oldIds = _.pluck( control.selectedAttachments.models, 'id' );
[130] Fix | Delete
[131] Fix | Delete
removedIds = _.difference( oldIds, newIds );
[132] Fix | Delete
_.each( removedIds, function( removedId ) {
[133] Fix | Delete
control.selectedAttachments.remove( control.selectedAttachments.get( removedId ) );
[134] Fix | Delete
});
[135] Fix | Delete
[136] Fix | Delete
addedIds = _.difference( newIds, oldIds );
[137] Fix | Delete
if ( addedIds.length ) {
[138] Fix | Delete
addedQuery = wp.media.query({
[139] Fix | Delete
order: 'ASC',
[140] Fix | Delete
orderby: 'post__in',
[141] Fix | Delete
perPage: -1,
[142] Fix | Delete
post__in: newIds,
[143] Fix | Delete
query: true,
[144] Fix | Delete
type: 'image'
[145] Fix | Delete
});
[146] Fix | Delete
addedQuery.more().done( function() {
[147] Fix | Delete
control.selectedAttachments.reset( addedQuery.models );
[148] Fix | Delete
});
[149] Fix | Delete
}
[150] Fix | Delete
},
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Render preview.
[154] Fix | Delete
*
[155] Fix | Delete
* @since 4.9.0
[156] Fix | Delete
* @return {void}
[157] Fix | Delete
*/
[158] Fix | Delete
renderPreview: function renderPreview() {
[159] Fix | Delete
var control = this, previewContainer, previewTemplate, data;
[160] Fix | Delete
[161] Fix | Delete
previewContainer = control.$el.find( '.media-widget-preview' );
[162] Fix | Delete
previewTemplate = wp.template( 'wp-media-widget-gallery-preview' );
[163] Fix | Delete
[164] Fix | Delete
data = control.previewTemplateProps.toJSON();
[165] Fix | Delete
data.attachments = {};
[166] Fix | Delete
control.selectedAttachments.each( function( attachment ) {
[167] Fix | Delete
data.attachments[ attachment.id ] = attachment.toJSON();
[168] Fix | Delete
} );
[169] Fix | Delete
[170] Fix | Delete
previewContainer.html( previewTemplate( data ) );
[171] Fix | Delete
},
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Determine whether there are selected attachments.
[175] Fix | Delete
*
[176] Fix | Delete
* @since 4.9.0
[177] Fix | Delete
* @return {boolean} Selected.
[178] Fix | Delete
*/
[179] Fix | Delete
isSelected: function isSelected() {
[180] Fix | Delete
var control = this;
[181] Fix | Delete
[182] Fix | Delete
if ( control.model.get( 'error' ) ) {
[183] Fix | Delete
return false;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return control.model.get( 'ids' ).length > 0;
[187] Fix | Delete
},
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Open the media select frame to edit images.
[191] Fix | Delete
*
[192] Fix | Delete
* @since 4.9.0
[193] Fix | Delete
* @return {void}
[194] Fix | Delete
*/
[195] Fix | Delete
editMedia: function editMedia() {
[196] Fix | Delete
var control = this, selection, mediaFrame, mediaFrameProps;
[197] Fix | Delete
[198] Fix | Delete
selection = new wp.media.model.Selection( control.selectedAttachments.models, {
[199] Fix | Delete
multiple: true
[200] Fix | Delete
});
[201] Fix | Delete
[202] Fix | Delete
mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() );
[203] Fix | Delete
selection.gallery = new Backbone.Model( mediaFrameProps );
[204] Fix | Delete
if ( mediaFrameProps.size ) {
[205] Fix | Delete
control.displaySettings.set( 'size', mediaFrameProps.size );
[206] Fix | Delete
}
[207] Fix | Delete
mediaFrame = new GalleryDetailsMediaFrame({
[208] Fix | Delete
frame: 'manage',
[209] Fix | Delete
text: control.l10n.add_to_widget,
[210] Fix | Delete
selection: selection,
[211] Fix | Delete
mimeType: control.mime_type,
[212] Fix | Delete
selectedDisplaySettings: control.displaySettings,
[213] Fix | Delete
showDisplaySettings: control.showDisplaySettings,
[214] Fix | Delete
metadata: mediaFrameProps,
[215] Fix | Delete
editing: true,
[216] Fix | Delete
multiple: true,
[217] Fix | Delete
state: 'gallery-edit'
[218] Fix | Delete
});
[219] Fix | Delete
wp.media.frame = mediaFrame; // See wp.media().
[220] Fix | Delete
[221] Fix | Delete
// Handle selection of a media item.
[222] Fix | Delete
mediaFrame.on( 'update', function onUpdate( newSelection ) {
[223] Fix | Delete
var state = mediaFrame.state(), resultSelection;
[224] Fix | Delete
[225] Fix | Delete
resultSelection = newSelection || state.get( 'selection' );
[226] Fix | Delete
if ( ! resultSelection ) {
[227] Fix | Delete
return;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
// Copy orderby_random from gallery state.
[231] Fix | Delete
if ( resultSelection.gallery ) {
[232] Fix | Delete
control.model.set( control.mapMediaToModelProps( resultSelection.gallery.toJSON() ) );
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
// Directly update selectedAttachments to prevent needing to do additional request.
[236] Fix | Delete
control.selectedAttachments.reset( resultSelection.models );
[237] Fix | Delete
[238] Fix | Delete
// Update models in the widget instance.
[239] Fix | Delete
control.model.set( {
[240] Fix | Delete
ids: _.pluck( resultSelection.models, 'id' )
[241] Fix | Delete
} );
[242] Fix | Delete
} );
[243] Fix | Delete
[244] Fix | Delete
mediaFrame.$el.addClass( 'media-widget' );
[245] Fix | Delete
mediaFrame.open();
[246] Fix | Delete
[247] Fix | Delete
if ( selection ) {
[248] Fix | Delete
selection.on( 'destroy', control.handleAttachmentDestroy );
[249] Fix | Delete
}
[250] Fix | Delete
},
[251] Fix | Delete
[252] Fix | Delete
/**
[253] Fix | Delete
* Open the media select frame to chose an item.
[254] Fix | Delete
*
[255] Fix | Delete
* @since 4.9.0
[256] Fix | Delete
* @return {void}
[257] Fix | Delete
*/
[258] Fix | Delete
selectMedia: function selectMedia() {
[259] Fix | Delete
var control = this, selection, mediaFrame, mediaFrameProps;
[260] Fix | Delete
selection = new wp.media.model.Selection( control.selectedAttachments.models, {
[261] Fix | Delete
multiple: true
[262] Fix | Delete
});
[263] Fix | Delete
[264] Fix | Delete
mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() );
[265] Fix | Delete
if ( mediaFrameProps.size ) {
[266] Fix | Delete
control.displaySettings.set( 'size', mediaFrameProps.size );
[267] Fix | Delete
}
[268] Fix | Delete
mediaFrame = new GalleryDetailsMediaFrame({
[269] Fix | Delete
frame: 'select',
[270] Fix | Delete
text: control.l10n.add_to_widget,
[271] Fix | Delete
selection: selection,
[272] Fix | Delete
mimeType: control.mime_type,
[273] Fix | Delete
selectedDisplaySettings: control.displaySettings,
[274] Fix | Delete
showDisplaySettings: control.showDisplaySettings,
[275] Fix | Delete
metadata: mediaFrameProps,
[276] Fix | Delete
state: 'gallery'
[277] Fix | Delete
});
[278] Fix | Delete
wp.media.frame = mediaFrame; // See wp.media().
[279] Fix | Delete
[280] Fix | Delete
// Handle selection of a media item.
[281] Fix | Delete
mediaFrame.on( 'update', function onUpdate( newSelection ) {
[282] Fix | Delete
var state = mediaFrame.state(), resultSelection;
[283] Fix | Delete
[284] Fix | Delete
resultSelection = newSelection || state.get( 'selection' );
[285] Fix | Delete
if ( ! resultSelection ) {
[286] Fix | Delete
return;
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
// Copy orderby_random from gallery state.
[290] Fix | Delete
if ( resultSelection.gallery ) {
[291] Fix | Delete
control.model.set( control.mapMediaToModelProps( resultSelection.gallery.toJSON() ) );
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
// Directly update selectedAttachments to prevent needing to do additional request.
[295] Fix | Delete
control.selectedAttachments.reset( resultSelection.models );
[296] Fix | Delete
[297] Fix | Delete
// Update widget instance.
[298] Fix | Delete
control.model.set( {
[299] Fix | Delete
ids: _.pluck( resultSelection.models, 'id' )
[300] Fix | Delete
} );
[301] Fix | Delete
} );
[302] Fix | Delete
[303] Fix | Delete
mediaFrame.$el.addClass( 'media-widget' );
[304] Fix | Delete
mediaFrame.open();
[305] Fix | Delete
[306] Fix | Delete
if ( selection ) {
[307] Fix | Delete
selection.on( 'destroy', control.handleAttachmentDestroy );
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
/*
[311] Fix | Delete
* Make sure focus is set inside of modal so that hitting Esc will close
[312] Fix | Delete
* the modal and not inadvertently cause the widget to collapse in the customizer.
[313] Fix | Delete
*/
[314] Fix | Delete
mediaFrame.$el.find( ':focusable:first' ).focus();
[315] Fix | Delete
},
[316] Fix | Delete
[317] Fix | Delete
/**
[318] Fix | Delete
* Clear the selected attachment when it is deleted in the media select frame.
[319] Fix | Delete
*
[320] Fix | Delete
* @since 4.9.0
[321] Fix | Delete
* @param {wp.media.models.Attachment} attachment - Attachment.
[322] Fix | Delete
* @return {void}
[323] Fix | Delete
*/
[324] Fix | Delete
handleAttachmentDestroy: function handleAttachmentDestroy( attachment ) {
[325] Fix | Delete
var control = this;
[326] Fix | Delete
control.model.set( {
[327] Fix | Delete
ids: _.difference(
[328] Fix | Delete
control.model.get( 'ids' ),
[329] Fix | Delete
[ attachment.id ]
[330] Fix | Delete
)
[331] Fix | Delete
} );
[332] Fix | Delete
}
[333] Fix | Delete
} );
[334] Fix | Delete
[335] Fix | Delete
// Exports.
[336] Fix | Delete
component.controlConstructors.media_gallery = GalleryWidgetControl;
[337] Fix | Delete
component.modelConstructors.media_gallery = GalleryWidgetModel;
[338] Fix | Delete
[339] Fix | Delete
})( wp.mediaWidgets );
[340] Fix | Delete
[341] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function