'); this.$el.before(this.$parent); this.$parent.append(this.$choice); this.$parent.append(this.$drop); this.$parent.css('width', this.options.width || this.$el.css('width') || this.$el.outerWidth() + 20); this.selectItemName = 'data-name="selectItem' + name + '"'; $(document).click(function (e) { if ($(e.target)[0] === that.$choice[0] || $(e.target).parents('.coi-choice')[0] === that.$choice[0] || $(e.target).children('.coi-choice')[0] === that.$choice[0]) { return; } if (($(e.target)[0] === that.$drop[0] || $(e.target).parents('.coi-drop')[0] !== that.$drop[0] && e.target !== $el[0]) && that.options.isOpen) { that.close(); } }); this.$el = $el.show(); } MultipleSelect.prototype = { constructor: MultipleSelect, init: function () { var that = this, $ul = $(''); this.$drop.html(''); $.each(this.$el.children(), function (i, elm) { $ul.append(that.optionToHtml(i, elm)); }); this.$drop.append($ul); this.$drop.find('ul').css('max-height', this.options.maxHeight + 'px'); this.$selectItems = this.$drop.find('input[' + this.selectItemName + ']:enabled'); this.events(); this.update(true); if (this.options.isOpen) { this.open(); } }, optionToHtml: function (i, elm, group) { var $elm = $(elm), classes = $elm.attr('class') || '', title = sprintf('title="%s"', $elm.attr('title')), disabled, type = 'checkbox'; if ($elm.is('option')) { var value = $elm.val(), text = $elm.html(), selected = $elm.prop('selected'), $el; $el = $([ sprintf('
  • ', classes, title), sprintf('', '
  • ' ].join('')); $el.find('input').val(value); return $el; } }, events: function () { var that = this, toggleOpen = function (e) { e.preventDefault(); e.stopPropagation(); that[that.options.isOpen ? 'close' : 'open'](); }; this.$choice.off('click').on('click', toggleOpen); this.$parent.off('keydown').on('keydown', function (e) { switch (e.which) { case 27: // esc key that.close(); that.$choice.focus(); break; } }); this.$selectItems.off('click').on('click', function () { that.update(); }); }, open: function () { this.options.isOpen = true; this.$drop['show'](); }, close: function () { this.options.isOpen = false; this.$drop['hide'](); }, update: function (isInit) { var selects = this.getSelects('text'), $span = this.$choice.find('>span'), sl = selects.length; if (sl === 0) { $span.addClass('placeholder').html(this.options.placeholder); } else { $span.removeClass('placeholder').text(selects.join(this.options.delimiter)); } // set selects to select this.$el.val(this.getSelects()).trigger('change'); // add selected class to selected li this.$drop.find('li').removeClass('selected'); this.$drop.find('input:checked').each(function () { $(this).parents('li').first().addClass('selected'); }); // trigger