/* * jQuery UI Multiselect * * Authors: * Michael Aufreiter (quasipartikel.at) * Yanick Rochon (yanick.rochon[at]gmail[dot]com) * * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * * http://yanickrochon.uuuq.com/multiselect/ * * * Depends: * 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) * * 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: * 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) */ // objectKeys return $jqPm.extend({ objectKeys: function(obj){ if (typeof Object.keys == 'function') return Object.keys(obj); var a = []; $jqPm.each(obj, function(k){ a.push(k) }); return a; }}); /******************************** * 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 selected, 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])) { selected = (line[0].lastIndexOf('+') == line.length - 1); if (selected) line[0] = line.substr(0,line.length-1); // if no value is specified, default to the key value data[line[0]] = { selected: false, value: line[1] || line[0] }; } } } else { this._messages($jqPm.ui.multiselect.constante.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($jqPm) { $jqPm.widget("ui.multiselect", { options: { // sortable and droppable sortable: 'none', droppable: 'none', // searchable searchable: true, searchDelay: 400, searchAtStart: false, remoteUrl: null, remoteParams: {}, remoteLimit: 50, remoteLimitIncrement: 50, remoteStart: 0, displayMore: false, // animated animated: 'fast', show: 'slideDown', hide: 'slideUp', // ui dividerLocation: 0.6, // callbacks dataParser: defaultDataParser, nodeComparator: defaultNodeComparator, nodeInserted: null, // Add Vincent - Permet le click sur le li pour le passer d'un côté à un autre triggerOnLiClick: false, // Add Vincent - Permet le choix de la langue... en fonction de l'iso_code de PrestaShop (en, fr) localeIsoCode: 'en' }, _create: function() { if (this.options.locale != undefined) { $jqPm.ui.multiselect.locale = this.options.locale; } else { $jqPm.ui.multiselect.locale = { addAll:'Add all', removeAll:'Remove all', itemsCount:'#{count} items selected', itemsTotal:'#{count} items total', busy:'please wait...', errorDataFormat:"Cannot add options, unknown data format", errorInsertNode:"There was a problem trying to add the item:\n\n\t[#{key}] => #{value}\n\nThe operation was aborted.", errorReadonly:"The option #{option} is readonly", errorRequest:"Sorry! There seemed to be a problem with the remote call. (Type: #{status})", sInputSearch:'Please enter the first letters of the search item', sInputShowMore: 'Show more' }; } this.element.hide(); this.busy = false; // busy state this.idMultiSelect = this._uniqid(); // busy state this.container = $jqPm('
').insertAfter(this.element); this.selectedContainer = $jqPm('
').appendTo(this.container); this.availableContainer = $jqPm('
').appendTo(this.container); this.selectedActions = $jqPm('
'+$jqPm.tmpl($jqPm.ui.multiselect.locale.itemsCount,{count:0})+''+$jqPm.tmpl($jqPm.ui.multiselect.locale.removeAll)+'
').appendTo(this.selectedContainer); this.availableActions = $jqPm('
'+$jqPm.tmpl($jqPm.ui.multiselect.locale.busy)+''+$jqPm.tmpl($jqPm.ui.multiselect.locale.addAll)+'
').appendTo(this.availableContainer); this.selectedList = $jqPm('').bind('selectstart', function(){return false;}).appendTo(this.selectedContainer); this.availableList = $jqPm('').bind('selectstart', function(){return false;}).appendTo(this.availableContainer); var that = this; // initialize data cache this.availableList.data('multiselect.cache', {}); this.selectedList.data('multiselect.cache', {}); if ( !this.options.animated ) { this.options.show = 'show'; this.options.hide = 'hide'; } // sortable / droppable / draggable var dragOptions = { selected: { sortable: ('both' == this.options.sortable || 'left' == this.options.sortable), droppable: ('both' == this.options.droppable || 'left' == this.options.droppable) }, available: { sortable: ('both' == this.options.sortable || 'right' == this.options.sortable), droppable: ('both' == this.options.droppable || 'right' == this.options.droppable) } }; this._prepareLists('selected', 'available', dragOptions); this._prepareLists('available', 'selected', dragOptions); // set up livesearch this._registerSearchEvents(this.availableContainer.find('input.search'), this.options.searchAtStart); // // make sure that we're not busy yet this._setBusy(false); // batch actions this.container.find(".remove-all").bind('click.multiselect', function() { that.selectNone(); return false; }); this.container.find(".add-all").bind('click.multiselect', function() { that.selectAll(); return false; }); // set dimensions this.container.width(this.element.width()+1); this._refreshDividerLocation(); // set max width of search input dynamically this.availableActions.find('input').width(Math.max(this.availableActions.width() - this.availableActions.find('a.add-all').width() - 30, 20)); // fix list height to match