Anton Shestakov <av6@dwimlabs.net>, Wed, 22 Jun 2016 16:32:13 +0800
demo: remove trailing spaces
jquery.lensy.js
Permissions: -rw-r--r--
function Lensy(element, options) { this.$select = $(element); configure: function(options) { this.settings = $.extend({}, $.fn.lensy.defaults, options); this.$filter = $('<input>') .addClass(this.settings.filterClass); this.$filterWrap = $('<div>') .addClass(this.settings.filterWrapClass) this.$container = $('<ul>') .addClass(this.settings.containerClass); this.$closeButton = $('<a>') .text(this.settings.closeButtonText) .addClass(this.settings.closeButtonClass); .addClass(this.settings.widgetClass) .insertAfter(this.$select); .addClass(this.settings.modalClass) .append(this.$filterWrap) .append(this.$closeButton) createItem: function($option) { var value = $option.val(); var text = $option.text(); .addClass(this.settings.itemClass) .attr('data-original-value', value) .attr('data-original-text', text) .data('$option', $option) .html(this.settings.itemFn(value, text, $option)); createWidgetItem: function($item) { var value = $item.attr('data-original-value'); var text = $item.attr('data-original-text'); var $option = $item.data('$option'); .addClass(this.settings.itemClass) .html((this.settings.widgetItemFn || this.settings.itemFn)(value, text, $option)); this.$container.find('[data-original-value]').each(function() { var value = $item.attr('data-original-value'); var text = $item.attr('data-original-text'); if (self.settings.filterFn(self.$filter.val(), value, text, $item)) { var values = this.$select.val() || []; this.$container.find('[data-original-value]').each(function() { var value = $item.attr('data-original-value'); if (values.indexOf(value) == -1) { $item.removeClass(self.settings.itemActiveClass); $item.addClass(self.settings.itemActiveClass); var $widgetItem = self.createWidgetItem($item); self.$widget.append($widgetItem); this.$widget.click($.proxy(this.show, this)); this.$closeButton.click($.proxy(this.hide, this)); // $item in $container gets clicked on var itemSelected = function() { var value = $item.attr('data-original-value'); var values = self.$select.val() || []; if (values.indexOf(value) == -1) { self.$select.val(values.concat(value)); self.$select.val($.grep(values, function(v) { return v != value; })); self.$select.trigger('change'); // $container is populated with $items this.$select.find('option').each(function() { var $item = self.createItem($option); $item.on('click', itemSelected); self.$container.append($item); this.$select.on('change', $.proxy(this.update, this)); this.$filter.on('change keyup', $.proxy(this.filter, this)); $.fn.lensy = function(options) { return this.each(function() { if (!$.data(this, 'lensy')) { $.data(this, 'lensy', new Lensy(this, options)); // escape action: close / clean filter containerClass: 'lensy-container', closeButtonText: 'Close', closeButtonClass: 'lensy-button', modalClass: 'lensy-modal', filterClass: 'lensy-filter', filterWrapClass: 'lensy-filter-wrap', widgetClass: 'lensy-widget', itemActiveClass: 'active', itemFn: function(value, text) { return $('<span>').text(text); }, filterFn: function(filterString, value, text) { return text.toLowerCase().indexOf(filterString.toLowerCase()) != -1; }