-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.js
450 lines (362 loc) · 12.5 KB
/
select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
layui.define(['jquery', 'dropdown'],function(exports){
"use strict";
var $ = layui.jquery;
var laytpl = layui.laytpl;
var dropdown = layui.dropdown;
var MOD_NAME = 'select';
var select = {
// 默认配置
config: {
// 值分隔符
valueSeparator: ',',
keywordPlaceholder: '请输入关键词',
// 搜索时,没有匹配结果时显示的文字
unfilteredText: '没有匹配的选项',
customName: {
id: 'id',
title: 'title',
selected: 'selected',
},
options: [],
// 搜索时,如果不能匹配时,是否允许新增
allowCreate: true,
// 是否折叠已选择的选项
collapseSelected: false,
}
};
var thisSelect = function(){
var that = this;
return {
config: that.config,
// 重载
reload: function(config){
that.reload.call(that, config);
},
// 获取值或设置值
val: function(value) {
if (value === undefined) {
return that.context.selectedIds.join(that.config.valueSeparator);
}
that.clear();
var ids = $.isArray(value) ? value : value.split(that.config.valueSeparator);
for (var i = 0; i < ids.length; i++) {
var option = that.getOptionById(ids[i]);
if (!option) {
continue;
}
that.select(option);
}
},
// 清空
clear: function() {
that.clear.call(that);
}
}
}
var Class = function(config) {
var that = this;
that.config = $.extend({}, that.config, select.config, config);
that.config.elem = $(config.elem);
that.config.elem.css({
position: 'absolute',
color: '#FFF',
userSelect: 'none',
top: 0,
height: '100%'
});
var width = that.config.elem.parent().outerWidth();
var layuiInputBlock = that.config.elem.parents('.layui-input-block');
if (layuiInputBlock.length > 0) {
width = layuiInputBlock.width();
}
var layuiInputGroup = that.config.elem.parents('.layui-input-group');
if (layuiInputGroup.length > 0) {
width -= layuiInputGroup.width();
}
// 将input包裹起来
that.config.elem.wrap(`<div class="layui-form-select multiple-select" style="width:${width}px"></div>`)
// 向下箭头
that.config.elem.after('<div class="layui-input-suffix"><i class="layui-edge"></i></div>');
that.render();
}
Class.prototype.render = function() {
var that = this;
// 初始化选项
var initOptions = that.config.options.map(function(option) {
return {
id: option[that.config.customName.id],
title: option[that.config.customName.title],
selected: option[that.config.customName.selected],
};
});
// 已选择的ID
var selectedIds = initOptions.filter(function(option) {
return option.selected;
}).map(function(option) {
return option.id
});
// 赋值
that.config.elem.val(selectedIds.join(that.config.valueSeparator));
// 移除选项
that.config.elem.parent().on('click', '.multiple-select-selection-item-remove', function(e) {
e.stopPropagation();
var id = $(this).parent().data('id');
var option = that.getOptionById(id);
that.remove(option);
});
// 上下文
that.context = {
// 搜索关键词
keyword: '',
// 满足搜索关键词的选项,null表示没有搜索
filteredOptions: null,
// 已选择的ID
selectedIds: selectedIds,
options: initOptions,
// dropdown滚动条的位置
dropdownMenuScrollTop: 0,
};
that.renderDropdown();
that.renderSelection();
};
// 清空所有值
Class.prototype.clear = function() {
this.context.selectedIds = [];
this.reloadDropdownData(this.buildRenderOptions());
};
// 移除选项
Class.prototype.remove = function(option) {
this.context.selectedIds = this.context.selectedIds.filter(function(id) { return id != option.id; });
this.reloadDropdownData(this.buildRenderOptions());
};
// 选择选项
Class.prototype.select = function(options) {
options = layui.isArray(options) ? options : [options];
var length = options.length;
for (var i = 0; i < length; i++) {
var id = options[i].id;
if (this.context.selectedIds.indexOf(id) !== -1) {
continue;
}
this.context.selectedIds.push(id);
}
this.reloadDropdownData(this.buildRenderOptions());
};
// 重置搜索
Class.prototype.resetSearch = function() {
this.context.filteredOptions = null;
this.context.keyword = '';
var input = this.getSearchInput();
input && input.val('');
};
// 重载
Class.prototype.reload = function(config) {
if (config) {
this.config = $.extend({}, this.config, config || {});
}
this.render();
};
Class.prototype.buildRenderOptions = function(options) {
var that = this,
renderOptions = options || [];
if (renderOptions.length === 0) {
if (that.context.filteredOptions !== null) {
renderOptions = that.context.filteredOptions;
} else {
renderOptions = this.getAllOptions();
}
}
return renderOptions.map(function(option) {
option.selected = that.context.selectedIds.indexOf(option.id) !== -1;
return option;
});
};
// 过滤选项
Class.prototype.filterOptions = function(keyword) {
var that = this,
keyword = keyword.toLowerCase()
;
return this.getAllOptions().filter(function(item) {
return -1 !== item.title.toLowerCase().indexOf(keyword);
});
};
// 获取所有选项
Class.prototype.getAllOptions = function() {
var that = this;
return that.context.options.map(function(item) {
return {
id: item.id,
title: item.title,
selected: that.context.selectedIds.indexOf(item.id) !== -1
}
});
};
Class.prototype.buildDropdownContent = function(options) {
return [
`<div class="multiple-select-search"><input class="layui-input" placeholder="${this.config.keywordPlaceholder}"/></div>`,
'<ul class="layui-menu layui-dropdown-menu" style="max-height: 300px;overflow-y: auto;">',
options.length > 0 ? options.map(function(option) {
return `<li data-value="${option.id}" class="multiple-select-option ${option.selected ? 'multiple-select-option-selected' : ''}">${option.title}</li>`;
}).join('') : `<div style="padding: 5px;font-size:12px;">${this.config.unfilteredText}</div>`,
'</ul>',
].join('');
};
Class.prototype.reloadDropdownData = function(options) {
if (options && options.length === this.getAllOptions().length) {
this.resetSearch();
}
var renderOptions = this.buildRenderOptions(options);
this.dropdown.reloadData({
content: this.buildDropdownContent(renderOptions)
});
this.renderSelection();
};
Class.prototype.getSearchInput = function() {
if (!this.panel) {
return ;
}
return this.panel.find('div.multiple-select-search > input');
};
Class.prototype.renderDropdown = function() {
var that = this;
that.dropdown = dropdown.render({
elem: that.config.elem.parent(),
style: 'width:' + that.config.elem.outerWidth() + 'px',
content: that.buildDropdownContent(that.getAllOptions()),
ready: function(panel, elem) {
that.panel = panel;
elem.addClass('multiple-select-panel-opended')
// 保持滚动条位置
if (that.context.dropdownMenuScrollTop > 0) {
panel.find('.layui-dropdown-menu').scrollTop(that.context.dropdownMenuScrollTop);
}
var inputCompositionStart = false,
searchInput = that.getSearchInput();
function reloadDropdown(keyword) {
var filterOptions = that.filterOptions(keyword);
// 当没有符合条件的项且允许创建时
if (filterOptions.length === 0 && that.config.allowCreate) {
filterOptions.push({
[that.config.customName.id]: keyword,
[that.config.customName.title]: keyword,
});
}
that.context.keyword = keyword;
that.context.filteredOptions = filterOptions;
that.reloadDropdownData(that.context.filteredOptions);
}
// 搜索框事件
searchInput.on('input', function() {
if (inputCompositionStart) {
return ;
}
reloadDropdown($(this).val());
}).on('compositionstart', function() {
// compositionstart compositionend是为了支持中文类字母
inputCompositionStart = true;
}).on('compositionend', function() {
inputCompositionStart = false;
reloadDropdown($(this).val());
});
// 每次输入都会重新渲染一次dropdown,所以要记已上次输入
if (that.context.keyword !== '') {
searchInput.val(that.context.keyword).focus();
}
// 检测dropdown的面版是否已关闭
var i = setInterval(function() {
// 是否已展开
if (!elem.data('layui_dropdown_index_opened')) {
clearInterval(i);
that.context.keyword = '';
that.context.filteredOptions = null;
that.reloadDropdownData();
elem.removeClass('multiple-select-panel-opended')
}
}, 100);
},
click: function(data, elem) {
// 获取点击项的索引
var value = elem.data('value'),
option = that.getOptionById(value)
;
if (!option) {
if (!that.config.allowCreate) {
return ;
}
option = {
[that.config.customName.id]: value,
[that.config.customName.title]: value,
[that.config.customName.selected]: true,
}
that.context.options.push(option);
}
// 记录选择面板滚动的位置
that.context.dropdownMenuScrollTop = elem.parent().scrollTop();
if (that.context.selectedIds.indexOf(option.id) === -1) {
that.select(option);
} else {
that.remove(option);
}
// false不关闭面板
return false;
}
});
};
// 根据ID获取选项
Class.prototype.getOptionById = function(id) {
return this.getAllOptions().filter(function(option) {
return option.id == id;
})[0];
};
// 渲染选择
Class.prototype.renderSelection = function() {
var that = this,
inputWrap = that.config.elem.parent(),
selectedOptions = that.getSelectedOptions(),
options = selectedOptions.length > 1 && that.config.collapseSelected ? [selectedOptions[0]] : selectedOptions,
selectionHtml = '',
selectionTpl = `
<div class="multiple-select-selection">
<div class="multiple-select-selection-overflow">
{{# layui.each(d.options, function(index, option) { }}
<div class="multiple-select-selection-overflow-item">
<span class="multiple-select-selection-item" data-id="{{= option.id }}">
<span class="multiple-select-selection-item-content">{{= option.title}}</span>
<i class="layui-icon layui-icon-close multiple-select-selection-item-remove"></i>
</span>
</div>
{{# }) }}
{{# if (d.collapseSelected && d.total > 1) { }}
<div class="multiple-select-selection-overflow-item">
<span class="multiple-select-selection-item">
<span class="multiple-select-selection-item-content">+ {{ d.total }}</span>
</span>
</div>
{{# } }}
</div>
</div>
`
;
if (selectedOptions.length > 0) {
selectionHtml = laytpl(selectionTpl).render({
total: selectedOptions.length,
options: options,
collapseSelected: that.config.collapseSelected
});
}
inputWrap.find('.multiple-select-selection').remove();
selectionHtml !== '' && inputWrap.append(selectionHtml);
this.config.elem.val(this.context.selectedIds.join(this.context.valueSeparator));
};
Class.prototype.getSelectedOptions = function() {
return this.getAllOptions().filter(function(option) {
return option.selected;
});
};
//核心入口
select.render = function(options){
var inst = new Class(options);
return thisSelect.call(inst);
};
exports(MOD_NAME, select);
});