/
dragdrop.js
1730 lines (1333 loc) · 45.4 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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@name glow.dragdrop
@namespace
@description Simplifying drag and drop behaviour
*/
(window.gloader || glow).module({
name: "glow.dragdrop",
library: ["glow", "@VERSION@"],
depends: [["glow", "@VERSION@", "glow.tweens", "glow.events", "glow.dom", "glow.anim"]],
builder: function(glow) {
var events = glow.events,
addListener = events.addListener,
fire = events.fire,
removeListener = events.removeListener,
dom = glow.dom,
$ = dom.get,
create = dom.create;
//public
var r = {},
_zIndex = 1000,
_ieStrict = (document.compatMode == "CSS1Compat" && glow.env.ie >= 5) ? true : false,
_ieTrans= (document.compatMode != "CSS1Compat" && glow.env.ie >= 5) ? true : false,
_ie = glow.env.ie >= 5,
sides = ['top', 'right', 'bottom', 'left'];
/*
PrivateFunction: memoize(clss, name)
Replace a method with a version that caches the result after the first run.
Arguments:
*clss* (function)
The class whose method is being memoized.
*name*
The name of the method to memoize.
*/
function memoize (clss, name) {
var orig = clss.prototype[name];
var cachedName = 'cached_' + name;
clss.prototype[name] = function () {
if (cachedName in this) return this[cachedName];
return this[cachedName] = orig.apply(this, arguments);
};
}
/*
Copy margins from one element to another
*/
function copyMargins(to, from) {
var i = sides.length, margin;
while (i--) {
margin = 'margin-' + sides[i];
to.css(margin, from.css(margin));
}
}
/*
PrivateFunction: memoizeNamed(clss, methodName)
Replace a method that takes a name with a version that caches the result for each name after the first run.
Arguments:
*clss* (function)
The class whose method is being memoized.
*methodName*
The name of the method to memoize.
*/
function memoizeNamed (clss, methodName) {
var orig = clss.prototype[methodName];
var cachedName = 'cached_' + methodName;
clss.prototype[methodName] = function (name) {
if (! this[cachedName]) this[cachedName] = {};
if (name in this[cachedName]) return this[cachedName][name];
return this[cachedName][name] = orig.apply(this, arguments);
};
}
/*
PrivateFunction: reset(obj, names)
Remove cached values for a set of memoized methods.
Arguments:
*obj* (object)
The object containing cached values.
*names* (array of strings)
The names of methods whose values have been cached.
*/
function reset (obj, names) {
for (var i = 0, l = names.length; i < l; i++) {
delete obj['cached_' + names[i]];
}
}
/*
PrivateFunction: resetNamed(obj, meth, names)
Remove cached values for a set of named properties for a method.
Arguments:
*obj* (object)
The object containing cached values.
*meth* (string)
The name of the method whose values have been cached.
*names* (array of strings)
The names of the cached properties.
function resetNamed (obj, meth, names) {
var cache = obj['cached_' + meth];
if (! cache) return;
for (var i = 0, l = names.length; i < l; i++) {
delete cache[names[i]];
}
}
*/
/*
PrivateClass: Box
Calculates and caches information about an element in the box model.
Constructor:
(code)
new Box(el)
(end)
Arguments:
*el* (glow.dom.NodeList)
The element that calculations will be performed on.
*/
var Box = function (el) {
this.el = el;
};
Box.prototype = {
/*
PrivateMethod: val
Get an pixel value for a CSS style.
Arguments:
*style* (string)
The name of a CSS style (e.g. "margin-top".
Returns:
An integer number of pixels.
*/
val: function (style) {
var val = parseInt(this.el.css(style));
// TODO - fix dom so margin-left return value is always defined?
// if (isNaN(val)) throw 'got NaN in val for ' + style + ': ' + this.el.css(style);
return val || 0;
// return val;
},
/*
PrivateMethod: width
Get the width of the element.
Returns:
An integer number of pixels.
*/
width: function () {
return this.borderWidth()
- this.val('border-left-width')
- this.val('border-right-width');
},
/*
PrivateMethod: height
Get the height of the element.
Returns:
An integer number of pixels.
*/
height: function () {
return this.borderHeight()
- this.val('border-top-width')
- this.val('border-bottom-width');
},
/*
PrivateMethod: offsetParentPageTop
Get the number of pixels from the top of nearest element with absolute, relative or fixed position to the
top of the page.
Returns:
An integer number of pixels.
*/
offsetParentPageTop: function () {
var el = this.el[0], pos, top;
while (el = el.offsetParent) {
if ( $(el).css('position') != 'static' ) {
break;
}
}
return el ?
$(el).offset().top :
0;
},
/*
PrivateMethod: offsetTop
This gets what CSS 'top' would be if the element were position "absolute"
Returns:
An integer number of pixels.
*/
offsetTop: function () {
return this.el.position().top;
},
/*
PrivateMethod: offsetLeft
This gets what CSS 'left' would be if the element were position "absolute"
Returns:
An integer number of pixels.
*/
offsetLeft: function () {
return this.el.position().left;
},
/*
PrivateMethod: borderWidth
Get the width of the element from the left edge of the left border to the right
edge of the right border.
Returns:
An integer number of pixels.
*/
borderWidth: function () {
var width = this.el[0].offsetWidth;
if (glow.env.khtml) {
width -= this.val('margin-left')
+ this.val('margin-right')
+ this.val('border-left-width')
+ this.val('border-right-width');
}
return width;
},
/*
PrivateMethod: borderHeight
Get the height of an element from the top edge of the top border to the bottom
edge of the bottom border.
Returns:
An integer number of pixels.
*/
borderHeight: function () {
if (this._logicalBottom) {
return this._logicalBottom - this.offsetTop();
}
var height = this.el[0].offsetHeight;
if (glow.env.khtml) {
height -= this.val('margin-top')
+ this.val('margin-bottom')
+ this.val('border-top-width')
+ this.val('border-bottom-width');
}
return height;
},
/*
PrivateMethod: outerWidth
Get the width of the element in including margin, borders and padding.
Returns:
An integer number of pixels.
*/
outerWidth: function () {
return this.borderWidth() + this.val('margin-left') + this.val('margin-right');
},
/*
PrivateMethod: outerHeight
Get the height of the element in including margin, borders and padding. This
does not take account of collapsable margins (i.e. it assumes the margins are
present).
Returns:
An integer number of pixels.
*/
outerHeight: function () {
return this.borderHeight() + this.val('margin-top') + this.val('margin-bottom');
},
/*
PrivateMethod: innerLeftPos
Get the offset of the left edge of the content of the box (i.e. excluding
margin, border and padding).
Returns:
An integer number of pixels.
*/
innerLeftPos: function () {
return this.offsetLeft()
+ this.val('margin-left')
+ this.val('border-left-width')
+ this.val('padding-left');
},
/*
PrivateMethod: innerTopPos
Get the offset of the top edge of the content of the box (i.e. excluding
margin, border and padding).
Returns:
An integer number of pixels.
*/
innerTopPos: function () {
return this.offsetTop()
+ this.val('margin-top')
+ this.val('border-top-width')
+ this.val('padding-top');
},
/*
PrivateMethod: surroundWidth
Get the combined width of the horizontal margins, borders and paddings.
Returns:
An integer number of pixels.
*/
surroundWidth: function () {
return this.val('border-left-width')
+ this.val('padding-left')
+ this.val('padding-right')
+ this.val('border-right-width');
},
/*
PrivateMethod: surroundHeight
Get the combined height of the horizontal margins, borders and paddings.
Returns:
An integer number of pixels.
*/
surroundHeight: function () {
return this.val('border-top-width')
+ this.val('padding-top')
+ this.val('padding-bottom')
+ this.val('border-bottom-width');
},
/*
PrivateMethod: verticalCenter
Get the vertical offset of the center of the element from it's offset parent.
Returns:
An integer number of pixels.
*/
verticalCenter: function () {
return this.offsetTop() + (this.outerHeight() / 2);
},
/*
PrivateMethod: verticalCenter
Get the vertical offset of the center of the element from it's offset parent.
Returns:
An integer number of pixels.
*/
horizontalCenter: function () {
return this.offsetTop() + (this.outerWidth() / 2);
}
};
for (var i in Box.prototype) {
if (i == 'val') memoizeNamed(Box, i);
else memoize(Box, i);
}
glow.lang.apply(Box.prototype, {
/*
PrivateMethod: resetPosition
Reset cached position values for the element.
*/
resetPosition: function () {
reset(this, [
'offsetTop',
'offsetLeft',
'borderTopPos',
'borderLeftPos',
'innerTopPos',
'innerLeftPos',
'verticalCenter',
'horizontalCenter'
]);
},
/*
PrivateMethod: setLogicalBottom
Set the logical value for the position of the bottom of the border (offsetTop + offsetHeight).
Arguments:
*bottom* (integer)
The value to use for the bottom of the box.
*/
setLogicalBottom: function (bottom) {
this._logicalBottom = bottom;
},
/*
PrivateMethod: boundsFor
Get the the bounds for the left and top css properties of a child box to
ensure that it stays within this element.
Arguments:
*childBox* (Box)
A Box object representing the space taken up by the child element.
Returns:
An array of top, right, bottom and left pixel bounds for the top and left
css properties of the child element.
*/
boundsFor: function (childBox) {
var top, left, pos = this.el.css('position');
if (pos != 'static') {
top = left = 0;
}
else {
top = this.innerTopPos();
left = this.innerLeftPos();
}
return [
top, // top
left + this.width() - childBox.outerWidth(), // right
top + this.height() - childBox.outerHeight(), // bottom
left // left
];
},
/*
PrivateMethod: outerBounds
Get the top, right, bottom and left offsets of the outside edge of the border
of the box.
Returns:
An array of integer pixel offsets for the top, right, bottom, left edges of the
boxes border.
*/
outerBounds: function () {
var offset = this.el.offset(),
left = offset.left,
top = offset.top;
return [
top,
left + this.borderWidth(),
top + this.borderHeight(),
left
];
},
/*
PrivateMethod: intersectSize
Get the intersection of this box with another box.
Arguments:
*that* (Box)
A Box object to test for intersection with this box.
*touches* (boolean)
If true, then the boxes don't have to intersect but can merely touch.
Returns:
An integer number of square pixels that the the outside of the
edge of the border of this box intersects with that of the passed
in box.
*/
intersectSize: function (that, touches) {
var a = this.outerBounds(), b = that.outerBounds();
if (touches) {
a[1]++; b[1]++; a[2]++; b[2]++;
}
return (
a[2] < b[0] ? 0 :
b[2] < a[0] ? 0 :
a[0] < b[0] ? (a[2] < b[2] ? a[2] - b[0] : b[2] - b[0]) :
b[2] < a[2] ? b[2] - a[0] : a[2] - a[0]
) * (
a[1] < b[3] ? 0 :
b[1] < a[3] ? 0 :
a[3] < b[3] ? (a[1] < b[1] ? a[1] - b[3] : b[1] - b[3]) :
b[1] < a[1] ? b[1] - a[3] : a[1] - a[3]
);
},
/*
PrivateMethod: sizePlaceholder
Size and position a placeholder/drop indicator element to match that
of the element.
Arguments:
*placeholder* (glow.dom.NodeList)
The element that will be sized.
*pos* (optional string)
The value for the placeholder's CSS position. Defaults to the position
of this element.
*startLeft* (integer)
The original left position of the element.
*startTop* (integer)
The original top position of the element.
*/
sizePlaceholder: function (placeholder, pos, startLeft, startTop) {
var placeholderBox = new Box(placeholder),
el = this.el,
position = pos || el.css('position');
placeholder.css('display', 'none');
el.after(placeholder);
placeholder.css('width', (el[0].offsetWidth - placeholderBox.surroundWidth()) + 'px')
.css('height', (el[0].offsetHeight - placeholderBox.surroundHeight()) + 'px');
// copy margin values
copyMargins(placeholder, el);
placeholder.remove();
placeholder.css('display', 'block');
if (position != 'static') {
placeholder.css('left', startLeft + 'px');
placeholder.css('top', startTop + 'px');
}
placeholder.css('position', position);
},
/*
PrivateMethod: contains
Check if a box is contained within this box.
Arguments:
*box* (Box)
The box to test.
Returns:
Boolean, true if contained.
*/
contains: function (box) {
var bounds = this.boundsFor(box),
position = box.el.position(),
top = position.top,
left = position.left;
return top >= bounds[0] // top
&& left <= bounds[1] // right
&& top <= bounds[2] // bottom
&& left >= bounds[3]; // left
},
/*
PrivateMethod: containsPoint
Arguments:
*offset* (object)
The offset to check - an object containing x and y integer pixel values.
Returns:
Boolean, true if the point over the visible part of the element (i.e. including the borders).
*/
containsPoint: function (offset) {
var elementOffset = this.el.offset();
return offset.x >= elementOffset.left
&& offset.y >= elementOffset.top
&& offset.x <= elementOffset.left + this.borderWidth()
&& offset.y <= elementOffset.top + this.borderHeight();
},
/*
PrivateMethod: positionedAncestorBox
Get a new Box for nearest ancestor of the element that has position 'absolute', 'fixed' or 'relative'.
Returns:
An integer pixel offset.
*/
positionedAncestorBox: function () {
var el = this.el.parent(), pos;
while (el[0]) {
pos = el.css('position') || 'static';
if (pos == 'relative' || pos == 'absolute' || pos == 'fixed')
return new Box(el);
el = el.parent();
}
return null;
}
});
function placeholderElement (el) {
var tag = el[0].tagName.toLowerCase() == 'li' ? 'li' : 'div';
var placeholder = create('<' + tag + '></' + tag + '>');
if (tag == 'li') placeholder.css('list-style-type', 'none');
return placeholder;
}
/**
@name glow.dragdrop.Draggable
@class
@description An element that can be dragged using the mouse.
@see <a href="../furtherinfo/dragdrop/draggables.shtml">Draggable examples</a>
@param {String | Element | glow.dom.NodeList} element The element or CSS selector for an element to be made draggable.
If a {@link glow.dom.NodeList NodeList} or CSS selector matching
multiple elements is passed only the first element is made draggable.
@param {Object} [opts]
An object of options.
The opts object allows you to pass in functions to use as event
listeners. This is purely for convenience, you can also use
{@link glow.events.addListener} to add them the normal way.
@param {String} [opts.placeholder=spacer] Defines what to leave in place of the draggable whilst being dragged.
Possible values for this param are:
<dl>
<dt>spacer</dt><dd>an empty div is created where the draggable started.</dd>
<dt>clone</dt><dd>an exact clone of the original element.</dd>
<dt>none</dt><dd>no placeholder will be created.</dd>
</dl>
@param {String} [opts.placeholderClass=glow-dragdrop-placeholder] A class be applied to the placeholder element.
This can be used to to add styling for indicating where the element
has been dragged from, add opacity, etc.
@param {Selector | Element | glow.dom.NodeList} [opts.handle] Restrict the drag 'handle' to an element within the draggable.
@param {Selector | Element | glow.dom.NodeList} [opts.container] Constrain dragging to within the bounds of the specified element.
@param {Array} [opts.dropTargets] An array of {@link glow.dragdrop.DropTarget DropTargets}.
Specifies which {@link glow.dragdrop.DropTarget DropTargets} this draggable is associated with.
@param {String} [opts.axis] Restrict dragging to an axis.
Possible values for this param are:
<dl>
<dt>x</dt><dd>Restricts dragging to the x-axis</dd>
<dt>y</dt><dd>Restricts dragging to the y-axis</dd>
</dl>
@param {String[]} [opts.dragPrevention=input, textarea, button, select, option, a] Disables dragging from the specified array of element names
By default dragging will not work when the user clicks in form
elements, otherwise these elements would be unusable.
@param {Number|Object} [opts.step=1] The pixel interval the draggable snaps to.
If a number, the draggable will step by that number of pixels on the x and y axis. You
can provide an object in the form <code>{x:2, y:4}</code> to set different steps to each
axis.
@param {Function} [opts.onDrag] An event listener that fires when the draggable starts being dragged.
@param {Function} [opts.onEnter] An event listener that fires when the draggable is dragged over a drop target.
@param {Function} [opts.onLeave] An event listener that fires when the draggable is dragged out of a drop target.
@param {Function} [opts.onDrop] An event listener that fires when the draggable is dropped.
@param {Function} [opts.onAfterDrop] An event listener that fires after the element has dropped, including any animations
The default action is to animate the draggable back to it's start
position. This can be cancelled by returning false from the listener
or calling {@link glow.events.Event.preventDefault} on the
{@link glow.events.Event} param.
@example
// create a draggable element with a corresponding DropTarget,
// container and two event listeners
var myDraggable = new glow.dragdrop.Draggable('#draggable', {
dropTargets : [ myDropTarget ],
container : '#container',
onDrag : function () {
this.element.css('opacity', '0.7');
},
onDrop : function () {
this.element.css('opacity', '1');
}
});
*/
/**
@name glow.dragdrop.Draggable#event:drag
@event
@description Fired when the draggable starts being dragged.
Concelling this event results in the user being unable to pick up
the draggable.
@param {glow.events.Event} event Event Object
*/
/**
@name glow.dragdrop.Draggable#event:enter
@event
@description Fired when the draggable is dragged over a drop target.
@param {glow.events.Event} event Event Object
*/
/**
@name glow.dragdrop.Draggable#event:leave
@event
@description Fired when the draggable is dragged out of a drop target.
@param {glow.events.Event} event Event Object
*/
/**
@name glow.dragdrop.Draggable#event:drop
@event
@description Fired when the draggable is dropped.
@param {glow.events.Event} event Event Object
*/
/**
@name glow.dragdrop.Draggable#event:afterDrop
@event
@description Fired after the element has dropped, including any animations
@param {glow.events.Event} event Event Object
*/
r.Draggable = function (el, opts) {
/**
@name glow.dragdrop.Draggable#element
@type glow.dom.NodeList
@description glow.dom.NodeList containing the draggable element
*/
this.element = $(el);
this._opts = opts = glow.lang.apply({
dragPrevention : ['input', 'textarea', 'button', 'select', 'option', 'a'],
placeholder : 'spacer',
placeholderClass : 'glow-dragdrop-placeholder',
step : {x:1, y:1}
}, opts || {});
//normalise the step param to an object
if (typeof opts.step == "number") {
opts.step = {x: opts.step, y: opts.step};
} else {
opts.step.x = opts.step.x || 1;
opts.step.y = opts.step.y || 1;
}
this._preventDrag = [];
for (var i = 0, l = opts.dragPrevention.length; i < l; i++) {
this._preventDrag[i] = opts.dragPrevention[i].toLowerCase();
}
if (opts.container) { this.container = $(opts.container); }
this._handle = opts.handle && this.element.get(opts.handle) || this.element;
if (opts.dropTargets) this.dropTargets = $(opts.dropTargets);
//used for IE literal edge case bug fix
//this._mouseUp = true;
//bug fix to get document.body.scrollTop to return true value (not 0) if using transitional 4.01 doctype
//get('body')[0].style.overflow = 'auto';
//this._opts = o, this._targetCoords = [], this.isOverTarget = false;
var listeners = this._listeners = [],
i = 0;
if (opts.onDrag) listeners[i++] = addListener(this, 'drag', this._opts.onDrag, this);
if (opts.onEnter) listeners[i++] = addListener(this, 'enter', this._opts.onEnter, this);
if (opts.onLeave) listeners[i++] = addListener(this, 'leave', this._opts.onLeave, this);
if (opts.onDrop) listeners[i++] = addListener(this, 'drop', this._opts.onDrop, this);
if (opts.onAfterDrop) listeners[i++] = addListener(this, 'afterDrop', this._opts.onAfterDrop, this);
this._dragListener = addListener(this._handle, 'mousedown', this._startDragMouse, this);
return;
};
/*
Group: Methods
*/
//var applyFloatBugfix = glow.env.ie;
r.Draggable.prototype = {
/*
PrivateMethod: _createPlaceholder
Create an element that occupies the space where the draggable has been dragged from.
*/
_createPlaceholder: function () {
var el = this.element,
placeholder,
box = this._box;
if (this._opts.placeholder == 'clone') {
placeholder = el.clone();
}
else { // placeholder == 'spacer'
placeholder = placeholderElement(el);
}
if (this._opts.placeholderClass) {
placeholder.addClass(this._opts.placeholderClass);
}
box.sizePlaceholder(placeholder, null, this._startLeft, this._startTop);
el.after(placeholder);
this._placeholder = placeholder;
},
/*
PrivateMethod: _removePlaceholder
Removes the placeholder (see above) from the document.
*/
_removePlaceholder: function () {
this._placeholder.remove();
},
/*
PrivateMethod: _resetPosition
Sets the position CSS property to what it started as without moving the draggable. If the
original position was 'static' and making it 'static' again would mean moving the draggable,
then the position is set to 'relative'.
*/
_resetPosition: function () {
var origPos = this._preDragPosition,
el = this.element,
box = this._box,
startOffset = this._startOffset,
pos = el.css('position'),
newLeft,
newTop;
box.resetPosition();
var position = box.el.position(),
offset = {
x: position.left,
y: position.top
};
if (this._placeholder || this._dropIndicator) {
el.remove();
}
if (origPos == 'static' && offset.y == startOffset.y && offset.x == startOffset.x) {
el.css('position', 'static');
el.css('left', '');
el.css('top', '');
}
else {
el.css('z-index', this._preDragZIndex);
el.css('position', origPos == 'static' ? 'relative' : origPos);
if (origPos == 'static') {
newLeft = offset.x - startOffset.x;
newTop = offset.y - startOffset.y;
}
else if (origPos == 'relative' && pos != 'relative') {
newLeft = this._startLeft + (offset.x - startOffset.x);
newTop = this._startTop + (offset.y - startOffset.y);
}
if (pos != origPos) {
el.css('left', newLeft ? newLeft + 'px' : '');
el.css('top', newTop ? newTop + 'px' : '');
}
}
if (this._dropIndicator) {
var parent = this._dropIndicator.parent()[0];
if (parent) parent.replaceChild(el[0], this._dropIndicator[0]);
delete this._dropIndicator;
if (this._placeholder) {
this._placeholder.remove();
delete this._placeholder;
}
// this is canceling out some of the stuff done in the if statement above, could be done better
el.css('position', origPos);
if (origPos == 'relative' && pos != 'relative') {
el.css('left', this._startLeft);
el.css('top', this._startTop);
}
}
else if (this._placeholder) {
var parent = this._placeholder.parent()[0];
if (parent) parent.replaceChild(el[0], this._placeholder[0]);
delete this._placeholder;
}
},
/*
PrivateFunction: _startDragMouse
Start the draggable dragging when the mousedown event is fired.
Arguments:
*e* (glow.events.Event)
The mousedown event that caused the listener to be fired.
*/