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