|
1 |
/*
|
|
2 |
* multiselect2side jQuery plugin
|
|
3 |
*
|
|
4 |
* Copyright (c) 2010 Giovanni Casassa (senamion.com - senamion.it)
|
|
5 |
*
|
|
6 |
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
7 |
* and GPL (GPL-LICENSE.txt) licenses.
|
|
8 |
*
|
|
9 |
* http://www.senamion.com
|
|
10 |
*
|
|
11 |
*/
|
|
12 |
|
|
13 |
(function($) {
|
|
14 |
jQuery.fn.multiselect2side = function (o) {
|
|
15 |
o = $.extend({
|
|
16 |
selectedPosition: 'right',
|
|
17 |
moveOptions: true,
|
|
18 |
labelTop: 'Top',
|
|
19 |
labelBottom: 'Bottom',
|
|
20 |
labelUp: 'Up',
|
|
21 |
labelDown: 'Down',
|
|
22 |
labelSort: 'Sort',
|
|
23 |
labelsx: 'Available',
|
|
24 |
labeldx: 'Selected',
|
|
25 |
maxSelected: -1
|
|
26 |
}, o);
|
|
27 |
|
|
28 |
return this.each(function () {
|
|
29 |
var el = $(this);
|
|
30 |
|
|
31 |
var originalName = $(this).attr("name");
|
|
32 |
if (originalName.indexOf('[') != -1)
|
|
33 |
originalName = originalName.substring(0, originalName.indexOf('['));
|
|
34 |
|
|
35 |
var nameDx = originalName + "ms2side__dx";
|
|
36 |
var nameSx = originalName + "ms2side__sx";
|
|
37 |
var size = $(this).attr("size");
|
|
38 |
// SIZE MIN
|
|
39 |
if (size < 6) {
|
|
40 |
$(this).attr("size", "6");
|
|
41 |
size = 6;
|
|
42 |
}
|
|
43 |
|
|
44 |
// UP AND DOWN
|
|
45 |
var divUpDown =
|
|
46 |
"<div class='ms2side__updown'>" +
|
|
47 |
"<p class='SelSort' title='Sort'>" + o.labelSort + "</p>" +
|
|
48 |
"<p class='MoveTop' title='Move on top selected option'>" + o.labelTop + "</p>" +
|
|
49 |
"<p class='MoveUp' title='Move up selected option'>" + o.labelUp + "</p>" +
|
|
50 |
"<p class='MoveDown' title='Move down selected option'>" + o.labelDown + "</p>" +
|
|
51 |
"<p class='MoveBottom' title='Move on bottom selected option'>" + o.labelBottom + "</p>" +
|
|
52 |
"</div>";
|
|
53 |
|
|
54 |
// CREATE NEW ELEMENT (AND HIDE IT) AFTER THE HIDDED ORGINAL SELECT
|
|
55 |
var htmlToAdd =
|
|
56 |
"<div class='ms2side__div'>" +
|
|
57 |
((o.selectedPosition != 'right' && o.moveOptions) ? divUpDown : "") +
|
|
58 |
"<div class='ms2side__select'>" +
|
|
59 |
(o.labelsx ? ("<div class='ms2side__header'>" + o.labelsx + "</div>") : "") +
|
|
60 |
"<select title='" + o.labelsx + "' name='" + nameSx + "' id='" + nameSx + "' size='" + size + "' multiple='multiple' ></select>" +
|
|
61 |
"</div>" +
|
|
62 |
"<div class='ms2side__options'>" +
|
|
63 |
((o.selectedPosition == 'right')
|
|
64 |
?
|
|
65 |
("<p class='AddOne' title='Add Selected'>›</p>" +
|
|
66 |
"<p class='AddAll' title='Add All'>»</p>" +
|
|
67 |
"<p class='RemoveOne' title='Remove Selected'>‹</p>" +
|
|
68 |
"<p class='RemoveAll' title='Remove All'>«</p>")
|
|
69 |
:
|
|
70 |
("<p class='AddOne' title='Add Selected'>‹</p>" +
|
|
71 |
"<p class='AddAll' title='Add All'>«</p>" +
|
|
72 |
"<p class='RemoveOne' title='Remove Selected'>›</p>" +
|
|
73 |
"<p class='RemoveAll' title='Remove All'>»</p>")
|
|
74 |
) +
|
|
75 |
"</div>" +
|
|
76 |
"<div class='ms2side__select'>" +
|
|
77 |
(o.labeldx ? ("<div class='ms2side__header'>" + o.labeldx + "</div>") : "") +
|
|
78 |
"<select title='" + o.labeldx + "' name='" + nameDx + "' id='" + nameDx + "' size='" + size + "' multiple='multiple' ></select>" +
|
|
79 |
"</div>" +
|
|
80 |
((o.selectedPosition == 'right' && o.moveOptions) ? divUpDown : "") +
|
|
81 |
"</div>";
|
|
82 |
$(this).after(htmlToAdd).hide();
|
|
83 |
|
|
84 |
// ELEMENTS
|
|
85 |
var allSel = $(this).next().find("select");
|
|
86 |
var leftSel = (o.selectedPosition == 'right') ? allSel.eq(0) : allSel.eq(1);
|
|
87 |
var rightSel = (o.selectedPosition == 'right') ? allSel.eq(1) : allSel.eq(0);
|
|
88 |
// HEIGHT DIV
|
|
89 |
var heightDiv = $(".ms2side__select").eq(0).height();
|
|
90 |
|
|
91 |
// CENTER MOVE OPTIONS AND UPDOWN OPTIONS
|
|
92 |
$(this).next().find('.ms2side__options, .ms2side__updown').each(function(){
|
|
93 |
var top = ((heightDiv/2) - ($(this).height()/2));
|
|
94 |
if (top > 0)
|
|
95 |
$(this).css('padding-top', top + 'px' );
|
|
96 |
});
|
|
97 |
|
|
98 |
// MOVE SELECTED OPTION TO RIGHT, NOT SELECTED TO LEFT
|
|
99 |
$(this).find("option:selected").clone().appendTo(rightSel);
|
|
100 |
$(this).find("option:not(:selected)").clone().appendTo(leftSel);
|
|
101 |
|
|
102 |
// SELECT FIRST LEFT ITEM
|
|
103 |
if (!($.browser.msie && $.browser.version == '6.0'))
|
|
104 |
leftSel.find("option").eq(0).attr("selected", true);
|
|
105 |
|
|
106 |
// ON CHANGE REFRESH ALL BUTTON STATUS
|
|
107 |
allSel.change(function() {
|
|
108 |
var div = $(this).parent().parent();
|
|
109 |
var selectSx = leftSel.children();
|
|
110 |
var selectDx = rightSel.children();
|
|
111 |
var selectedSx = leftSel.find("option:selected");
|
|
112 |
var selectedDx = rightSel.find("option:selected");
|
|
113 |
|
|
114 |
if (selectedSx.size() == 0 || (o.maxSelected >= 0 && (selectedSx.size() + selectDx.size()) > o.maxSelected))
|
|
115 |
div.find(".AddOne").addClass('ms2side__hide');
|
|
116 |
else
|
|
117 |
div.find(".AddOne").removeClass('ms2side__hide');
|
|
118 |
|
|
119 |
// FIRST HIDE ALL
|
|
120 |
div.find(".RemoveOne, .MoveUp, .MoveDown, .MoveTop, .MoveBottom, .SelSort").addClass('ms2side__hide');
|
|
121 |
if (selectDx.size() > 1)
|
|
122 |
div.find(".SelSort").removeClass('ms2side__hide');
|
|
123 |
if (selectedDx.size() > 0) {
|
|
124 |
div.find(".RemoveOne").removeClass('ms2side__hide');
|
|
125 |
// ALL SELECTED - NO MOVE
|
|
126 |
if (selectedDx.size() < selectDx.size()) { // FOR NOW (JOE) && selectedDx.size() == 1
|
|
127 |
if (selectedDx.val() != selectDx.val()) // FIRST OPTION, NO UP AND TOP BUTTON
|
|
128 |
div.find(".MoveUp, .MoveTop").removeClass('ms2side__hide');
|
|
129 |
if (selectedDx.last().val() != selectDx.last().val()) // LAST OPTION, NO DOWN AND BOTTOM BUTTON
|
|
130 |
div.find(".MoveDown, .MoveBottom").removeClass('ms2side__hide');
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
if (selectSx.size() == 0 || (o.maxSelected >= 0 && selectSx.size() >= o.maxSelected))
|
|
135 |
div.find(".AddAll").addClass('ms2side__hide');
|
|
136 |
else
|
|
137 |
div.find(".AddAll").removeClass('ms2side__hide');
|
|
138 |
|
|
139 |
if (selectDx.size() == 0)
|
|
140 |
div.find(".RemoveAll").addClass('ms2side__hide');
|
|
141 |
else
|
|
142 |
div.find(".RemoveAll").removeClass('ms2side__hide');
|
|
143 |
});
|
|
144 |
|
|
145 |
// DOUBLE CLICK ON LEFT SELECT OPTION
|
|
146 |
leftSel.dblclick(function () {
|
|
147 |
$(this).find("option:selected").each(function(i, selected){
|
|
148 |
|
|
149 |
if (o.maxSelected < 0 || rightSel.children().size() < o.maxSelected) {
|
|
150 |
$(this).remove().appendTo(rightSel);
|
|
151 |
el.find("[value=" + $(selected).val() + "]").attr("selected", true).remove().appendTo(el);
|
|
152 |
}
|
|
153 |
});
|
|
154 |
$(this).trigger('change');
|
|
155 |
});
|
|
156 |
|
|
157 |
// DOUBLE CLICK ON RIGHT SELECT OPTION
|
|
158 |
rightSel.dblclick(function () {
|
|
159 |
$(this).find("option:selected").each(function(i, selected){
|
|
160 |
$(this).remove().appendTo(leftSel);
|
|
161 |
el.find("[value=" + $(selected).val() + "]").attr("selected", false).remove().appendTo(el);
|
|
162 |
});
|
|
163 |
$(this).trigger('change');
|
|
164 |
});
|
|
165 |
|
|
166 |
// CLICK ON OPTION
|
|
167 |
$(this).next().find('.ms2side__options').children().click(function () {
|
|
168 |
if (!$(this).hasClass("ms2side__hide")) {
|
|
169 |
if ($(this).hasClass("AddOne")) {
|
|
170 |
leftSel.find("option:selected").each(function(i, selected){
|
|
171 |
$(this).remove().appendTo(rightSel);
|
|
172 |
el.find("[value=" + $(selected).val() + "]").attr("selected", true).remove().appendTo(el);
|
|
173 |
});
|
|
174 |
|
|
175 |
} else if ($(this).hasClass("AddAll")) { // ALL SELECTED
|
|
176 |
leftSel.children().appendTo(rightSel);
|
|
177 |
leftSel.children().remove();
|
|
178 |
el.find('option').attr("selected", true);
|
|
179 |
// el.children().attr("selected", true); -- PROBLEM WITH OPTGROUP
|
|
180 |
|
|
181 |
} else if ($(this).hasClass("RemoveOne")) {
|
|
182 |
rightSel.find("option:selected").each(function(i, selected){
|
|
183 |
$(this).remove().appendTo(leftSel);
|
|
184 |
el.find("[value=" + $(selected).val() + "]").attr("selected", false).remove().appendTo(el);
|
|
185 |
});
|
|
186 |
|
|
187 |
} else if ($(this).hasClass("RemoveAll")) { // ALL REMOVED
|
|
188 |
rightSel.children().appendTo(leftSel);
|
|
189 |
rightSel.children().remove();
|
|
190 |
el.find('option').attr("selected", false);
|
|
191 |
//el.children().attr("selected", false); -- PROBLEM WITH OPTGROUP
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
leftSel.trigger('change');
|
|
196 |
});
|
|
197 |
|
|
198 |
// CLICK ON UP - DOWN
|
|
199 |
$(this).next().find('.ms2side__updown').children().click(function () {
|
|
200 |
var selectedDx = rightSel.find("option:selected");
|
|
201 |
var selectDx = rightSel.find("option");
|
|
202 |
|
|
203 |
if (!$(this).hasClass("ms2side__hide")) {
|
|
204 |
if ($(this).hasClass("SelSort")) {
|
|
205 |
// SORT SELECTED ELEMENT
|
|
206 |
selectDx.sort(function(a, b) {
|
|
207 |
var compA = $(a).text().toUpperCase();
|
|
208 |
var compB = $(b).text().toUpperCase();
|
|
209 |
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
|
|
210 |
})
|
|
211 |
// FIRST REMOVE FROM ORIGINAL SELECT
|
|
212 |
el.find("option:selected").remove();
|
|
213 |
// AFTER ADD ON ORIGINAL AND RIGHT SELECT
|
|
214 |
selectDx.each(function() {
|
|
215 |
rightSel.append($(this).clone().attr("selected", true));
|
|
216 |
el.append($(this).attr("selected", true));
|
|
217 |
});
|
|
218 |
|
|
219 |
} else if ($(this).hasClass("MoveUp")) {
|
|
220 |
var prev = selectedDx.first().prev();
|
|
221 |
var hPrev = el.find("[value=" + prev.val() + "]");
|
|
222 |
|
|
223 |
selectedDx.each(function() {
|
|
224 |
$(this).insertBefore(prev);
|
|
225 |
el.find("[value=" + $(this).val() + "]").insertBefore(hPrev); // HIDDEN SELECT
|
|
226 |
});
|
|
227 |
|
|
228 |
} else if ($(this).hasClass("MoveDown")) {
|
|
229 |
var next = selectedDx.last().next();
|
|
230 |
var hNext = el.find("[value=" + next.val() + "]");
|
|
231 |
|
|
232 |
selectedDx.each(function() {
|
|
233 |
$(this).insertAfter(next);
|
|
234 |
el.find("[value=" + $(this).val() + "]").insertAfter(hNext); // HIDDEN SELECT
|
|
235 |
});
|
|
236 |
|
|
237 |
} else if ($(this).hasClass("MoveTop")) {
|
|
238 |
var first = selectDx.first();
|
|
239 |
var hFirst = el.find("[value=" + first.val() + "]");
|
|
240 |
|
|
241 |
selectedDx.each(function() {
|
|
242 |
$(this).insertBefore(first);
|
|
243 |
el.find("[value=" + $(this).val() + "]").insertBefore(hFirst); // HIDDEN SELECT
|
|
244 |
});
|
|
245 |
|
|
246 |
} else if ($(this).hasClass("MoveBottom")) {
|
|
247 |
var last = selectDx.last();
|
|
248 |
var hLast = el.find("[value=" + last.val() + "]");
|
|
249 |
|
|
250 |
selectedDx.each(function() {
|
|
251 |
last = $(this).insertAfter(last); // WITH last = SAME POSITION OF SELECTED OPTION AFTER MOVE
|
|
252 |
hLast = el.find("[value=" + $(this).val() + "]").insertAfter(hLast); // HIDDEN SELECT
|
|
253 |
});
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
leftSel.trigger('change');
|
|
258 |
});
|
|
259 |
|
|
260 |
// HOVER ON OPTION
|
|
261 |
$(this).next().find('.ms2side__options, .ms2side__updown').children().hover(
|
|
262 |
function () {
|
|
263 |
$(this).addClass('ms2side_hover');
|
|
264 |
},
|
|
265 |
function () {
|
|
266 |
$(this).removeClass('ms2side_hover');
|
|
267 |
}
|
|
268 |
);
|
|
269 |
|
|
270 |
// UPDATE BUTTON ON START
|
|
271 |
leftSel.trigger('change');
|
|
272 |
// SHOW WHEN ALL READY
|
|
273 |
$(this).next().show();
|
|
274 |
});
|
|
275 |
};
|
|
276 |
})(jQuery);
|
jquery.multiselect2side hinzugefügt