/* * RailsAdmin filtering multiselect @VERSION * * License * * http://www.railsadmin.org * * Depends: * jquery.ui.core.js * jquery.ui.widget.js */ (function($) { $.widget("ra.filteringMultiselect", { _cache: {}, options: { createQuery: function(query) { return { query: query }; }, sortable: false, removable: true, regional: { up: "Up", down: "Down", add: "Add", chooseAll: "Choose all", chosen: "Chosen records", clearAll: "Clear all", remove: "Remove" }, searchDelay: 400, remote_source: null, xhr: false }, _create: function() { this._cache = {}; this._build(); this._buildCache(); this._bindEvents(); }, _build: function() { var i; this.wrapper = $('
'); this.wrapper.insertAfter(this.element); this.header = $('
'); this.filter = $(''); this.header.append(this.filter); this.wrapper.append(this.header); this.columns = { left: $('
'), center: $('
'), right: $('
') }; for (i in this.columns) { if (this.columns.hasOwnProperty(i)) { this.wrapper.append(this.columns[i]); } } this.collection = $(''); this.collection.addClass("form-control ra-multiselect-collection"); this.addAll = $('' + this.options.regional.chooseAll + ''); this.columns.left.html(this.collection) .append(this.addAll); this.collection.wrap('
'); this.add = $('' + this.options.regional.add + ''); this.columns.center.append(this.add); if (this.options.removable) { this.remove = $('' + this.options.regional.remove + ''); this.columns.center.append(this.remove); } if (this.options.sortable) { this.up = $('' + this.options.regional.up + ''); this.down = $('' + this.options.regional.down + ''); this.columns.center.append(this.up).append(this.down); } this.selection = $(''); this.columns.right.append(this.selection); if (this.options.removable) { this.removeAll = $('' + this.options.regional.clearAll + ''); this.columns.right.append(this.removeAll); } this.selection.wrap('
'); this.element.css({display: "none"}); this.tooManyObjectsPlaceholder = $('').attr('value', matches[filtered[i]].id).attr('title', matches[filtered[i]].label).text(matches[filtered[i]].label) ); } } else { widget.collection.html(widget.noObjectsPlaceholder); } }); }, /* * Cache key is stored in the format `o_').attr('value', option.value).attr('selected', "selected")); } }); $(options).appendTo(this.selection).attr('selected', false); }, _move: function(direction, options) { var widget = this; if(direction == 'up') { options.each(function(i, option) { var prev = $(option).prev(); if (prev.length > 0) { var el = widget.element.find('option[value="' + option.value + '"]'); var el_prev = widget.element.find('option[value="' + prev[0].value + '"]'); el_prev.before(el); prev.before($(option)); } }); } else { $.fn.reverse = [].reverse; // needed to lower last items first options.reverse().each(function(i, option) { var next = $(option).next(); if (next.length > 0) { var el = widget.element.find('option[value="' + option.value + '"]'); var el_next = widget.element.find('option[value="' + next[0].value + '"]'); el_next.after(el); next.after($(option)); } }); } }, selected: function(value) { return this.element.find('option[value="' + value + '"]').attr("selected"); }, destroy: function() { this.wrapper.remove(); this.element.css({display: "inline"}); $.Widget.prototype.destroy.apply(this, arguments); } }); })(jQuery);