-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathdemo-app.component.ts
348 lines (311 loc) · 9.79 KB
/
demo-app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { Component, OnInit } from '@angular/core';
import { DualListComponent } from 'angular-dual-listbox';
@Component({
selector: 'app-demo',
styles: [ 'form { margin-top: 15px; }', '.checkbox { margin-top: inherit; }', 'ul.nav-tabs { cursor: pointer; }' ],
template: `
<div class="container-fluid">
<p></p>
<dual-list [sort]="keepSorted" [source]="source" [key]="key" [display]="display" [filter]="filter"
[(destination)]="confirmed" height="265px" [format]="format" [disabled]="disabled"></dual-list>
<ul class="nav nav-tabs" style="margin-top:50px;">
<li [class.active]="tab===1"><a (click)="tab=1">Arrays</a><li>
<li [class.active]="tab===2"><a (click)="tab=2">Format</a></li>
<li [class.active]="tab===3"><a (click)="tab=3">Programmatic changes</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" [class.active]="tab===1">
<div class="row">
<div class="col-sm-6" style="margin-top:32px;"><h4>Source</h4><pre><small>{{source|json}}</small></pre></div>
<div class="col-sm-6" style="margin-top:32px;"><h4>Confirmed</h4><pre><small>{{confirmed|json}}</small></pre></div>
</div>
</div>
<div class="tab-pane" [class.active]="tab===2">
<form class="form">
<div class="form-group">
<label style="display:block;">Direction</label>
<div class="btn-group">
<button type="button" class="btn" [ngClass]="{ 'btn-primary' : sourceLeft, 'btn-default' : !sourceLeft }"
(click)="swapDirection()">left-to-right</button>
<button type="button" class="btn" [ngClass]="{ 'btn-primary' : !sourceLeft, 'btn-default' : sourceLeft }"
(click)="swapDirection()">right-to-left</button>
</div>
</div>
<div class="form-group">
<label>Enable drag-and-drop</label>
<div class="checkbox">
<label><input type="checkbox" [(ngModel)]="format.draggable" name="drag">draggable</label>
</div>
</div>
<div class="form-group">
<label>Locale</label>
<input class="form-control" [(ngModel)]="format.locale" name="locale">
</div>
<div class="form-group">
<label>Add button</label>
<input class="form-control" [(ngModel)]="format.add" name="addBtn">
</div>
<div class="form-group">
<label>Remove button</label>
<input class="form-control" [(ngModel)]="format.remove" name="rmBtn">
</div>
<div class="form-group">
<label>All button</label>
<input class="form-control" [(ngModel)]="format.all" name="allBtn">
</div>
<div class="form-group">
<label>None button</label>
<input class="form-control" [(ngModel)]="format.none" name="noneBtn">
</div>
</form>
</div>
<div class="tab-pane" [class.active]="tab===3">
<div class="row" style="margin-top:20px;">
<div class="col-sm-6">
<label>Modify parent's source</label>
<form class="form-inline well">
<input class="form-control col-sm-1" style="margin-right:4px;" [(ngModel)]="userAdd" name="userAdd">
<button class="btn btn-success" (click)="doCreate()" [disabled]="!userAdd.length">Create</button>
<button class="btn btn-danger" (click)="doDelete()">Delete</button>
</form>
</div>
<div class="col-sm-6">
<label>Modify parent's confirmed</label>
<form class="form-inline well">
<button class="btn btn-default" (click)="doAdd()">Add</button>
<button class="btn btn-default" (click)="doRemove()">Remove</button>
</form>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<label>Array source</label>
<form class="form well">
<div class="radio" *ngFor="let item of arrayType">
<label>
<input type="radio" name="sourceType" [value]="item.value" [(ngModel)]="type" (change)="swapSource()">
{{item.name}} — {{item.detail}}
</label>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<label>General</label><br/>
<form class="form-inline well">
<button class="btn btn-default" (click)="doFilter()">{{filterBtn()}}</button>
<button class="btn btn-default" (click)="doDisable()">{{disableBtn()}}</button>
<button class="btn btn-primary" (click)="doReset()">Reset</button>
</form>
</div>
</div>
</div>
</div>
</div>
`
})
export class DemoAppComponent implements OnInit {
tab = 1;
keepSorted = true;
key: string;
display: any;
filter = false;
source: Array<any>;
confirmed: Array<any>;
userAdd = '';
disabled = false;
sourceLeft = true;
format: any = DualListComponent.DEFAULT_FORMAT;
private sourceTube: Array<string>;
private sourceStations: Array<any>;
private sourceChessmen: Array<any>;
private confirmedTube: Array<string>;
private confirmedStations: Array<any>;
private confirmedChessmen: Array<any>;
arrayType = [
{ name: 'Rio Grande', detail: '(object array)', value: 'station' },
{ name: 'Chessmen', detail: '(object array)', value: 'chess' },
{ name: 'Underground', detail: '(string array)', value: 'tube' }
];
type = this.arrayType[0].value;
private stations: Array<any> = [
{ key: 1, station: 'Antonito', state: 'CO' },
{ key: 2, station: 'Big Horn', state: 'NM' },
{ key: 3, station: 'Sublette', state: 'NM' },
{ key: 4, station: 'Toltec', state: 'NM' },
{ key: 5, station: 'Osier', state: 'CO' },
{ key: 6, station: 'Chama', state: 'NM'},
{ key: 7, station: 'Monero', state: 'NM' },
{ key: 8, station: 'Lumberton', state: 'NM' },
{ key: 9, station: 'Duice', state: 'NM' },
{ key: 10, station: 'Navajo', state: 'NM' },
{ key: 11, station: 'Juanita', state: 'CO' },
{ key: 12, station: 'Pagosa Jct', state: 'CO' },
{ key: 13, station: 'Carracha', state: 'CO' },
{ key: 14, station: 'Arboles', state: 'CO' },
{ key: 15, station: 'Solidad', state: 'CO' },
{ key: 16, station: 'Tiffany', state: 'CO' },
{ key: 17, station: 'La Boca', state: 'CO' },
{ key: 18, station: 'Ignacio', state: 'CO' },
{ key: 19, station: 'Oxford', state: 'CO' },
{ key: 20, station: 'Florida', state: 'CO' },
{ key: 21, station: 'Bocea', state: 'CO' },
{ key: 22, station: 'Carbon Jct', state: 'CO' },
{ key: 23, station: 'Durango', state: 'CO' },
{ key: 24, station: 'Home Ranch', state: 'CO' },
{ key: 25, station: 'Trimble Springs', state: 'CO' },
{ key: 26, station: 'Hermosa', state: 'CO' },
{ key: 27, station: 'Rockwood', state: 'CO' },
{ key: 28, station: 'Tacoma', state: 'CO' },
{ key: 29, station: 'Needleton', state: 'CO' },
{ key: 30, station: 'Elk Park', state: 'CO' },
{ key: 31, station: 'Silverton', state: 'CO' },
{ key: 32, station: 'Eureka', state: 'CO' }
];
private chessmen: Array<any> = [
{ _id: 1, name: 'Pawn' },
{ _id: 2, name: 'Rook' },
{ _id: 3, name: 'Knight' },
{ _id: 4, name: 'Bishop' },
{ _id: 5, name: 'Queen' },
{ _id: 6, name: 'King' }
];
private tube: Array<string> = [
'Harrow & Wealdstone',
'Kenton',
'South Kenton',
'North Wembley',
'Wembley Central',
'Stonebridge Park',
'Harlesden',
'Willesden Junction',
'Kensal Green',
"Queen's Park",
'Kilburn Park',
'Maida Vale',
'Warwick Avenue',
'Paddington',
'Edgware Road',
'Marylebone',
'Baker Street',
"Regent's Park",
'Oxford Circus',
'Piccadilly Circus',
'Charing Cross',
'Embankment',
'Waterloo',
'Lambeth North',
'Elephant & Castle'
];
ngOnInit() {
this.doReset();
}
private stationLabel(item: any) {
return item.station + ', ' + item.state;
}
private useStations() {
this.key = 'key';
this.display = this.stationLabel;
this.keepSorted = true;
this.source = this.sourceStations;
this.confirmed = this.confirmedStations;
}
private useChessmen() {
this.key = '_id';
this.display = 'name';
this.keepSorted = false;
this.source = this.sourceChessmen;
this.confirmed = this.confirmedChessmen;
}
private useTube() {
this.key = undefined;
this.display = undefined;
this.keepSorted = false;
this.source = this.sourceTube;
this.confirmed = this.confirmedTube;
}
swapSource() {
switch (this.type) {
case this.arrayType[0].value:
this.useStations();
break;
case this.arrayType[1].value:
this.useChessmen();
break;
case this.arrayType[2].value:
this.useTube();
break;
}
}
doReset() {
this.sourceChessmen = JSON.parse(JSON.stringify(this.chessmen));
this.sourceStations = JSON.parse(JSON.stringify(this.stations));
this.sourceTube = JSON.parse(JSON.stringify(this.tube));
this.confirmedChessmen = new Array<any>();
this.confirmedStations = new Array<any>();
this.confirmedTube = new Array<string>();
// Preconfirm some items.
this.confirmedStations.push( this.stations[31] );
this.confirmedTube.push( this.tube[13] );
this.confirmedTube.push( this.tube[23] );
switch (this.type) {
case this.arrayType[0].value:
this.useStations();
break;
case this.arrayType[1].value:
this.useChessmen();
break;
case this.arrayType[2].value:
this.useTube();
break;
}
}
doDelete() {
if (this.source.length > 0) {
this.source.splice(0, 1);
}
}
doCreate() {
if (typeof this.source[0] === 'object') {
const o = {};
o[this.key] = this.source.length + 1;
o[this.display] = this.userAdd;
this.source.push( o );
} else {
this.source.push(this.userAdd);
}
this.userAdd = '';
}
doAdd() {
for (let i = 0, len = this.source.length; i < len; i += 1) {
const o = this.source[i];
const found = this.confirmed.find( (e: any) => e === o );
if (!found) {
this.confirmed.push(o);
break;
}
}
}
doRemove() {
if (this.confirmed.length > 0) {
this.confirmed.splice(0, 1);
}
}
doFilter() {
this.filter = !this.filter;
}
filterBtn() {
return (this.filter ? 'Hide Filter' : 'Show Filter');
}
doDisable() {
this.disabled = !this.disabled;
}
disableBtn() {
return (this.disabled ? 'Enable' : 'Disabled');
}
swapDirection() {
this.sourceLeft = !this.sourceLeft;
this.format.direction = this.sourceLeft ? DualListComponent.LTR : DualListComponent.RTL;
}
}