import jQuery from "jquery"; import "jquery-ui/ui/widget.js"; import I18n from "./i18n"; (function ($) { $.widget("ra.filteringMultiselect", { _cache: {}, options: { createQuery: function (query) { return { query: query }; }, sortable: false, removable: true, regional: { add: "Add", chooseAll: "Choose all", clearAll: "Clear all", down: "Down", remove: "Remove", search: "Search", up: "Up", }, searchDelay: 400, remote_source: null, xhr: false, }, wrapper: null, filter: null, collection: null, addAll: null, add: null, remove: null, up: null, down: null, selection: null, removeAll: null, _create: function () { this._cache = {}; this._build(); this._buildCache(); this._bindEvents(); }, _build: function () { var i; this.wrapper = this.element.siblings( '.ra-multiselect[data-input-for="' + this.element.attr("id") + '"]' ); // Prevent duplication on browser back if (this.wrapper.length > 0) { this.filter = this.wrapper.find("input.ra-multiselect-search"); this.collection = this.wrapper.find("select.ra-multiselect-collection"); this.addAll = this.wrapper.find("a.ra-multiselect-item-add-all"); this.add = this.wrapper.find("a.ra-multiselect-item-add"); this.remove = this.wrapper.find("a.ra-multiselect-item-remove"); this.up = this.wrapper.find("a.ra-multiselect-item-up"); this.down = this.wrapper.find("a.ra-multiselect-item-down"); this.selection = this.wrapper.find("select.ra-multiselect-selection"); this.removeAll = this.wrapper.find("a.ra-multiselect-item-remove-all"); return; } this.wrapper = $('
').attr( "data-input-for", this.element.attr("id") ); this.wrapper.insertAfter(this.element); var header = $('
'); this.filter = $( '' ); header.append(this.filter); this.wrapper.append(header); var columns = { left: $('
'), center: $('
'), right: $('
'), }; for (i in columns) { if (columns.hasOwnProperty(i)) { this.wrapper.append(columns[i]); } } this.collection = $(''); this.collection.addClass("form-control ra-multiselect-collection"); this.addAll = $( '' + this.options.regional.chooseAll + "" ); columns.left.html(this.collection).append(this.addAll); this.collection.wrap('
'); this.add = $( '' ).attr("title", this.options.regional.add); columns.center.append(this.add); if (this.options.removable) { this.remove = $( '' ).attr("title", this.options.regional.remove); columns.center.append(this.remove); } if (this.options.sortable) { this.up = $( '' ).attr("title", this.options.regional.up); this.down = $( '' ).attr("title", this.options.regional.down); columns.center.append(this.up).append(this.down); } this.selection = $( '' ); columns.right.append(this.selection); if (this.options.removable) { this.removeAll = $( '' + this.options.regional.clearAll + "" ); columns.right.append(this.removeAll); } this.selection.wrap('
'); this.element.css({ display: "none" }); this.tooManyObjectsPlaceholder = $('") .prop("value", matches[filtered[i]].id) .prop("title", matches[filtered[i]].label) .text(matches[filtered[i]].label); widget.collection.append(newOptions); } } else { widget.collection.html(widget.noObjectsPlaceholder); } }); }, /* * Cache key is stored in the format `o_") .prop("value", option.value) .prop("selected", true) ); } }); $(options).appendTo(this.selection).prop("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) { if ( this.selection[0].querySelectorAll('option[value="' + value + '"]')[0] ) { return true; } }, destroy: function () { this.wrapper.remove(); this.element.css({ display: "inline" }); $.Widget.prototype.destroy.apply(this, arguments); }, }); })(jQuery);