-
Notifications
You must be signed in to change notification settings - Fork 638
/
DragDrop.js
266 lines (248 loc) · 9.41 KB
/
DragDrop.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Module: TYPO3/CMS/Backend/LayoutModule/DragDrop
* this JS code does the drag+drop logic for the Layout module (Web => Page)
* based on jQuery UI
*/
define(['jquery', 'jquery-ui/droppable'], function($) {
'use strict';
/**
*
* @type {{contentIdentifier: string, dragIdentifier: string, dragHeaderIdentifier: string, dropZoneIdentifier: string, columnIdentifier: string, validDropZoneClass: string, dropPossibleHoverClass: string, addContentIdentifier: string, originalStyles: string}}
* @exports TYPO3/CMS/Backend/LayoutModule/DragDrop
*/
var DragDrop = {
contentIdentifier: '.t3js-page-ce',
dragIdentifier: '.t3-page-ce-dragitem',
dragHeaderIdentifier: '.t3js-page-ce-draghandle',
dropZoneIdentifier: '.t3js-page-ce-dropzone-available',
columnIdentifier: '.t3js-page-column',
validDropZoneClass: 'active',
dropPossibleHoverClass: 't3-page-ce-dropzone-possible',
addContentIdentifier: '.t3js-page-new-ce',
clone: true,
originalStyles: ''
};
/**
* initializes Drag+Drop for all content elements on the page
*/
DragDrop.initialize = function() {
$(DragDrop.contentIdentifier).draggable({
handle: DragDrop.dragHeaderIdentifier,
scope: 'tt_content',
cursor: 'move',
distance: 20,
addClasses: 'active-drag',
revert: 'invalid',
zIndex: 100,
start: function(evt, ui) {
DragDrop.onDragStart($(this));
},
stop: function(evt, ui) {
DragDrop.onDragStop($(this));
}
});
$(DragDrop.dropZoneIdentifier).droppable({
accept: this.contentIdentifier,
scope: 'tt_content',
tolerance: 'pointer',
over: function(evt, ui) {
DragDrop.onDropHoverOver($(ui.draggable), $(this));
},
out: function(evt, ui) {
DragDrop.onDropHoverOut($(ui.draggable), $(this));
},
drop: function(evt, ui) {
DragDrop.onDrop($(ui.draggable), $(this), evt);
}
});
};
/**
* called when a draggable is selected to be moved
* @param $element a jQuery object for the draggable
* @private
*/
DragDrop.onDragStart = function($element) {
// Add css class for the drag shadow
DragDrop.originalStyles = $element.get(0).style.cssText;
$element.children(DragDrop.dragIdentifier).addClass('dragitem-shadow');
$element.append('<div class="ui-draggable-copy-message">' + TYPO3.lang['dragdrop.copy.message'] + '</div>');
// Hide create new element button
$element.children(DragDrop.dropZoneIdentifier).addClass('drag-start');
$element.closest(DragDrop.columnIdentifier).removeClass('active');
$element.parents(DragDrop.columnHolderIdentifier).find(DragDrop.addContentIdentifier).hide();
$element.find(DragDrop.dropZoneIdentifier).hide();
$(DragDrop.dropZoneIdentifier).each(function() {
var $me = $(this);
if ($me.parent().find('.t3js-toggle-new-content-element-wizard').length) {
$me.addClass(DragDrop.validDropZoneClass);
} else {
$me.closest(DragDrop.contentIdentifier).find('> ' + DragDrop.addContentIdentifier + ', > > ' + DragDrop.addContentIdentifier).show();
}
});
};
/**
* called when a draggable is released
* @param $element a jQuery object for the draggable
* @private
*/
DragDrop.onDragStop = function($element) {
// Remove css class for the drag shadow
$element.children(DragDrop.dragIdentifier).removeClass('dragitem-shadow');
// Show create new element button
$element.children(DragDrop.dropZoneIdentifier).removeClass('drag-start');
$element.closest(DragDrop.columnIdentifier).addClass('active');
$element.parents(DragDrop.columnHolderIdentifier).find(DragDrop.addContentIdentifier).show();
$element.find(DragDrop.dropZoneIdentifier).show();
$element.find('.ui-draggable-copy-message').remove();
// Reset inline style
$element.get(0).style.cssText = DragDrop.originalStyles;
$(DragDrop.dropZoneIdentifier + '.' + DragDrop.validDropZoneClass).removeClass(DragDrop.validDropZoneClass);
};
/**
* adds CSS classes when hovering over a dropzone
* @param $draggableElement
* @param $droppableElement
* @private
*/
DragDrop.onDropHoverOver = function($draggableElement, $droppableElement) {
if ($droppableElement.hasClass(DragDrop.validDropZoneClass)) {
$droppableElement.addClass(DragDrop.dropPossibleHoverClass);
}
};
/**
* removes the CSS classes after hovering out of a dropzone again
* @param $draggableElement
* @param $droppableElement
* @private
*/
DragDrop.onDropHoverOut = function($draggableElement, $droppableElement) {
$droppableElement.removeClass(DragDrop.dropPossibleHoverClass);
};
/**
* this method does the whole logic when a draggable is dropped on to a dropzone
* sending out the request and afterwards move the HTML element in the right place.
*
* @param $draggableElement
* @param $droppableElement
* @param {Event} evt the event
* @private
*/
DragDrop.onDrop = function($draggableElement, $droppableElement, evt) {
var newColumn = DragDrop.getColumnPositionForElement($droppableElement);
$droppableElement.removeClass(DragDrop.dropPossibleHoverClass);
var $pasteAction = typeof $draggableElement === 'number';
// send an AJAX requst via the AjaxDataHandler
var contentElementUid = $pasteAction ? $draggableElement : parseInt($draggableElement.data('uid'));
if (contentElementUid > 0) {
var parameters = {};
// add the information about a possible column position change
var targetFound = $droppableElement.closest(DragDrop.contentIdentifier).data('uid');
// the item was moved to the top of the colPos, so the page ID is used here
var targetPid = 0;
if (typeof targetFound === 'undefined') {
// the actual page is needed
targetPid = $('[data-page]').first().data('page');
} else {
// the negative value of the content element after where it should be moved
targetPid = 0 - parseInt(targetFound);
}
var language = parseInt($droppableElement.closest('[data-language-uid]').data('language-uid'));
var colPos = 0;
if (targetPid !== 0) {
colPos = newColumn;
}
parameters['cmd'] = {tt_content: {}};
parameters['data'] = {tt_content: {}};
var copyAction = (evt && evt.originalEvent.ctrlKey || $droppableElement.hasClass('t3js-paste-copy'));
if (copyAction) {
parameters['cmd']['tt_content'][contentElementUid] = {
copy: {
action: 'paste',
target: targetPid,
update: {
colPos: colPos,
sys_language_uid: language
}
}
};
DragDrop.ajaxAction($droppableElement, $draggableElement, parameters, copyAction, $pasteAction);
} else {
parameters['data']['tt_content'][contentElementUid] = {
colPos: colPos,
sys_language_uid: language
};
if ($pasteAction) {
parameters = {
CB: {
paste: 'tt_content|' + targetPid,
update: {
colPos: colPos,
sys_language_uid: language
}
}
};
} else {
parameters['cmd']['tt_content'][contentElementUid] = {move: targetPid};
}
// fire the request, and show a message if it has failed
DragDrop.ajaxAction($droppableElement, $draggableElement, parameters, copyAction, $pasteAction);
}
}
};
/**
* this method does the actual AJAX request for both, the move and the copy action.
*
* @param $droppableElement
* @param $draggableElement
* @param parameters
* @param $copyAction
* @param $pasteAction
* @private
*/
DragDrop.ajaxAction = function($droppableElement, $draggableElement, parameters, $copyAction, $pasteAction) {
require(['TYPO3/CMS/Backend/AjaxDataHandler'], function(DataHandler) {
DataHandler.process(parameters).done(function(result) {
if (!result.hasErrors) {
// insert draggable on the new position
if (!$pasteAction) {
if (!$droppableElement.parent().hasClass(DragDrop.contentIdentifier.substring(1))) {
$draggableElement.detach().css({top: 0, left: 0})
.insertAfter($droppableElement.closest(DragDrop.dropZoneIdentifier));
} else {
$draggableElement.detach().css({top: 0, left: 0})
.insertAfter($droppableElement.closest(DragDrop.contentIdentifier));
}
}
self.location.reload(true);
}
});
});
};
/**
* returns the next "upper" container colPos parameter inside the code
* @param $element
* @return int|null the colPos
*/
DragDrop.getColumnPositionForElement = function($element) {
var $columnContainer = $element.closest('[data-colpos]');
if ($columnContainer.length && $columnContainer.data('colpos') !== 'undefined') {
return $columnContainer.data('colpos');
} else {
return false;
}
};
$(DragDrop.initialize);
return DragDrop;
});