summaryrefslogtreecommitdiffstats
path: root/js/bootstrap-strength-meter.js
blob: f0256d0aa028fc34ff56783d8d828d865afdb33a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * bootstrap-strength-meter.js
 * https://github.com/davidstutz/bootstrap-strength-meter
 *
 * Copyright 2013 David Stutz
 */
!function($) {
    
    "use strict";// jshint ;_;

    var StrengthMeter = function(input, options) {
        
        var defaults = {
            container: input.parent(),
            base: 100,
            hierarchy: {
                '0': 'progress-bar-danger',
                '50': 'progress-bar-warning',
                '80': 'progress-bar-success'
            },
            dictionaries: [
                
            ],
            keyboards: [
                
            ]
        },
            
            settings = $.extend({}, defaults, options),
            
            template = '<div class="progress"><div class="progress-bar" role="progressbar" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0"></div></div>',
            
            progress,
            
            progressBar,
            
            methods = {
                
            },
                    
            core = {
                
                /**
                 * Initialize the plugin.
                 */
                init: function() {
                    input.on('keyup', core.keyup);
                    progress = settings.container.append($(template));
                    progressBar = $('.progress-bar', progress);
                },
                
                /**
                 * Update progress abr accordning 
                 */
                update: function(value) {
                    var width = Math.floor(value/settings.base*100);
                    
                    if (width > 100) {
                        width = 100;
                    }
                    
                    progressBar
                            .attr('area-valuenow', width)
                            .css('width', width + '%');
                    
                    for (var value in settings.hierarchy) {
                        if (width > value) {
                            progressBar
                                    .removeClass()
                                    .addClass('progress-bar')
                                    .addClass(settings.hierarchy[value]);
                        }
                    }
                },
                
                /**
                 * Binding on keydown for updateing the progrssbar.
                 */
                keyup: function(event) {
                    var password = $(event.target).val()
                    var value = 0;
                    
                    if (password.length > 0) {
                        var score = new Score(password);
                        value = score.calculateEntropyScore(core.getScoreOptions());
                    }
                    
                    core.update(value);
                },
                
                /**
                 * Get options for score.
                 */
                getScoreOptions: function() {
                    var options = [];
                    
                    for (var i = 0; i < settings.dictionaries.length; i++) {
                        options[options.length] = {
                            type: 'dictionary',
                            dictionary: settings.dictionaries[i]
                        };
                
                        options[options.length] = {
                            type: 'leet',
                            dictionary: settings.dictionaries[i]
                        };
                    }
                    
                    for (var i = 0; i < settings.keyboards.length; i++) {
                        options[options.length] = {
                            type: 'keyboard',
                            dictionary: settings.keyboards[i]
                        };
                    }
                    
                    options[options.length] = {
                        type: 'repitition'
                    };
                    
                    options[options.length] = {
                        type: 'sequences'
                    };
                    
                    options[options.length] = {
                        type: 'dates'
                    };
                    
                    return options;
                }
            };
        
        core.init();
        
        return methods;
    }
    
    $.fn.strengthMeter = function(options) {
        var instance = this.data('strengthMeter'),
            elem = this;
        
        return elem.each(function() {
            var strengthMeter;
            
            if (instance) {
                return;
            }
            
            strengthMeter = new StrengthMeter(elem, options);
            elem.data('strengthMeter', strengthMeter);
        });
    };
    
}(window.jQuery);