Skip to content

Commit 8883c34

Browse files
author
hikki
committed
7.1
1 parent f10a9c5 commit 8883c34

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

resources/assets/component.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,13 @@ window.ComponentDot = class {
275275
delete: 'delete'
276276
};
277277

278-
constructor(selector, select, selected, limit = 0) {
279-
if (!Array.isArray(selected)) {
280-
console.error('Dot param selected must be array!');
281-
return;
282-
}
278+
constructor(selector, select) {
283279
if (Array.isArray(select) || typeof select !== 'object') {
284280
console.error('Dot param select must be object such as {key:val,key2:val2,...} !');
285281
return;
286282
}
287283
this.select = select;
288-
this.limit = limit;
284+
this.limit = 0;
289285
if (selector instanceof HTMLElement) {
290286
this.DOM = selector;
291287
} else {
@@ -295,24 +291,19 @@ window.ComponentDot = class {
295291
e.preventDefault();
296292
});
297293

298-
selected = selected.map(function(elem) {
299-
return elem.toString();
300-
});
301-
selected = selected.filter(d => {
302-
if (select[d] === undefined) return false;
303-
return true;
304-
});
305-
this.selected_data = selected;
294+
this.selected_data = [];
306295
this.select_data = [];
307296
this.insert_data = [];
308297
this.delete_data = [];
309298
this._modSettings = {mode: false};
310299
this._useSearchMod = false;
311-
this._triggerEvent = null;
300+
this._triggerEvent = {func:null,enable:false};
312301

313302
this._tagSelect = function (element) {
314303
if (this.limit > 0 && this.select_data.length >= this.limit && this.SELECTED_DOM.firstChild instanceof HTMLElement) {
304+
this._triggerEvent.enable = false;
315305
this.SELECTED_DOM.firstChild.click();
306+
this._triggerEvent.enable = true;
316307
}
317308
let clone = element.cloneNode(true);
318309
clone.addEventListener('click', () => this._tagCancel(clone), false);
@@ -361,7 +352,8 @@ window.ComponentDot = class {
361352
if (this.insertInputDOM instanceof HTMLElement) this.insertInputDOM.value = JSON.stringify(this.insert_data);
362353
}
363354
}
364-
if (typeof this._triggerEvent == 'function') this._triggerEvent(this.select_data, this.insert_data, this.delete_data);
355+
if (typeof this._triggerEvent.func == 'function' && this._triggerEvent.enable === true)
356+
this._triggerEvent.func(this.select_data, this.insert_data, this.delete_data);
365357
};
366358

367359
this._search = function (search) {
@@ -433,14 +425,14 @@ window.ComponentDot = class {
433425

434426
this._bind = function () {
435427
setTimeout(() => {
436-
let queue = [];
437428
this.CONTENT_DOM.childNodes.forEach((D) => {
438429
let id = D.getAttribute('data-id');
439430
if (this.selected_data.indexOf(id) !== -1) {
440-
queue.push(D);
431+
this._triggerEvent.enable = false;
432+
D.click();
433+
this._triggerEvent.enable = true;
441434
}
442435
});
443-
queue.forEach((D) => D.click());
444436
if (this._modSettings.mode === true) this.DOM.querySelector('.menu-list').style.display = 'none';
445437
});
446438
if (this._useSearchMod === false) return;
@@ -486,7 +478,9 @@ window.ComponentDot = class {
486478
return;
487479
}
488480
if (this.limit > 0 && this.select_data.length >= this.limit) {
481+
this._triggerEvent.enable = false;
489482
list.childNodes[this.id_line_hash[this.select_data[0].toString()]].click();
483+
this._triggerEvent.enable = true;
490484
}
491485
option.classList.add('option-active');
492486
this._tagCal(id, this.MODE.insert);
@@ -533,6 +527,27 @@ window.ComponentDot = class {
533527
return this;
534528
}
535529

530+
selected(selected){
531+
if (!Array.isArray(selected)) {
532+
console.error('Dot param selected must be array [key1,key2...]!');
533+
return this;
534+
}
535+
selected = selected.map(function(elem) {
536+
return elem.toString();
537+
});
538+
selected = selected.filter(d => {
539+
if (this.select[d] === undefined) return false;
540+
return true;
541+
});
542+
this.selected_data = selected;
543+
return this;
544+
}
545+
546+
limitNum(num){
547+
this.limit = num;
548+
return this;
549+
}
550+
536551
mod(settings = {mode: false, placeholder: '未选择', height: '150px',direction:'down'}) {
537552
this._modSettings = Object.assign({
538553
mode: false,
@@ -550,7 +565,7 @@ window.ComponentDot = class {
550565

551566
trigger(f = function () {
552567
}) {
553-
this._triggerEvent = f;
568+
this._triggerEvent = {func:f,enable:true};
554569
return this;
555570
}
556571

@@ -594,11 +609,7 @@ window.ComponentCascadeDot = class {
594609
delete: 'delete'
595610
};
596611

597-
constructor(selector, select, selected, limit = 0) {
598-
if (!Array.isArray(selected)) {
599-
console.error('CascadeDot param selected must be array!');
600-
return;
601-
}
612+
constructor(selector, select) {
602613
if (!Array.isArray(select) || typeof select[0] !== 'object') {
603614
console.error('CascadeDot param select must be object such as [{"key":1,"val":"root-node","nodes":[]},...] !');
604615
return;
@@ -609,9 +620,9 @@ window.ComponentCascadeDot = class {
609620
} else {
610621
this.DOM = document.querySelector(selector);
611622
}
612-
this.limit = limit;
623+
this.limit = 0;
613624
this.select = select;
614-
this.selected_data = selected;
625+
this.selected_data = [];
615626
this.select_data = [];
616627
this.insert_data = [];
617628
this.delete_data = [];
@@ -960,6 +971,20 @@ window.ComponentCascadeDot = class {
960971
return this;
961972
}
962973

974+
selected(selected){
975+
if (!Array.isArray(selected)) {
976+
console.error('CascadeDot param selected must be array!');
977+
return this;
978+
}
979+
this.selected_data = selected;
980+
return this;
981+
}
982+
983+
limitNum(num){
984+
this.limit = num;
985+
return this;
986+
}
987+
963988
useHiddenInput(name) {
964989
this.DOM.insertAdjacentHTML('beforeend', `<input name="${name}[select]" value="[]" type="hidden" /><input name="${name}[insert]" value="[]" type="hidden" /><input name="${name}[delete]" value="[]" type="hidden" />`);
965990
this.selectInputDOM = this.DOM.querySelector(`input[name='${name}[select]']`);

test/index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@
225225
"49": "木南日菜",
226226
"50": "上原亜衣"
227227
};
228-
new ComponentDot('#test', test_data, [2, 5, 10]).useSearch().trigger(function (select,insert,del) {
228+
new ComponentDot('#test', test_data).useSearch().trigger(function (select,insert,del) {
229229
console.log(select,insert,del)
230-
}).make();
230+
}).selected([2, 5, 10]).limitNum(3).make();
231231

232-
new ComponentDot('#test7', test_data,[2]).mod({mode:true,placeholder: '选择女优',height:'120px'}).trigger(function (select,insert,del) {
232+
/* new ComponentDot('#test7', test_data).mod({mode:true,placeholder: '选择女优',height:'120px'}).trigger(function (select,insert,del) {
233233
console.log(select,insert,del)
234-
}).make();
234+
}).selected([2]).limitNum(2).make();*/
235235

236236
new ComponentLine("#test2", {
237237
"name": {"name": "名称", "type": "input"},
@@ -306,11 +306,11 @@
306306
}).make();
307307

308308
document.getElementById('test3').addEventListener('click', function (e) {
309-
new ComponentPlane({url:"http://333"}).make();
309+
new ComponentPlane({url:"http://page.url"}).make();
310310
});
311-
new ComponentCascadeDot('#test4', China, [2261,2262,2263]).useSearch().trigger(function (select,insert,del) {
311+
/* new ComponentCascadeDot('#test4', China).useSearch().trigger(function (select,insert,del) {
312312
console.log(select,insert,del)
313-
}).make();
313+
}).selected([2261,2262,2263]).limitNum(3).make();*/
314314

315315
let cascadeTestData = [
316316
{

0 commit comments

Comments
 (0)