js/ui/popup/factories/debounce.js   B
last analyzed

Complexity

Conditions 1
Paths 2

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 2
nop 0
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.directive(ꞌngDebounceꞌ) 0 34 1
1
(function () {
2
    'use strict';
3
4
    /**
5
     * @ngdoc function
6
     * @name passmanApp.controller:MainCtrl
7
     * @description
8
     * # MainCtrl
9
     * Controller of the passmanApp
10
     */
11
    angular.module('passmanExtension').directive('ngDebounce', function ($timeout) {
12
        return {
13
            restrict: 'A',
14
            require: 'ngModel',
15
            priority: 99,
16
            link: function (scope, elm, attr, ngModelCtrl) {
17
                if (attr.type === 'radio' || attr.type === 'checkbox') {
18
                    return;
19
                }
20
21
                var delay = parseInt(attr.ngDebounce, 10);
22
                if (isNaN(delay)) {
23
                    delay = 1000;
24
                }
25
26
                elm.unbind('input');
27
28
                var debounce;
29
                elm.bind('input', function () {
30
                    $timeout.cancel(debounce);
31
                    debounce = $timeout(function () {
32
                        scope.$apply(function () {
33
                            ngModelCtrl.$setViewValue(elm.val());
34
                        });
35
                    }, delay);
36
                });
37
                elm.bind('blur', function () {
38
                    scope.$apply(function () {
39
                        ngModelCtrl.$setViewValue(elm.val());
40
                    });
41
                });
42
            }
43
        };
44
    });
45
}());
46
47