aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/tag-cloud/tag-cloud.component.ts
blob: 1635b8d076e8cae3c94658849119a08a8b10b378 (plain)
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
import { Component, EventEmitter, Input, Output } from "@angular/core";
import template from "./tag-cloud.component.html";

@Component({
    selector: 'sdc-tag-cloud',
    template: template,
})
export class TagCloudComponent {
    @Input() public list: string[];
    @Input() public isViewOnly: boolean|number[]; // get a boolean parameter or array of specific items indexes.
    @Input() public isUniqueList: boolean;
    @Input() public uniqueErrorMessage: string = "Unique error";
    @Input() public label: string;
    @Input() public placeholder: string;
    @Output() public listChanged: EventEmitter<string[]> = new EventEmitter<string[]>();
    private newTagItem: string;
    private uniqueError: boolean;

    private onKeyup = (e): void => {
        if (e.keyCode === 13) {
            this.insertItemToList();
        }
    }

    private insertItemToList = (): void => {
        this.validateTag();
        if (!this.uniqueError && this.newTagItem.length) {
            this.list.push(this.newTagItem);
            this.newTagItem = "";
            this.listChanged.emit(this.list);
        }
    }

    private deleteItemFromList = (index: number): void => {
        this.list.splice(index, 1);
        if (Array.isArray(this.isViewOnly)) {
            this.isViewOnly = this.isViewOnly.map((i: number) => {
                return i > index ? i - 1 : i;
            });
        }
    }

    private validateTag = (): void => {
        this.uniqueError = this.list && this.list.indexOf(this.newTagItem) > -1;
    }
}