From 0f43a289d62e8d141dc1a12bf3c741238fd7c2c7 Mon Sep 17 00:00:00 2001 From: Dan Miser Date: Wed, 25 May 2011 22:58:52 -0500 Subject: [PATCH] Cleaned up several issues: - constante typo fix - clear out options when calling addOptions - clean up parsing code to identify selected items --- js/ui.multiselect.js | 1017 +++++++++++++++++++++++++++++++++--------- 1 file changed, 799 insertions(+), 218 deletions(-) diff --git a/js/ui.multiselect.js b/js/ui.multiselect.js index 1234fa7..c87aba3 100755 --- a/js/ui.multiselect.js +++ b/js/ui.multiselect.js @@ -8,329 +8,910 @@ * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * - * http://www.quasipartikel.at/multiselect/ + * http://yanickrochon.uuuq.com/multiselect/ * * * Depends: - * ui.core.js - * ui.sortable.js + * ui.core.js + * ui.draggable.js + * ui.droppable.js + * ui.sortable.js + * jquery.blockUI (http://github.com/malsup/blockui/) + * jquery.tmpl (http://andrew.hedges.name/blog/2008/09/03/introducing-jquery-simple-templates * * Optional: - * localization (http://plugins.jquery.com/project/localisation) - * scrollTo (http://plugins.jquery.com/project/ScrollTo) + * localization (http://plugins.jquery.com/project/localisation) + * + * Notes: + * The strings in this plugin use a templating engine to enable localization + * and allow flexibility in the messages. Read the documentation for more details. * * Todo: - * Make batch actions faster - * Implement dynamic insertion through remote calls + * restore selected items on remote searchable multiselect upon page reload (same behavior as local mode) + * (is it worth it??) add a public function to apply the nodeComparator to all items (when using nodeComparator setter) + * support for option groups, disabled options, etc. + * speed improvements + * tests and optimizations + * - test getters/setters (including options from the defaults) */ +/******************************** + * Default callbacks + ********************************/ + +// expect data to be "val1=text1[\nval2=text2[\n...]]" +var defaultDataParser = function(data) { + if ( typeof data == 'string' ) { + var pattern = /^(\s\n\r\t)*\+?$/; + var lineSelected, line, lines = data.split(/\n/); + data = {}; + for (var i in lines) { + line = lines[i].split("="); + // make sure the key is not empty + if (!pattern.test(line[0])) { + lineSelected = (line[0].lastIndexOf('+') == line[0].length - 1); + if (lineSelected) line[0] = line[0].substr(0,line[0].length-1); + // if no value is specified, default to the key value + data[line[0]] = { + selected: lineSelected, + value: line[1] || line[0] + }; + } + } + } else { + this._messages($.ui.multiselect.constants.MESSAGE_ERROR, $.ui.multiselect.locale.errorDataFormat); + data = false; + } + return data; +}; + +var defaultNodeComparator = function(node1,node2) { + var text1 = node1.text(), + text2 = node2.text(); + return text1 == text2 ? 0 : (text1 < text2 ? -1 : 1); +}; + (function($) { $.widget("ui.multiselect", { options: { - sortable: true, + // sortable and droppable + sortable: 'left', + droppable: 'both', + // searchable searchable: true, - doubleClickable: true, + searchDelay: 400, + remoteUrl: null, + remoteParams: {}, + // animated animated: 'fast', show: 'slideDown', hide: 'slideUp', + // ui dividerLocation: 0.6, - nodeComparator: function(node1,node2) { - var text1 = node1.text(), - text2 = node2.text(); - return text1 == text2 ? 0 : (text1 < text2 ? -1 : 1); - } + // callbacks + dataParser: defaultDataParser, + nodeComparator: defaultNodeComparator, + nodeInserted: null }, _create: function() { this.element.hide(); - this.id = this.element.attr("id"); + this.busy = false; // busy state this.container = $('
').insertAfter(this.element); - this.count = 0; // number of currently selected options - this.selectedContainer = $('
').appendTo(this.container); - this.availableContainer = $('
').appendTo(this.container); - this.selectedActions = $('
0 '+$.ui.multiselect.locale.itemsCount+''+$.ui.multiselect.locale.removeAll+'
').appendTo(this.selectedContainer); - this.availableActions = $('
'+$.ui.multiselect.locale.addAll+'
').appendTo(this.availableContainer); - this.selectedList = $('').bind('selectstart', function(){return false;}).appendTo(this.selectedContainer); - this.availableList = $('').bind('selectstart', function(){return false;}).appendTo(this.availableContainer); + this.selectedContainer = $('
').appendTo(this.container); + this.availableContainer = $('
').appendTo(this.container); + this.selectedActions = $('
'+$.tmpl($.ui.multiselect.locale.itemsCount,{count:0})+''+$.tmpl($.ui.multiselect.locale.removeAll)+'
').appendTo(this.selectedContainer); + this.availableActions = $('
'+$.tmpl($.ui.multiselect.locale.busy)+''+$.tmpl($.ui.multiselect.locale.addAll)+'
').appendTo(this.availableContainer); + this.selectedList = $('').bind('selectstart', function(){return false;}).appendTo(this.selectedContainer); + this.availableList = $('').bind('selectstart', function(){return false;}).appendTo(this.availableContainer); var that = this; - // set dimensions - this.container.width(this.element.width()+1); - this.selectedContainer.width(Math.floor(this.element.width()*this.options.dividerLocation)); - this.availableContainer.width(Math.floor(this.element.width()*(1-this.options.dividerLocation))); - - // fix list height to match