summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/utils/sdc-tags/sdc-tags.ts
blob: 082a77dd9fb911d31f26aee08fc1b418af989d90 (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
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
'use strict';

export interface ISdcTagsScope extends ng.IScope {
    tags:Array<string>;
    specialTag:string;
    newTag:string;
    formElement:ng.IFormController;
    elementName:string;
    pattern:any;
    sdcDisabled:boolean;
    maxTags:number;
    deleteTag(tag:string):void;
    addTag(tag:string):void;
    validateName():void;
}

export class SdcTagsDirective implements ng.IDirective {

    constructor() {
    }

    scope = {
        tags: '=',
        specialTag: '=',
        pattern: '=',
        sdcDisabled: '=',
        formElement: '=',
        elementName: '@',
        maxTags: '@'
    };

    public replace = false;
    public restrict = 'E';
    public transclude = false;

    template = ():string => {
        return require('./sdc-tags.html');
    };

    link = (scope:ISdcTagsScope, element:ng.INgModelController) => {

        scope.deleteTag = (tag:string):void => {
            scope.tags.splice(scope.tags.indexOf(tag), 1);
        };

        scope.addTag = ():void => {
            let valid = scope.formElement[scope.elementName].$valid;
            if (valid &&
                scope.tags.length < scope.maxTags &&
                scope.newTag &&
                scope.newTag !== '' &&
                scope.tags.indexOf(scope.newTag) === -1 &&
                scope.newTag !== scope.specialTag) {
                scope.tags.push(scope.newTag);
                scope.newTag = '';
            }
        };

        scope.validateName = ():void => {
            if (scope.tags.indexOf(scope.newTag) > -1) {
                scope.formElement[scope.elementName].$setValidity('nameExist', false);
            } else {
                scope.formElement[scope.elementName].$setValidity('nameExist', true);
            }
        }

    };

    public static factory = ()=> {
        return new SdcTagsDirective();
    };

}

SdcTagsDirective.factory.$inject = [];