Skip to content

Commit 05b97a3

Browse files
committed
Always check to see if item is in destination list before adding.
1 parent 040341c commit 05b97a3

File tree

4 files changed

+4451
-36
lines changed

4 files changed

+4451
-36
lines changed

lib/dual-list.component.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ export class DualListComponent implements DoCheck, OnChanges {
107107
}
108108

109109
buildAvailable(source:Array<any>) : boolean {
110-
let sourceChanges = this.sourceDiffer.diff(source);
110+
const sourceChanges = this.sourceDiffer.diff(source);
111111
if (sourceChanges) {
112112
sourceChanges.forEachRemovedItem((r:any) => {
113-
let idx = this.findItemIndex(this.available.list, r.item, this.key);
113+
const idx = this.findItemIndex(this.available.list, r.item, this.key);
114114
if (idx !== -1) {
115115
this.available.list.splice(idx, 1);
116116
}
@@ -135,10 +135,10 @@ export class DualListComponent implements DoCheck, OnChanges {
135135

136136
buildConfirmed(destination:Array<any>) : boolean {
137137
let moved = false;
138-
let destChanges = this.destinationDiffer.diff(destination);
138+
const destChanges = this.destinationDiffer.diff(destination);
139139
if (destChanges) {
140140
destChanges.forEachRemovedItem((r:any) => {
141-
let idx = this.findItemIndex(this.confirmed.list, r.item, this.key);
141+
const idx = this.findItemIndex(this.confirmed.list, r.item, this.key);
142142
if (idx !== -1) {
143143
if (!this.isItemSelected(this.confirmed.pick, this.confirmed.list[idx])) {
144144
this.selectItem(this.confirmed.pick, this.confirmed.list[idx]);
@@ -149,7 +149,7 @@ export class DualListComponent implements DoCheck, OnChanges {
149149
});
150150

151151
destChanges.forEachAddedItem((r:any) => {
152-
let idx = this.findItemIndex(this.available.list, r.item, this.key);
152+
const idx = this.findItemIndex(this.available.list, r.item, this.key);
153153
if (idx !== -1) {
154154
if (!this.isItemSelected(this.available.pick, this.available.list[idx])) {
155155
this.selectItem(this.available.pick, this.available.list[idx]);
@@ -227,9 +227,9 @@ export class DualListComponent implements DoCheck, OnChanges {
227227
this.dragLeave();
228228
this.dragEnd();
229229

230-
let id = event.dataTransfer.getData('text');
230+
const id = event.dataTransfer.getData('text');
231231

232-
let mv = list.list.filter( (e:any) => e._id === id );
232+
const mv = list.list.filter( (e:any) => e._id === id );
233233
if (mv.length > 0) {
234234
for (let i = 0, len = mv.length; i < len; i += 1) {
235235
list.pick.push( mv[i] );
@@ -248,7 +248,7 @@ export class DualListComponent implements DoCheck, OnChanges {
248248
// Clear removed items.
249249
let pos = this.destination.length;
250250
while ((pos -= 1) >= 0) {
251-
let mv = this.confirmed.list.filter( conf => {
251+
const mv = this.confirmed.list.filter( conf => {
252252
if (typeof this.destination[pos] === 'object') {
253253
return conf._id === this.destination[pos][this.key];
254254
} else {
@@ -325,7 +325,7 @@ export class DualListComponent implements DoCheck, OnChanges {
325325
}
326326

327327
private makeUnavailable(source:BasicList, item:any) {
328-
let idx = source.list.indexOf(item);
328+
const idx = source.list.indexOf(item);
329329
if (idx !== -1) {
330330
source.list.splice(idx, 1);
331331
}
@@ -344,7 +344,7 @@ export class DualListComponent implements DoCheck, OnChanges {
344344
// Is the pick still in list?
345345
let mv:Array<any> = [];
346346
if (item) {
347-
let idx = this.findItemIndex(source.pick, item);
347+
const idx = this.findItemIndex(source.pick, item);
348348
if (idx !== -1) {
349349
mv[0] = source.pick[idx];
350350
}
@@ -356,14 +356,9 @@ export class DualListComponent implements DoCheck, OnChanges {
356356

357357
// Should only ever be 1
358358
if (mv.length === 1) {
359-
// Move if item wasn't already moved by drag-and-drop.
360-
if (item && item._id === mv[0]._id) {
359+
// Add if not already in target.
360+
if ( target.list.filter( trg => { return trg._id === mv[0]._id; }).length === 0) {
361361
target.list.push( mv[0] );
362-
} else {
363-
// see if it is already in target?
364-
if ( target.list.filter( trg => { return trg._id === mv[0]._id; }).length === 0) {
365-
target.list.push( mv[0] );
366-
}
367362
}
368363

369364
this.makeUnavailable(source, mv[0]);
@@ -397,7 +392,7 @@ export class DualListComponent implements DoCheck, OnChanges {
397392

398393
shiftClick(event:MouseEvent, index:number, source:BasicList, item:any) {
399394
if (event.shiftKey && source.last && !Object.is(item, source.last)) {
400-
let idx = source.sift.indexOf(source.last);
395+
const idx = source.sift.indexOf(source.last);
401396
if (index > idx) {
402397
for (let i = (idx + 1); i < index; i += 1) {
403398
this.selectItem(source.pick, source.sift[i]);
@@ -412,13 +407,13 @@ export class DualListComponent implements DoCheck, OnChanges {
412407
}
413408

414409
selectItem(list:Array<any>, item:any) {
415-
let pk = list.filter( (e:any) => {
410+
const pk = list.filter( (e:any) => {
416411
return Object.is(e, item);
417412
});
418413
if (pk.length > 0) {
419414
// Already in list, so deselect.
420415
for (let i = 0, len = pk.length; i < len; i += 1) {
421-
let idx = list.indexOf(pk[i]);
416+
const idx = list.indexOf(pk[i]);
422417
if (idx !== -1) {
423418
list.splice(idx, 1);
424419
}
@@ -468,7 +463,7 @@ export class DualListComponent implements DoCheck, OnChanges {
468463

469464
onFilter(source:BasicList) {
470465
if (source.picker.length > 0) {
471-
let filtered = source.list.filter( (item:any) => {
466+
const filtered = source.list.filter( (item:any) => {
472467
if (Object.prototype.toString.call(item) === '[object Object]') {
473468
if (item._name !== undefined) {
474469
return item._name.toLowerCase().indexOf(source.picker.toLowerCase()) !== -1;
@@ -530,13 +525,13 @@ export class DualListComponent implements DoCheck, OnChanges {
530525

531526
} else {
532527
// Complex, some action needs to be performed
533-
let parts = this.display[i].split('.');
528+
const parts = this.display[i].split('.');
534529

535-
let s = item[parts[0]];
530+
const s = item[parts[0]];
536531
if (s) {
537532
// Use brute force
538533
if (parts[1].indexOf('substring') !== -1) {
539-
let nums = (parts[1].substring(parts[1].indexOf('(') + 1, parts[1].indexOf(')'))).split(',');
534+
const nums = (parts[1].substring(parts[1].indexOf('(') + 1, parts[1].indexOf(')'))).split(',');
540535

541536
switch (nums.length) {
542537
case 1:

0 commit comments

Comments
 (0)