Skip to content

Commit 7b4f5cf

Browse files
committed
docs(examples): re-enable simple sortable, with many comments
1 parent 9fce342 commit 7b4f5cf

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

packages/angular-skyhook-card-list/src/card-renderer.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class CardRendererDirective<Data> implements OnInit, OnDestroy {
8181
},
8282
endDrag: monitor => {
8383
const item = monitor.getItem();
84-
if (item) {
84+
if (item && !monitor.didDrop()) {
8585
this.spec && this.spec.endDrag && this.spec.endDrag(item);
8686
}
8787
}

packages/examples/src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ <h3>Sortable</h3>
7878
<li>
7979
<a [routerLink]="['sortable', 'kanban']" routerLinkActive="active">Kanban board</a>
8080
</li>
81-
<!-- <li> -->
82-
<!-- <a [routerLink]="['sortable', 'simple']" routerLinkActive="active">Ad&#45;hoc sharing</a> -->
83-
<!-- </li> -->
81+
<li>
82+
<a [routerLink]="['sortable', 'simple']" routerLinkActive="active">Simple list</a>
83+
</li>
8484
</ul>
8585
</section>
8686
</nav>
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
import { Component, ViewChild, ElementRef } from "@angular/core";
2-
import { SkyhookDndService } from "angular-skyhook";
3-
import { DraggedItem, SharedSortableService, Size } from "angular-skyhook-card-list";
1+
import { Component } from "@angular/core";
42

53
@Component({
64
selector: 'simple-sortable-container',
75
template:
86
`
7+
<p> Note: uses unreleased code. </p>
8+
9+
<p> This example is like the 'Basic Sortable', except you don't have to
10+
write a complicated hover function. You can focus on the model data. </p>
11+
912
<app-simple-sortable></app-simple-sortable>
1013
`
1114
})
1215
export class ContainerComponent {
13-
id = 1000;
14-
source = this.dnd.dragSource<DraggedItem<any>>("SIMPLE", {
15-
beginDrag: monitor => {
16-
return {
17-
data: { id: this.id++, name: "whatever" },
18-
type: "SIMPLE",
19-
index: 0,
20-
listId: "somewhere EXTERNAL",
21-
size: new Size(0,0),
22-
hover: { index: 0, listId: "somewhere EXTERNAL" }
23-
}
24-
}
25-
});
26-
constructor(private dnd: SkyhookDndService) { }
2716
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.flex { display: flex; }
2+
.flex > * { flex-grow: 1; flex-shrink: 0; min-width: 0; }
3+
.card { min-width: 0; width: 100%; max-width: 300px; margin: 4px; border: 1px dashed #333; }
4+
.card { cursor: move; }
5+
pre { margin: 0; }
6+
.placeholder pre { opacity: 0.4; background: #eee; }
7+
.list--right pre { color: #a70000; }
8+
.list--right pre { background: rgba(255, 0, 0, 0.1); }

packages/examples/src/app/sortable/simple/simple.component.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Component, ChangeDetectionStrategy } from "@angular/core";
1+
import { Component } from "@angular/core";
22
import * as faker from 'faker';
33
import { SortableSpec, DraggedItem } from "angular-skyhook-card-list";
4-
import { BehaviorSubject, combineLatest } from "rxjs";
4+
import { BehaviorSubject } from "rxjs";
55

66
interface SimpleData {
77
id: number;
@@ -10,36 +10,31 @@ interface SimpleData {
1010

1111
@Component({
1212
selector: 'app-simple-sortable',
13-
changeDetection: ChangeDetectionStrategy.OnPush,
13+
styleUrls: ['./simple.component.scss'],
1414
template: `
15+
<!-- this thing will make a list for you, using simpleSpec.getList(), and the cardTemplate provided. -->
1516
<skyhook-card-list class="list"
1617
cardListType="SIMPLE"
1718
cardListId="simple-demo"
1819
[cardListSpec]="simpleSpec">
20+
1921
<ng-template cardTemplate let-context>
22+
<!-- cardRenderer configures a DragSource for you, but you have to attach it. -->
2023
<div class="card"
2124
[cardRenderer]="context"
2225
#render="cardRenderer"
2326
[class.placeholder]="render.isDragging$|async"
24-
[dragSource]="render.source">
27+
[dragSource]="render.source"> <!-- <<< attached here! -->
28+
2529
<pre>{{ render.data.name | json }}</pre>
2630
</div>
2731
</ng-template>
2832
</skyhook-card-list>
2933
`,
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-
`]
4034
})
4135
export class SimpleComponent {
4236

37+
// you need data types that have a unique value, like SimpleData.id
4338
list: SimpleData[] = [
4439
{ id: 1, name: faker.name.firstName() },
4540
{ id: 2, name: faker.name.firstName() },
@@ -48,26 +43,39 @@ export class SimpleComponent {
4843
{ id: 5, name: faker.name.firstName() },
4944
];
5045

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.
5149
list$ = new BehaviorSubject(this.list);
5250

5351
move(item: DraggedItem<SimpleData>) {
52+
// shallow clone the list
53+
// do this so we can avoid overwriting our 'saved' list.
5454
const temp = this.list.slice(0);
55+
// delete where it was previously
5556
temp.splice(item.index, 1);
57+
// add it back in at the new location
5658
temp.splice(item.hover.index, 0, item.data);
57-
this.list$.next(temp);
5859
return temp;
5960
}
6061

6162
simpleSpec: SortableSpec<SimpleData> = {
63+
// required.
6264
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$,
6468
hover: (item) => {
65-
this.move(item);
69+
// fire off a new list$ but don't save yet
70+
this.list$.next(this.move(item));
6671
},
6772
drop: (item) => {
73+
// 'save the changes'
6874
this.list = this.move(item);
75+
this.list$.next(this.list);
6976
},
7077
endDrag: item => {
78+
// revert
7179
this.list$.next(this.list);
7280
}
7381
}

0 commit comments

Comments
 (0)