Skip to content

Commit 95ff17e

Browse files
committed
docs(examples): simple sortable rewritten simply
1 parent 94968d0 commit 95ff17e

File tree

2 files changed

+52
-63
lines changed

2 files changed

+52
-63
lines changed
Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,27 @@
11
import { Component, ViewChild, ElementRef } from "@angular/core";
22
import { SkyhookDndService } from "angular-skyhook";
3-
import { DraggedItem, SharedSortableService } from "angular-skyhook-card-list";
3+
import { DraggedItem, SharedSortableService, Size } from "angular-skyhook-card-list";
44

55
@Component({
66
selector: 'simple-sortable-container',
77
template:
88
`
9-
<div [dragSource]="source">
10-
<input #text value="drag me in" />
11-
</div>
129
<app-simple-sortable></app-simple-sortable>
1310
`
1411
})
1512
export class ContainerComponent {
1613
id = 1000;
17-
@ViewChild('text') text: ElementRef<HTMLInputElement>;
1814
source = this.dnd.dragSource<DraggedItem<any>>("SIMPLE", {
1915
beginDrag: monitor => {
20-
// TODO: provide static method for implementing beginDrag
2116
return {
22-
data: {
23-
id: this.id++,
24-
name: this.text.nativeElement.value
25-
},
17+
data: { id: this.id++, name: "whatever" },
2618
type: "SIMPLE",
2719
index: 0,
28-
isCopy: false,
29-
// TODO: allow string IDs
30-
listId: -1,
31-
// TODO: remove size
32-
size: { width: 0, height: 0, style: () => ({ width: '', height: '' }) },
33-
hover: { index: 1, listId: -1 }
20+
listId: "somewhere EXTERNAL",
21+
size: new Size(0,0),
22+
hover: { index: 0, listId: "somewhere EXTERNAL" }
3423
}
3524
}
3625
});
37-
constructor(private dnd: SkyhookDndService, private sortable: SharedSortableService<any>) {
38-
}
39-
ngOnInit() {
40-
this.sortable.register("SIMPLE", {
41-
trackBy: (data) => data.id,
42-
// copy: (item) => {
43-
// return item.listId === 'me';
44-
// },
45-
// clone: (data: { name: string; }) => {
46-
// return { name: data.name + ' cloned' };
47-
// },
48-
// canDrag: (data, listId) => {
49-
// return listId === 'me';
50-
// },
51-
// canDrop: (item) => {
52-
// return item.hover.listId === 'you';
53-
// }
54-
});
55-
}
26+
constructor(private dnd: SkyhookDndService) { }
5627
}
Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,75 @@
11
import { Component, ChangeDetectionStrategy } from "@angular/core";
22
import * as faker from 'faker';
3+
import { SortableSpec, DraggedItem } from "angular-skyhook-card-list";
4+
import { BehaviorSubject, combineLatest } from "rxjs";
5+
6+
interface SimpleData {
7+
id: number;
8+
name: string;
9+
}
310

411
@Component({
512
selector: 'app-simple-sortable',
613
changeDetection: ChangeDetectionStrategy.OnPush,
714
template: `
8-
<ng-template #card cardTemplate let-context>
9-
<div [cardRenderer]="context"
10-
#render="cardRenderer"
11-
[class.dragging]="render.isDragging$|async"
12-
[dragSource]="render.source">
13-
14-
<div class="card">
15+
<skyhook-card-list class="list"
16+
cardListType="SIMPLE"
17+
cardListId="simple-demo"
18+
[cardListSpec]="simpleSpec">
19+
<ng-template cardTemplate let-context>
20+
<div class="card"
21+
[cardRenderer]="context"
22+
#render="cardRenderer"
23+
[class.placeholder]="render.isDragging$|async"
24+
[dragSource]="render.source">
1525
<pre>{{ render.data.name | json }}</pre>
1626
</div>
17-
18-
</div>
19-
</ng-template>
20-
21-
<div class="flex">
22-
<skyhook-card-list class="list" cardListType="SIMPLE" cardListId="me" [(shared)]="list" [template]="card">
23-
</skyhook-card-list>
24-
<skyhook-card-list class="list list--right" cardListType="SIMPLE" cardListId="you" [(shared)]="list2" [template]="card">
25-
</skyhook-card-list>
26-
</div>
27-
<div class="flex">
28-
<pre>{{list|json}}</pre>
29-
<pre>{{list2|json}}</pre>
30-
</div>
27+
</ng-template>
28+
</skyhook-card-list>
3129
`,
3230
styles: [`
3331
.flex { display: flex; }
3432
.flex > * { flex-grow: 1; flex-shrink: 0; min-width: 0; }
3533
.card { min-width: 0; width: 100%; max-width: 300px; margin: 4px; border: 1px dashed #333; }
34+
.card { cursor: move; }
3635
pre { margin: 0; }
37-
.dragging pre { opacity: 0.4; background: #eee; }
36+
.placeholder pre { opacity: 0.4; background: #eee; }
3837
.list--right pre { color: #a70000; }
3938
.list--right pre { background: rgba(255, 0, 0, 0.1); }
4039
`]
4140
})
4241
export class SimpleComponent {
4342

44-
list = [
43+
list: SimpleData[] = [
4544
{ id: 1, name: faker.name.firstName() },
4645
{ id: 2, name: faker.name.firstName() },
4746
{ id: 3, name: faker.name.firstName() },
4847
{ id: 4, name: faker.name.firstName() },
4948
{ id: 5, name: faker.name.firstName() },
5049
];
5150

52-
list2 = [
53-
{ id: 11, name: faker.name.firstName() },
54-
{ id: 12, name: faker.name.firstName() },
55-
{ id: 13, name: faker.name.firstName() },
56-
];
51+
list$ = new BehaviorSubject(this.list);
52+
53+
move(item: DraggedItem<SimpleData>) {
54+
const temp = this.list.slice(0);
55+
temp.splice(item.index, 1);
56+
temp.splice(item.hover.index, 0, item.data);
57+
this.list$.next(temp);
58+
return temp;
59+
}
60+
61+
simpleSpec: SortableSpec<SimpleData> = {
62+
trackBy: x => x.id,
63+
getList: _ => this.list$,
64+
hover: (item) => {
65+
this.move(item);
66+
},
67+
drop: (item) => {
68+
this.list = this.move(item);
69+
},
70+
endDrag: item => {
71+
this.list$.next(this.list);
72+
}
73+
}
74+
5775
}

0 commit comments

Comments
 (0)