');
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('', disabled ? 'disabled' : ''),
sprintf(' ',
type, this.selectItemName,
selected ? ' checked="checked"' : '',
disabled ? ' disabled="disabled"' : '',
sprintf(' data-group="%s"', group)),
sprintf('%s ', text),
' ',
' '
].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
change event
if (!isInit) {
this.$el.trigger('change');
}
},
//value or text, default: 'value'
getSelects: function (type) {
var texts = [],
values = [];
this.$drop.find(sprintf('input[%s]:checked', this.selectItemName)).each(function () {
texts.push($(this).parents('li').first().text());
values.push($(this).val());
});
return type === 'text' ? texts : values;
},
setSelects: function (values) {
this.$selectItems.prop('checked', false);
this.update();
},
focus: function () {
this.$choice.focus();
},
blur: function () {
this.$choice.blur();
},
destroy: function () {
this.$el.show();
this.$parent.remove();
this.$el.data('multipleSelect', null);
}
};
$.fn.multipleSelect = function () {
var option = arguments[0],
args = arguments,
value;
this.each(function () {
var $this = $(this),
data = $this.data('multipleSelect'),
options = $.extend({}, $.fn.multipleSelect.defaults,
$this.data(), typeof option === 'object' && option);
if (!data) {
data = new MultipleSelect($this, options);
$this.data('multipleSelect', data);
}
if (typeof option === 'string') {
value = data[option](args[1]);
} else {
data.init();
if (args[1]) {
value = data[args[1]].apply(data, [].slice.call(args, 2));
}
}
});
return typeof value !== 'undefined' ? value : this;
};
$.fn.multipleSelect.defaults = {
name: '',
isOpen: false,
placeholder: 'Area of Interest (select all that apply)',
width: undefined,
maxHeight: 290,
delimiter: ', ',
};
})(jQuery);