2 * multiselect2side jQuery plugin
4 * Copyright (c) 2010 Giovanni Casassa (senamion.com - senamion.it)
6 * Dual licensed under the MIT (MIT-LICENSE.txt)
7 * and GPL (GPL-LICENSE.txt) licenses.
9 * http://www.senamion.com
14 jQuery.fn.multiselect2side = function (o) {
16 selectedPosition: 'right',
19 labelBottom: 'Bottom',
29 sortOptions: function() {
30 o.leftSel.sortOptions();
31 o.rightSel.sortOptions();
35 return this.each(function () {
38 var hiddenName = $(this).attr("name");
39 var originalName = $(this).attr("name");
40 if (originalName.indexOf('[') != -1)
41 originalName = originalName.substring(0, originalName.indexOf('['));
43 var nameDx = originalName + "ms2side__dx";
44 var idDx = originalName + "ms2side__dx";
45 var nameSx = originalName + "ms2side__sx";
46 var hiddenId = originalName + "ms2side_hidden";
47 var size = $(this).attr("size");
48 $(this).attr("name", originalName + "ms2side__orig");
51 $(this).attr("size", "6");
57 "<div class='ms2side__updown'>" +
58 "<p class='SelSort' title='Sort'>" + o.labelSort + "</p>" +
59 "<p class='MoveTop' title='Move on top selected option'>" + o.labelTop + "</p>" +
60 "<p class='MoveUp' title='Move up selected option'>" + o.labelUp + "</p>" +
61 "<p class='MoveDown' title='Move down selected option'>" + o.labelDown + "</p>" +
62 "<p class='MoveBottom' title='Move on bottom selected option'>" + o.labelBottom + "</p>" +
65 // CREATE NEW ELEMENT (AND HIDE IT) AFTER THE HIDDED ORGINAL SELECT
67 "<div class='ms2side__div'>" +
68 ((o.selectedPosition != 'right' && o.moveOptions) ? divUpDown : "") +
69 "<div class='ms2side__select'>" +
70 (o.labelsx ? ("<div class='ms2side__header'>" + o.labelsx + "</div>") : "") +
71 "<select title='" + o.labelsx + "' name='" + nameSx + "' id='" + nameSx + "' size='" + size + "' multiple='multiple' ></select>" +
73 "<div class='ms2side__options'>" +
74 ((o.selectedPosition == 'right')
76 ("<p class='AddOne' title='Add Selected'>›</p>" +
77 "<p class='AddAll' title='Add All'>»</p>" +
78 "<p class='RemoveOne' title='Remove Selected'>‹</p>" +
79 "<p class='RemoveAll' title='Remove All'>«</p>")
81 ("<p class='AddOne' title='Add Selected'>‹</p>" +
82 "<p class='AddAll' title='Add All'>«</p>" +
83 "<p class='RemoveOne' title='Remove Selected'>›</p>" +
84 "<p class='RemoveAll' title='Remove All'>»</p>")
87 "<div class='ms2side__select'>" +
88 (o.labeldx ? ("<div class='ms2side__header'>" + o.labeldx + "</div>") : "") +
89 "<select title='" + o.labeldx + "' name='" + nameDx + "' id='" + idDx + "' size='" + size + "' multiple='multiple' ></select>" +
91 "<span id=\"" + hiddenId + "\"></span>" +
92 ((o.selectedPosition == 'right' && o.moveOptions) ? divUpDown : "") +
94 $(this).after(htmlToAdd).hide();
95 $("#" + hiddenId).hide();
98 var allSel = $(this).next().find("select");
99 var leftSel = (o.selectedPosition == 'right') ? allSel.eq(0) : allSel.eq(1);
100 var rightSel = (o.selectedPosition == 'right') ? allSel.eq(1) : allSel.eq(0);
102 var heightDiv = $(".ms2side__select").eq(0).height();
104 o.rightSel = rightSel;
106 // CENTER MOVE OPTIONS AND UPDOWN OPTIONS
107 $(this).next().find('.ms2side__options, .ms2side__updown').each(function(){
108 var top = ((heightDiv/2) - ($(this).height()/2));
110 $(this).css('padding-top', top + 'px' );
113 // MOVE SELECTED OPTION TO RIGHT, NOT SELECTED TO LEFT
114 $(this).find("option:selected").clone().appendTo(rightSel);
115 $(this).find("option:not(:selected)").clone().appendTo(leftSel);
117 // Mark all unselected.
118 rightSel.find("option").attr("selected", false);
120 // ON CHANGE REFRESH ALL BUTTON STATUS
121 allSel.change(function() {
122 var div = $(this).parent().parent();
123 var selectSx = leftSel.children();
124 var selectDx = rightSel.children();
125 var selectedSx = leftSel.find("option:selected");
126 var selectedDx = rightSel.find("option:selected");
127 var hiddenCont = $("#" + hiddenId);
129 if (selectedSx.size() == 0 || (o.maxSelected >= 0 && (selectedSx.size() + selectDx.size()) > o.maxSelected))
130 div.find(".AddOne").addClass('ms2side__hide');
132 div.find(".AddOne").removeClass('ms2side__hide');
135 div.find(".RemoveOne, .MoveUp, .MoveDown, .MoveTop, .MoveBottom, .SelSort").addClass('ms2side__hide');
136 // if (selectDx.size() > 1)
137 // div.find(".SelSort").removeClass('ms2side__hide');
138 if (selectedDx.size() > 0) {
139 div.find(".RemoveOne").removeClass('ms2side__hide');
140 // // ALL SELECTED - NO MOVE
141 // if (selectedDx.size() < selectDx.size()) { // FOR NOW (JOE) && selectedDx.size() == 1
142 // if (selectedDx.val() != selectDx.val()) // FIRST OPTION, NO UP AND TOP BUTTON
143 // div.find(".MoveUp, .MoveTop").removeClass('ms2side__hide');
144 // if (selectedDx.last().val() != selectDx.last().val()) // LAST OPTION, NO DOWN AND BOTTOM BUTTON
145 // div.find(".MoveDown, .MoveBottom").removeClass('ms2side__hide');
149 if (selectSx.size() == 0 || (o.maxSelected >= 0 && selectSx.size() >= o.maxSelected))
150 div.find(".AddAll").addClass('ms2side__hide');
152 div.find(".AddAll").removeClass('ms2side__hide');
154 if (selectDx.size() == 0)
155 div.find(".RemoveAll").addClass('ms2side__hide');
157 div.find(".RemoveAll").removeClass('ms2side__hide');
159 // Rebuild hidden inputs...
161 rightSel.find("option").each(function(idx, option) {
162 $('<input type="hidden"/>').attr("name", hiddenName).attr("value", $(option).attr("value")).appendTo(hiddenCont);
166 // DOUBLE CLICK ON LEFT SELECT OPTION
167 leftSel.dblclick(function () {
168 $(this).find("option:selected").each(function(i, selected){
170 if (o.maxSelected < 0 || rightSel.children().size() < o.maxSelected) {
171 $(this).remove().appendTo(rightSel);
172 el.find("[value=" + $(selected).val() + "]").attr("selected", true).remove().appendTo(el);
175 $(this).trigger('change');
179 // DOUBLE CLICK ON RIGHT SELECT OPTION
180 rightSel.dblclick(function () {
181 $(this).find("option:selected").each(function(i, selected){
182 $(this).remove().appendTo(leftSel);
183 el.find("[value=" + $(selected).val() + "]").attr("selected", false).remove().appendTo(el);
185 $(this).trigger('change');
190 $(this).next().find('.ms2side__options').children().click(function () {
191 if (!$(this).hasClass("ms2side__hide")) {
192 if ($(this).hasClass("AddOne")) {
193 leftSel.find("option:selected").each(function(i, selected){
194 $(this).remove().appendTo(rightSel);
195 el.find("[value=" + $(selected).val() + "]").attr("selected", true).remove().appendTo(el);
199 } else if ($(this).hasClass("AddAll")) { // ALL SELECTED
200 leftSel.children().appendTo(rightSel);
201 leftSel.children().remove();
202 el.find('option').attr("selected", true);
203 // el.children().attr("selected", true); -- PROBLEM WITH OPTGROUP
206 } else if ($(this).hasClass("RemoveOne")) {
207 rightSel.find("option:selected").each(function(i, selected){
208 $(this).remove().appendTo(leftSel);
209 el.find("[value=" + $(selected).val() + "]").attr("selected", false).remove().appendTo(el);
213 } else if ($(this).hasClass("RemoveAll")) { // ALL REMOVED
214 rightSel.children().appendTo(leftSel);
215 rightSel.children().remove();
216 el.find('option').attr("selected", false);
217 //el.children().attr("selected", false); -- PROBLEM WITH OPTGROUP
222 leftSel.trigger('change');
225 // CLICK ON UP - DOWN
226 $(this).next().find('.ms2side__updown').children().click(function () {
227 var selectedDx = rightSel.find("option:selected");
228 var selectDx = rightSel.find("option");
230 if (!$(this).hasClass("ms2side__hide")) {
231 if ($(this).hasClass("SelSort")) {
232 // SORT SELECTED ELEMENT
233 selectDx.sort(function(a, b) {
234 var compA = $(a).text().toUpperCase();
235 var compB = $(b).text().toUpperCase();
236 return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
238 // FIRST REMOVE FROM ORIGINAL SELECT
239 el.find("option:selected").remove();
240 // AFTER ADD ON ORIGINAL AND RIGHT SELECT
241 selectDx.each(function() {
242 rightSel.append($(this).clone().attr("selected", true));
243 el.append($(this).attr("selected", true));
246 } else if ($(this).hasClass("MoveUp")) {
247 var prev = selectedDx.first().prev();
248 var hPrev = el.find("[value=" + prev.val() + "]");
250 selectedDx.each(function() {
251 $(this).insertBefore(prev);
252 el.find("[value=" + $(this).val() + "]").insertBefore(hPrev); // HIDDEN SELECT
255 } else if ($(this).hasClass("MoveDown")) {
256 var next = selectedDx.last().next();
257 var hNext = el.find("[value=" + next.val() + "]");
259 selectedDx.each(function() {
260 $(this).insertAfter(next);
261 el.find("[value=" + $(this).val() + "]").insertAfter(hNext); // HIDDEN SELECT
264 } else if ($(this).hasClass("MoveTop")) {
265 var first = selectDx.first();
266 var hFirst = el.find("[value=" + first.val() + "]");
268 selectedDx.each(function() {
269 $(this).insertBefore(first);
270 el.find("[value=" + $(this).val() + "]").insertBefore(hFirst); // HIDDEN SELECT
273 } else if ($(this).hasClass("MoveBottom")) {
274 var last = selectDx.last();
275 var hLast = el.find("[value=" + last.val() + "]");
277 selectedDx.each(function() {
278 last = $(this).insertAfter(last); // WITH last = SAME POSITION OF SELECTED OPTION AFTER MOVE
279 hLast = el.find("[value=" + $(this).val() + "]").insertAfter(hLast); // HIDDEN SELECT
284 leftSel.trigger('change');
288 $(this).next().find('.ms2side__options, .ms2side__updown').children().hover(
290 $(this).addClass('ms2side_hover');
293 $(this).removeClass('ms2side_hover');
297 // UPDATE BUTTON ON START
298 leftSel.trigger('change');
300 // SHOW WHEN ALL READY
301 $(this).next().show();