Skip to content

Commit

Permalink
Improved support for the Custom Element
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmalonenz committed Mar 27, 2016
1 parent ec0fe0a commit 6b478a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/dragula-and-drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {Dragula} from './dragula';
@bindable({ name: 'mirrorContainer', attribute: 'mirror-container', defaultBindingMode: bindingMode.oneTime })
@bindable({ name: 'targetClass', attribute: 'target-class', defaultBindingMode: bindingMode.oneTime, defaultValue: 'drop-target' })
@bindable({ name: 'sourceClass', attribute: 'source-class', defaultBindingMode: bindingMode.oneTime, defaultValue: 'drag-source' })
@bindable({ name: 'dragFn', attribute: 'drag-fn', defaultBindingMode: bindingMode.oneTime, defaultValue: (item, source) => {}})
@bindable({ name: 'dropFn', attribute: 'drop-fn', defaultBindingMode: bindingMode.oneTime, defaultValue: (item, target, source, sibling) => {}})
@bindable({ name: 'dragEndFn', attribute: 'drag-end-fn', defaultBindingMode: bindingMode.oneTime, defaultValue: (item) => {}})
@bindable({ name: 'dragFn', attribute: 'drag-fn', defaultBindingMode: bindingMode.oneTime })
@bindable({ name: 'dropFn', attribute: 'drop-fn', defaultBindingMode: bindingMode.oneTime })
@bindable({ name: 'dragEndFn', attribute: 'drag-end-fn', defaultBindingMode: bindingMode.oneTime })
@customElement('dragula-and-drop')
@noView()
@inject(GLOBAL_OPTIONS)
Expand All @@ -47,15 +47,18 @@ export class DragulaAndDrop {

this.dragula.on('drop', (item, target, source, sibling) => {
this.dragula.cancel();
this.dropFn({ item: item, target: target, source: source, sibling: sibling });
if (typeof this.dropFn === 'function')
this.dropFn({ item: item, target: target, source: source, sibling: sibling });
});

this.dragula.on('drag', (item, source) => {
this.dragFn({ item: item, source: source});
if (typeof this.dragFn === 'function')
this.dragFn({ item: item, source: source});
});

this.dragula.on('dragend', (item) => {
this.dragEndFn({ item: item });
if (typeof this.dragEndFn === 'function')
this.dragEndFn({ item: item });
})
}

Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Options, GLOBAL_OPTIONS} from './options';

import {Options, GLOBAL_OPTIONS, DIRECTION} from './options';
import {Dragula} from './dragula';
export {Dragula, Options};
import {moveBefore} from './move-before';

export {Dragula, Options, DIRECTION, moveBefore};

export function configure(config, callback) {
let defaults = new Options();
Expand Down
12 changes: 12 additions & 0 deletions src/move-before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function moveBefore(array, itemMatcherFn, siblingMatcherFn) {
let removedItem = remove(array, itemMatcherFn);
let nextIndex = array.findIndex(siblingMatcherFn);
array.splice(nextIndex >= 0 ? nextIndex : array.length, 0, removedItem);
}

function remove(array, matcherFn) {
let index = array.findIndex(matcherFn);
if (index >= 0) {
return array.splice(index, 1)[0];
}
}

0 comments on commit 6b478a4

Please sign in to comment.