-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathchip-list.ts
1492 lines (1373 loc) · 59 KB
/
chip-list.ts
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Component, NotifyPropertyChanges, INotifyPropertyChanged, Property, append, isNullOrUndefined, remove } from '@syncfusion/ej2-base';
import { removeClass, KeyboardEventArgs, rippleEffect, closest, MouseEventArgs } from '@syncfusion/ej2-base';
import { Draggable, DragEventArgs } from '@syncfusion/ej2-base';
import { EventHandler, detach, EmitType, Event, addClass, compile} from '@syncfusion/ej2-base';
import { ChipListModel } from './chip-list-model';
import { ChipModel } from './chip';
export const classNames: ClassNames = {
chipSet: 'e-chip-set',
chip: 'e-chip',
avatar: 'e-chip-avatar',
text: 'e-chip-text',
icon: 'e-chip-icon',
delete: 'e-chip-delete',
deleteIcon: 'e-dlt-btn',
multiSelection: 'e-multi-selection',
singleSelection: 'e-selection',
active: 'e-active',
chipWrapper: 'e-chip-avatar-wrap',
iconWrapper: 'e-chip-icon-wrap',
focused: 'e-focused',
disabled: 'e-disabled',
rtl: 'e-rtl',
template: 'e-chip-template',
chipList: 'e-chip-list',
customIcon: 'e-icons',
chipDrag: 'e-chip-drag',
dragAndDrop: 'e-drag-and-drop',
dropRestricted: 'e-error-treeview',
cloneChip: 'e-clone-chip',
dragIndicator: 'e-drag-indicator'
};
/**
* ```props
* index :- Refers to the position of the selected chip in the list of chips
* value :- Refers to the underlying data value associated with the selected chip.
* text :-Refers to the displayed text on the selected chip.
* ```
*/
export type selectionType = 'index' | 'value' | 'text';
/**
* ```props
* Single :- Allows the user to select single chip at the same time.
* Multiple :- Allows the user to select multiple chips at the same time.
* None :- Chips are displayed as read-only.
* ```
*/
export type Selection = 'Single' | 'Multiple' | 'None';
export interface ClassNames {
chipSet: string;
chip: string;
avatar: string;
text: string;
icon: string;
delete: string;
deleteIcon: string;
multiSelection: string;
singleSelection: string;
active: string;
chipWrapper: string;
iconWrapper: string;
focused: string;
disabled: string;
rtl: string;
template: string;
chipList: string;
customIcon: string;
chipDrag: string;
dragAndDrop: string;
dropRestricted: string;
cloneChip: string;
dragIndicator: string;
}
interface ChipFields {
text: string;
cssClass: string;
avatarText: string;
avatarIconCss: string;
htmlAttributes: { [key: string]: string };
leadingIconCss: string;
trailingIconCss: string;
enabled: boolean;
value: string | number | null;
leadingIconUrl: string;
trailingIconUrl: string;
template: string | Function;
}
interface EJ2Instance extends HTMLElement {
// eslint-disable-next-line
ej2_instances: Object[];
}
export interface SelectedItems {
/**
* It denotes the selected items text.
*/
texts: string[];
/**
* It denotes the selected items index.
*/
Indexes: number[];
/**
* It denotes the selected items data.
*/
data: string[] | number[] | ChipModel[];
/**
* It denotes the selected items element.
*/
elements: HTMLElement[];
}
export interface SelectedItem {
/**
* It denotes the selected item text.
*/
text: string;
/**
* It denotes the selected item index.
*/
index: number;
/**
* It denotes the selected item data.
*/
data: string | number | ChipModel;
/**
* It denotes the selected item element.
*/
element: HTMLElement;
}
export interface ClickEventArgs {
/**
* It denotes the clicked item text.
*/
text: string;
/**
* It denotes the clicked item index.
*/
index?: number;
/**
* It denotes the clicked item data.
*/
data: string | number | ChipModel;
/**
* It denotes the clicked item element.
*/
element: HTMLElement;
/**
* It denotes whether the clicked item is selected or not.
*/
selected?: boolean;
/**
* It denotes whether the item can be clicked or not.
*/
cancel: boolean;
/**
* It denotes the event.
*/
event: MouseEventArgs | KeyboardEventArgs;
}
export interface DeleteEventArgs {
/**
* It denotes the deleted item text.
*/
text: string;
/**
* It denotes the deleted item index.
*/
index: number;
/**
* It denotes the deleted item data.
*/
data: string | number | ChipModel;
/**
* It denotes the deleted Item element.
*/
element: HTMLElement;
/**
* It denotes whether the item can be deleted or not.
*/
cancel: boolean;
/**
* It denotes the event.
*/
event: MouseEventArgs | KeyboardEventArgs;
}
export interface ChipDeletedEventArgs {
/**
* Specifies the text value of the deleted chip item.
*/
text: string;
/**
* Specifies the index value of the deleted chip item.
*/
index: number;
/**
* Specifies the data of the deleted chip item.
*/
data: string | number | ChipModel;
}
export interface DragAndDropEventArgs {
/**
* If you want to cancel this event then, set cancel as true. Otherwise, false.
*
* @default false
*/
cancel?: boolean;
/** Return the actual event. */
event: MouseEvent & TouchEvent;
/** Return the currently dragged chip item. */
draggedItem: HTMLElement;
/** Return the currently dragged chip item details as array of JSON object */
draggedItemData: { [key: string]: Object };
/** Return the dragged element destination target. */
dropTarget: HTMLElement;
}
export interface ChipDataArgs {
/**
* It denotes the item text.
*/
text: string | undefined;
/**
* It denotes the Item index.
*/
index: number;
/**
* It denotes the item data.
*/
data: string | number | ChipModel;
/**
* It denotes the item element.
*/
element: HTMLElement;
}
/**
* A chip component is a small block of essential information, mostly used on contacts or filter tags.
* ```html
* <div id="chip"></div>
* ```
* ```typescript
* <script>
* var chipObj = new ChipList();
* chipObj.appendTo("#chip");
* </script>
* ```
*/
@NotifyPropertyChanges
export class ChipList extends Component<HTMLElement> implements INotifyPropertyChanged {
/**
* This chips property helps to render ChipList component.
*
* {% codeBlock src='chips/chips/index.md' %}{% endcodeBlock %}
*
* @default []
*
*/
@Property([])
public chips: string[] | number[] | ChipModel[];
/**
* Specifies the text content for the chip.
*
* {% codeBlock src='chips/text/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public text: string;
/**
* Specifies the customized text value for the avatar in the chip.
*
* {% codeBlock src='chips/avatarText/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public avatarText: string;
/**
* Specifies the icon CSS class for the avatar in the chip.
*
* {% codeBlock src='chips/avatarIconCss/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public avatarIconCss: string;
/**
* Allows additional HTML attributes such as aria labels, title, name, etc., and
* accepts n number of attributes in a key-value pair format.
*
* {% codeBlock src='chiplist/htmlAttributes/index.md' %}{% endcodeBlock %}
*
* @default {}
*/
@Property('')
public htmlAttributes: { [key: string]: string };
/**
* Specifies the leading icon CSS class for the chip.
*
* {% codeBlock src='chips/leadingIconCss/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public leadingIconCss: string;
/**
* Specifies the trailing icon CSS class for the chip.
*
* {% codeBlock src='chips/trailingIconCss/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public trailingIconCss: string;
/**
* Specifies the leading icon url for the chip.
*
* @default ''
*/
@Property('')
public leadingIconUrl: string;
/**
* Specifies the trailing icon url for the chip.
*
* @default ''
*/
@Property('')
public trailingIconUrl: string;
/**
* Specifies the custom classes to be added to the chip element used to customize the ChipList component.
*
* {% codeBlock src='chips/cssClass/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Specifies a value that indicates whether the chip component is enabled or not.
*
* @default true
*/
@Property(true)
public enabled: boolean;
/**
* Sets or gets the selected chip items in the chip list.
*
* {% codeBlock src='chips/selectedChips/index.md' %}{% endcodeBlock %}
*
* @default []
*/
@Property([])
public selectedChips: string[] | number[] | number;
/**
* Defines the selection type of the chip. The available types are:
* 1. Input chip
* 2. Choice chip
* 3. Filter chip
* 4. Action chip
*
* @default 'None'
*/
@Property('None')
public selection: Selection;
/**
* Enables or disables the delete functionality of a chip.
*
* {% codeBlock src='chips/enableDelete/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public enableDelete: boolean;
/**
* Specifies a boolean value that indicates whether the chip item can be dragged and reordered.
* This enables drag-and-drop functionality within a single container or across multiple containers of chips when dragging is enabled.
*
* @default false
*/
@Property(false)
public allowDragAndDrop: boolean;
/**
* Specifies the target in which the draggable element can be moved and dropped.
* By default, the draggable element movement occurs in the page.
*
* @default null
*/
@Property(null)
public dragArea: HTMLElement | string;
/**
* Triggers when the component is created successfully.
*
* {% codeBlock src='chips/created/index.md' %}{% endcodeBlock %}
*
* @event created
*/
@Event()
public created: EmitType<Event>;
/**
* Triggers when a chip is clicked.
*
* {% codeBlock src='chips/click/index.md' %}{% endcodeBlock %}
*
* @event click
*/
@Event()
public click: EmitType<ClickEventArgs>;
/**
* Triggers before the click event of the chip is fired.
* This event can be used to prevent the further process and restrict the click action over a chip.
*
* {% codeBlock src='chips/beforeClick/index.md' %}{% endcodeBlock %}
*
* @event beforeClick
*/
@Event()
public beforeClick: EmitType<ClickEventArgs>;
/**
* Fires before removing the chip element.
*
* {% codeBlock src='chips/delete/index.md' %}{% endcodeBlock %}
*
* @event delete
*/
@Event()
public delete: EmitType<DeleteEventArgs>;
/**
* Triggers when the chip item is removed.
*
* {% codeBlock src='chips/deleted/index.md' %}{% endcodeBlock %}
*
* @event deleted
*/
@Event()
public deleted: EmitType<ChipDeletedEventArgs>;
/**
* Fires when a chip item starts moving due to a drag action.
*
* @event dragStart
*/
@Event()
public dragStart: EmitType<DragAndDropEventArgs>;
/**
* Fires while a chip item is being dragged.
*
* @event dragging
*/
@Event()
public dragging: EmitType<DragAndDropEventArgs>;
/**
* Fires when a chip item is reordered after completing a drag action.
*
* @event dragStop
*/
@Event()
public dragStop: EmitType<DragAndDropEventArgs>;
constructor(options?: ChipListModel, element?: string | HTMLElement) {
super(options, element);
}
private rippleFunction: Function;
private type: string;
private innerText: string;
public multiSelectedChip: number[] = [];
private dragObj: Draggable;
private dragCollection: Draggable[];
private dragIndicator: HTMLElement;
private updatedInstance: HTMLElement;
/**
* Initialize the event handler
*
* @private
*/
protected preRender(): void {
//prerender
}
/**
* To find the chips length.
*
* @returns boolean
* @private
*/
protected chipType(): boolean {
return (this.chips && this.chips.length && this.chips.length > 0) as boolean;
}
/**
* To Initialize the control rendering.
*
* @returns void
* @private
*/
protected render(): void {
this.type = (!isNullOrUndefined(this.chips) && this.chips.length) ? 'chipset' : (this.text || this.element.innerText ? 'chip' : 'chipset');
this.setAttributes();
this.createChip();
this.setRtl();
this.select(this.selectedChips);
this.wireEvent(false);
this.rippleFunction = rippleEffect(this.element, {
selector: '.' + classNames.chip
});
this.renderComplete();
this.dragCollection = [];
if (this.allowDragAndDrop) {
this.enableDraggingChips();
}
}
private enableDraggingChips(): void {
let clonedChipElement: HTMLElement;
const chipElements: NodeListOf<HTMLElement> = this.element.querySelectorAll('.' + classNames.chip);
chipElements.forEach((chip: HTMLElement, index: number) => {
this.dragObj = new Draggable(chip, {
preventDefault: false,
clone: true,
dragArea: this.dragArea,
helper: () => {
clonedChipElement = chip.cloneNode(true) as HTMLElement;
clonedChipElement.classList.add(classNames.cloneChip);
this.element.appendChild(clonedChipElement);
return clonedChipElement;
},
dragStart: (args: DragEventArgs) => {
this.dragIndicator = this.createElement('div', { className: classNames.dragIndicator });
document.body.appendChild(this.dragIndicator);
const chipData: ChipDataArgs = this.find(args.element);
const dragStartArgs: DragAndDropEventArgs = {
cancel: false,
event: args.event,
draggedItem: args.element,
draggedItemData: chipData as any,
dropTarget: null
};
this.trigger('dragStart', dragStartArgs, () => {
if (isNullOrUndefined(dragStartArgs.cancel)) {
dragStartArgs.cancel = false;
}
});
if (!dragStartArgs.cancel) {
clonedChipElement.setAttribute('drag-indicator-index', index.toString());
} else {
this.dragObj.intDestroy(args.event);
}
},
drag: (args: DragEventArgs) => {
const chipData: ChipDataArgs = this.find(args.element);
const draggingArgs: DragAndDropEventArgs = {
event: args.event,
draggedItem: args.element,
draggedItemData: chipData as any,
dropTarget: null
};
this.trigger('dragging', draggingArgs);
let draggingIconEle: HTMLElement | null = clonedChipElement.querySelector('.' + classNames.chipDrag);
if (isNullOrUndefined(draggingIconEle)) {
draggingIconEle = this.createElement('span', { className: `${classNames.customIcon} ${classNames.dragAndDrop} ${classNames.chipDrag}` }) as HTMLElement;
clonedChipElement.prepend(draggingIconEle);
}
this.allowExternalDragging(args, clonedChipElement, draggingIconEle);
},
dragStop: (args: DragEventArgs) => {
const chipData: ChipDataArgs = this.find(args.element);
const dragStopArgs: DragAndDropEventArgs = {
cancel: false,
event: args.event,
draggedItem: args.element,
draggedItemData: chipData as any,
dropTarget: args.target
};
this.trigger('dragStop', dragStopArgs, () => {
if (isNullOrUndefined(dragStopArgs.cancel)) {
dragStopArgs.cancel = false;
}
});
if (!dragStopArgs.cancel) {
this.allowExternalDrop(args, clonedChipElement);
}
if (!isNullOrUndefined(this.dragIndicator)) {
remove(this.dragIndicator);
}
if (!isNullOrUndefined(clonedChipElement)) {
clonedChipElement.remove();
}
}
});
if (this.dragCollection.indexOf(this.dragObj) === -1) {
this.dragCollection.push(this.dragObj);
}
});
}
private checkInstance(args: DragEventArgs, context: ChipList): boolean {
const isInstanceMatched: boolean = !isNullOrUndefined(args.target.closest('.' + classNames.chipList)) &&
args.target.closest('.' + classNames.chipList).id !== context.element.id;
if (isInstanceMatched) {
this.updatedInstance = args.target.closest('.' + classNames.chipList) as HTMLElement;
}
return isInstanceMatched;
}
private setIcons(currentInstance: ChipList, draggingIconEle: HTMLElement, target: HTMLElement,
indicatorEle: HTMLElement, outOfDragArea: boolean): void {
const isTargetInside: boolean = currentInstance.element.contains(target);
const isDroppable: Element = target.closest('.e-droppable');
if ((isTargetInside || isDroppable) && !outOfDragArea) {
draggingIconEle.classList.add(classNames.dragAndDrop);
draggingIconEle.classList.remove(classNames.dropRestricted);
if (isDroppable) {
indicatorEle.style.display = 'none';
}
} else {
draggingIconEle.classList.remove(classNames.dragAndDrop);
draggingIconEle.classList.add(classNames.dropRestricted);
indicatorEle.style.display = 'none';
}
}
private allowExternalDragging(args: DragEventArgs, clonedChipElement: HTMLElement, draggingIconEle: HTMLElement): void {
let currentInstance: ChipList;
let closestChip: Element | null = null;
let closestDistance: number = Infinity;
let newIndex: number = -1;
let outOfDragArea: boolean = false;
if (this.checkInstance(args, this)) {
this.dragIndicator.style.display = 'none';
currentInstance = this.getCurrentInstance(args);
currentInstance.dragIndicator = this.dragIndicator;
if (!currentInstance.allowDragAndDrop) { return; }
} else {
currentInstance = this as ChipList;
}
const indicatorEle: HTMLElement = currentInstance.dragIndicator;
indicatorEle.style.display = 'inline';
outOfDragArea = this.dragAreaCheck(this.dragArea, args.target, outOfDragArea, draggingIconEle, indicatorEle);
this.setIcons(currentInstance, draggingIconEle, args.target, indicatorEle, outOfDragArea);
currentInstance.element.appendChild(clonedChipElement);
const droppedRect: DOMRect = clonedChipElement.getBoundingClientRect() as DOMRect;
const allChips: Element[] = Array.from(currentInstance.element.querySelectorAll('.' + classNames.chip));
allChips.forEach((chip: Element, i: number) => {
if (chip !== clonedChipElement) {
const rect: DOMRect = chip.getBoundingClientRect() as DOMRect;
const distance: number = Math.sqrt(Math.pow(droppedRect.left - rect.left, 2) + Math.pow(droppedRect.top - rect.top, 2));
if (distance < closestDistance) {
closestDistance = distance;
closestChip = chip;
newIndex = i;
}
}
});
if (newIndex === -1) {
newIndex = allChips.length;
}
const chipsDistance: number = this.getChipsDistance(currentInstance);
const cloneRect: DOMRect = clonedChipElement.getBoundingClientRect() as DOMRect;
let rect: DOMRect;
if (closestChip || allChips.length > 0) {
const targetChip: Element = closestChip || allChips[allChips.length - 1];
rect = targetChip.getBoundingClientRect() as DOMRect;
indicatorEle.style.top = rect.top + window.scrollY + 'px';
indicatorEle.style.left = currentInstance.enableRtl ? (rect.right + chipsDistance + 'px') :
(rect.left - chipsDistance + window.scrollX + 'px');
}
if (currentInstance.enableRtl) {
if (cloneRect.left < rect.left - rect.width / 2 && cloneRect.top > rect.top) {
indicatorEle.style.left = rect.left - chipsDistance + window.scrollX + 'px';
}
} else if (cloneRect.left > rect.left + rect.width / 2 && cloneRect.top > rect.top) {
indicatorEle.style.left = rect.left + rect.width + chipsDistance + window.scrollX + 'px';
}
}
private dragAreaCheck(dragArea: string | HTMLElement, target: HTMLElement, outOfDragArea: boolean,
draggingIconEle: HTMLElement, indicatorEle: HTMLElement): boolean {
if (isNullOrUndefined(dragArea)) {
return false;
}
const isString: boolean = typeof dragArea === 'string';
const isHtmlElement: boolean = dragArea instanceof HTMLElement;
const dragAreaElement: string | Element = isString ? document.querySelector(dragArea as string) : dragArea;
if (!isNullOrUndefined(dragAreaElement)) {
if ((isString || isHtmlElement) && !(dragAreaElement as HTMLElement).contains(target)) {
outOfDragArea = true;
indicatorEle.style.display = 'none';
draggingIconEle.classList.add(classNames.dropRestricted);
draggingIconEle.classList.remove(classNames.dragAndDrop);
}
}
return outOfDragArea;
}
private getChipsDistance(currentInstance: ChipList): number {
const constValue: number = 4;
if (currentInstance.chips.length <= 1) {
return constValue;
}
let constantDistance: number;
const firstChipClientRect: DOMRect = currentInstance.find(0).element.getBoundingClientRect() as DOMRect;
const secondChipClientRect: DOMRect = currentInstance.find(1).element.getBoundingClientRect() as DOMRect;
const firstChipLeft: number = firstChipClientRect.left;
if (currentInstance.enableRtl) {
const secondChipRight: number = secondChipClientRect.right;
constantDistance = firstChipLeft < secondChipRight ? constValue : ((firstChipLeft - secondChipRight) / 2);
return constantDistance;
} else {
const firstChipWidth: number = firstChipClientRect.width;
const secondChipLeft: number = secondChipClientRect.left;
constantDistance = secondChipLeft < (firstChipLeft + firstChipWidth) ?
constValue : (secondChipLeft - (firstChipLeft + firstChipWidth)) / 2;
return constantDistance;
}
}
private getCurrentInstance(args: DragEventArgs): ChipList {
const chipContainer: HTMLElement = args.target.closest('.' + classNames.chipList) as HTMLElement;
if (!isNullOrUndefined(chipContainer) && !isNullOrUndefined((chipContainer as EJ2Instance).ej2_instances)) {
for (let i: number = 0; i < (chipContainer as EJ2Instance).ej2_instances.length; i++) {
if ((chipContainer as EJ2Instance).ej2_instances[parseInt(i.toString(), 10)] instanceof ChipList) {
return (chipContainer as EJ2Instance).ej2_instances[i as number] as ChipList;
}
}
}
return null;
}
private allowExternalDrop(args: DragEventArgs, clonedChipElement: HTMLElement): void {
const originalIndex: number = parseInt(clonedChipElement.getAttribute('drag-indicator-index') as string, 10);
let currentInstance: ChipList;
let outOfDragArea: boolean = false;
let isInstanceChanged: boolean = false;
if (this.checkInstance(args, this)) {
isInstanceChanged = true;
currentInstance = this.getCurrentInstance(args);
if (!currentInstance.allowDragAndDrop) { return; }
} else {
currentInstance = this as ChipList;
}
const indicatorEle: HTMLElement = currentInstance.dragIndicator;
indicatorEle.style.display = 'inline';
if (!currentInstance.element.contains(args.target)) {
return;
}
outOfDragArea = this.dragAreaCheck(this.dragArea, args.target, outOfDragArea, clonedChipElement.querySelector('.' + classNames.chipDrag), indicatorEle);
if (outOfDragArea) { return; }
const indicatorRect: DOMRect = indicatorEle.getBoundingClientRect() as DOMRect;
const allChips: Element[] = Array.from(currentInstance.element.querySelectorAll('.' + classNames.chip));
let newIndex: number = -1;
let topOffset: boolean = false;
let leftOffset: boolean = false;
let rightOffset: boolean = false;
for (let i: number = 0; i < allChips.length; i++) {
if (allChips[i as number] !== clonedChipElement) {
const chipRect: DOMRect = allChips[i as number].getBoundingClientRect() as DOMRect;
topOffset = indicatorRect.top < chipRect.top + chipRect.height / 2;
leftOffset = indicatorRect.left < chipRect.left + chipRect.width / 2;
rightOffset = indicatorRect.left > chipRect.left + chipRect.width / 2;
if ((!currentInstance.enableRtl && topOffset && leftOffset) || (currentInstance.enableRtl && topOffset && rightOffset)) {
newIndex = i;
if (i > originalIndex && !isInstanceChanged) {
newIndex = i - 1;
}
break;
}
}
}
if (newIndex === -1) {
let nextChipIndex: number;
for (let i: number = 0; i < allChips.length; i++) {
const chipRect: DOMRect = allChips[i as number].getBoundingClientRect() as DOMRect;
if ((chipRect.top > indicatorRect.top) || (chipRect.top === indicatorRect.top && chipRect.left > indicatorRect.left)) {
nextChipIndex = i as number;
break;
}
}
if (nextChipIndex !== allChips.length) {
newIndex = nextChipIndex;
} else {
newIndex = allChips.length;
}
}
const currentChipList: string[] = Array.from(this.chips as string[]);
if (isInstanceChanged) {
this.dropChip(currentChipList, originalIndex, currentInstance, newIndex, true);
} else if (newIndex !== originalIndex) {
this.dropChip(currentChipList, originalIndex, currentInstance, newIndex, false);
}
}
private dropChip(currentChipList: string[], originalIndex: number, currentInstance: ChipList,
newIndex: number, instanceChanged: boolean): void {
const draggedChip: string = currentChipList.splice(originalIndex, 1)[0];
if (!instanceChanged) {
currentChipList.splice(newIndex, 0, draggedChip);
currentInstance.chips = currentChipList;
} else {
const newChips: string[] = Array.from(currentInstance.chips as string[]);
newChips.splice(newIndex, 0, draggedChip);
currentInstance.chips = newChips;
}
this.chips = currentChipList;
currentInstance.dataBind();
this.dataBind();
currentInstance.enableDraggingChips();
}
private createChip(): void {
this.innerText = (this.element.innerText && this.element.innerText.length !== 0)
? this.element.innerText.trim() : this.element.innerText;
this.element.innerHTML = '';
this.chipCreation(this.type === 'chip' ? [this.innerText ? this.innerText : this.text] : this.chips);
}
private setAttributes(): void {
if (this.type === 'chip') {
if (this.enabled) {this.element.tabIndex = 0; }
this.element.setAttribute('role', 'button');
} else {
this.element.classList.add(classNames.chipSet);
this.element.setAttribute('role', 'listbox');
if (this.selection === 'Multiple') {
this.element.classList.add(classNames.multiSelection);
this.element.setAttribute('aria-multiselectable', 'true');
} else if (this.selection === 'Single') {
this.element.classList.add(classNames.singleSelection);
this.element.setAttribute('aria-multiselectable', 'false');
} else {
this.element.setAttribute('aria-multiselectable', 'false');
}
}
}
private setRtl(): void {
this.element.classList[this.enableRtl ? 'add' : 'remove'](classNames.rtl);
}
private renderTemplates(): void {
if ((this as any).isReact) {
this.renderReactTemplates();
}
}
private templateParser(template: string | Function): Function {
if (template) {
try {
if (typeof template !== 'function' && document.querySelectorAll(template).length) {
return compile(document.querySelector(template).innerHTML.trim());
} else {
return compile(template);
}
} catch (error) {
return compile(template);
}
}
return undefined;
}
private chipCreation(data: string[] | number[] | ChipModel[]): void {
if (isNullOrUndefined(data)) { return; }
let chipListArray: HTMLElement[] = [];
const attributeArray: { [key: string]: string; }[] = [];
for (let i: number = 0; i < data.length; i++) {
const fieldsData: ChipFields = this.getFieldValues(data[i as number]);
const attributesValue: { [key: string]: string; } = fieldsData.htmlAttributes;
attributeArray.push(attributesValue);
const chipArray: HTMLElement[] = this.elementCreation(fieldsData);
const className: string[] = (classNames.chip + ' ' + (fieldsData.enabled ? ' ' : classNames.disabled) + ' ' +
(fieldsData.avatarIconCss || fieldsData.avatarText ? classNames.chipWrapper : (fieldsData.leadingIconCss ?
classNames.iconWrapper : ' ')) + ' ' + fieldsData.cssClass).split(' ').filter((css: string) => css);
if (!this.chipType() || this.type === 'chip') {
chipListArray = chipArray;
addClass([this.element], className);
this.element.setAttribute('aria-label', fieldsData.text);
if (fieldsData.value) {
this.element.setAttribute('data-value', fieldsData.value.toString());
}
} else {
const wrapper: HTMLElement = this.createElement('DIV', {
className: className.join(' '), attrs: {
tabIndex: '0', role: 'option',
'aria-label': fieldsData.text, 'aria-selected': 'false'
}
});
if (this.enableDelete) { wrapper.setAttribute('aria-keyshortcuts', 'Press delete or backspace key to delete'); }
if (fieldsData.value) {
wrapper.setAttribute('data-value', fieldsData.value.toString());
}
if (fieldsData.enabled) { wrapper.setAttribute('aria-disabled', 'false'); }
else {
wrapper.removeAttribute('tabindex');
wrapper.setAttribute('aria-disabled', 'true');
}
if (!isNullOrUndefined(attributeArray[i as number])) {
if (attributeArray.length > i && Object.keys(attributeArray[i as number]).length) {
let htmlAttr: string[] = [];
htmlAttr = (Object.keys(attributeArray[i as number]));
for (let j: number = 0; j < htmlAttr.length; j++) {
wrapper.setAttribute(htmlAttr[j as number], attributeArray[i as number][htmlAttr[j as number]]);
}
}
}
append(chipArray, wrapper);
chipListArray.push(wrapper);
}
}
append(chipListArray, this.element);
}
private getFieldValues(data: string | number | ChipModel): ChipFields {
const chipEnabled: boolean = !(this.enabled.toString() === 'false');
const fields: ChipFields = {
text: typeof data === 'object' ? (data.text ? data.text.toString() : this.text.toString()) :
(!this.chipType() ? (this.innerText ? this.innerText : this.text.toString()) : data.toString()),
cssClass: typeof data === 'object' ? (data.cssClass ? data.cssClass.toString() : this.cssClass.toString()) :
(this.cssClass.toString()),
leadingIconCss: typeof data === 'object' ? (data.leadingIconCss ? data.leadingIconCss.toString() :
this.leadingIconCss.toString()) : (this.leadingIconCss.toString()),
avatarIconCss: typeof data === 'object' ? (data.avatarIconCss ? data.avatarIconCss.toString() :
this.avatarIconCss.toString()) : (this.avatarIconCss.toString()),
avatarText: typeof data === 'object' ? (data.avatarText ? data.avatarText.toString() : this.avatarText.toString()) :
(this.avatarText.toString()),
trailingIconCss: typeof data === 'object' ? (data.trailingIconCss ? data.trailingIconCss.toString() :
this.trailingIconCss.toString()) : (this.trailingIconCss.toString()),
enabled: typeof data === 'object' ? (data.enabled !== undefined ? (data.enabled.toString() === 'false' ? false : true) :
chipEnabled) : (chipEnabled),
value: typeof data === 'object' ? ((data.value ? data.value.toString() : null)) : null,
leadingIconUrl: typeof data === 'object' ? (data.leadingIconUrl ? data.leadingIconUrl.toString() : this.leadingIconUrl) :
this.leadingIconUrl,
trailingIconUrl: typeof data === 'object' ? (data.trailingIconUrl ? data.trailingIconUrl.toString() : this.trailingIconUrl) :
this.trailingIconUrl,
htmlAttributes: typeof data === 'object' ? (data.htmlAttributes ? data.htmlAttributes : this.htmlAttributes) : this.htmlAttributes,
template: typeof data === 'object' ? (data.template ? data.template : null) : null
};
return fields;