Skip to content

Commit

Permalink
feat: Custom copyItem to clone model objects
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

If you are using `copy` with `[dragulaModel]`, you must now provide your
own `copyItem` function. The old one was `JSON.parse(JSON.stringify(x))`,
which was terrible.
  • Loading branch information
cormacrelf committed Jul 19, 2018
1 parent d2886c3 commit 78f3f9f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
9 changes: 8 additions & 1 deletion modules/ng2-dragula/src/DragulaOptions.ts
Expand Up @@ -3,7 +3,12 @@
// the copy prop in @types/dragula are just booleans, which is severely limiting.
// and it doesn't have copySortSource.
// TODO: PR this against @types/dragula
export interface DragulaOptions {
export interface DragulaOptions<T = any> {

// note: it's possible you could wrap DragulaOptions such that
// the important functions would get model data included.
// that wouldn't be a breaking change if you appended the args to the callbacks.

/* from @types/dragula */
containers?: Element[];
isContainer?: (el?: Element) => boolean;
Expand All @@ -19,4 +24,6 @@ export interface DragulaOptions {
/* modifications */
copy?: boolean | ((el: Element, source: Element) => boolean);
copySortSource?: boolean | ((el: Element, source: Element) => boolean);

copyItem?: (item: T) => T;
}
25 changes: 16 additions & 9 deletions modules/ng2-dragula/src/components/dragula.service.ts
Expand Up @@ -210,21 +210,28 @@ export class DragulaService {
// but targetModel still has the old value
targetModel = sourceModel;
} else {
let notCopy = dragElm === dropElm;
let dropElmModel = notCopy
? sourceModel[dragIndex]
// TODO: BURN WITH FIRE
: JSON.parse(JSON.stringify(sourceModel[dragIndex]));
item = dropElmModel;

if (notCopy) {
let isCopying = dragElm !== dropElm;
item = sourceModel[dragIndex];
if (isCopying) {
if (!options.copyItem) {
throw new Error("If you have enabled `copy` on a group, you must provide a `copyItem` function.")
}
item = options.copyItem(item);
}

if (!isCopying) {
sourceModel = sourceModel.slice(0)
sourceModel.splice(dragIndex, 1);
}
targetModel = targetModel.slice(0)
targetModel.splice(dropIndex, 0, dropElmModel);
targetModel.splice(dropIndex, 0, item);
drake.models[drake.containers.indexOf(source)] = sourceModel;
drake.models[drake.containers.indexOf(target)] = targetModel;
if (isCopying) {
try {
target.removeChild(dropElm);
} catch (e) {}
}
}
this.dispatch$.next({
event: EventTypes.DropModel,
Expand Down
1 change: 1 addition & 0 deletions modules/ng2-dragula/src/public_api.ts
Expand Up @@ -5,6 +5,7 @@ export { DragulaModule } from './components/dragula.module';
export { dragula, DrakeFactory } from './DrakeFactory';
export { Group } from './Group';
export { DrakeWithModels } from './DrakeWithModels';
export { DragulaOptions } from './DragulaOptions';

export { EventTypes } from './EventTypes';
export { MockDrake, MockDrakeFactory } from './spec/MockDrake';

0 comments on commit 78f3f9f

Please sign in to comment.