Skip to content
This repository was archived by the owner on Aug 11, 2023. It is now read-only.

Commit 5029d2b

Browse files
committed
feat: on- prefix is a global syntax, use sortable- prefix for events and allow to bind to options
1 parent 5951c3f commit 5029d2b

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ export function configure(aurelia: Aurelia) {
5151
```
5252
## Usage:
5353
```html
54-
<div sortable="options.bind: options" />
54+
<div sortable.bind="options" />
5555
```
5656

5757
Each event can be used in the following way:
5858

5959
```html
60-
<div sortable="options.bind: options" on-move.delegate="func($event)" />
60+
<div sortable.bind="options" sortable-move.delegate="func($event)" />
6161
```
6262

6363
```javascript
@@ -72,15 +72,13 @@ export class Home {
7272
## sortable
7373
The following attributes can be set to catch events
7474

75-
| Attribute | Sortablejs event | Description
76-
| ---------- | ---------------- | --------------
77-
| on-add | onAdd | Element is dropped into the list from another list
78-
| on-end | onEnd | Dragging ended
79-
| on-filter | onFilter | Attempt to drag a filtered element
80-
| on-move | onMove | Event when you move an item in the list or between lists
81-
| on-remove | onRemove | Element is removed from the list into another list
82-
| on-sort | onSort | Called by any change to the list (add / update / remove)
83-
| on-start | onStart | Dragging started
84-
| on-update | onUpdate | Changed sorting within list
85-
86-
When events are specified in the options property the override the above events.
75+
| Attribute | Sortablejs event | Description
76+
| ---------------- | ---------------- | --------------
77+
| sortable-add | onAdd | Element is dropped into the list from another list
78+
| sortable-end | onEnd | Dragging ended
79+
| sortable-filter | onFilter | Attempt to drag a filtered element
80+
| sortable-move | onMove | Event when you move an item in the list or between lists
81+
| sortable-remove | onRemove | Element is removed from the list into another list
82+
| sortable-sort | onSort | Called by any change to the list (add / update / remove)
83+
| sortable-start | onStart | Dragging started
84+
| sortable-update | onUpdate | Changed sorting within list

src/sortable.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
import { bindable, bindingMode, inject } from "aurelia-framework";
1+
import { inject } from "aurelia-framework";
22
import * as Sortable from "sortablejs";
33

44
@inject(Element)
55
export class SortableCustomAttribute {
66

7-
@bindable({ defaultBindingMode: bindingMode.oneTime })
8-
public options;
9-
107
private sortable;
118

12-
private defaultOptions = {
13-
onAdd: event => this.dispatch("on-add", event),
14-
onEnd: event => this.dispatch("on-end", event),
15-
onFilter: event => this.dispatch("on-filter", event),
16-
onMove: event => this.dispatch("on-move", event),
17-
onRemove: event => this.dispatch("on-remove", event),
18-
onSort: event => this.dispatch("on-sort", event),
19-
onStart: event => this.dispatch("on-start", event),
20-
onUpdate: event => this.dispatch("on-update", event),
21-
};
22-
239
constructor(private element: Element) { }
2410

11+
public detached() {
12+
this.sortable.destroy();
13+
}
14+
2515
public attached() {
26-
this.sortable = Sortable.create(this.element, Object.assign(this.defaultOptions, this.options || {}));
16+
if (!this.sortable) {
17+
this.sortable = Sortable.create(this.element, {});
18+
}
2719
}
2820

29-
public detached() {
30-
this.sortable.destroy();
21+
public valueChanged(newValue) {
22+
if (this.sortable) {
23+
this.sortable.destroy();
24+
}
25+
26+
this.sortable = Sortable.create(this.element, Object.assign({}, newValue || {}));
27+
this.attachListeners();
28+
}
29+
30+
private attachListeners() {
31+
Sortable.utils.on(this.element, "add", event => this.dispatch("sortable-add", event));
32+
Sortable.utils.on(this.element, "end", event => this.dispatch("sortable-end", event));
33+
Sortable.utils.on(this.element, "filter", event => this.dispatch("sortable-filter", event));
34+
Sortable.utils.on(this.element, "move", event => this.dispatch("sortable-move", event));
35+
Sortable.utils.on(this.element, "remove", event => this.dispatch("sortable-remove", event));
36+
Sortable.utils.on(this.element, "sort", event => this.dispatch("sortable-sort", event));
37+
Sortable.utils.on(this.element, "start", event => this.dispatch("sortable-start", event));
38+
Sortable.utils.on(this.element, "update", event => this.dispatch("sortable-update", event));
3139
}
3240

3341
private dispatch(name, data) {

0 commit comments

Comments
 (0)