|
1 | 1 | import { Component, ChangeDetectionStrategy } from "@angular/core";
|
2 | 2 | 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 | +} |
3 | 10 |
|
4 | 11 | @Component({
|
5 | 12 | selector: 'app-simple-sortable',
|
6 | 13 | changeDetection: ChangeDetectionStrategy.OnPush,
|
7 | 14 | 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"> |
15 | 25 | <pre>{{ render.data.name | json }}</pre>
|
16 | 26 | </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> |
31 | 29 | `,
|
32 | 30 | styles: [`
|
33 | 31 | .flex { display: flex; }
|
34 | 32 | .flex > * { flex-grow: 1; flex-shrink: 0; min-width: 0; }
|
35 | 33 | .card { min-width: 0; width: 100%; max-width: 300px; margin: 4px; border: 1px dashed #333; }
|
| 34 | + .card { cursor: move; } |
36 | 35 | pre { margin: 0; }
|
37 |
| - .dragging pre { opacity: 0.4; background: #eee; } |
| 36 | + .placeholder pre { opacity: 0.4; background: #eee; } |
38 | 37 | .list--right pre { color: #a70000; }
|
39 | 38 | .list--right pre { background: rgba(255, 0, 0, 0.1); }
|
40 | 39 | `]
|
41 | 40 | })
|
42 | 41 | export class SimpleComponent {
|
43 | 42 |
|
44 |
| - list = [ |
| 43 | + list: SimpleData[] = [ |
45 | 44 | { id: 1, name: faker.name.firstName() },
|
46 | 45 | { id: 2, name: faker.name.firstName() },
|
47 | 46 | { id: 3, name: faker.name.firstName() },
|
48 | 47 | { id: 4, name: faker.name.firstName() },
|
49 | 48 | { id: 5, name: faker.name.firstName() },
|
50 | 49 | ];
|
51 | 50 |
|
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 | + |
57 | 75 | }
|
0 commit comments