|
1
|
|
|
/** |
|
2
|
|
|
* @package assets |
|
3
|
|
|
*/ |
|
4
|
|
|
|
|
5
|
|
|
(function($, Symphony) { |
|
6
|
|
|
'use strict'; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Create orderable elements. |
|
10
|
|
|
* |
|
11
|
|
|
* @name $.symphonyOrderable |
|
12
|
|
|
* @class |
|
13
|
|
|
* |
|
14
|
|
|
* @param {Object} options An object specifying containing the attributes specified below |
|
15
|
|
|
* @param {String} [options.items='li'] Selector to find items to be orderable |
|
16
|
|
|
* @param {String} [options.handles='*'] Selector to find children that can be grabbed to re-order |
|
17
|
|
|
* @param {String} [options.ignore='input, textarea, select'] Selector to find elements that should not propagate to the handle |
|
18
|
|
|
* @param {Integer} [options.delay=250] Time used to delay actions |
|
19
|
|
|
* |
|
20
|
|
|
* @example |
|
21
|
|
|
|
|
22
|
|
|
$('table').symphonyOrderable({ |
|
23
|
|
|
items: 'tr', |
|
24
|
|
|
handles: 'td' |
|
25
|
|
|
}); |
|
26
|
|
|
*/ |
|
27
|
|
|
$.fn.symphonyOrderable = function(options) { |
|
28
|
|
|
var objects = this, |
|
29
|
|
|
settings = { |
|
30
|
|
|
items: 'li', |
|
31
|
|
|
handles: '*', |
|
32
|
|
|
ignore: 'input, textarea, select, a', |
|
33
|
|
|
delay: 250 |
|
34
|
|
|
}; |
|
35
|
|
|
|
|
36
|
|
|
$.extend(settings, options); |
|
37
|
|
|
|
|
38
|
|
|
/*------------------------------------------------------------------------- |
|
39
|
|
|
Events |
|
40
|
|
|
-------------------------------------------------------------------------*/ |
|
41
|
|
|
|
|
42
|
|
|
// Start ordering |
|
43
|
|
|
objects.on('mousedown.orderable', settings.items + ' ' + settings.handles, function startOrdering(event) { |
|
44
|
|
|
var handle = $(this), |
|
45
|
|
|
item = handle.parents(settings.items), |
|
46
|
|
|
object = handle.parents('.orderable'); |
|
47
|
|
|
|
|
48
|
|
|
// Needed to prevent browsers from selecting texts and focusing textinputs |
|
49
|
|
|
if(!$(event.target).is('input, textarea')) { |
|
50
|
|
|
event.preventDefault(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if(!handle.is(settings.ignore) && !$(event.target).is(settings.ignore)) { |
|
54
|
|
|
object.data('ordering', 1); |
|
55
|
|
|
|
|
56
|
|
|
// Highlight item |
|
57
|
|
|
if(object.is('.selectable, .collapsible')) { |
|
58
|
|
|
|
|
59
|
|
|
// Delay ordering to avoid conflicts with scripts bound to the click event |
|
60
|
|
|
setTimeout(function() { |
|
61
|
|
|
if(object.data('ordering') == 1) { |
|
|
|
|
|
|
62
|
|
|
object.trigger('orderstart.orderable', [item]); |
|
63
|
|
|
item.addClass('ordering'); |
|
64
|
|
|
} |
|
65
|
|
|
}, settings.delay); |
|
66
|
|
|
} |
|
67
|
|
|
else { |
|
68
|
|
|
object.trigger('orderstart.orderable', [item]); |
|
69
|
|
|
item.addClass('ordering'); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
// Stop ordering |
|
75
|
|
|
objects.on('mouseup.orderable mouseleave.orderable', function stopOrdering() { |
|
76
|
|
|
var object = $(this), |
|
77
|
|
|
item; |
|
78
|
|
|
|
|
79
|
|
|
if(object.data('ordering') == 1) { |
|
|
|
|
|
|
80
|
|
|
item = object.find('.ordering'); |
|
81
|
|
|
item.removeClass('ordering'); |
|
82
|
|
|
object.data('ordering', 0); |
|
83
|
|
|
object.trigger('orderstop.orderable', [item]); |
|
84
|
|
|
|
|
85
|
|
|
// Lock item to avoid conflicts with scripts bound to the click event |
|
86
|
|
|
object.trigger('orderstoplock.orderable', [item]); |
|
87
|
|
|
item.addClass('locked'); |
|
88
|
|
|
setTimeout(function() { |
|
89
|
|
|
item.removeClass('locked'); |
|
90
|
|
|
object.trigger('orderstopunlock.orderable', [item]); |
|
91
|
|
|
}, settings.delay); |
|
92
|
|
|
} |
|
93
|
|
|
}); |
|
94
|
|
|
|
|
95
|
|
|
// Order items |
|
96
|
|
|
$(document).on('mousemove.orderable', '.orderable:has(.ordering)', function order(event) { |
|
97
|
|
|
var object = $(this); |
|
98
|
|
|
if (object.data('ordering') != 1) { |
|
|
|
|
|
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
// Only keep what we need from event object |
|
102
|
|
|
var pageY = event.pageY; |
|
103
|
|
|
Symphony.Utilities.requestAnimationFrame(function () { |
|
104
|
|
|
var item = object.find('.ordering'); |
|
105
|
|
|
|
|
106
|
|
|
// If there is still an ordering item in DOM |
|
107
|
|
|
if (!item.length) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
var top = item.offset().top, |
|
112
|
|
|
bottom = top + item.outerHeight(), |
|
113
|
|
|
prev, next; |
|
114
|
|
|
|
|
115
|
|
|
// Remove text ranges |
|
116
|
|
|
if(window.getSelection) { |
|
117
|
|
|
window.getSelection().removeAllRanges(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Move item up |
|
121
|
|
|
if(pageY < top) { |
|
122
|
|
|
prev = item.prev(settings.items); |
|
123
|
|
|
if(prev.length > 0) { |
|
124
|
|
|
item.insertBefore(prev); |
|
125
|
|
|
object.trigger('orderchange', [item]); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Move item down |
|
130
|
|
|
else if(pageY > bottom) { |
|
131
|
|
|
next = item.next(settings.items); |
|
132
|
|
|
if(next.length > 0) { |
|
133
|
|
|
item.insertAfter(next); |
|
134
|
|
|
object.trigger('orderchange', [item]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
}); |
|
138
|
|
|
}); |
|
139
|
|
|
|
|
140
|
|
|
/*------------------------------------------------------------------------- |
|
141
|
|
|
Initialisation |
|
142
|
|
|
-------------------------------------------------------------------------*/ |
|
143
|
|
|
|
|
144
|
|
|
// Make orderable |
|
145
|
|
|
objects.addClass('orderable'); |
|
146
|
|
|
|
|
147
|
|
|
/*-----------------------------------------------------------------------*/ |
|
148
|
|
|
|
|
149
|
|
|
return objects; |
|
150
|
|
|
}; |
|
151
|
|
|
|
|
152
|
|
|
})(window.jQuery, window.Symphony); |
|
153
|
|
|
|