forked from parvez/jQuery-Dual-Listbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.dualListBox-1.3.js
310 lines (290 loc) · 13.7 KB
/
jQuery.dualListBox-1.3.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
/*
* Developed by Justin Mead
* Copyright (c) 2011 MeadMiracle
* www.meadmiracle.com / [email protected]
* Licensed under the MIT License http://opensource.org/licenses/MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* As this Dual Listbox jQuery Plug-in was initially released with GPL v2 License which was quite restrictive, I got the following explicit permission
* to release it under MIT License from the original author and copyright holder.
* From: Justin Mead <[email protected]>
* Date: December 19, 2012, 4:11:28 PM PST
* To: Parvez <[email protected]>
* Cc: [email protected]
* Subject: Re: Dual Listbox jQuery Plug-in
* Happy to help! You have my permission to release my code under the MIT license. Good luck with your project!
* Justin
*
* Version 1.3
* Testing: IE8/Windows XP
* Firefox/Windows XP
* Chrome/Windows XP
*
* OPTIONS LISTING:
* *box1View, box2View - the id attributes of the VISIBLE listboxes
* *box1Storage, box2Storage - the id attributes of the HIDDEN listboxes (used for filtering)
* *box1Filter, box2Filter - the id attributes of the textboxes used to filter the lists
* *box1Clear, box2Clear - the id attributes of the elements used to clear/reset the filters
* *box1Counter, box2Counter - the id attributes of the elements used to display counts of visible/total items (used when filtering)
* *to1, to2 - the id attributes of the elements used to transfer only selected items between boxes
* *allTo1, allTo2 - the id attributes of the elements used to transfer ALL (visible) items between boxes
* *transferMode - the type of transfer to perform on items (see heading TRANSFER MODES)
* *sortBy - the attribute to use when sorting items (values: 'text' or 'value')
* *useFilters - allow the filtering of items in either box (true/false)
* *useCounters - use the visible/total counters (true/false)
* *useSorting - sort items after executing a transfer (true/false)
* *selectOnSubmit - select items in box 2 when the form is submitted (true/false)
*
* All options have default values, and as such, are optional. Check the 'settings' JSON object below to see the defaults.
*
* TRANSFER MODES:
* * 'move' - In this mode, items will be removed from the box in which they currently reside and moved to the other box. This is the default.
* * 'copy' - In this mode, items in box 1 will ALWAYS remain in box 1 (unless they are hidden by filtering). When they are selected for transfer
* they will be copied to box 2 and will be given the class 'copiedOption' in box 1 (my default styling for this class is yellow background
* but you may choose whatever styling suits you). If they are then transferred from box 2, they disappear from box 2, and the 'copiedOption'
* class is removed from the corresponding option in box 1.
*
*/
(function($) {
var settings = new Array();
var group1 = new Array();
var group2 = new Array();
var onSort = new Array();
//the main method that the end user will execute to setup the DLB
$.configureBoxes = function(options) {
//define default settings
var index = settings.push({
box1View: 'box1View',
box1Storage: 'box1Storage',
box1Filter: 'box1Filter',
box1Clear: 'box1Clear',
box1Counter: 'box1Counter',
box2View: 'box2View',
box2Storage: 'box2Storage',
box2Filter: 'box2Filter',
box2Clear: 'box2Clear',
box2Counter: 'box2Counter',
to1: 'to1',
allTo1: 'allTo1',
to2: 'to2',
allTo2: 'allTo2',
transferMode: 'move',
sortBy: 'text',
useFilters: true,
useCounters: true,
useSorting: true,
selectOnSubmit: true
});
index--;
//merge default settings w/ user defined settings (with user-defined settings overriding defaults)
$.extend(settings[index], options);
//define box groups
group1.push({
view: settings[index].box1View,
storage: settings[index].box1Storage,
filter: settings[index].box1Filter,
clear: settings[index].box1Clear,
counter: settings[index].box1Counter,
index: index
});
group2.push({
view: settings[index].box2View,
storage: settings[index].box2Storage,
filter: settings[index].box2Filter,
clear: settings[index].box2Clear,
counter: settings[index].box2Counter,
index: index
});
//define sort function
if (settings[index].sortBy == 'text') {
onSort.push(function(a, b) {
var aVal = a.text.toLowerCase();
var bVal = b.text.toLowerCase();
if (aVal < bVal) { return -1; }
if (aVal > bVal) { return 1; }
return 0;
});
} else {
onSort.push(function(a, b) {
var aVal = a.value.toLowerCase();
var bVal = b.value.toLowerCase();
if (aVal < bVal) { return -1; }
if (aVal > bVal) { return 1; }
return 0;
});
}
//configure events
if (settings[index].useFilters) {
$('#' + group1[index].filter).keyup(function() {
Filter(group1[index]);
});
$('#' + group2[index].filter).keyup(function() {
Filter(group2[index]);
});
$('#' + group1[index].clear).click(function() {
ClearFilter(group1[index]);
});
$('#' + group2[index].clear).click(function() {
ClearFilter(group2[index]);
});
}
if (IsMoveMode(settings[index])) {
$('#' + group2[index].view).dblclick(function() {
MoveSelected(group2[index], group1[index]);
});
$('#' + settings[index].to1).click(function() {
MoveSelected(group2[index], group1[index]);
});
$('#' + settings[index].allTo1).click(function() {
MoveAll(group2[index], group1[index]);
});
} else {
$('#' + group2[index].view).dblclick(function() {
RemoveSelected(group2[index], group1[index]);
});
$('#' + settings[index].to1).click(function() {
RemoveSelected(group2[index], group1[index]);
});
$('#' + settings[index].allTo1).click(function() {
RemoveAll(group2[index], group1[index]);
});
}
$('#' + group1[index].view).dblclick(function() {
MoveSelected(group1[index], group2[index]);
});
$('#' + settings[index].to2).click(function() {
MoveSelected(group1[index], group2[index]);
});
$('#' + settings[index].allTo2).click(function() {
MoveAll(group1[index], group2[index]);
});
//initialize the counters
if (settings[index].useCounters) {
UpdateLabel(group1[index]);
UpdateLabel(group2[index]);
}
//pre-sort item sets
if (settings[index].useSorting) {
SortOptions(group1[index]);
SortOptions(group2[index]);
}
//hide the storage boxes
$('#' + group1[index].storage + ',#' + group2[index].storage).css('display', 'none');
//attach onSubmit functionality if desired
if (settings[index].selectOnSubmit) {
$('#' + settings[index].box2View).closest('form').submit(function() {
$('#' + settings[index].box2View).children('option').attr('selected', 'selected');
});
}
};
function UpdateLabel(group) {
var showingCount = $("#" + group.view + " option").size();
var hiddenCount = $("#" + group.storage + " option").size();
$("#" + group.counter).text('Showing ' + showingCount + ' of ' + (showingCount + hiddenCount));
}
function Filter(group) {
var index = group.index;
var filterLower;
if (settings[index].useFilters) {
filterLower = $('#' + group.filter).val().toString().toLowerCase();
} else {
filterLower = '';
}
$('#' + group.view + ' option').filter(function(i) {
var toMatch = $(this).text().toString().toLowerCase();
return toMatch.indexOf(filterLower) == -1;
}).appendTo('#' + group.storage);
$('#' + group.storage + ' option').filter(function(i) {
var toMatch = $(this).text().toString().toLowerCase();
return toMatch.indexOf(filterLower) != -1;
}).appendTo('#' + group.view);
try {
$('#' + group.view + ' option').removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
if (settings[index].useSorting) { SortOptions(group); }
if (settings[index].useCounters) { UpdateLabel(group); }
}
function SortOptions(group) {
var $toSortOptions = $('#' + group.view + ' option');
$toSortOptions.sort(onSort[group.index]);
$('#' + group.view).empty().append($toSortOptions);
}
function MoveSelected(fromGroup, toGroup) {
if (IsMoveMode(settings[fromGroup.index])) {
$('#' + fromGroup.view + ' option:selected').appendTo('#' + toGroup.view);
} else {
$('#' + fromGroup.view + ' option:selected:not([class*=copiedOption])').clone().appendTo('#' + toGroup.view).end().end().addClass('copiedOption');
}
try {
$('#' + fromGroup.view + ' option,#' + toGroup.view + ' option').removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
Filter(toGroup);
if (settings[fromGroup.index].useCounters) { UpdateLabel(fromGroup); }
}
function MoveAll(fromGroup, toGroup) {
if (IsMoveMode(settings[fromGroup.index])) {
$('#' + fromGroup.view + ' option').appendTo('#' + toGroup.view);
} else {
$('#' + fromGroup.view + ' option:not([class*=copiedOption])').clone().appendTo('#' + toGroup.view).end().end().addClass('copiedOption');
}
try {
$('#' + fromGroup.view + ' option,#' + toGroup.view + ' option').removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
Filter(toGroup);
if (settings[fromGroup.index].useCounters) { UpdateLabel(fromGroup); }
}
function RemoveSelected(removeGroup, otherGroup) {
$('#' + otherGroup.view + ' option.copiedOption').add('#' + otherGroup.storage + ' option.copiedOption').remove();
try {
$('#' + removeGroup.view + ' option:selected').appendTo('#' + otherGroup.view).removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
$('#' + removeGroup.view + ' option').add('#' + removeGroup.storage + ' option').clone().addClass('copiedOption').appendTo('#' + otherGroup.view);
Filter(otherGroup);
if (settings[removeGroup.index].useCounters) { UpdateLabel(removeGroup); }
}
function RemoveAll(removeGroup, otherGroup) {
$('#' + otherGroup.view + ' option.copiedOption').add('#' + otherGroup.storage + ' option.copiedOption').remove();
try {
$('#' + removeGroup.storage + ' option').clone().addClass('copiedOption').add('#' + removeGroup.view + ' option').appendTo('#' + otherGroup.view).removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
Filter(otherGroup);
if (settings[removeGroup.index].useCounters) { UpdateLabel(removeGroup); }
}
function ClearFilter(group) {
$('#' + group.filter).val('');
$('#' + group.storage + ' option').appendTo('#' + group.view);
try {
$('#' + group.view + ' option').removeAttr('selected');
}
catch (ex) {
//swallow the error for IE6
}
if (settings[group.index].useSorting) { SortOptions(group); }
if (settings[group.index].useCounters) { UpdateLabel(group); }
}
function IsMoveMode(currSettings) {
return currSettings.transferMode == 'move';
}
})(jQuery);