Skip to content

Commit 914b979

Browse files
committed
Add try/catch around toLocaleLowerCase and allow function for display
1 parent e08a53f commit 914b979

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

lib/src/dual-list.component.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { Component, DoCheck, EventEmitter, Input, IterableDiffers, OnChanges,
32
Output, SimpleChange } from '@angular/core';
43

@@ -33,7 +32,7 @@ export class DualListComponent implements DoCheck, OnChanges {
3332

3433
@Input() id = `dual-list-${nextId++}`;
3534
@Input() key = '_id';
36-
@Input() display = '_name';
35+
@Input() display:any = '_name';
3736
@Input() height = '100px';
3837
@Input() filter = false;
3938
@Input() format = DualListComponent.DEFAULT_FORMAT;
@@ -208,7 +207,7 @@ export class DualListComponent implements DoCheck, OnChanges {
208207
return this.format.direction === DualListComponent.LTR;
209208
}
210209

211-
dragEnd(list:BasicList = null) {
210+
dragEnd(list:BasicList = null) : boolean {
212211
if (list) {
213212
list.dragStart = false;
214213
} else {
@@ -228,7 +227,7 @@ export class DualListComponent implements DoCheck, OnChanges {
228227
event.dataTransfer.setData(this.id, item['_id']);
229228
}
230229

231-
allowDrop(event:DragEvent, list:BasicList) {
230+
allowDrop(event:DragEvent, list:BasicList) : boolean {
232231
if (event.dataTransfer.types.length && (event.dataTransfer.types[0] === this.id)) {
233232
event.preventDefault();
234233
if (!list.dragStart) {
@@ -277,7 +276,6 @@ export class DualListComponent implements DoCheck, OnChanges {
277276
}
278277
}
279278

280-
281279
// Push added items.
282280
for (let i = 0, len = this.confirmed.list.length; i < len; i += 1) {
283281
let mv = this.destination.filter( (d:any) => {
@@ -398,7 +396,7 @@ export class DualListComponent implements DoCheck, OnChanges {
398396
}, 10);
399397
}
400398

401-
isItemSelected(list:Array<any>, item:any) {
399+
isItemSelected(list:Array<any>, item:any) : boolean {
402400
if (list.filter(e => Object.is(e, item)).length > 0) {
403401
return true;
404402
}
@@ -447,14 +445,14 @@ export class DualListComponent implements DoCheck, OnChanges {
447445
source.pick.length = 0;
448446
}
449447

450-
isAllSelected(source:BasicList) {
448+
isAllSelected(source:BasicList) : boolean {
451449
if (source.list.length === 0 || source.list.length === source.pick.length) {
452450
return true;
453451
}
454452
return false;
455453
}
456454

457-
isAnySelected(source:BasicList) {
455+
isAnySelected(source:BasicList) : boolean {
458456
if (source.pick.length > 0) {
459457
return true;
460458
}
@@ -478,22 +476,30 @@ export class DualListComponent implements DoCheck, OnChanges {
478476

479477
onFilter(source:BasicList) {
480478
if (source.picker.length > 0) {
481-
const filtered = source.list.filter( (item:any) => {
482-
if (Object.prototype.toString.call(item) === '[object Object]') {
483-
if (item._name !== undefined) {
484-
// @ts-ignore: remove when d.ts has locale as an argument.
485-
return item._name.toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
479+
try {
480+
const filtered = source.list.filter( (item:any) => {
481+
if (Object.prototype.toString.call(item) === '[object Object]') {
482+
if (item._name !== undefined) {
483+
// @ts-ignore: remove when d.ts has locale as an argument.
484+
return item._name.toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
485+
} else {
486+
// @ts-ignore: remove when d.ts has locale as an argument.
487+
return JSON.stringify(item).toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
488+
}
486489
} else {
487-
// @ts-ignore: remove when d.ts has locale as an argument.
488-
return JSON.stringify(item).toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
490+
// @ts-ignore: remove when d.ts has locale as an argument.
491+
return item.toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
489492
}
490-
} else {
491-
// @ts-ignore: remove when d.ts has locale as an argument.
492-
return item.toLocaleLowerCase(this.format.locale).indexOf(source.picker.toLocaleLowerCase(this.format.locale)) !== -1;
493+
});
494+
source.sift = filtered;
495+
this.unpick(source);
496+
}
497+
catch (e) {
498+
if (e instanceof RangeError) {
499+
this.format.locale = undefined;
493500
}
494-
});
495-
source.sift = filtered;
496-
this.unpick(source);
501+
source.sift = source.list;
502+
}
497503
} else {
498504
source.sift = source.list;
499505
}
@@ -530,17 +536,20 @@ export class DualListComponent implements DoCheck, OnChanges {
530536
let str = '';
531537

532538
if (this.display !== undefined) {
533-
if (Object.prototype.toString.call( this.display ) === '[object Array]' ) {
539+
switch (Object.prototype.toString.call(this.display)) {
540+
case '[object Function]':
541+
str = this.display(item);
542+
break;
534543

535-
for (let i = 0; i < this.display.length; i += 1) {
544+
case '[object Array]':
545+
for (let i = 0, len = this.display.length; i < len; i += 1) {
536546
if (str.length > 0) {
537547
str = str + separator;
538548
}
539549

540550
if (this.display[i].indexOf('.') === -1) {
541551
// Simple, just add to string.
542552
str = str + item[this.display[i]];
543-
544553
} else {
545554
// Complex, some action needs to be performed
546555
const parts = this.display[i].split('.');
@@ -569,12 +578,15 @@ export class DualListComponent implements DoCheck, OnChanges {
569578
}
570579
}
571580
}
572-
return str;
573-
} else {
574-
return fallback(item);
581+
break;
582+
default:
583+
str = fallback(item);
584+
break;
575585
}
586+
} else {
587+
str = fallback(item);
576588
}
577589

578-
return fallback(item);
590+
return str;
579591
}
580592
}

0 commit comments

Comments
 (0)