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