UNPKG

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