-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_browser.js
54 lines (50 loc) · 1.9 KB
/
entity_browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @file
* JavaScript behaviors for entity browser.
*/
(function ($, Drupal, once) {
/**
* Keep track of selections across paginated lists.
*
* @type Object
*/
let selections = {};
Drupal.behaviors.EntityBrowserSelections = {
attach: function (context, settings) {
once("entity-browser-form", 'form.entity-browser-form', context).forEach((form) => {
// Reset selections when the form is opened.
selections = {};
form.addEventListener("submit", (event) => {
Object.keys(selections).forEach((key) => {
if (!form.contains(selections[key])) {
// Add previous selections when they don't exist.
form.appendChild(selections[key]);
}
});
});
});
once("entity-browser-input", 'form.entity-browser-form > .view').forEach((view) => {
const counter = document.createElement("div");
counter.className = "entity-browser-selection-counter";
counter.innerText = Drupal.formatPlural(Object.keys(selections).length, "1 selection", "@count selections");
const table = view.querySelector(".view-content table.views-table");
table.parentNode.insertBefore(counter, table);
view.querySelectorAll('input[type="checkbox"][value]').forEach((element) => {
if (selections[element.name]) {
// Restore selection from the previous page.
element.checked = true;
}
$(element).on("change", () => {
if (element.checked) {
selections[element.name] = element.cloneNode();
selections[element.name].type = "hidden";
} else {
delete selections[element.name];
}
counter.innerText = Drupal.formatPlural(Object.keys(selections).length, "1 selection", "@count selections");
});
});
});
}
};
})(jQuery, Drupal, once);