re base code
[sdc.git] / catalog-ui / src / app / directives / prevent-double-click / prevent-double-click.ts
1 /**
2  * Created by ob0695 on 5/15/2018.
3  */
4 'use strict';
5
6 export class PreventDoubleClickDirective implements ng.IDirective {
7
8     constructor(private $timeout:ng.ITimeoutService) {
9     }
10
11     restrict:'A';
12
13     link = (scope, elem) => {
14
15         let delay = 600;
16         let disabled = false;
17
18         scope.onClick = (evt) => {
19             if (disabled) {
20                 evt.preventDefault();
21                 evt.stopImmediatePropagation();
22             } else {
23                 disabled = true;
24                 this.$timeout(function () {
25                     disabled = false;
26                 }, delay, false);
27             }
28         }
29
30         scope.$on('$destroy', function () {
31             elem.off('click', scope.onClick);
32         });
33         elem.on('click', scope.onClick);
34     };
35
36     public static factory = ($timeout:ng.ITimeoutService) => {
37         return new PreventDoubleClickDirective($timeout);
38     }
39 }
40
41 PreventDoubleClickDirective.factory.$inject = ['$timeout'];