1
- import { Component , ChangeDetectionStrategy } from "@angular/core" ;
1
+ import { Component } from "@angular/core" ;
2
2
import * as faker from 'faker' ;
3
3
import { SortableSpec , DraggedItem } from "angular-skyhook-card-list" ;
4
- import { BehaviorSubject , combineLatest } from "rxjs" ;
4
+ import { BehaviorSubject } from "rxjs" ;
5
5
6
6
interface SimpleData {
7
7
id : number ;
@@ -10,36 +10,31 @@ interface SimpleData {
10
10
11
11
@Component ( {
12
12
selector : 'app-simple-sortable' ,
13
- changeDetection : ChangeDetectionStrategy . OnPush ,
13
+ styleUrls : [ './simple.component.scss' ] ,
14
14
template : `
15
+ <!-- this thing will make a list for you, using simpleSpec.getList(), and the cardTemplate provided. -->
15
16
<skyhook-card-list class="list"
16
17
cardListType="SIMPLE"
17
18
cardListId="simple-demo"
18
19
[cardListSpec]="simpleSpec">
20
+
19
21
<ng-template cardTemplate let-context>
22
+ <!-- cardRenderer configures a DragSource for you, but you have to attach it. -->
20
23
<div class="card"
21
24
[cardRenderer]="context"
22
25
#render="cardRenderer"
23
26
[class.placeholder]="render.isDragging$|async"
24
- [dragSource]="render.source">
27
+ [dragSource]="render.source"> <!-- <<< attached here! -->
28
+
25
29
<pre>{{ render.data.name | json }}</pre>
26
30
</div>
27
31
</ng-template>
28
32
</skyhook-card-list>
29
33
` ,
30
- styles : [ `
31
- .flex { display: flex; }
32
- .flex > * { flex-grow: 1; flex-shrink: 0; min-width: 0; }
33
- .card { min-width: 0; width: 100%; max-width: 300px; margin: 4px; border: 1px dashed #333; }
34
- .card { cursor: move; }
35
- pre { margin: 0; }
36
- .placeholder pre { opacity: 0.4; background: #eee; }
37
- .list--right pre { color: #a70000; }
38
- .list--right pre { background: rgba(255, 0, 0, 0.1); }
39
- ` ]
40
34
} )
41
35
export class SimpleComponent {
42
36
37
+ // you need data types that have a unique value, like SimpleData.id
43
38
list : SimpleData [ ] = [
44
39
{ id : 1 , name : faker . name . firstName ( ) } ,
45
40
{ id : 2 , name : faker . name . firstName ( ) } ,
@@ -48,26 +43,39 @@ export class SimpleComponent {
48
43
{ id : 5 , name : faker . name . firstName ( ) } ,
49
44
] ;
50
45
46
+ // BehaviorSubject is great for simple pieces of changing state.
47
+ // It will happily replay the latest value to new subscribers, and behaves a bit
48
+ // like an @ngrx /store does.
51
49
list$ = new BehaviorSubject ( this . list ) ;
52
50
53
51
move ( item : DraggedItem < SimpleData > ) {
52
+ // shallow clone the list
53
+ // do this so we can avoid overwriting our 'saved' list.
54
54
const temp = this . list . slice ( 0 ) ;
55
+ // delete where it was previously
55
56
temp . splice ( item . index , 1 ) ;
57
+ // add it back in at the new location
56
58
temp . splice ( item . hover . index , 0 , item . data ) ;
57
- this . list$ . next ( temp ) ;
58
59
return temp ;
59
60
}
60
61
61
62
simpleSpec : SortableSpec < SimpleData > = {
63
+ // required.
62
64
trackBy : x => x . id ,
63
- getList : _ => this . list$ ,
65
+ // required. MUST return an Observable.
66
+ // conceptually, this.list$ is in flux, but this.list is the 'saved' version.
67
+ getList : _listId => this . list$ ,
64
68
hover : ( item ) => {
65
- this . move ( item ) ;
69
+ // fire off a new list$ but don't save yet
70
+ this . list$ . next ( this . move ( item ) ) ;
66
71
} ,
67
72
drop : ( item ) => {
73
+ // 'save the changes'
68
74
this . list = this . move ( item ) ;
75
+ this . list$ . next ( this . list ) ;
69
76
} ,
70
77
endDrag : item => {
78
+ // revert
71
79
this . list$ . next ( this . list ) ;
72
80
}
73
81
}
0 commit comments