cq.screens.dcc.delete.js
5 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
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
(function(window, $, i18n) {
'use strict';
// global actions for GraniteUI foundation
var ui = $(window).adaptTo('foundation-ui');
// deletes entries of a collection with the sling post servlet
$(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
name: 'cq.screens.dcc.delete',
handler: function(name, el, config, collection, selections) {
var paths = selections.map(function(v) {
return $(v).data('foundationCollectionItemId');
});
if (!paths.length) {
return;
}
var JCR_CONTENT_PATTERN = '/jcr:content';
function pathThatContainsJcrContent(path) {
return path.indexOf(JCR_CONTENT_PATTERN) > -1;
}
function substringJcrContentPathOnly(path) {
var index = path.indexOf(JCR_CONTENT_PATTERN);
return path.substring(0, index + JCR_CONTENT_PATTERN.length);
}
function removeDuplicates(value, index, array) {
return array.indexOf(value) === index;
}
// touch (i.e. update lastModified and lastModifiedBy properties) of the provided path to jcr:content content path
function touchPageAsyncNoWait(pathToJcrContent) {
// "touch" the page, no need to wait for the response
$.post(pathToJcrContent, {
'jcr:lastModified': '',
'jcr:lastModifiedBy': ''
});
}
// the paths refer to nodes in pages, the touchContainingPages config allows to touch the containing pages
// i.e. update the last modified date and by on those pages ¯
function handleTouchContainingPages(pathArray) {
pathArray
.filter(pathThatContainsJcrContent)
.map(substringJcrContentPathOnly)
.filter(removeDuplicates)
.forEach(touchPageAsyncNoWait);
}
function handlePrompt() {
ui.wait();
var promises = paths.map(function(path) {
return $.ajax({
'method': 'DELETE',
'url': '/api/screens' + path
});
});
$.when.apply(null, promises)
.then(function() { // eslint-disable-line max-nested-callbacks
if (config.data && !!config.data.touchContainingPages) {
// touch pages (async), no need to wait for responses
handleTouchContainingPages(paths);
}
ui.clearWait();
$(collection).adaptTo('foundation-collection').reload();
})
.fail(function(jqXHR, textStatus, error) { // eslint-disable-line max-nested-callbacks
ui.clearWait();
ui.notify(i18n.get('Could not delete items.'), error, 'error');
});
}
var message = $('<div/>');
var intro = $('<p/>').appendTo(message);
if (selections.length === 1) {
intro.text(Granite.I18n.get('You are going to delete the following item:'));
} else {
intro.text(Granite.I18n.get('You are going to delete the following {0} items:', selections.length));
}
var list = [];
var maxCount = Math.min(selections.length, 12);
for (var i = 0, ln = maxCount; i < ln; i++) {
var title = $(selections[i]).find('.foundation-collection-item-title').text();
list.push($('<b/>').text(title).prop('outerHTML'));
}
if (selections.length > maxCount) {
list.push('…'); // … is ellipsis
}
$('<p/>').html(list.join('<br>')).appendTo(message);
// Let the user choose what to do next
ui.prompt(i18n.get('Delete'), message.html(), 'notice', [
{text: i18n.get('Cancel')},
{text: i18n.get('Delete'), warning: true, handler: handlePrompt}
]);
}
});
}(window, Granite.$, Granite.I18n));