-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathadmin.limitation.pick.js
More file actions
196 lines (170 loc) · 8.45 KB
/
Copy pathadmin.limitation.pick.js
File metadata and controls
196 lines (170 loc) · 8.45 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function (global, doc, ibexa, React, ReactDOMClient, Translator) {
const SELECTOR_LOCATION_LIMITATION_BTN = '.ibexa-pick-location-limitation-button';
const SELECTOR_IBEXA_TAG = '.ibexa-tag';
const IDS_SEPARATOR = ',';
const token = doc.querySelector('meta[name="CSRF-Token"]').content;
const siteaccess = doc.querySelector('meta[name="SiteAccess"]').content;
const udwContainer = doc.getElementById('react-udw');
const limitationBtns = doc.querySelectorAll(SELECTOR_LOCATION_LIMITATION_BTN);
let udwRoot = null;
const findLocationsByIdList = (pathArraysWithoutRoot, callback) => {
const bulkOperations = getBulkOperations(pathArraysWithoutRoot);
const request = new Request('/api/ibexa/v2/bulk', {
method: 'POST',
headers: {
Accept: 'application/vnd.ibexa.api.BulkOperationResponse+json',
'Content-Type': 'application/vnd.ibexa.api.BulkOperation+json',
'X-Siteaccess': siteaccess,
'X-CSRF-Token': token,
},
body: JSON.stringify({
bulkOperations: {
operations: bulkOperations,
},
}),
mode: 'same-origin',
credentials: 'same-origin',
});
const errorMessage = Translator.trans(
/* @Desc("Could not fetch content names") */ 'limitation.pick.error',
{},
'ibexa_universal_discovery_widget',
);
fetch(request)
.then(ibexa.helpers.request.getJsonFromResponse)
.then(callback)
.catch(() => ibexa.helpers.notification.showErrorNotification(errorMessage));
};
const getBulkOperations = (pathArraysWithoutRoot) =>
pathArraysWithoutRoot.reduce((operations, pathArray) => {
const locationId = pathArray[pathArray.length - 1];
operations[locationId] = {
uri: '/api/ibexa/v2/views',
method: 'POST',
headers: {
Accept: 'application/vnd.ibexa.api.View+json; version=1.1',
'Content-Type': 'application/vnd.ibexa.api.ViewInput+json; version=1.1',
'X-Requested-With': 'XMLHttpRequest',
},
content: JSON.stringify({
ViewInput: {
identifier: `udw-locations-by-path-string-${pathArray.join('-')}`,
public: false,
LocationQuery: {
FacetBuilders: {},
SortClauses: { SectionIdentifier: 'ascending' },
Filter: { LocationIdCriterion: pathArray.join(IDS_SEPARATOR) },
limit: 50,
offset: 0,
},
},
}),
};
return operations;
}, {});
const removeRootLocation = (pathArray) => pathArray.slice(1);
const pathStringToPathArray = (pathString) => pathString.split('/').filter((el) => el);
const buildContentBreadcrumbs = (viewData) => {
const searchHitList = viewData.View.Result.searchHits.searchHit;
return searchHitList.map((searchHit) => searchHit.value.Location.ContentInfo.Content.TranslatedName).join(' / ');
};
const addLocationsToInput = (limitationBtn, selectedItems) => {
const input = doc.querySelector(limitationBtn.dataset.locationInputSelector);
const selectedLocationsIds = selectedItems.map((item) => item.id).join(IDS_SEPARATOR);
input.value = selectedLocationsIds;
};
const removeLocationFromInput = (locationInputSelector, removedLocationId) => {
const input = doc.querySelector(locationInputSelector);
const locationsIdsWithoutRemoved = input.value.split(IDS_SEPARATOR).filter((locationId) => locationId !== removedLocationId);
input.value = locationsIdsWithoutRemoved.join(IDS_SEPARATOR);
};
const addLocationsTags = (limitationBtn, selectedItems) => {
const tagsList = doc.querySelector(limitationBtn.dataset.selectedLocationListSelector);
const tagTemplate = limitationBtn.dataset.valueTemplate;
const fragment = doc.createDocumentFragment();
selectedItems.forEach((location) => {
const locationId = location.id;
const container = doc.createElement('ul');
container.insertAdjacentHTML('beforeend', tagTemplate);
const tagTemplateUnescaped = container.innerHTML;
const renderedItem = tagTemplateUnescaped.replace('{{ location_id }}', locationId);
container.innerHTML = '';
container.insertAdjacentHTML('beforeend', renderedItem);
const listItemNode = container.querySelector('li');
const tagNode = listItemNode.querySelector(SELECTOR_IBEXA_TAG);
attachTagEventHandlers(limitationBtn, tagNode);
fragment.append(listItemNode);
});
tagsList.innerHTML = '';
tagsList.append(fragment);
setTagsBreadcrumbs(tagsList, selectedItems);
};
const setTagsBreadcrumbs = (tagsList, selectedItems) => {
const pathArraysWithoutRoot = selectedItems.map(getLocationPathArray);
findLocationsByIdList(pathArraysWithoutRoot, (response) => {
const { operations } = response.BulkOperationResponse;
Object.entries(operations).forEach(([locationId, { content }]) => {
const viewData = JSON.parse(content);
const tag = tagsList.querySelector(`[data-location-id="${locationId}"]`);
const tagContent = tag.querySelector('.ibexa-tag__content');
const tagSpinner = tag.querySelector('.ibexa-tag__spinner');
tagContent.innerText = buildContentBreadcrumbs(viewData);
tagSpinner.hidden = true;
tagContent.hidden = false;
});
});
};
const getLocationPathArray = ({ pathString }) => {
const pathArray = pathStringToPathArray(pathString);
const pathArrayWithoutRoot = removeRootLocation(pathArray);
return pathArrayWithoutRoot;
};
const handleTagRemove = (limitationBtn, tag) => {
const removedLocationId = tag.dataset.locationId;
const { locationInputSelector } = limitationBtn.dataset;
removeLocationFromInput(locationInputSelector, removedLocationId);
tag.remove();
};
const attachTagEventHandlers = (limitationBtn, tag) => {
const removeTagBtn = tag.querySelector('.ibexa-tag__remove-btn');
if (removeTagBtn !== null) {
removeTagBtn.addEventListener('click', () => handleTagRemove(limitationBtn, tag), false);
}
};
const closeUDW = () => udwRoot.unmount();
const handleUdwConfirm = (limitationBtn, selectedItems) => {
if (selectedItems.length) {
addLocationsToInput(limitationBtn, selectedItems);
addLocationsTags(limitationBtn, selectedItems);
}
closeUDW();
};
const openUDW = (event) => {
event.preventDefault();
const limitationBtn = event.currentTarget;
const input = doc.querySelector(limitationBtn.dataset.locationInputSelector);
const selectedLocationsIds = input.value
.split(IDS_SEPARATOR)
.filter((idString) => !!idString)
.map((idString) => parseInt(idString, 10));
const config = JSON.parse(event.currentTarget.dataset.udwConfig);
const title = Translator.trans(/* @Desc("Choose Locations") */ 'subtree_limitation.title', {}, 'ibexa_universal_discovery_widget');
udwRoot = ReactDOMClient.createRoot(udwContainer);
udwRoot.render(
React.createElement(ibexa.modules.UniversalDiscovery, {
onConfirm: handleUdwConfirm.bind(this, event.currentTarget),
onCancel: closeUDW,
title,
multiple: true,
selectedLocations: selectedLocationsIds,
...config,
}),
);
};
limitationBtns.forEach((limitationBtn) => {
const tagsList = doc.querySelector(limitationBtn.dataset.selectedLocationListSelector);
const tags = tagsList.querySelectorAll(SELECTOR_IBEXA_TAG);
tags.forEach(attachTagEventHandlers.bind(null, limitationBtn));
limitationBtn.addEventListener('click', openUDW, false);
});
})(window, window.document, window.ibexa, window.React, window.ReactDOMClient, window.Translator);