aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/prevent-double-click/prevent-double-click.ts
blob: fff25c4d9c3a996f77d7d5a7ba211147f36eb503 (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
/**
 * Created by ob0695 on 5/15/2018.
 */
'use strict';

export class PreventDoubleClickDirective implements ng.IDirective {

    constructor(private $timeout:ng.ITimeoutService) {
    }

    restrict:'A';

    link = (scope, elem) => {

        let delay = 600;
        let disabled = false;

        scope.onClick = (evt) => {
            if (disabled) {
                evt.preventDefault();
                evt.stopImmediatePropagation();
            } else {
                disabled = true;
                this.$timeout(function () {
                    disabled = false;
                }, delay, false);
            }
        }

        scope.$on('$destroy', function () {
            elem.off('click', scope.onClick);
        });
        elem.on('click', scope.onClick);
    };

    public static factory = ($timeout:ng.ITimeoutService) => {
        return new PreventDoubleClickDirective($timeout);
    }
}

PreventDoubleClickDirective.factory.$inject = ['$timeout'];