-
Notifications
You must be signed in to change notification settings - Fork 533
/
dragbehavior.js
516 lines (465 loc) · 13.5 KB
/
dragbehavior.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*global window:true, define:true, document:true */
/* dragbehavior.js is part of Aloha Editor project http://aloha-editor.org
*
* Aloha Editor is a WYSIWYG HTML5 inline editing library and editor.
* Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria.
* Contributors http://aloha-editor.org/contribution.php
*
* Aloha Editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* Aloha Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* As an additional permission to the GNU GPL version 2, you may distribute
* non-source (e.g., minimized or compacted) forms of the Aloha-Editor
* source code without the copy of the GNU GPL normally required,
* provided you include this license notice and a URL through which
* recipients can access the Corresponding Source.
*/
define([
'aloha/jquery',
'aloha',
'PubSub',
'aloha/copypaste',
'block/block-utils'
], function (
$,
Aloha,
PubSub,
CopyPaste,
BlockUtils
) {
'use strict';
/**
* Tags that is not allowed to wrap BlockElements. For this elements the
* mouseover event is not listened
* @type {Array.<String>}
*/
var notAllowedOverTags = ['TABLE', 'TR',
'HR', 'TBODY', 'UL', 'OL', 'DL', 'B', 'STRONG', 'A', 'EM', 'I', 'PRE',
'CODE', 'SUP', 'SUB'],
/**
* This element are suitable to listen the mouseover event, but can´t wrap
* a BlockElement
* @type {Array.<String>}
*/
notAllowedDropSelector = ['.aloha-block', '.aloha-block-handle',
'.aloha-table-selectrow', 'p', 'span', 'h1', 'h2', 'h3', 'h4', 'h5',
'h6', 'br'],
/**
* Parents of element that can´t wrap BlockElements
* @type {Array.<String>}
*/
notAllowedDropParentsSelector = [
'.aloha-table-selectcolumn',
'.aloha-ui',
'.ui-draggable-dragging'
];
/**
* Checks if the drag and drop is for a nested Table.
*
* @param {jQuery<Element>} $hovering
* @param {jQuery<Element>} $dragging
* @return {boolean}
*/
function isNestedTable ($hovering, $dragging) {
return $hovering
&& $dragging
&& $hovering.parents('table').length !== 0
&& BlockUtils.isTable($dragging);
}
/**
* Checks whether or not the element over which we are hovering should allow
* drop a region, in or around it?
*
* @param {jQuery.<HTMLElement> $hovering
* @param {jQuery.<HTMLElement> $dragging
* @return {boolean}
*/
function allowDropRegions($hovering, $dragging) {
return !$hovering || !(
$hovering.is('.ui-draggable-dragging') ||
$hovering.closest($dragging).length > 0
);
}
/**
* @private
* @class Aloha.plugins.extra.DragBehavior
* @param {block.block.AbstractBlock} blockObject An AbstractBlock child
* instance
*/
function DragBehavior(blockObject) {
this.blockObject = blockObject;
this.$element = blockObject.$element;
this.insertBeforeOrAfterMode = false;
this.setDraggable();
}
PubSub.sub('aloha.block.initialized', function (data) {
DragBehavior.follow(data.block);
});
/**
* Because the Dom.isEditable don't work well I need to rewrite it.
* checks if a HTMLElement is editable, if the element have inherited
* behavior check the parents behaivor until get a true or false behavior.
*
* @param {HTMLElement} domElm
* @return {Boolean}
*/
function isEditable(domElm) {
if (domElm.nodeType === 1) {
if (domElm.contentEditable === 'inherit') {
while (domElm.contentEditable === 'inherit') {
domElm = domElm.parentNode;
}
}
return (domElm.contentEditable === 'true');
}
return false;
}
/**
* Creates an instance of DragBehavior following a BlockElement
* only if the BlockElement given is suitable to be draggable.
*
* @static
* @param {block.block.AbstractBlock} blockObject An AbstractBlock child
* instance
*/
DragBehavior.follow = function (blockObject) {
if ($('>.aloha-block-draghandle', blockObject.$element).length > 0) {
var jsLintMustBeHappyNotMe = new DragBehavior(blockObject);
}
};
/**
* Internet Explorer loses the selection and scroll position between
* dragging and dropping.
*
* So we need to save the selection and scroll position before, and restore
* it after each drag and drop cycle.
*/
var IESelectionState = (function () {
var range = null;
var x = 0;
var y = 0;
return $.browser.msie ? {
/**
* Remember the selection state.
*/
save: function saveSelection() {
x = window.scrollX || document.documentElement.scrollLeft;
y = window.scrollY || document.documentElement.scrollTop;
range = CopyPaste.getRange();
},
/**
* Set the selection to the given range and focus on the editable
* inwhich the selection is in (if any).
*
* This function is used to restore the selection to what it was
* before dragging is started.
*/
restore: function restoreSelection() {
if (!range) {
return;
}
var editable = CopyPaste.getEditableAt(range);
if (editable) {
editable.obj.focus();
}
try {
CopyPaste.setSelectionAt(range);
} catch (e) {
Console.warn(e);
}
window.scrollTo(x, y);
}
}
: {
save: function () {},
restore: function () {}
};
}());
/**
* Setup the draggable behavior to the BlockElement
*/
DragBehavior.prototype.setDraggable = function () {
var dragBehavior = this;
var $handle = $('>.aloha-block-draghandle', this.$element);
var element = this.$element.get(0);
element.onselectstart = function () {
window.event.cancelBubble = true;
};
// Prevent the prevention of drag inside a cell
element.ondragstart = function (e) {
if (e) {
if (typeof e.stopPropagation === 'function') {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
} else {
window.event.cancelBubble = true;
}
};
$handle
.on('mousedown', function () {
IESelectionState.save();
})
.on('mouseup', function () {
dragBehavior._getHiglightElement().hide();
dragBehavior.stopListenMouseOver();
});
this.$element.draggable({
handle: '>.aloha-block-draghandle',
helper: 'clone',
cursorAt: {
left: -10,
top: -10
},
start: function (event, ui) {
ui.helper.css('zIndex', 100000);
dragBehavior.listenMouseOver();
event.stopImmediatePropagation();
},
drag: function (event) {
event.stopImmediatePropagation();
},
stop: function (event, ui) {
dragBehavior._getHiglightElement().hide();
dragBehavior.stopListenMouseOver();
dragBehavior.onDragStop();
ui.helper.remove();
IESelectionState.restore();
return true;
}
});
};
/**
* Returns all the childs in all editables registered in Aloha
*
* @return jQuery.<HTMLElement>
*/
DragBehavior.prototype._getEditableChilds = function () {
var i, editable, elms = [];
for (i = 0; i < Aloha.editables.length; i++) {
editable = Aloha.editables[i];
elms = elms.concat(
$('*:not(' + notAllowedOverTags.join(',') + ')', editable.obj).toArray()
);
}
return $(elms);
};
/**
* Listen the mouseOver events over all elements in the editables, since the
* drag action start until the drag action stop, to know in which element
* the user may drop the block element
*/
DragBehavior.prototype.listenMouseOver = function () {
var dragBehavior = this,
$elms = this._getEditableChilds();
// @todo use the DOM Events for this, jQuery is very slow
$elms.bind('mouseover.blockbehavior', function (e) {
return dragBehavior.onMouseover(this, e);
});
};
/**
* Stop listen the mouseover event, this method is called when the drag
* operation is ended (droped)
*/
DragBehavior.prototype.stopListenMouseOver = function () {
var $elms = this._getEditableChilds();
$elms.unbind('.blockbehavior');
};
/**
* Checks the element that is below of the draggable element in a drag
* operation, if is a valid element, this method call to highlight methods
*
* @param {HTMLElement} elm
* @param {jqEvent} event
*
* @return {Boolean}
*/
DragBehavior.prototype.onMouseover = function (elm, event) {
this.disableInsertBeforeOrAfter(this.$overElement);
this.$overElement = $(elm);
if (!this._isAllowedOverElement(elm)) {
this.enableInsertBeforeOrAfter(elm);
return true; // to continue bubbling to find a element where can insert the block
} else {
this.highlightElement(elm);
event.stopImmediatePropagation();
return false;
}
};
/**
* Highlight an entirely element to be a container of the BlockElement
*
* @param {HTMLElement} elm
*/
DragBehavior.prototype.highlightElement = function (elm) {
if (elm.nodeName === 'DIV' &&
elm.parentNode.nodeName === 'TD' &&
elm.parentNode.firstChild === elm &&
elm.parentNode.lastChild === elm) {
elm = elm.parentNode;
}
var $elm = $(elm),
$hElm = this._getHiglightElement().show();
$hElm
.css('zIndex', parseInt($elm.zIndex(), 10) + 1)
.offset($elm.offset())
.width($elm.outerWidth())
.height($elm.outerHeight());
};
/**
* Activate the functionality to highlight the top or bottom border of an
* element to inform to the user that the block must be inserted before or
* after
*
* @param {HTMLElement} elm
*/
DragBehavior.prototype.enableInsertBeforeOrAfter = function (elm) {
var $elm = $(elm),
dragBehavior = this,
elmTop = $elm.offset().top,
halfHeight = $elm.outerHeight() / 2;
if (!allowDropRegions($elm, this.$element)) {
return;
}
$elm.bind('mousemove.brIBOA', function (event) {
var top = event.pageY - elmTop;
if (top >= halfHeight) {
dragBehavior.insertBeforeOrAfterMode = 'AFTER';
} else {
dragBehavior.insertBeforeOrAfterMode = 'BEFORE';
}
dragBehavior.highlightEdge(this);
});
};
/**
* Disables the InsertBeforeOrAfter functionality.
*
* @param {jQuery.<HTMLElement>} $elm
*/
DragBehavior.prototype.disableInsertBeforeOrAfter = function ($elm) {
this.insertBeforeOrAfterMode = false;
if ($elm) {
$elm.unbind('.brIBOA');
}
};
/**
* Highlites only a edge of the element recived, depending the value of
* this.insertBeforeOrAfterMode
*
* @param {HTMLElement} elm
*/
DragBehavior.prototype.highlightEdge = function (elm) {
var $elm = $(elm),
$hElm = this._getHiglightElement().show(),
offset;
if (this.insertBeforeOrAfterMode === 'BEFORE') {
$hElm
.css('zIndex', parseInt($elm.zIndex(), 10) + 1)
.offset($elm.offset())
.width($elm.outerWidth())
.height(10);
} else {
offset = $elm.offset();
offset.top = (offset.top + $elm.outerHeight()) - 10;
$hElm
.css('zIndex', parseInt($elm.zIndex(), 10) + 1)
.offset(offset)
.width($elm.outerWidth())
.height(10);
}
};
/**
* Returns true or false depending if the element given is allowed to
* contain BlockElements
*
* @param {HTMLElement} elm
*
* @return {Boolean}
*/
DragBehavior.prototype._isAllowedOverElement = function (elm) {
if ($.inArray(elm.nodeName, notAllowedOverTags) < 0) {
if (!isEditable(elm)) {
return false;
}
var $elm = $(elm);
if ($elm.parents(notAllowedDropParentsSelector.join(',')).length > 0) {
return false;
}
if ($elm.is(notAllowedDropSelector.join(','))) {
return false;
}
if (isNestedTable($elm, this.$element)) {
return false;
}
return true;
} else {
return false;
}
};
/**
* Check if the element in the user want to drop the block elements is
* suitable to contain the BlockElement or is suitable to insert the
* element before or after this
*/
DragBehavior.prototype.onDragStop = function () {
// @todo check if the $overElement is a Valid element to drop the block
if (allowDropRegions(this.$overElement, this.$element)) {
if (this.$overElement &&
!this._isAllowedOverElement(this.$overElement[0])) {
this.enableInsertBeforeOrAfter(this.$overElement[0]);
// prevent dragging the block into a position, where it should not be
// when _isAllowedOverElement() return false, insertBeforeOrAfterMode must not be false
if (this.insertBeforeOrAfterMode === false) {
this.insertBeforeOrAfterMode = 'AFTER';
}
}
if (this.insertBeforeOrAfterMode !== false) {
if (this.insertBeforeOrAfterMode === 'BEFORE') {
this.$overElement.before(this.$element);
} else {
this.$overElement.after(this.$element);
}
} else {
this.$element.appendTo(this.$overElement);
}
}
this.disableInsertBeforeOrAfter(this.$overElement);
};
/**
* Returns a reference to the element used to highlight over elements,
* if not exist is created, this element is shared for all instances of
* DragBehavior
*
* @return {jQuery.<HTMLElement>}
*/
DragBehavior.prototype._getHiglightElement = function () {
if (undefined === this.$hElm) {
if ($('#aloha-highlight-element').length === 0) {
this.$hElm = $('<div>')
.attr('id', 'aloha-highlight-element')
.appendTo('body')
.css({
position: 'absolute',
opacity: 0.5,
background: 'blue'
})
.hide();
} else {
this.$hElm = $('#aloha-highlight-element');
}
}
return this.$hElm;
};
});