2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, _, Backbone, Drupal, drupalSettings, JSON, storage) {
9 var options = $.extend(drupalSettings.quickedit, {
11 quickEdit: Drupal.t('Quick edit')
15 var fieldsMetadataQueue = [];
17 var fieldsAvailableQueue = [];
19 var contextualLinksQueue = [];
21 var entityInstancesTracker = {};
23 function initQuickEdit(bodyElement) {
24 Drupal.quickedit.collections.entities = new Drupal.quickedit.EntityCollection();
25 Drupal.quickedit.collections.fields = new Drupal.quickedit.FieldCollection();
27 Drupal.quickedit.app = new Drupal.quickedit.AppView({
29 model: new Drupal.quickedit.AppModel(),
30 entitiesCollection: Drupal.quickedit.collections.entities,
31 fieldsCollection: Drupal.quickedit.collections.fields
35 function processEntity(entityElement) {
36 var entityID = entityElement.getAttribute('data-quickedit-entity-id');
37 if (!entityInstancesTracker.hasOwnProperty(entityID)) {
38 entityInstancesTracker[entityID] = 0;
40 entityInstancesTracker[entityID]++;
43 var entityInstanceID = entityInstancesTracker[entityID];
44 entityElement.setAttribute('data-quickedit-entity-instance-id', entityInstanceID);
47 function initializeField(fieldElement, fieldID, entityID, entityInstanceID) {
48 var entity = Drupal.quickedit.collections.entities.findWhere({
50 entityInstanceID: entityInstanceID
53 $(fieldElement).addClass('quickedit-field');
55 var field = new Drupal.quickedit.FieldModel({
58 id: fieldID + '[' + entity.get('entityInstanceID') + ']',
60 metadata: Drupal.quickedit.metadata.get(fieldID),
61 acceptStateChange: _.bind(Drupal.quickedit.app.acceptEditorStateChange, Drupal.quickedit.app)
64 Drupal.quickedit.collections.fields.add(field);
67 function loadMissingEditors(callback) {
68 var loadedEditors = _.keys(Drupal.quickedit.editors);
69 var missingEditors = [];
70 Drupal.quickedit.collections.fields.each(function (fieldModel) {
71 var metadata = Drupal.quickedit.metadata.get(fieldModel.get('fieldID'));
72 if (metadata.access && _.indexOf(loadedEditors, metadata.editor) === -1) {
73 missingEditors.push(metadata.editor);
75 Drupal.quickedit.editors[metadata.editor] = false;
78 missingEditors = _.uniq(missingEditors);
79 if (missingEditors.length === 0) {
84 var loadEditorsAjax = Drupal.ajax({
85 url: Drupal.url('quickedit/attachments'),
86 submit: { 'editors[]': missingEditors }
89 var realInsert = Drupal.AjaxCommands.prototype.insert;
90 loadEditorsAjax.commands.insert = function (ajax, response, status) {
92 realInsert(ajax, response, status);
95 loadEditorsAjax.execute();
98 function initializeEntityContextualLink(contextualLink) {
99 var metadata = Drupal.quickedit.metadata;
101 function hasFieldWithPermission(fieldIDs) {
102 for (var i = 0; i < fieldIDs.length; i++) {
103 var fieldID = fieldIDs[i];
104 if (metadata.get(fieldID, 'access') === true) {
111 function allMetadataExists(fieldIDs) {
112 return fieldIDs.length === metadata.intersection(fieldIDs).length;
115 var fields = _.where(fieldsAvailableQueue, {
116 entityID: contextualLink.entityID,
117 entityInstanceID: contextualLink.entityInstanceID
119 var fieldIDs = _.pluck(fields, 'fieldID');
121 if (fieldIDs.length === 0) {
125 if (hasFieldWithPermission(fieldIDs)) {
126 var entityModel = new Drupal.quickedit.EntityModel({
127 el: contextualLink.region,
128 entityID: contextualLink.entityID,
129 entityInstanceID: contextualLink.entityInstanceID,
130 id: contextualLink.entityID + '[' + contextualLink.entityInstanceID + ']',
131 label: Drupal.quickedit.metadata.get(contextualLink.entityID, 'label')
133 Drupal.quickedit.collections.entities.add(entityModel);
135 var entityDecorationView = new Drupal.quickedit.EntityDecorationView({
136 el: contextualLink.region,
139 entityModel.set('entityDecorationView', entityDecorationView);
141 _.each(fields, function (field) {
142 initializeField(field.el, field.fieldID, contextualLink.entityID, contextualLink.entityInstanceID);
144 fieldsAvailableQueue = _.difference(fieldsAvailableQueue, fields);
146 var initContextualLink = _.once(function () {
147 var $links = $(contextualLink.el).find('.contextual-links');
148 var contextualLinkView = new Drupal.quickedit.ContextualLinkView($.extend({
149 el: $('<li class="quickedit"><a href="" role="button" aria-pressed="false"></a></li>').prependTo($links),
151 appModel: Drupal.quickedit.app.model
153 entityModel.set('contextualLinkView', contextualLinkView);
156 loadMissingEditors(initContextualLink);
161 if (allMetadataExists(fieldIDs)) {
168 function extractEntityID(fieldID) {
169 return fieldID.split('/').slice(0, 2).join('/');
172 function processField(fieldElement) {
173 var metadata = Drupal.quickedit.metadata;
174 var fieldID = fieldElement.getAttribute('data-quickedit-field-id');
175 var entityID = extractEntityID(fieldID);
177 var entityElementSelector = '[data-quickedit-entity-id="' + entityID + '"]';
178 var $entityElement = $(entityElementSelector);
180 if (!$entityElement.length) {
181 throw new Error('Quick Edit could not associate the rendered entity field markup (with [data-quickedit-field-id="' + fieldID + '"]) with the corresponding rendered entity markup: no parent DOM node found with [data-quickedit-entity-id="' + entityID + '"]. This is typically caused by the theme\'s template for this entity type forgetting to print the attributes.');
183 var entityElement = $(fieldElement).closest($entityElement);
185 if (entityElement.length === 0) {
186 var $lowestCommonParent = $entityElement.parents().has(fieldElement).first();
187 entityElement = $lowestCommonParent.find($entityElement);
189 var entityInstanceID = entityElement.get(0).getAttribute('data-quickedit-entity-instance-id');
191 if (!metadata.has(fieldID)) {
192 fieldsMetadataQueue.push({
196 entityInstanceID: entityInstanceID
201 if (metadata.get(fieldID, 'access') !== true) {
205 if (Drupal.quickedit.collections.entities.findWhere({
207 entityInstanceID: entityInstanceID
209 initializeField(fieldElement, fieldID, entityID, entityInstanceID);
211 fieldsAvailableQueue.push({
215 entityInstanceID: entityInstanceID
220 function deleteContainedModelsAndQueues($context) {
221 $context.find('[data-quickedit-entity-id]').addBack('[data-quickedit-entity-id]').each(function (index, entityElement) {
222 var entityModel = Drupal.quickedit.collections.entities.findWhere({
226 var contextualLinkView = entityModel.get('contextualLinkView');
227 contextualLinkView.undelegateEvents();
228 contextualLinkView.remove();
230 entityModel.get('entityDecorationView').remove();
232 entityModel.destroy();
235 function hasOtherRegion(contextualLink) {
236 return contextualLink.region !== entityElement;
239 contextualLinksQueue = _.filter(contextualLinksQueue, hasOtherRegion);
242 $context.find('[data-quickedit-field-id]').addBack('[data-quickedit-field-id]').each(function (index, fieldElement) {
243 Drupal.quickedit.collections.fields.chain().filter(function (fieldModel) {
244 return fieldModel.get('el') === fieldElement;
245 }).invoke('destroy');
247 function hasOtherFieldElement(field) {
248 return field.el !== fieldElement;
251 fieldsMetadataQueue = _.filter(fieldsMetadataQueue, hasOtherFieldElement);
252 fieldsAvailableQueue = _.filter(fieldsAvailableQueue, hasOtherFieldElement);
256 function fetchMissingMetadata(callback) {
257 if (fieldsMetadataQueue.length) {
258 var fieldIDs = _.pluck(fieldsMetadataQueue, 'fieldID');
259 var fieldElementsWithoutMetadata = _.pluck(fieldsMetadataQueue, 'el');
260 var entityIDs = _.uniq(_.pluck(fieldsMetadataQueue, 'entityID'), true);
262 entityIDs = _.difference(entityIDs, Drupal.quickedit.metadata.intersection(entityIDs));
263 fieldsMetadataQueue = [];
266 url: Drupal.url('quickedit/metadata'),
269 'fields[]': fieldIDs,
270 'entities[]': entityIDs
273 success: function success(results) {
274 _.each(results, function (fieldMetadata, fieldID) {
275 Drupal.quickedit.metadata.add(fieldID, fieldMetadata);
278 callback(fieldElementsWithoutMetadata);
284 Drupal.behaviors.quickedit = {
285 attach: function attach(context) {
286 $('body').once('quickedit-init').each(initQuickEdit);
288 var $fields = $(context).find('[data-quickedit-field-id]').once('quickedit');
289 if ($fields.length === 0) {
293 $(context).find('[data-quickedit-entity-id]').once('quickedit').each(function (index, entityElement) {
294 processEntity(entityElement);
297 $fields.each(function (index, fieldElement) {
298 processField(fieldElement);
301 contextualLinksQueue = _.filter(contextualLinksQueue, function (contextualLink) {
302 return !initializeEntityContextualLink(contextualLink);
305 fetchMissingMetadata(function (fieldElementsWithFreshMetadata) {
306 _.each(fieldElementsWithFreshMetadata, processField);
308 contextualLinksQueue = _.filter(contextualLinksQueue, function (contextualLink) {
309 return !initializeEntityContextualLink(contextualLink);
313 detach: function detach(context, settings, trigger) {
314 if (trigger === 'unload') {
315 deleteContainedModelsAndQueues($(context));
332 has: function has(fieldID) {
333 return storage.getItem(this._prefixFieldID(fieldID)) !== null;
335 add: function add(fieldID, metadata) {
336 storage.setItem(this._prefixFieldID(fieldID), JSON.stringify(metadata));
338 get: function get(fieldID, key) {
339 var metadata = JSON.parse(storage.getItem(this._prefixFieldID(fieldID)));
340 return typeof key === 'undefined' ? metadata : metadata[key];
342 _prefixFieldID: function _prefixFieldID(fieldID) {
343 return 'Drupal.quickedit.metadata.' + fieldID;
345 _unprefixFieldID: function _unprefixFieldID(fieldID) {
346 return fieldID.substring(26);
348 intersection: function intersection(fieldIDs) {
349 var prefixedFieldIDs = _.map(fieldIDs, this._prefixFieldID);
350 var intersection = _.intersection(prefixedFieldIDs, _.keys(sessionStorage));
351 return _.map(intersection, this._unprefixFieldID);
356 var permissionsHashKey = Drupal.quickedit.metadata._prefixFieldID('permissionsHash');
357 var permissionsHashValue = storage.getItem(permissionsHashKey);
358 var permissionsHash = drupalSettings.user.permissionsHash;
359 if (permissionsHashValue !== permissionsHash) {
360 if (typeof permissionsHash === 'string') {
361 _.chain(storage).keys().each(function (key) {
362 if (key.substring(0, 26) === 'Drupal.quickedit.metadata.') {
363 storage.removeItem(key);
367 storage.setItem(permissionsHashKey, permissionsHash);
370 $(document).on('drupalContextualLinkAdded', function (event, data) {
371 if (data.$region.is('[data-quickedit-entity-id]')) {
372 if (!data.$region.is('[data-quickedit-entity-instance-id]')) {
373 data.$region.once('quickedit');
374 processEntity(data.$region.get(0));
376 var contextualLink = {
377 entityID: data.$region.attr('data-quickedit-entity-id'),
378 entityInstanceID: data.$region.attr('data-quickedit-entity-instance-id'),
380 region: data.$region[0]
383 if (!initializeEntityContextualLink(contextualLink)) {
384 contextualLinksQueue.push(contextualLink);
388 })(jQuery, _, Backbone, Drupal, drupalSettings, window.JSON, window.sessionStorage);