summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/invalid-characters/invalid-characters.ts
blob: 20ed71e53fca58ecfae092fd2b897004f13303a3 (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
'use strict';

export class InvalidCharactersDirective implements ng.IDirective {

    constructor() {
    }

    require = 'ngModel';

    link = (scope, elem, attrs, ngModel) => {

        let invalidCharacters = [];

        attrs.$observe('invalidCharacters', (val:string) => {
            invalidCharacters = val.split('');
            validate(ngModel.$viewValue);
        });

        let validate:Function = function (value) {

            let valid:boolean = true;

            if (value) {
                for (let i = 0; i < invalidCharacters.length; i++) {
                    if (value.indexOf(invalidCharacters[i]) != -1) {
                        valid = false;
                    }
                }
            }

            ngModel.$setValidity('invalidCharacters', valid);
            if (!value) {
                ngModel.$setPristine();
            }
            return value;
        };

        //For DOM -> model validation
        ngModel.$parsers.unshift(validate);
        //For model -> DOM validation
        ngModel.$formatters.unshift(validate);

    };

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

}

InvalidCharactersDirective.factory.$inject = [];