From 1633d71b4a53ff40ed2bfba4c3ffbcdb023f08ec Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Mon, 28 May 2012 23:15:41 -0700 Subject: [PATCH 1/3] add container() method that retrieves the main container --- select2.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/select2.js b/select2.js index 87fe693d..9f4cf380 100755 --- a/select2.js +++ b/select2.js @@ -1424,7 +1424,7 @@ var args = Array.prototype.slice.call(arguments, 0), opts, select2, - value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused"]; + value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container"]; this.each(function () { if (args.length === 0 || typeof(args[0]) === "object") { @@ -1449,7 +1449,11 @@ value = undefined; select2 = $(this).data("select2"); if (select2 === undefined) return; - value = select2[args[0]].apply(select2, args.slice(1)); + if (args[0] === "container") { + value=select2.container; + } else { + value = select2[args[0]].apply(select2, args.slice(1)); + } if (value !== undefined) {return false;} } else { throw "Invalid arguments to select2 plugin: " + args; From 23720246f29d8651a1f06b720657a5632423af88 Mon Sep 17 00:00:00 2001 From: nicola Date: Tue, 29 May 2012 16:48:38 +0400 Subject: [PATCH 2/3] fix internal function indexOf, when undefined passed --- select2.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/select2.js b/select2.js index 87fe693d..69acb381 100755 --- a/select2.js +++ b/select2.js @@ -70,6 +70,10 @@ function indexOf(value, array) { var i = 0, l = array.length, v; + if (typeof value == 'undefined') { + return -1; + } + if (value.constructor === String) { for (; i < l; i = i + 1) if (value.localeCompare(array[i]) === 0) return i; } else { From f368464132ea55f979693e4587603c1a84f75ee8 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Tue, 29 May 2012 17:04:52 -0700 Subject: [PATCH 3/3] add drag and drop sort support, fixes #60 --- select2.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/select2.js b/select2.js index 9f4cf380..b5ee47b0 100755 --- a/select2.js +++ b/select2.js @@ -1376,15 +1376,15 @@ }, setVal: function (val) { - var unique = []; + var unique; if (this.select) { this.select.val(val); } else { + unique = []; // filter out duplicates $(val).each(function () { if (indexOf(this, unique) < 0) unique.push(this); }); - this.opts.element.val(unique.length === 0 ? "" : unique.join(",")); } }, @@ -1416,6 +1416,35 @@ } this.clearSearch(); + }, + onSortStart: function() { + if (this.select) { + throw new Error("Sorting of elements is not supported when attached to instead."); + } + + // collapse search field into 0 width so its container can be collapsed as well + this.search.width(0); + // hide the container + this.searchContainer.hide(); + }, + onSortEnd:function() { + + var val=[], self=this; + + // show search and move it to the end of the list + this.searchContainer.show(); + // make sure the search container is the last item in the list + this.searchContainer.appendTo(this.searchContainer.parent()); + // since we collapsed the width in dragStarteed, we resize it here + this.resizeSearch(); + + // update selection + + this.selection.find(".select2-search-choice").each(function() { + val.push(self.opts.id($(this).data("select2-data"))); + }); + this.setVal(val); + this.triggerChange(); } }); @@ -1424,7 +1453,7 @@ var args = Array.prototype.slice.call(arguments, 0), opts, select2, - value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container"]; + value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd"]; this.each(function () { if (args.length === 0 || typeof(args[0]) === "object") {