Skip to content

Commit 64c8cc2

Browse files
committed
fixes #75 only add "draggable" attribute on drag handle mouse over to prevent unselectable text in Firefox
1 parent a9f88b0 commit 64c8cc2

File tree

1 file changed

+56
-78
lines changed

1 file changed

+56
-78
lines changed

src/plugins/sortable.js

Lines changed: 56 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,70 +24,43 @@
2424
// configure jQuery to use dataTransfer
2525
$.event.props.push('dataTransfer');
2626

27-
var placeholder, src, isHandle = false;
27+
var placeholder, src,
28+
that = this;
2829

29-
// only init drag from drag handle
30-
this.$el.on('mousedown', '.drag-handle', function(e) {
31-
isHandle = true;
30+
// only add "draggable" attribute when hovering drag handle
31+
// preventing text select bug in Firefox
32+
this.$el.on('mouseover', '.drag-handle', function() {
33+
that.$el.find('.rule-container, .rules-group-container').attr('draggable', true);
3234
});
33-
this.$el.on('mouseup', '.drag-handle', function(e) {
34-
isHandle = false;
35+
this.$el.on('mouseout', '.drag-handle', function() {
36+
that.$el.find('.rule-container, .rules-group-container').removeAttr('draggable');
3537
});
3638

3739
// dragstart: create placeholder and hide current element
3840
this.$el.on('dragstart', '[draggable]', function(e) {
3941
e.stopPropagation();
4042

41-
if (isHandle) {
42-
isHandle = false;
43+
// notify drag and drop (only dummy text)
44+
e.dataTransfer.setData('text', 'drag');
4345

44-
// notify drag and drop (only dummy text)
45-
e.dataTransfer.setData('text', 'drag');
46+
src = $(e.target);
4647

47-
src = $(e.target);
48+
placeholder = $('<div class="rule-placeholder">&nbsp;</div>');
49+
placeholder.css('min-height', src.height());
50+
placeholder.insertAfter(src);
4851

49-
placeholder = $('<div class="rule-placeholder">&nbsp;</div>');
50-
placeholder.css('min-height', src.height());
51-
placeholder.insertAfter(src);
52-
53-
// Chrome glitch (helper invisible if hidden immediately)
54-
setTimeout(function() {
55-
src.hide();
56-
}, 0);
57-
}
58-
else {
59-
e.preventDefault();
60-
}
52+
// Chrome glitch (helper invisible if hidden immediately)
53+
setTimeout(function() {
54+
src.hide();
55+
}, 0);
6156
});
6257

6358
// dragenter: move the placeholder
6459
this.$el.on('dragenter', '[draggable]', function(e) {
6560
e.preventDefault();
6661
e.stopPropagation();
6762

68-
var target = $(e.target), parent;
69-
70-
// on rule
71-
parent = target.closest('.rule-container');
72-
if (parent.length) {
73-
placeholder.detach().insertAfter(parent);
74-
return;
75-
}
76-
77-
// on group header
78-
parent = target.closest('.rules-group-header');
79-
if (parent.length) {
80-
parent = target.closest('.rules-group-container');
81-
placeholder.detach().prependTo(parent.find('.rules-list').eq(0));
82-
return;
83-
}
84-
85-
// on group
86-
parent = target.closest('.rules-group-container');
87-
if (parent.length) {
88-
placeholder.detach().appendTo(parent.find('.rules-list').eq(0));
89-
return;
90-
}
63+
moveSortableToTarget(placeholder, $(e.target));
9164
});
9265

9366
// dragover: prevent glitches
@@ -101,29 +74,7 @@
10174
e.preventDefault();
10275
e.stopPropagation();
10376

104-
var target = $(e.target), parent;
105-
106-
// on rule
107-
parent = target.closest('.rule-container');
108-
if (parent.length) {
109-
src.detach().insertAfter(parent);
110-
return;
111-
}
112-
113-
// on group header
114-
parent = target.closest('.rules-group-header');
115-
if (parent.length) {
116-
parent = target.closest('.rules-group-container');
117-
src.detach().prependTo(parent.find('.rules-list').eq(0));
118-
return;
119-
}
120-
121-
// on group
122-
parent = target.closest('.rules-group-container');
123-
if (parent.length) {
124-
src.detach().appendTo(parent.find('.rules-list').eq(0));
125-
return;
126-
}
77+
moveSortableToTarget(src, $(e.target));
12778
});
12879

12980
// dragend: show current element and delete placeholder
@@ -133,6 +84,10 @@
13384

13485
src.show();
13586
placeholder.remove();
87+
88+
src = placeholder = null;
89+
90+
that.$el.find('.rule-container, .rules-group-container').removeAttr('draggable');
13691
});
13792
});
13893

@@ -141,7 +96,7 @@
14196
*/
14297
this.on('afterApplyRuleFlags', function($rule, rule, flags) {
14398
if (flags.no_sortable) {
144-
$rule.removeAttr('draggable').find('.drag-handle').remove();
99+
$rule.find('.drag-handle').remove();
145100
}
146101
});
147102

@@ -151,10 +106,7 @@
151106
this.on('getGroupTemplate', function(h, level) {
152107
if (level>1) {
153108
var $h = $(h);
154-
155-
$h.attr('draggable', 'true')
156-
.find('.group-conditions').after('<div class="drag-handle"><i class="' + this.icons.sort + '"></i></div>');
157-
109+
$h.find('.group-conditions').after('<div class="drag-handle"><i class="' + this.icons.sort + '"></i></div>');
158110
h = $h.prop('outerHTML');
159111
}
160112

@@ -163,12 +115,38 @@
163115

164116
this.on('getRuleTemplate', function(h) {
165117
var $h = $(h);
166-
167-
$h.attr('draggable', 'true')
168-
.find('.rule-header').after('<div class="drag-handle"><i class="' + this.icons.sort + '"></i></div>');
169-
118+
$h.find('.rule-header').after('<div class="drag-handle"><i class="' + this.icons.sort + '"></i></div>');
170119
return $h.prop('outerHTML');
171120
});
172121
});
173122

123+
/**
124+
* Move an element (placeholder or actual object) depending on active target
125+
*/
126+
function moveSortableToTarget(element, target) {
127+
var parent;
128+
129+
// on rule
130+
parent = target.closest('.rule-container');
131+
if (parent.length) {
132+
element.detach().insertAfter(parent);
133+
return;
134+
}
135+
136+
// on group header
137+
parent = target.closest('.rules-group-header');
138+
if (parent.length) {
139+
parent = target.closest('.rules-group-container');
140+
element.detach().prependTo(parent.find('.rules-list').eq(0));
141+
return;
142+
}
143+
144+
// on group
145+
parent = target.closest('.rules-group-container');
146+
if (parent.length) {
147+
element.detach().appendTo(parent.find('.rules-list').eq(0));
148+
return;
149+
}
150+
}
151+
174152
}(jQuery));

0 commit comments

Comments
 (0)