@syncfusion/ej2-lists 20.2.36 → 20.2.43

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.
@@ -1,458 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Base, Event, getUniqueID, NotifyPropertyChanges, INotifyPropertyChanged, Property } from '@syncfusion/ej2-base';
3
- import { closest, Draggable, DragPosition, MouseEventArgs, remove, compareElementParent } from '@syncfusion/ej2-base';
4
- import { addClass, isNullOrUndefined, getComponent, isBlazor, BlazorDragEventArgs } from '@syncfusion/ej2-base';
5
- import { SortableModel } from './sortable-model';
6
-
7
- /**
8
- * Sortable Module provides support to enable sortable functionality in Dom Elements.
9
- * ```html
10
- * <div id="sortable">
11
- * <div>Item 1</div>
12
- * <div>Item 2</div>
13
- * <div>Item 3</div>
14
- * <div>Item 4</div>
15
- * <div>Item 5</div>
16
- * </div>
17
- * ```
18
- * ```typescript
19
- * let ele: HTMLElement = document.getElementById('sortable');
20
- * let sortObj: Sortable = new Sortable(ele, {});
21
- * ```
22
- */
23
- @NotifyPropertyChanges
24
- export class Sortable extends Base<HTMLElement> implements INotifyPropertyChanged {
25
- private target: HTMLElement;
26
- private curTarget: HTMLElement;
27
- private placeHolderElement: HTMLElement;
28
- /**
29
- * It is used to enable or disable the built-in animations. The default value is `false`
30
- *
31
- * @default false
32
- */
33
-
34
- @Property(false)
35
- public enableAnimation: boolean;
36
- /**
37
- * Specifies the sortable item class.
38
- *
39
- * @default null
40
- */
41
- @Property(null)
42
- public itemClass: string;
43
- /**
44
- * Defines the scope value to group sets of sortable libraries.
45
- * More than one Sortable with same scope allows to transfer elements between different sortable libraries which has same scope value.
46
- */
47
- @Property(null)
48
- public scope: string;
49
- /**
50
- * Defines the callback function for customizing the cloned element.
51
- */
52
- @Property()
53
- public helper: (Element: object) => HTMLElement;
54
- /**
55
- * Defines the callback function for customizing the placeHolder element.
56
- */
57
- @Property()
58
- public placeHolder: (Element: object) => HTMLElement;
59
- /**
60
- * Specifies the callback function for drag event.
61
- *
62
- * @event 'object'
63
- */
64
-
65
-
66
- @Event()
67
- public drag: (e: any) => void;
68
- /* es-lint enable */
69
- /**
70
- * Specifies the callback function for beforeDragStart event.
71
- *
72
- * @event 'object'
73
- */
74
-
75
- @Event()
76
- public beforeDragStart: (e: any) => void;
77
- /* es-lint enable */
78
- /**
79
- * Specifies the callback function for dragStart event.
80
- *
81
- * @event 'object'
82
- */
83
- @Event()
84
- public dragStart: (e: any) => void;
85
- /* es-lint enable */
86
- /**
87
- * Specifies the callback function for beforeDrop event.
88
- *
89
- * @event 'object'
90
- */
91
- @Event()
92
- public beforeDrop: (e: any) => void;
93
- /* es-lint enable */
94
- /**
95
- * Specifies the callback function for drop event.
96
- *
97
- * @event 'object'
98
- */
99
-
100
- @Event()
101
- public drop: (e: any) => void;
102
- /* es-lint enable */
103
- constructor(element: HTMLElement, options?: SortableModel) {
104
- super(options, element);
105
- this.bind();
106
- }
107
- protected bind(): void {
108
- if (!this.element.id) {
109
- this.element.id = getUniqueID('sortable');
110
- }
111
- if (!this.itemClass) {
112
- this.itemClass = 'e-sort-item';
113
- this.dataBind();
114
- }
115
- this.initializeDraggable();
116
- }
117
- private initializeDraggable(): void {
118
- new Draggable(this.element, {
119
- helper: this.getHelper,
120
- dragStart: this.onDragStart,
121
- drag: this.onDrag,
122
- dragStop: this.onDragStop,
123
- dragTarget: `.${this.itemClass}`,
124
- enableTapHold: true,
125
- tapHoldThreshold: 200,
126
- queryPositionInfo: this.queryPositionInfo,
127
- distance: 5
128
- });
129
- }
130
- private getPlaceHolder(target: HTMLElement, instance: Sortable): HTMLElement {
131
- if (instance.placeHolder) {
132
- const placeHolderElement: HTMLElement = instance.placeHolder(
133
- { element: instance.element, grabbedElement: this.target, target: target });
134
- placeHolderElement.classList.add('e-sortable-placeholder');
135
- return placeHolderElement;
136
- }
137
- return null;
138
- }
139
- // eslint-disable-next-line
140
- private getHelper: Function = (e: { sender: MouseEventArgs, element: HTMLElement }) => {
141
- // eslint-disable-next-line prefer-const
142
- let target: HTMLElement = this.getSortableElement(e.sender.target as HTMLElement);
143
- if (!this.isValidTarget(target as Element, this)) {
144
- return false;
145
- }
146
- let element: HTMLElement;
147
- if (this.helper) {
148
- element = this.helper({ sender: target, element: e.element });
149
- } else {
150
- element = target.cloneNode(true) as HTMLElement;
151
- element.style.width = `${target.offsetWidth}px`; element.style.height = `${target.offsetHeight}px`;
152
- }
153
- addClass([element], ['e-sortableclone']);
154
- document.body.appendChild(element);
155
- return element;
156
- }
157
- private isValidTarget(target: Element, instance: Sortable): boolean {
158
- return target && compareElementParent(target, instance.element) && target.classList.contains(instance.itemClass) &&
159
- !target.classList.contains('e-disabled');
160
- }
161
- // eslint-disable-next-line
162
- private onDrag: Function = (e: { target: HTMLElement, event: MouseEventArgs }) => {
163
- if (!e.target) return;
164
- this.trigger('drag', { event: e.event, element: this.element, target: e.target });
165
- let newInst: Sortable = this.getSortableInstance(e.target); let target: HTMLElement = this.getSortableElement(e.target, newInst);
166
- if ((this.isValidTarget(target, newInst) || e.target.className.indexOf('e-list-group-item') > -1) && (this.curTarget !== target ||
167
- !isNullOrUndefined(newInst.placeHolder)) && (newInst.placeHolderElement ? newInst.placeHolderElement !== e.target :
168
- true)) {
169
- if (e.target.className.indexOf('e-list-group-item') > -1) {
170
- target = e.target;
171
- }
172
- this.curTarget = target;
173
- if (this.target === target) { return; }
174
- let oldIdx: number = this.getIndex(newInst.placeHolderElement, newInst);
175
- const placeHolder: HTMLElement = this.getPlaceHolder(target, newInst);
176
- let newIdx: number;
177
- if (placeHolder) {
178
- oldIdx = isNullOrUndefined(oldIdx) ? this.getIndex(this.target) : oldIdx;
179
- newIdx = this.getIndex(target, newInst, e.event);
180
- const isPlaceHolderPresent: boolean = this.isPlaceHolderPresent(newInst);
181
- if (isPlaceHolderPresent && oldIdx === newIdx) { return; }
182
- if (isPlaceHolderPresent) { remove(newInst.placeHolderElement); }
183
- newInst.placeHolderElement = placeHolder;
184
- if (e.target.className.indexOf('e-list-group-item') > -1) {
185
- newInst.element.insertBefore(newInst.placeHolderElement, newInst.element.children[newIdx]);
186
- } else if (newInst.element !== this.element && newIdx === newInst.element.childElementCount) {
187
- newInst.element.appendChild(newInst.placeHolderElement);
188
- } else {
189
- newInst.element.insertBefore(newInst.placeHolderElement, newInst.element.children[newIdx]);
190
- }
191
- this.refreshDisabled(oldIdx, newIdx, newInst);
192
- } else {
193
- oldIdx = isNullOrUndefined(oldIdx) ? this.getIndex(this.target) :
194
- this.getIndex(target, newInst) < oldIdx || !oldIdx ? oldIdx : oldIdx - 1;
195
- newIdx = this.getIndex(target, newInst);
196
- const idx: number = newInst.element !== this.element ? newIdx : oldIdx < newIdx ? newIdx + 1 : newIdx;
197
- this.updateItemClass(newInst);
198
- newInst.element.insertBefore(this.target, newInst.element.children[idx]);
199
- this.refreshDisabled(oldIdx, newIdx, newInst);
200
- this.curTarget = this.target;
201
- this.trigger('drop', {
202
- droppedElement: this.target, element: newInst.element, previousIndex: oldIdx, currentIndex: newIdx,
203
- target: e.target, helper: document.getElementsByClassName('e-sortableclone')[0], event: e.event, scope: this.scope
204
- });
205
- }
206
- }
207
- newInst = this.getSortableInstance(this.curTarget);
208
- if (isNullOrUndefined(target) && e.target !== newInst.placeHolderElement) {
209
- if (this.isPlaceHolderPresent(newInst)) { this.removePlaceHolder(newInst); }
210
- } else {
211
- const placeHolders: Element[] = [].slice.call(document.getElementsByClassName('e-sortable-placeholder')); let inst: Sortable;
212
- placeHolders.forEach((placeHolder: Element) => {
213
- inst = this.getSortableInstance(placeHolder);
214
- if (inst.element && inst !== newInst) { this.removePlaceHolder(inst); }
215
- });
216
- }
217
- }
218
- private removePlaceHolder(instance: Sortable): void {
219
- remove(instance.placeHolderElement);
220
- instance.placeHolderElement = null;
221
- }
222
- private updateItemClass(instance: Sortable): void {
223
- if (this !== instance) {
224
- this.target.classList.remove(this.itemClass);
225
- this.target.classList.add(instance.itemClass);
226
- }
227
- }
228
- private getSortableInstance(element: Element): Sortable {
229
- element = closest(element, `.e-${this.getModuleName()}`);
230
- if (element) {
231
- const inst: Sortable = getComponent(element as HTMLElement, Sortable) as Sortable;
232
- return inst.scope && this.scope && inst.scope === this.scope ? inst : this;
233
- } else {
234
- return this;
235
- }
236
- }
237
- private refreshDisabled(oldIdx: number, newIdx: number, instance: Sortable): void {
238
- if (instance === this) {
239
- let element: Element; const increased: boolean = oldIdx < newIdx; let disabledIdx: number;
240
- let start: number = increased ? oldIdx : newIdx; const end: number = increased ? newIdx : oldIdx;
241
- while (start <= end) {
242
- element = this.element.children[start];
243
- if (element.classList.contains('e-disabled')) {
244
- disabledIdx = this.getIndex(element);
245
- this.element.insertBefore(element, this.element.children[increased ? disabledIdx + 2 : disabledIdx - 1]);
246
- start = increased ? disabledIdx + 2 : disabledIdx + 1;
247
- } else {
248
- start++;
249
- }
250
- }
251
- }
252
- }
253
- private getIndex(target: Element, instance: Sortable = this, e?: MouseEventArgs): number {
254
- let idx: number; let placeHolderPresent: boolean;
255
- [].slice.call(instance.element.children).forEach((element: HTMLElement, index: number): void => {
256
- if (element.classList.contains('e-sortable-placeholder')) { placeHolderPresent = true; }
257
- if (element === target) {
258
- idx = index;
259
- if (!isNullOrUndefined(e)) {
260
- if (placeHolderPresent) { idx -= 1; }
261
- const offset: ClientRect = target.getBoundingClientRect();
262
- const clientY: number = offset.bottom - ((offset.bottom - offset.top) / 2);
263
- idx = e.clientY <= clientY ? idx : idx + 1;
264
- }
265
- return;
266
- }
267
- });
268
- return idx;
269
- }
270
- private getSortableElement(element: HTMLElement, instance: Sortable = this): HTMLElement {
271
- return closest(element, `.${instance.itemClass}`) as HTMLElement;
272
- }
273
- // eslint-disable-next-line
274
- private onDragStart: Function = (e: { target: HTMLElement, event: MouseEventArgs, helper: Element } & BlazorDragEventArgs) => {
275
- this.target = this.getSortableElement(e.target);
276
- let cancelDrag: boolean = false;
277
- this.target.classList.add('e-grabbed');
278
- this.curTarget = this.target;
279
- e.helper = document.getElementsByClassName('e-sortableclone')[0];
280
- const args: object = { cancel: false, element: this.element, target: this.target };
281
- this.trigger('beforeDragStart', args, (observedArgs: BeforeDragEventArgs) => {
282
- if (observedArgs.cancel) {
283
- cancelDrag = observedArgs.cancel;
284
- this.onDragStop(e);
285
- }
286
- });
287
- if (cancelDrag) {
288
- return;
289
- }
290
- if (isBlazor) {
291
- this.trigger('dragStart', {
292
- event: e.event, element: this.element, target: this.target,
293
- bindEvents: e.bindEvents, dragElement: e.dragElement
294
- });
295
- } else {
296
- this.trigger('dragStart', { event: e.event, element: this.element, target: this.target });
297
- }
298
- }
299
- private queryPositionInfo(value: DragPosition): DragPosition {
300
- value.left = pageXOffset ? `${parseFloat(value.left) - pageXOffset}px` : value.left;
301
- value.top = pageYOffset ? `${parseFloat(value.top) - pageYOffset}px` : value.top;
302
- return value;
303
- }
304
- private isPlaceHolderPresent(instance: Sortable): boolean {
305
- return instance.placeHolderElement && !!closest(instance.placeHolderElement, `#${instance.element.id}`);
306
- }
307
- // eslint-disable-next-line
308
- private onDragStop: Function = (e: { target: HTMLElement, event: MouseEvent & TouchEvent, helper: Element }) => {
309
- let dropInst: Sortable = this.getSortableInstance(this.curTarget); let prevIdx: number; let curIdx: number; let handled: boolean;
310
- prevIdx = this.getIndex(this.target);
311
- const isPlaceHolderPresent: boolean = this.isPlaceHolderPresent(dropInst);
312
- if (isPlaceHolderPresent) {
313
- const curIdx: number = this.getIndex(dropInst.placeHolderElement, dropInst);
314
- const args: DropEventArgs = {
315
- previousIndex: prevIdx, currentIndex: curIdx, target: e.target, droppedElement: this.target,
316
- helper: e.helper, cancel: false, handled: false
317
- };
318
- this.trigger('beforeDrop', args, (observedArgs: DropEventArgs) => {
319
- if (!observedArgs.cancel) {
320
- handled = observedArgs.handled;
321
- this.updateItemClass(dropInst);
322
- if (observedArgs.handled) {
323
- const ele: Node = this.target.cloneNode(true);
324
- this.target.classList.remove('e-grabbed');
325
- this.target = ele as HTMLElement;
326
- }
327
- dropInst.element.insertBefore(this.target, dropInst.placeHolderElement);
328
- const curIdx: number = this.getIndex(this.target, dropInst);
329
- prevIdx = this === dropInst && (prevIdx - curIdx) > 1 ? prevIdx - 1 : prevIdx;
330
- this.trigger('drop', {
331
- event: e.event, element: dropInst.element, previousIndex: prevIdx, currentIndex: curIdx,
332
- target: e.target, helper: e.helper, droppedElement: this.target, scopeName: this.scope, handled: handled
333
- });
334
- }
335
- remove(dropInst.placeHolderElement);
336
- });
337
- }
338
- dropInst = this.getSortableInstance(e.target);
339
- // eslint-disable-next-line prefer-const
340
- curIdx = dropInst.element.childElementCount;
341
- prevIdx = this.getIndex(this.target);
342
- if (dropInst.element === e.target || (!isPlaceHolderPresent && this.curTarget === this.target)) {
343
- const beforeDropArgs: DropEventArgs = {
344
- previousIndex: prevIdx, currentIndex: this.curTarget === this.target ? prevIdx : curIdx,
345
- target: e.target, droppedElement: this.target, helper: e.helper, cancel: false
346
- };
347
- this.trigger('beforeDrop', beforeDropArgs, (observedArgs: DropEventArgs) => {
348
- if (dropInst.element === e.target && !observedArgs.cancel) {
349
- this.updateItemClass(dropInst);
350
- dropInst.element.appendChild(this.target);
351
- this.trigger('drop', {
352
- event: e.event, element: dropInst.element, previousIndex: prevIdx, currentIndex: curIdx,
353
- target: e.target, helper: e.helper, droppedElement: this.target, scopeName: this.scope
354
- });
355
- }
356
- });
357
- }
358
- this.target.classList.remove('e-grabbed');
359
- this.target = null;
360
- this.curTarget = null;
361
- remove(e.helper);
362
- (getComponent(this.element, Draggable) as Draggable).intDestroy(e.event);
363
- }
364
- /**
365
- * It is used to sort array of elements from source element to destination element.
366
- *
367
- * @param destination - Defines the destination element to which the sortable elements needs to be appended.
368
- *
369
- * If it is null, then the Sortable library element will be considered as destination.
370
- * @param targetIndexes - Specifies the sortable elements indexes which needs to be sorted.
371
- * @param insertBefore - Specifies the index before which the sortable elements needs to be appended.
372
- * If it is null, elements will be appended as last child.
373
- * @function moveTo
374
- * @returns {void}
375
- */
376
-
377
- public moveTo(destination?: HTMLElement, targetIndexes?: number[], insertBefore?: number): void {
378
- moveTo(this.element, destination, targetIndexes, insertBefore);
379
- }
380
-
381
- /**
382
- * It is used to destroy the Sortable library.
383
- */
384
-
385
- public destroy(): void {
386
- if (this.itemClass === 'e-sort-item') { this.itemClass = null; this.dataBind(); }
387
- (getComponent(this.element, Draggable) as Draggable).destroy();
388
- super.destroy();
389
- }
390
- public getModuleName(): string {
391
- return 'sortable';
392
- }
393
- public onPropertyChanged(newProp: SortableModel, oldProp: SortableModel): void {
394
- for (const prop of Object.keys(newProp)) {
395
- switch (prop) {
396
- case 'itemClass':
397
- [].slice.call(this.element.children).forEach((element: HTMLElement): void => {
398
- if (element.classList.contains(oldProp.itemClass)) {
399
- element.classList.remove(oldProp.itemClass);
400
- }
401
- if (newProp.itemClass) {
402
- element.classList.add(newProp.itemClass);
403
- }
404
- });
405
- break;
406
- }
407
- }
408
- }
409
- }
410
-
411
- /**
412
- * It is used to sort array of elements from source element to destination element.
413
- *
414
- * @private
415
- */
416
-
417
- export function moveTo(from: HTMLElement, to?: HTMLElement, targetIndexes?: number[], insertBefore?: number): void {
418
- let targetElements: Element[] = [];
419
- if (!to) { to = from; }
420
- if (targetIndexes && targetIndexes.length) {
421
- targetIndexes.forEach((index: number): void => {
422
- targetElements.push(from.children[index]);
423
- });
424
- } else {
425
- targetElements = [].slice.call(from.children);
426
- }
427
- if (isNullOrUndefined(insertBefore)) {
428
- targetElements.forEach((target: Element): void => {
429
- to.appendChild(target);
430
- });
431
- } else {
432
- const insertElement: HTMLElement = to.children[insertBefore] as HTMLElement;
433
- targetElements.forEach((target: Element): void => {
434
- to.insertBefore(target, insertElement);
435
- });
436
- }
437
- }
438
-
439
- /**
440
- * An interface that holds item drop event arguments
441
- */
442
- export interface DropEventArgs {
443
- previousIndex: number;
444
- currentIndex: number;
445
- droppedElement: Element;
446
- target: Element;
447
- helper: Element;
448
- cancel?: boolean;
449
- handled?: boolean;
450
- }
451
-
452
- /**
453
- * An interface that holds item before drag event arguments
454
- */
455
- export interface BeforeDragEventArgs {
456
- cancel?: boolean;
457
- target: Element;
458
- }