diff --git a/src/MultipleSelect.js b/src/MultipleSelect.js index 0faec545..26518f97 100644 --- a/src/MultipleSelect.js +++ b/src/MultipleSelect.js @@ -10,6 +10,7 @@ import { } from './utils/index.js' class MultipleSelect { + constructor ($el, options) { this.$el = $el this.options = $.extend({}, Constants.DEFAULTS, options) @@ -23,7 +24,7 @@ class MultipleSelect { this.initFilter() this.initDrop() this.initView() - this.options.onAfterCreate() + this.trigger('after-create') } initLocale () { @@ -246,9 +247,9 @@ class MultipleSelect { if (!ignoreTrigger) { if (this.allSelected) { - this.options.onCheckAll() + this.trigger('check-all') } else if (selectedTotal === 0) { - this.options.onUncheckAll() + this.trigger('uncheck-all') } } } @@ -502,8 +503,8 @@ class MultipleSelect { } this.$choice.off('click').on('click', toggleOpen) - .off('focus').on('focus', this.options.onFocus) - .off('blur').on('blur', this.options.onBlur) + .off('focus').on('focus', this.trigger('focus')) + .off('blur').on('blur', this.trigger('blur')) this.$parent.off('keydown').on('keydown', e => { // esc key @@ -560,7 +561,7 @@ class MultipleSelect { const group = findByParam(this.data, '_key', $this.data('key')) this._checkGroup(group, checked) - this.options.onOptgroupClick(removeUndefined({ + this.trigger('onOptgroupClick', removeUndefined({ label: group.label, selected: group.selected, data: group._data, @@ -582,7 +583,7 @@ class MultipleSelect { const option = findByParam(this.data, '_key', $this.data('key')) this._check(option, checked) - this.options.onClick(removeUndefined({ + this.trigger('click', removeUndefined({ text: option.text, value: option.value, selected: option.selected, @@ -656,7 +657,7 @@ class MultipleSelect { this.$searchInput.focus() this.filter(true) } - this.options.onOpen() + this.trigger('open') } close () { @@ -671,7 +672,7 @@ class MultipleSelect { 'left': 'auto' }) } - this.options.onClose() + this.trigger('close') } animateMethod (method) { @@ -988,7 +989,7 @@ class MultipleSelect { this.updateSelected() if (!ignoreTrigger) { - this.options.onFilter(text) + this.trigger('filter', text) } } @@ -1009,6 +1010,13 @@ class MultipleSelect { this.fromHtml = false } } + + trigger (_name, ...args) { + const name = `${_name}.bs.select` + + this.options[Constants.EVENTS[name]](...[...args, this]) + this.$el.trigger($.Event(name, { sender: this }), args) + } } export default MultipleSelect diff --git a/src/constants/index.js b/src/constants/index.js index 31a45dd9..6d840ec8 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -115,6 +115,19 @@ const METHODS = [ 'refresh', 'destroy' ] +const EVENTS = { + 'open.bs.select': 'onOpen', + 'close.bs.select': 'onClose', + 'check-all.bs.select': 'onCheckAll', + 'uncheck-all.bs.select': 'onUncheckAll', + 'focus.bs.select': 'onFocus', + 'blur.bs.select': 'onBlur', + 'opt-group-click.bs.select': 'onOptgroupClick', + 'click.bs.select': 'onClick', + 'filter.bs.select': 'onFilter', + 'after-create.bs.select': 'onAfterCreate', +} + Object.assign(DEFAULTS, EN) const Constants = { @@ -128,6 +141,8 @@ const Constants = { METHODS, + EVENTS, + LOCALES: { en: EN, 'en-US': EN