1
|
|
|
/** |
2
|
|
|
* @package assets |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
(function($, Symphony) { |
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Create selectable elements. Clicking an item will select it |
9
|
|
|
* by adding the class <code>.selected</code>. Holding down the shift key |
10
|
|
|
* while clicking multiple items creates a selection range. Holding the meta key |
11
|
|
|
* (which is <code>cmd</code> on a Mac or <code>ctrl</code> on Windows) allows |
12
|
|
|
* the selection of multiple items or the modification of an already selected |
13
|
|
|
* range of items. Doubleclicking outside the selection list will |
14
|
|
|
* remove the selection. |
15
|
|
|
* |
16
|
|
|
* @name $.symphonySelectable |
17
|
|
|
* @class |
18
|
|
|
* |
19
|
|
|
* @param {Object} options An object specifying containing the attributes specified below |
20
|
|
|
* @param {String} [options.items='tbody tr:has(input)'] Selector to find items that are selectable |
21
|
|
|
* item. Needed to properly handle item highlighting when used in connection with the orderable plugin |
22
|
|
|
* @param {String} [options.ignore='a'] Selector to find elements that should not propagate to the handle |
23
|
|
|
* @param {String} [optinos.mode='single'] Either 'default' (click removes other selections) or 'additive' (click adds to exisiting selection) |
24
|
|
|
* |
25
|
|
|
* @example |
26
|
|
|
|
27
|
|
|
var selectable = $('table').symphonySelectable(); |
28
|
|
|
selectable.find('a').mousedown(function(event) { |
29
|
|
|
event.stopPropagation(); |
30
|
|
|
}); |
31
|
|
|
*/ |
32
|
|
|
$.fn.symphonySelectable = function(options) { |
33
|
|
|
var objects = this, |
34
|
|
|
settings = { |
35
|
|
|
items: 'tbody tr:has(input)', |
36
|
|
|
ignore: 'a', |
37
|
|
|
mode: 'single' |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
$.extend(settings, options); |
41
|
|
|
|
42
|
|
|
/*------------------------------------------------------------------------- |
43
|
|
|
Events |
44
|
|
|
-------------------------------------------------------------------------*/ |
45
|
|
|
|
46
|
|
|
// Select |
47
|
|
|
objects.on('click.selectable', settings.items, function select(event) { |
48
|
|
|
var item = $(this), |
49
|
|
|
items = item.siblings().addBack(), |
50
|
|
|
object = $(event.liveFired), |
51
|
|
|
target = $(event.target), |
52
|
|
|
selection, deselection, first, last; |
53
|
|
|
|
54
|
|
|
// Ignored elements |
55
|
|
|
if(target.is(settings.ignore)) { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Remove text ranges |
60
|
|
|
if(window.getSelection) { |
61
|
|
|
window.getSelection().removeAllRanges(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Range selection |
65
|
|
|
if((event.shiftKey) && items.filter('.selected').length > 0 && !object.is('.single')) { |
66
|
|
|
|
67
|
|
|
// Select upwards |
68
|
|
|
if(item.prevAll().filter('.selected').length > 0) { |
69
|
|
|
first = items.filter('.selected:first').index(); |
70
|
|
|
last = item.index() + 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Select downwards |
74
|
|
|
else { |
75
|
|
|
first = item.index(); |
76
|
|
|
last = items.filter('.selected:last').index() + 1; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Get selection |
80
|
|
|
selection = items.slice(first, last); |
81
|
|
|
|
82
|
|
|
// Deselect items outside the selection range |
83
|
|
|
deselection = items.filter('.selected').not(selection).removeClass('selected').trigger('deselect.selectable'); |
84
|
|
|
deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
85
|
|
|
|
86
|
|
|
// Select range |
87
|
|
|
selection.addClass('selected').trigger('select.selectable'); |
88
|
|
|
selection.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Single selection |
92
|
|
|
else { |
93
|
|
|
|
94
|
|
|
// Press meta or ctrl key to adjust current range, otherwise the selection will be removed |
95
|
|
|
if((!event.metaKey && !event.ctrlKey && settings.mode != 'additive' && !target.is('input')) || object.is('.single')) { |
96
|
|
|
deselection = items.not(item).filter('.selected').removeClass('selected').trigger('deselect.selectable'); |
97
|
|
|
deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Toggle selection |
101
|
|
|
if(item.is('.selected')) { |
102
|
|
|
item.removeClass('selected').trigger('deselect.selectable'); |
103
|
|
|
item.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
else { |
106
|
|
|
item.addClass('selected').trigger('select.selectable'); |
107
|
|
|
item.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
// Remove all selections by doubleclicking the body |
114
|
|
|
$('body').bind('dblclick.selectable', function removeAllSelection() { |
115
|
|
|
objects.find(settings.items).removeClass('selected').trigger('deselect.selectable'); |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
/*------------------------------------------------------------------------- |
119
|
|
|
Initialisation |
120
|
|
|
-------------------------------------------------------------------------*/ |
121
|
|
|
|
122
|
|
|
// Make selectable |
123
|
|
|
objects.addClass('selectable'); |
124
|
|
|
|
125
|
|
|
/*-----------------------------------------------------------------------*/ |
126
|
|
|
|
127
|
|
|
return objects; |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
})(window.jQuery, window.Symphony); |
131
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.