Skip to content

Commit ea5fb03

Browse files
committed
feat: include sourceIndex and targetIndex in model events
1 parent 1f8d80c commit ea5fb03

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

modules/ng2-dragula/src/components/dragula.service.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Injectable, EventEmitter, Optional } from '@angular/core';
2-
import { DrakeWithModels } from '../DrakeWithModels';
1+
import { Injectable, Optional } from '@angular/core';
32
import { Group } from '../Group';
43
import { DragulaOptions } from '../DragulaOptions';
54
import { Subject, Observable } from 'rxjs';
@@ -91,9 +90,9 @@ export class DragulaService {
9190
EventTypes.DropModel,
9291
groupName,
9392
(name, [
94-
el, target, source, sibling, item, sourceModel, targetModel
95-
]: [Element, Element, Element, Element, T, T[], T[]]) => {
96-
return { name, el, target, source, sibling, item, sourceModel, targetModel }
93+
el, target, source, sibling, item, sourceModel, targetModel, sourceIndex, targetIndex
94+
]: [Element, Element, Element, Element, T, T[], T[], number, number]) => {
95+
return { name, el, target, source, sibling, item, sourceModel, targetModel, sourceIndex, targetIndex }
9796
})
9897
);
9998

@@ -102,9 +101,9 @@ export class DragulaService {
102101
EventTypes.RemoveModel,
103102
groupName,
104103
(name, [
105-
el, container, source, item, sourceModel
106-
]: [Element, Element, Element, T, T[]]) => {
107-
return { name, el, container, source, item, sourceModel }
104+
el, container, source, item, sourceModel, sourceIndex
105+
]: [Element, Element, Element, T, T[], number]) => {
106+
return { name, el, container, source, item, sourceModel, sourceIndex }
108107
}
109108
)
110109
);
@@ -171,7 +170,7 @@ export class DragulaService {
171170
this.dispatch$.next({
172171
event: EventTypes.RemoveModel,
173172
name,
174-
args: [ el, container, source, item, sourceModel ]
173+
args: [ el, container, source, item, sourceModel, dragIndex ]
175174
});
176175
});
177176
drake.on('drag', (el: any, source: any) => {
@@ -223,7 +222,7 @@ export class DragulaService {
223222
this.dispatch$.next({
224223
event: EventTypes.DropModel,
225224
name,
226-
args: [ dropElm, target, source, sibling, item, sourceModel, targetModel ]
225+
args: [ dropElm, target, source, sibling, item, sourceModel, targetModel, dragIndex, dropIndex ]
227226
});
228227
});
229228
}

modules/ng2-dragula/src/spec/StaticService.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ type Interface<T> = {
99
}
1010
export class StaticService implements Interface<DragulaService> {
1111
public drag: (groupName?: string) => Observable<{ name: string; el: Element; source: Element; }>
12-
= (name) => empty();
12+
= () => empty();
1313
public dragend: (groupName?: string) => Observable<{ name: string; el: Element; }>
14-
= (name) => empty();
14+
= () => empty();
1515
public drop: (groupName?: string) => Observable<{ name: string; el: Element; target: Element; source: Element; sibling: Element; }>
16-
= (name) => empty();
16+
= () => empty();
1717
public cancel: (groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; }>
18-
= (name) => empty();
18+
= () => empty();
1919
public remove: (groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; }>
20-
= (name) => empty();
20+
= () => empty();
2121
public shadow: (groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; }>
22-
= (name) => empty();
22+
= () => empty();
2323
public over: (groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; }>
24-
= (name) => empty();
24+
= () => empty();
2525
public out: (groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; }>
26-
= (name) => empty();
26+
= () => empty();
2727
public cloned: (groupName?: string) => Observable<{ name: string; clone: Element; original: Element; cloneType: "mirror" | "copy"; }>
28-
= (name) => empty();
29-
public dropModel: <T = any>(groupName?: string) => Observable<{ name: string; el: Element; target: Element; source: Element; sibling: Element; item: T; sourceModel: T[]; targetModel: T[]; }>
30-
= (name) => empty();
31-
public removeModel: <T = any>(groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; item: T; sourceModel: T[]; }>
32-
= (name) => empty();
28+
= () => empty();
29+
public dropModel: <T = any>(groupName?: string) => Observable<{ name: string; el: Element; target: Element; source: Element; sibling: Element; item: T; sourceModel: T[]; targetModel: T[]; sourceIndex: number; targetIndex: number; }>
30+
= () => empty();
31+
public removeModel: <T = any>(groupName?: string) => Observable<{ name: string; el: Element; container: Element; source: Element; item: T; sourceModel: T[]; sourceIndex: number; }>
32+
= () => empty();
3333

3434
groups: { [k: string]: Group } = {};
3535

modules/ng2-dragula/src/spec/dragula.service.spec.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22
/// <reference path="./testdouble-jasmine.d.ts" />
33
import { } from 'jasmine';
44
import * as td from 'testdouble'
5-
import { TestBed, inject, async, ComponentFixture } from '@angular/core/testing';
6-
import { DragulaDirective } from '../components/dragula.directive';
75
import { DragulaService } from '../components/dragula.service';
86
import { DrakeWithModels } from '../DrakeWithModels';
97
import { Group } from '../Group';
10-
import { Component, ElementRef, EventEmitter } from "@angular/core";
11-
import { TestHostComponent } from './test-host.component';
12-
import dragula = require('dragula');
13-
import { Subject, Subscription, Observable } from 'rxjs';
14-
import { filter, take } from 'rxjs/operators';
8+
import { Observable } from 'rxjs';
9+
import { take } from 'rxjs/operators';
1510
import { MockDrake, MockDrakeFactory } from '../MockDrake';
1611
import { EventTypes } from '../EventTypes';
1712
import { DragulaOptions } from '../DragulaOptions';
1813

1914
const GROUP = "GROUP";
2015

21-
type SimpleDrake = Partial<DrakeWithModels>;
22-
2316
describe('DragulaService', () => {
2417
let service: DragulaService;
2518

@@ -204,8 +197,8 @@ describe('DragulaService', () => {
204197

205198
let ev = subscribeSync(service.dropModel("DROPMODEL"), () => {
206199
mock.emit(EventTypes.Drag, li, ul);
207-
ul.removeChild(li);
208-
ul.appendChild(li);
200+
ul.removeChild(li); // removes a at index 0
201+
ul.appendChild(li); // adds a at index 1
209202
mock.emit(EventTypes.Drop, li, ul, ul, undefined);
210203
});
211204

@@ -215,6 +208,8 @@ describe('DragulaService', () => {
215208
expect(ev.sourceModel.length).toBe(model.length);
216209
expect(ev.targetModel).not.toBe(model, 'targetModel not cloned');
217210
expect(ev.targetModel).toContain('a');
211+
expect(ev.sourceIndex).toBe(0, 'sourceIndex');
212+
expect(ev.targetIndex).toBe(1, 'targetIndex');
218213

219214
expect(ev.targetModel[0]).toBe('b');
220215
expect(ev.targetModel[1]).toBe('a');
@@ -224,14 +219,14 @@ describe('DragulaService', () => {
224219

225220
it('should dropModel correctly in same list (backwards)', () => {
226221
const ul = buildList('ul', ['li', 'li', 'li']);
227-
const li = ul.children[2];
222+
const li = ul.children[2]; // c
228223
let model = ['a', 'b', 'c'];
229224
let mock = _addMockDrake("DROPMODEL", [ ul ], {}, [ model ]);
230225

231226
let ev = subscribeSync(service.dropModel("DROPMODEL"), () => {
232227
mock.emit(EventTypes.Drag, li, ul);
233-
ul.removeChild(li);
234-
ul.insertBefore(li, ul.firstChild);
228+
ul.removeChild(li); // remove c from index 2
229+
ul.insertBefore(li, ul.firstChild); // add c at index 0
235230
mock.emit(EventTypes.Drop, li, ul, ul, undefined);
236231
});
237232

@@ -242,6 +237,8 @@ describe('DragulaService', () => {
242237
expect(ev.sourceModel).toBe(ev.targetModel, 'sourceModel !== targetModel');
243238
expect(ev.targetModel).not.toBe(model, 'targetModel not cloned');
244239
expect(ev.targetModel).toContain('a');
240+
expect(ev.sourceIndex).toBe(2, 'sourceIndex');
241+
expect(ev.targetIndex).toBe(0, 'targetIndex');
245242

246243
expect(ev.targetModel[0]).toBe('c');
247244
expect(ev.targetModel[1]).toBe('a');
@@ -260,8 +257,8 @@ describe('DragulaService', () => {
260257

261258
let ev = subscribeSync(service.dropModel("DROPMODEL"), () => {
262259
mock.emit(EventTypes.Drag, li, source);
263-
source.removeChild(li);
264-
target.appendChild(li);
260+
source.removeChild(li); // remove b at index 1
261+
target.appendChild(li); // add b at index 2
265262
mock.emit(EventTypes.Drop, li, target, source, undefined);
266263
});
267264

@@ -276,6 +273,9 @@ describe('DragulaService', () => {
276273
expect(ev.targetModel).not.toBe(targetModel, 'targetModel not cloned');
277274
expect(ev.targetModel).toContain('b', 'targetModel should have b in it');
278275

276+
expect(ev.sourceIndex).toBe(1, 'sourceIndex');
277+
expect(ev.targetIndex).toBe(2, 'targetIndex');
278+
279279
expect(ev.targetModel.length).toBe(3);
280280
expect(ev.targetModel[0]).toBe('x');
281281
expect(ev.targetModel[1]).toBe('y');
@@ -303,6 +303,7 @@ describe('DragulaService', () => {
303303
expect(ev.sourceModel[0]).toBe('a');
304304
// no b
305305
expect(ev.sourceModel[1]).toBe('c');
306+
expect(ev.sourceIndex).toBe(1, 'sourceIndex');
306307

307308
service.destroy("REMOVEMODEL");
308309
});

0 commit comments

Comments
 (0)