UNPKG

9.61 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.MultipleSelect = factory());
5})(this, (function () { 'use strict';
6
7 //
8 //
9 //
10 //
11 //
12 //
13 //
14 //
15 //
16 //
17
18 const $ = window.jQuery;
19 const deepCopy = arg => {
20 if (!arg) {
21 return arg
22 }
23 return $.extend(true, Array.isArray(arg) ? [] : {}, arg)
24 };
25
26 var script = {
27 name: 'MultipleSelect',
28
29 props: {
30 value: {
31 type: [String, Number, Array, Object],
32 default: undefined
33 },
34 name: {
35 type: String,
36 default: undefined
37 },
38 multiple: {
39 type: [Boolean, String],
40 default: false
41 },
42 disabled: {
43 type: Boolean,
44 default: false
45 },
46 width: {
47 type: [Number, String],
48 default: undefined
49 },
50 size: {
51 type: String,
52 default: undefined
53 },
54 data: {
55 type: [Array, Object],
56 default () {
57 return undefined
58 }
59 },
60 options: {
61 type: Object,
62 default () {
63 return {}
64 }
65 }
66 },
67
68 data () {
69 return {
70 currentValue: this.value
71 }
72 },
73
74 watch: {
75 value () {
76 if (this.currentValue === this.value) {
77 return
78 }
79 this.currentValue = this.value;
80 this._initDefaultValue();
81 },
82 multiple () {
83 this._initSelect();
84 },
85 disabled () {
86 this.$nextTick(() => {
87 if (this.disabled) {
88 this.disable();
89 } else {
90 this.enable();
91 }
92 });
93 },
94 width () {
95 this._initSelectValue();
96 },
97 options: {
98 handler () {
99 this._initSelectValue();
100 },
101 deep: true
102 },
103
104 data: {
105 handler () {
106 this._initSelectValue();
107 },
108 deep: true
109 }
110 },
111
112 beforeUpdate () {
113 if (this.slotDefault || this.slotDefault !== this.$slots.default) {
114 this.slotDefault = this.$slots.default;
115 this.$nextTick(() => {
116 this._refresh();
117 this._initSelectValue();
118 });
119 }
120 },
121
122 destroyed () {
123 this.destroy(true);
124 },
125
126 mounted () {
127 this._refresh();
128
129 this.$select = $(this.$el).change(() => {
130 const selects = this.getSelects();
131
132 if (Array.isArray(this.currentValue)) {
133 this.currentValue = selects;
134 } else if (typeof this.currentValue === 'number') {
135 this.currentValue = selects.length ? +selects[0] : undefined;
136 } else {
137 this.currentValue = selects.length ? selects[0] : undefined;
138 }
139
140 this.$emit('input', this.currentValue);
141 this.$emit('change', this.currentValue);
142 });
143
144 if (
145 this._hasInit &&
146 this.$select.val() &&
147 (typeof this.currentValue === 'undefined' ||
148 Array.isArray(this.currentValue) && !this.currentValue.length)
149 ) {
150 this.currentValue = this.$select.val();
151
152 this.$emit('input', this.currentValue);
153 this.$emit('change', this.currentValue);
154 }
155
156 for (const event in $.fn.multipleSelect.defaults) {
157 if (/^on[A-Z]/.test(event)) {
158 $.fn.multipleSelect.defaults[event] = (...args) => {
159 this.$emit(event.replace(/([A-Z])/g, '-$1').toLowerCase(), ...args);
160 };
161 }
162 }
163
164 this._initSelectValue();
165 },
166
167 methods: {
168 _initSelectValue () {
169 this._initSelect();
170
171 if (
172 typeof this.currentValue === 'undefined' ||
173 Array.isArray(this.currentValue) && !this.currentValue.length
174 ) {
175 return
176 }
177
178 this._initDefaultValue();
179 },
180
181 _initSelect () {
182 const options = {
183 ...deepCopy(this.options),
184 single: !this.multiple,
185 width: this.width,
186 size: this.size,
187 data: this.data
188 };
189 if (!this._hasInit) {
190 this.$select.multipleSelect(options);
191 this._hasInit = true;
192 } else {
193 this.refreshOptions(options);
194 }
195 },
196
197 _initDefaultValue () {
198 this.$nextTick(() => {
199 try {
200 this.setSelects(Array.isArray(this.currentValue) ?
201 this.currentValue : [this.currentValue], null, true);
202 } catch (e) {
203 // ignore error
204 }
205 });
206 },
207
208 ...(() => {
209 const res = {};
210 for (const method of $.fn.multipleSelect.methods) {
211 res[method] = function (...args) {
212 return this.$select.multipleSelect(method, ...args)
213 };
214 }
215 return res
216 })(),
217
218 _refresh () {
219 if (this.$slots.default) {
220 for (const el of this.$slots.default) {
221 if (el.elm.nodeName === 'OPTION' && el.data.domProps && el.data.domProps.value) {
222 $(el.elm).data('value', el.data.domProps.value);
223 }
224 }
225 }
226 },
227
228 refresh () {
229 this._refresh();
230 this.$select.multipleSelect('refresh');
231 }
232 }
233 };
234
235 function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier /* server only */, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) {
236 if (typeof shadowMode !== 'boolean') {
237 createInjectorSSR = createInjector;
238 createInjector = shadowMode;
239 shadowMode = false;
240 }
241 // Vue.extend constructor export interop.
242 const options = typeof script === 'function' ? script.options : script;
243 // render functions
244 if (template && template.render) {
245 options.render = template.render;
246 options.staticRenderFns = template.staticRenderFns;
247 options._compiled = true;
248 // functional template
249 if (isFunctionalTemplate) {
250 options.functional = true;
251 }
252 }
253 // scopedId
254 if (scopeId) {
255 options._scopeId = scopeId;
256 }
257 let hook;
258 if (moduleIdentifier) {
259 // server build
260 hook = function (context) {
261 // 2.3 injection
262 context =
263 context || // cached call
264 (this.$vnode && this.$vnode.ssrContext) || // stateful
265 (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional
266 // 2.2 with runInNewContext: true
267 if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
268 context = __VUE_SSR_CONTEXT__;
269 }
270 // inject component styles
271 if (style) {
272 style.call(this, createInjectorSSR(context));
273 }
274 // register component module identifier for async chunk inference
275 if (context && context._registeredComponents) {
276 context._registeredComponents.add(moduleIdentifier);
277 }
278 };
279 // used by ssr in case component is cached and beforeCreate
280 // never gets called
281 options._ssrRegister = hook;
282 }
283 else if (style) {
284 hook = shadowMode
285 ? function (context) {
286 style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot));
287 }
288 : function (context) {
289 style.call(this, createInjector(context));
290 };
291 }
292 if (hook) {
293 if (options.functional) {
294 // register for functional component in vue file
295 const originalRender = options.render;
296 options.render = function renderWithStyleInjection(h, context) {
297 hook.call(context);
298 return originalRender(h, context);
299 };
300 }
301 else {
302 // inject component registration as beforeCreate hook
303 const existing = options.beforeCreate;
304 options.beforeCreate = existing ? [].concat(existing, hook) : [hook];
305 }
306 }
307 return script;
308 }
309
310 /* script */
311 const __vue_script__ = script;
312
313 /* template */
314 var __vue_render__ = function() {
315 var _vm = this;
316 var _h = _vm.$createElement;
317 var _c = _vm._self._c || _h;
318 return _c(
319 "select",
320 {
321 attrs: { name: _vm.name, multiple: _vm.multiple, disabled: _vm.disabled }
322 },
323 [_vm._t("default")],
324 2
325 )
326 };
327 var __vue_staticRenderFns__ = [];
328 __vue_render__._withStripped = true;
329
330 /* style */
331 const __vue_inject_styles__ = undefined;
332 /* scoped */
333 const __vue_scope_id__ = undefined;
334 /* module identifier */
335 const __vue_module_identifier__ = undefined;
336 /* functional template */
337 const __vue_is_functional_template__ = false;
338 /* style inject */
339
340 /* style inject SSR */
341
342 /* style inject shadow dom */
343
344
345
346 const __vue_component__ = /*#__PURE__*/normalizeComponent(
347 { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
348 __vue_inject_styles__,
349 __vue_script__,
350 __vue_scope_id__,
351 __vue_is_functional_template__,
352 __vue_module_identifier__,
353 false,
354 undefined,
355 undefined,
356 undefined
357 );
358
359 return __vue_component__;
360
361}));