cq.screens.dcc.copy.js
5.78 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
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2017 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.
*/
/* eslint max-nested-callbacks: [2, 5] */
(function(window, $, i18n) {
'use strict';
var CLIPBOARD_KEY = 'cq-wcm-copy-items';
var COMMAND_URL = Granite.HTTP.externalize('/content/screens/svc/copy');
var ACTIVATOR_SELECTOR = '.screens-dcc-actions-paste-activator';
var clipboard = $(window).adaptTo('foundation-clipboard');
var ids = null;
function closeBrowserWarning() {
return Granite.I18n.get('Paste operation is in progress. Refreshing page may cause unexpected results.');
}
function updatePasteButton(context) {
context = context || document;
var pasteButton = $(ACTIVATOR_SELECTOR, context);
ids = clipboard.get(CLIPBOARD_KEY);
if (!ids) {
pasteButton.attr('hidden', 'hidden');
return;
}
pasteButton.removeAttr('hidden');
}
function getDestPath(activator) {
var dest = activator.data('cqWcmPasteActivatorDest');
if (dest) {
return dest;
}
var target = activator.data('cqWcmPasteActivatorTarget');
if (!target) {
return null;
}
var collection = $(target);
if (!collection.hasClass('foundation-collection')) {
return null;
}
return collection.data('foundationCollectionId');
}
function triggerPasteCompletionEvent(args, params) {
var destPath;
if ($.isArray(args[0])) {
// Multiple items pasted
for (var i = 0; i < args.length; i++) {
destPath = $(args[i][0]).find('#Message').html();
params[i].destPath = destPath;
}
} else {
// Single item pasted
destPath = $(args[0]).find('#Message').html();
params[0].destPath = destPath;
}
$(document).trigger('cq-wcm-paste-completed', [params]);
}
$(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
name: 'cq.screens.dcc.copy',
handler: function(name, el, config, collection, selections) {
ids = selections.map(function(v) {
return $(v).data('foundationCollectionItemId');
});
if (!ids.length) {
return;
}
clipboard.set(CLIPBOARD_KEY, ids);
updatePasteButton();
$(collection).adaptTo('foundation-selections').clear();
}
});
$(document).on('click', ACTIVATOR_SELECTOR, function(e) {
e.preventDefault();
var activator = $(this);
var destParentPath = getDestPath(activator);
var outputParams = [];
if (!destParentPath) {
return;
}
var ui = $(window).adaptTo('foundation-ui');
ui.wait();
$(window).on('beforeunload', closeBrowserWarning);
var promises = ids.map(function(v) {
// check if we are copying a template which is stored in /conf
var relativeTemplateParent = 'settings/wcm/templates';
if (v.indexOf('/conf') === 0
&& v.indexOf(relativeTemplateParent) > 0
&& destParentPath.indexOf(relativeTemplateParent) === -1) {
destParentPath += '/' + relativeTemplateParent;
}
var outputData = {
srcPath: v,
destParentPath: destParentPath,
before: '',
shallow: false
};
outputParams.push(outputData);
return $.ajax({
url: COMMAND_URL,
type: 'POST',
data: {
_charset_: 'UTF-8',
srcPath: v,
destParentPath: destParentPath,
before: '',
shallow: false
}
});
});
$.when.apply(null, promises)
.always(function() {
$(window).off('beforeunload', closeBrowserWarning);
ui.clearWait();
})
.done(function() {
triggerPasteCompletionEvent(arguments, outputParams);
var target = activator.data('cqWcmPasteActivatorTarget');
if (target) {
var api = $(target).adaptTo('foundation-collection');
if (api && 'reload' in api) {
api.reload();
return;
}
}
var contentApi = $('.foundation-content').adaptTo('foundation-content');
if (contentApi) {
contentApi.refresh();
}
}).fail(function(xhr) {
if (xhr.status === 0 || xhr.readyState === 0) {
// premature reload, do nothing
return;
}
var title = Granite.I18n.get('Error');
var message = Granite.I18n.getVar(xhr.responseText);
ui.alert(title, message, 'error');
});
});
$(document).on('foundation-contentloaded', function(e) {
updatePasteButton(e.target);
});
}(window, Granite.$, Granite.I18n));