@sd-angular/core 0.0.981 → 0.0.985

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -320,7 +320,7 @@
320
320
  { type: core.TemplateRef }
321
321
  ]; };
322
322
 
323
- var _input, _name, _form, _model, _items, _currentItems, _allItem, _delay, _validator, _updateValidator, _loadItems, _onChange, _customValidator;
323
+ var _input, _name, _form, _model, _selectedItem, _selectedItems, _items, _currentItems, _allItem, _delay, _validator, _updateValidator, _search, _loadItems, _onChange, _customValidator;
324
324
  var SdSelect = /** @class */ (function () {
325
325
  function SdSelect(ref, utilityService) {
326
326
  var _this = this;
@@ -334,6 +334,8 @@
334
334
  _form.set(this, void 0);
335
335
  _model.set(this, void 0);
336
336
  // Items
337
+ _selectedItem.set(this, void 0);
338
+ _selectedItems.set(this, []);
337
339
  _items.set(this, []);
338
340
  _currentItems.set(this, []);
339
341
  _allItem.set(this, {});
@@ -366,8 +368,23 @@
366
368
  _this.formControl.setAsyncValidators(asyncValidators);
367
369
  _this.formControl.updateValueAndValidity();
368
370
  });
371
+ _search.set(this, function (value, items) { return __awaiter(_this, void 0, void 0, function () {
372
+ var results;
373
+ var _this = this;
374
+ return __generator(this, function (_c) {
375
+ switch (_c.label) {
376
+ case 0: return [4 /*yield*/, items(value).catch(function () { return []; })];
377
+ case 1:
378
+ results = _c.sent();
379
+ if (Array.isArray(value)) {
380
+ return [2 /*return*/, results.filter(function (e) { return !!e && (value.includes(e) || value.includes(e[_this.valueField])); })];
381
+ }
382
+ return [2 /*return*/, results.filter(function (e) { return !!e && (e === value || e[_this.valueField] === value); })];
383
+ }
384
+ });
385
+ }); });
369
386
  _loadItems.set(this, function (searchText, items) { return __awaiter(_this, void 0, void 0, function () {
370
- var results, objItem_1, values;
387
+ var _a, results, objItem, values;
371
388
  var _this = this;
372
389
  return __generator(this, function (_c) {
373
390
  switch (_c.label) {
@@ -376,32 +393,31 @@
376
393
  return [4 /*yield*/, items(searchText).finally(function () { return _this.loading = false; })];
377
394
  case 1:
378
395
  results = _c.sent();
379
- if (this.multiple) {
380
- objItem_1 = {};
381
- if (this.valueField) {
382
- objItem_1 = this.utilityService.arrayToObject(results, this.valueField);
383
- }
384
- else {
385
- results.forEach(function (item) { return objItem_1[item === null || item === void 0 ? void 0 : item.toString()] = item; });
386
- }
387
- Object.assign(__classPrivateFieldGet(this, _allItem), objItem_1);
388
- values = this.formControl.value || [];
389
- values.forEach(function (val) {
390
- var _c;
391
- if (!objItem_1[val]) {
392
- if (__classPrivateFieldGet(_this, _allItem)[val]) {
393
- results.push(__classPrivateFieldGet(_this, _allItem)[val]);
394
- }
395
- else {
396
+ objItem = this.valueField ? this.utilityService.arrayToObject(results, this.valueField) : results.forEach(function (item) { return objItem[item === null || item === void 0 ? void 0 : item.toString()] = item; });
397
+ Object.assign(__classPrivateFieldGet(this, _allItem), objItem);
398
+ if (!((_a = this.formControl.value) === null || _a === void 0 ? void 0 : _a.toString())) {
399
+ return [2 /*return*/, results];
400
+ }
401
+ values = Array.isArray(this.formControl.value) ? this.formControl.value : [this.formControl.value];
402
+ values.forEach(function (val) {
403
+ var _c;
404
+ if (!objItem[val]) {
405
+ if (__classPrivateFieldGet(_this, _allItem)[val]) {
406
+ results.push(__classPrivateFieldGet(_this, _allItem)[val]);
407
+ }
408
+ else {
409
+ if (_this.valueField && _this.displayField) {
396
410
  results.push((_c = {},
397
411
  _c[_this.valueField] = val,
398
412
  _c[_this.displayField] = val,
399
413
  _c));
400
414
  }
415
+ else {
416
+ results.push(val);
417
+ }
401
418
  }
402
- });
403
- }
404
- this.ref.detectChanges();
419
+ }
420
+ });
405
421
  return [2 /*return*/, results];
406
422
  }
407
423
  });
@@ -428,11 +444,12 @@
428
444
  if (_this.dataType === 'number' && value.every(function (val) { return Number.isNumber(val); })) {
429
445
  vals_1 = value.map(function (e) { return +e; });
430
446
  }
447
+ __classPrivateFieldSet(_this, _selectedItems, (_a = __classPrivateFieldGet(_this, _currentItems)) === null || _a === void 0 ? void 0 : _a.filter(function (e) { return vals_1.some(function (e1) { var _a; return (e1 === null || e1 === void 0 ? void 0 : e1.toString()) === ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()); }); }));
431
448
  _this.modelChange.emit(vals_1);
432
449
  _this.sdChange.emit(vals_1);
433
450
  _this.sdSelection.emit({
434
451
  value: vals_1,
435
- items: (_a = __classPrivateFieldGet(_this, _currentItems)) === null || _a === void 0 ? void 0 : _a.filter(function (e) { return vals_1.some(function (e1) { var _a; return (e1 === null || e1 === void 0 ? void 0 : e1.toString()) === ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()); }); })
452
+ items: __classPrivateFieldGet(_this, _selectedItems)
436
453
  });
437
454
  }
438
455
  else {
@@ -451,11 +468,12 @@
451
468
  val_1 = !!value;
452
469
  }
453
470
  }
471
+ __classPrivateFieldSet(_this, _selectedItem, (_b = __classPrivateFieldGet(_this, _currentItems)) === null || _b === void 0 ? void 0 : _b.find(function (e) { var _a; return (val_1 === null || val_1 === void 0 ? void 0 : val_1.toString()) === ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()); }));
454
472
  _this.modelChange.emit(val_1);
455
473
  _this.sdChange.emit(val_1);
456
474
  _this.sdSelection.emit({
457
475
  value: val_1,
458
- item: (_b = __classPrivateFieldGet(_this, _currentItems)) === null || _b === void 0 ? void 0 : _b.find(function (e) { var _a; return (val_1 === null || val_1 === void 0 ? void 0 : val_1.toString()) === ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()); })
476
+ item: __classPrivateFieldGet(_this, _selectedItem)
459
477
  });
460
478
  }
461
479
  });
@@ -687,121 +705,152 @@
687
705
  });
688
706
  SdSelect.prototype.ngOnInit = function () {
689
707
  var _this = this;
690
- this.filteredItems = this.searchTerm$.asObservable().pipe(operators.startWith(''), operators.debounceTime(typeof (__classPrivateFieldGet(this, _items)) === 'function' ? __classPrivateFieldGet(this, _delay) : 0),
691
- // distinctUntilChanged(), Dùng sẽ gây ra bug khi items changes
692
- operators.switchMap(function (val) { return __awaiter(_this, void 0, void 0, function () {
693
- var items, changeAliasLowerCase;
708
+ this.allItems = this.searchTerm$.asObservable().pipe(operators.startWith(''), operators.debounceTime(typeof (__classPrivateFieldGet(this, _items)) === 'function' ? __classPrivateFieldGet(this, _delay) : 0), operators.switchMap(function (val) { return __awaiter(_this, void 0, void 0, function () {
709
+ var changeAliasLowerCase, searchText, formValue, isArray, hasFields;
694
710
  var _this = this;
695
711
  return __generator(this, function (_c) {
696
712
  switch (_c.label) {
697
713
  case 0:
698
714
  if (!(typeof (__classPrivateFieldGet(this, _items)) === 'function')) return [3 /*break*/, 2];
699
715
  return [4 /*yield*/, __classPrivateFieldGet(this, _loadItems).call(this, val, __classPrivateFieldGet(this, _items))];
700
- case 1:
701
- items = _c.sent();
702
- __classPrivateFieldSet(this, _currentItems, items.filter(function (item, index) {
703
- var _a;
704
- var formValue = _this.formControl.value;
705
- var value = (item === null || item === void 0 ? void 0 : item.toString()) || '';
706
- if (_this.valueField && _this.displayField) {
707
- value = ((_a = item[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
708
- }
709
- return (Array.isArray(formValue) ? formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value; }) : (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value)
710
- || index < _this.limit;
711
- }));
712
- return [2 /*return*/, __classPrivateFieldGet(this, _currentItems).map(function (e) {
713
- var _c;
714
- var _a;
715
- if (_this.valueField && _this.displayField) {
716
- return Object.assign(Object.assign({}, e), (_c = {}, _c["" + _this.valueField] = (_a = e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString(), _c));
717
- }
718
- return e === null || e === void 0 ? void 0 : e.toString();
719
- })];
716
+ case 1: return [2 /*return*/, _c.sent()];
720
717
  case 2:
721
718
  changeAliasLowerCase = this.utilityService.changeAliasLowerCase;
722
- __classPrivateFieldSet(this, _currentItems, __classPrivateFieldGet(this, _items).filter(function (item) {
723
- var _a, _b;
724
- var searchText = changeAliasLowerCase(val);
725
- var formValue = _this.formControl.value;
726
- if (_this.valueField && _this.displayField) {
727
- var value_1 = ((_a = item[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
728
- var display = ((_b = item[_this.displayField]) === null || _b === void 0 ? void 0 : _b.toString()) || '';
729
- return (Array.isArray(formValue) ? formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value_1; }) : (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value_1)
730
- || changeAliasLowerCase(value_1).indexOf(searchText) !== -1
731
- || changeAliasLowerCase(display).indexOf(searchText) !== -1;
732
- }
733
- else {
734
- var value_2 = (item === null || item === void 0 ? void 0 : item.toString()) || '';
735
- return (Array.isArray(formValue) ? formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value_2; }) : (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value_2)
736
- || changeAliasLowerCase(value_2).indexOf(searchText) !== -1;
737
- }
738
- }).filter(function (item, index) {
739
- var _a;
740
- var formValue = _this.formControl.value;
741
- var value = (item === null || item === void 0 ? void 0 : item.toString()) || '';
742
- if (_this.valueField && _this.displayField) {
743
- value = ((_a = item[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
744
- }
745
- return (Array.isArray(formValue) ? formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value; }) : (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value)
746
- || index < _this.limit;
747
- }));
748
- return [2 /*return*/, __classPrivateFieldGet(this, _currentItems).map(function (e) {
749
- var _c;
750
- var _a;
751
- if (_this.valueField && _this.displayField) {
752
- return Object.assign(Object.assign({}, e), (_c = {}, _c["" + _this.valueField] = (_a = e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString(), _c));
719
+ searchText = changeAliasLowerCase(val);
720
+ formValue = this.formControl.value;
721
+ isArray = Array.isArray(formValue);
722
+ hasFields = !!this.valueField && !!this.displayField;
723
+ console.log(__classPrivateFieldGet(this, _items), val);
724
+ return [2 /*return*/, __classPrivateFieldGet(this, _items).filter(function (item) {
725
+ var _a, _b;
726
+ var value = hasFields ? (_a = item[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString() : (item === null || item === void 0 ? void 0 : item.toString()) || '';
727
+ var display = hasFields ? (_b = item[_this.displayField]) === null || _b === void 0 ? void 0 : _b.toString() : (item === null || item === void 0 ? void 0 : item.toString()) || '';
728
+ if (changeAliasLowerCase(value).includes(searchText) || changeAliasLowerCase(display).includes(searchText)) {
729
+ return true;
730
+ }
731
+ if (isArray) {
732
+ return formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value; });
733
+ }
734
+ return (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value;
735
+ }).sort(function (a, b) {
736
+ var _a, _b;
737
+ var value1 = hasFields ? (_a = a[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString() : (a === null || a === void 0 ? void 0 : a.toString()) || '';
738
+ var value2 = hasFields ? (_b = b[_this.valueField]) === null || _b === void 0 ? void 0 : _b.toString() : (b === null || b === void 0 ? void 0 : b.toString()) || '';
739
+ if (isArray) {
740
+ var t1_1 = formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value1; });
741
+ var t2_1 = formValue.some(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value2; });
742
+ if (t1_1 && !t2_1) {
743
+ return -1;
744
+ }
745
+ if (!t1_1 && t2_1) {
746
+ return 1;
747
+ }
748
+ return 0;
749
+ }
750
+ var t1 = (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value1;
751
+ var t2 = (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) === value2;
752
+ if (t1 && !t2) {
753
+ return -1;
754
+ }
755
+ if (!t1 && t2) {
756
+ return 1;
753
757
  }
754
- return e === null || e === void 0 ? void 0 : e.toString();
758
+ return 0;
755
759
  })];
756
760
  }
757
761
  });
758
762
  }); }));
759
- this.display = this.filteredItems.pipe(operators.map(function (items) {
760
- var e_1, _c;
761
- var _a, _b;
762
- var formValue = _this.formControl.value;
763
- if (Array.isArray(formValue)) {
764
- if (!formValue.length) {
765
- return '';
766
- }
767
- var results = [];
768
- var _loop_1 = function (value) {
769
- if (_this.valueField && _this.displayField) {
770
- var selectedItem = items === null || items === void 0 ? void 0 : items.find(function (e) { var _a; return ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()) === value; });
771
- results.push((_a = selectedItem === null || selectedItem === void 0 ? void 0 : selectedItem[_this.displayField]) !== null && _a !== void 0 ? _a : value);
772
- }
773
- else {
774
- var selectedItem = items === null || items === void 0 ? void 0 : items.find(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value; });
775
- results.push(selectedItem !== null && selectedItem !== void 0 ? selectedItem : value);
776
- }
777
- };
778
- try {
779
- for (var _d = __values(formValue.map(function (e) { var _a; return (_a = e === null || e === void 0 ? void 0 : e.toString()) !== null && _a !== void 0 ? _a : ''; }).filter(function (e) { return !!e; })), _e = _d.next(); !_e.done; _e = _d.next()) {
780
- var value = _e.value;
781
- _loop_1(value);
782
- }
783
- }
784
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
785
- finally {
786
- try {
787
- if (_e && !_e.done && (_c = _d.return)) _c.call(_d);
788
- }
789
- finally { if (e_1) throw e_1.error; }
790
- }
791
- return results.join(', ');
792
- }
793
- else {
794
- var value_3 = (formValue === null || formValue === void 0 ? void 0 : formValue.toString()) || '';
795
- if (!value_3) {
796
- return '';
797
- }
798
- if (_this.valueField && _this.displayField) {
799
- var selectedItem_1 = items === null || items === void 0 ? void 0 : items.find(function (e) { var _a; return ((_a = e === null || e === void 0 ? void 0 : e[_this.valueField]) === null || _a === void 0 ? void 0 : _a.toString()) === value_3; });
800
- return (_b = selectedItem_1 === null || selectedItem_1 === void 0 ? void 0 : selectedItem_1[_this.displayField]) !== null && _b !== void 0 ? _b : '';
801
- }
802
- var selectedItem = items === null || items === void 0 ? void 0 : items.find(function (e) { return (e === null || e === void 0 ? void 0 : e.toString()) === value_3; });
803
- return selectedItem !== null && selectedItem !== void 0 ? selectedItem : value_3;
763
+ this.filteredItems = this.allItems.pipe(operators.map(function (allItems) {
764
+ return allItems.filter(function (item, index) { return index < _this.limit; });
765
+ }));
766
+ // this.filteredItems = this.searchTerm$.asObservable().pipe(
767
+ // startWith(''),
768
+ // debounceTime(typeof (this.#items) === 'function' ? this.#delay : 0),
769
+ // // distinctUntilChanged(), Dùng sẽ gây ra bug khi items changes
770
+ // switchMap(async (val: string) => {
771
+ // if (typeof (this.#items) === 'function') {
772
+ // const items = await this.#loadItems(val, this.#items);
773
+ // this.#currentItems = items.filter((item, index) => {
774
+ // const formValue: any | any[] = this.formControl.value;
775
+ // let value = item?.toString() || '';
776
+ // if (this.valueField && this.displayField) {
777
+ // value = item[this.valueField]?.toString() || '';
778
+ // }
779
+ // return (Array.isArray(formValue) ? formValue.some(e => e?.toString() === value) : formValue?.toString() === value)
780
+ // || index < this.limit;
781
+ // });
782
+ // return this.#currentItems.map(e => {
783
+ // if (this.valueField && this.displayField) {
784
+ // return {
785
+ // ...e,
786
+ // [`${this.valueField}`]: e[this.valueField]?.toString()
787
+ // };
788
+ // }
789
+ // return e?.toString();
790
+ // });
791
+ // }
792
+ // const { changeAliasLowerCase } = this.utilityService;
793
+ // this.#currentItems = this.#items.filter(item => {
794
+ // const searchText = changeAliasLowerCase(val);
795
+ // const formValue: any | any[] = this.formControl.value;
796
+ // if (this.valueField && this.displayField) {
797
+ // const value = item[this.valueField]?.toString() || '';
798
+ // const display = item[this.displayField]?.toString() || '';
799
+ // return (Array.isArray(formValue) ? formValue.some(e => e?.toString() === value) : formValue?.toString() === value)
800
+ // || changeAliasLowerCase(value).indexOf(searchText) !== -1
801
+ // || changeAliasLowerCase(display).indexOf(searchText) !== -1;
802
+ // } else {
803
+ // const value = item?.toString() || '';
804
+ // return (Array.isArray(formValue) ? formValue.some(e => e?.toString() === value) : formValue?.toString() === value)
805
+ // || changeAliasLowerCase(value).indexOf(searchText) !== -1;
806
+ // }
807
+ // }).filter((item, index) => {
808
+ // const formValue: any | any[] = this.formControl.value;
809
+ // let value = item?.toString() || '';
810
+ // if (this.valueField && this.displayField) {
811
+ // value = item[this.valueField]?.toString() || '';
812
+ // }
813
+ // return (Array.isArray(formValue) ? formValue.some(e => e?.toString() === value) : formValue?.toString() === value)
814
+ // || index < this.limit;
815
+ // });
816
+ // return this.#currentItems.map(e => {
817
+ // if (this.valueField && this.displayField) {
818
+ // return {
819
+ // ...e,
820
+ // [`${this.valueField}`]: e[this.valueField]?.toString()
821
+ // };
822
+ // }
823
+ // return e?.toString();
824
+ // });
825
+ // })
826
+ // );
827
+ // this.display = this.formControl.valueChanges.pipe(map(formValue => {
828
+ // if (!this.displayField) {
829
+ // return Array.isArray(formValue) ? formValue?.join(', ') : formValue;
830
+ // }
831
+ // if (Array.isArray(formValue)) {
832
+ // if (!formValue.length) {
833
+ // return '';
834
+ // }
835
+ // return this.#selectedItems.map(e => e[this.displayField]?.toString()).join(', ');
836
+ // } else {
837
+ // const value = formValue?.toString() || '';
838
+ // if (!value) {
839
+ // return '';
840
+ // }
841
+ // return this.#selectedItem[this.displayField]?.toString();
842
+ // }
843
+ // }));
844
+ this.display = rxjs.combineLatest([this.allItems, this.formControl.valueChanges]).pipe(operators.map(function (_c) {
845
+ var _d = __read(_c, 2), items = _d[0], formValue = _d[1];
846
+ console.log(items);
847
+ var isArray = Array.isArray(formValue);
848
+ if (!_this.valueField || !_this.displayField) {
849
+ return isArray ? formValue === null || formValue === void 0 ? void 0 : formValue.join(', ') : formValue;
804
850
  }
851
+ return items.filter(function (e) { return isArray ? formValue.includes(e[_this.valueField]) : formValue === e[_this.valueField]; })
852
+ .map(function (e) { var _a; return (_a = e[_this.displayField]) === null || _a === void 0 ? void 0 : _a.toString(); })
853
+ .join(', ');
805
854
  }));
806
855
  };
807
856
  SdSelect.prototype.ngAfterViewInit = function () {
@@ -823,7 +872,7 @@
823
872
  };
824
873
  return SdSelect;
825
874
  }());
826
- _input = new WeakMap(), _name = new WeakMap(), _form = new WeakMap(), _model = new WeakMap(), _items = new WeakMap(), _currentItems = new WeakMap(), _allItem = new WeakMap(), _delay = new WeakMap(), _validator = new WeakMap(), _updateValidator = new WeakMap(), _loadItems = new WeakMap(), _onChange = new WeakMap(), _customValidator = new WeakMap();
875
+ _input = new WeakMap(), _name = new WeakMap(), _form = new WeakMap(), _model = new WeakMap(), _selectedItem = new WeakMap(), _selectedItems = new WeakMap(), _items = new WeakMap(), _currentItems = new WeakMap(), _allItem = new WeakMap(), _delay = new WeakMap(), _validator = new WeakMap(), _updateValidator = new WeakMap(), _search = new WeakMap(), _loadItems = new WeakMap(), _onChange = new WeakMap(), _customValidator = new WeakMap();
827
876
  SdSelect.decorators = [
828
877
  { type: core.Component, args: [{
829
878
  selector: 'sd-select',