diff options
Diffstat (limited to 'js/bootstrap-strength-meter.js')
-rw-r--r-- | js/bootstrap-strength-meter.js | 376 |
1 files changed, 259 insertions, 117 deletions
diff --git a/js/bootstrap-strength-meter.js b/js/bootstrap-strength-meter.js index f0256d0..e975ffc 100644 --- a/js/bootstrap-strength-meter.js +++ b/js/bootstrap-strength-meter.js @@ -7,134 +7,276 @@ !function($) { "use strict";// jshint ;_; + + var StrengthMeter = { + core: { + /** + * Get options for score. + */ + getScoreOptions: function(settings) { + var options = []; - 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] - }; - } - + for (var i = 0; i < settings.dictionaries.length; i++) { options[options.length] = { - type: 'repitition' + type: 'dictionary', + dictionary: settings.dictionaries[i] }; - + options[options.length] = { - type: 'sequences' + type: 'leet', + dictionary: settings.dictionaries[i] }; - + } + + for (var i = 0; i < settings.keyboards.length; i++) { options[options.length] = { - type: 'dates' + type: 'keyboard', + dictionary: settings.keyboards[i] }; - - return options; } - }; + + options[options.length] = { + type: 'repitition' + }; + + options[options.length] = { + type: 'sequences' + }; + + options[options.length] = { + type: 'dates' + }; + + return options; + } + }, - core.init(); + progressBar: function(input, options) { - return methods; + var defaults = { + container: input.parent(), + base: 120, + hierarchy: { + '0': 'progress-bar-danger', + '40': 'progress-bar-warning', + '60': '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, + + core = { + + /** + * Initialize the plugin. + */ + init: function() { + progress = settings.container.append($(template)); + progressBar = $('.progress-bar', progress); + input.on('keyup', core.keyup) + .keyup(); + }, + + /** + * 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(StrengthMeter.core.getScoreOptions(settings)); + } + + core.update(value); + }, + }; + + core.init(); + }, + + text: function(input, options) { + + var defaults = { + container: input.parent(), + hierarchy: { + '0': ['text-danger', 'ridiculus'], + '10': ['text-danger', 'very weak'], + '30': ['text-warning', 'weak'], + '50': ['text-warning', 'good'], + '70': ['text-success', 'strong'], + '100': ['text-success', 'very strong'], + }, + dictionaries: [ + + ], + keyboards: [ + + ] + }, + + settings = $.extend({}, defaults, options), + + core = { + + /** + * Initialize the plugin. + */ + init: function() { + input.on('keyup', core.keyup) + .keyup(); + }, + + /** + * Update progress abr accordning + */ + update: function(value) { + for (var border in settings.hierarchy) { + if (value >= border) { + var text = settings.hierarchy[border][1]; + var color = settings.hierarchy[border][0]; + + settings.container.text(text) + .removeClass() + .addClass(color); + } + } + }, + + /** + * 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(StrengthMeter.core.getScoreOptions(settings)); + } + + core.update(value); + }, + }; + + core.init(); + }, + + tooltip: function(input, options) { + + var defaults = { + hierarchy: { + '0': 'ridiculus', + '10': 'very weak', + '30': 'weak', + '50': 'good', + '70': 'string', + '100': 'very strong', + }, + dictionaries: [ + + ], + keyboards: [ + + ] + }, + + settings = $.extend({}, defaults, options), + + core = { + + /** + * Initialize the plugin. + */ + init: function() { + input.tooltip({ + title: '' + }); + + input.on('keyup', core.keyup) + .keyup(); + }, + + /** + * Update progress abr accordning + */ + update: function(value) { + for (var border in settings.hierarchy) { + if (value >= border) { + var text = settings.hierarchy[border]; + + input.attr('data-original-title', text) + .tooltip('show'); + } + } + }, + + /** + * 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(StrengthMeter.core.getScoreOptions(settings)); + } + + core.update(value); + }, + }; + + core.init(); + }, } - $.fn.strengthMeter = function(options) { + $.fn.strengthMeter = function(type, options) { + if (type === undefined) { + return; + } + + if (!type in StrengthMeter) { + return; + } + var instance = this.data('strengthMeter'), elem = this; @@ -145,7 +287,7 @@ return; } - strengthMeter = new StrengthMeter(elem, options); + strengthMeter = StrengthMeter[type](elem, options); elem.data('strengthMeter', strengthMeter); }); }; |