| Current Path : /home/jeromecohp/www/aesecure/third/entropizer/js/ |
| Current File : /home/jeromecohp/www/aesecure/third/entropizer/js/jquery-entropizer.js |
// @file : /third/entropizer/js/jquery-entropizer.js
// @version : 2.0.2
// @author : AVONTURE Christophe - christophe@aesecure.com
// @copyright : (C) 2013-2015 - Christophe Avonture - all right reserved.
// @url : http://www.aesecure.com/
// @package : 2015-02-14 13:37:51
// @license : This program is a commercial software. You CAN'T redistribute it and/or modify it.
// Source code is the property of Christophe Avonture and can't be reused, in whole or in part, in any programs.
(function(){
'use strict';
function factory($, Entropizer){
var defaults={
target: 'input[type=password]:first',
on: 'keydown keyup',
maximum: 100,
buckets: [
{ max: 45, strength: 'poor', color: '#e13' },
{ min: 45, max: 60, strength: 'ok', color: '#f80' },
{ min: 60, max: 75, strength: 'good', color: '#8c0' },
{ min: 75, strength: 'excellent', color: '#0c8' }
],
create: function(container){
var track=$('<div>').addClass('entropizer-track').appendTo(container),
bar=$('<div>').addClass('entropizer-bar').appendTo(track),
text=$('<div>').addClass('entropizer-text').appendTo(track);
return {
track: track,
bar: bar,
text: text
};},
destroy: function(ui){
ui.track.remove();},
map: function(entropy, mapOptions){
var selectedBucket=$.entropizer.classify(entropy, mapOptions.buckets);var percent=Math.min(1, entropy / mapOptions.maximum) * 100;
return $.extend({ entropy: entropy, percent: percent }, selectedBucket);},
update: function(data, ui){
ui.bar.css({
'background-color': data.color,'width': data.percent + '%'
});
ui.text.html(data.strength + ' (' + data.entropy.toFixed(0) + ' bits)');},
engine: null
};
function cloneWithout(object, names){
var clone=$.extend({}, object);
$.each(names, function(index, name){
delete clone[name];});
return clone;}
function Meter(container, options){
this.options=$.extend({}, defaults, options);
this.mapOptions=cloneWithout(this.options, ['target','on','create','destroy','map','update','engine']);
this.entropizer=this.createEngine(this.options.engine);
this.ui=this.options.create(container);
this.target=$(this.options.target);
this.target.on(this.namespace(this.options.on), $.proxy(this._update, this));
this._update();}
Meter.prototype={
createEngine: function(engineOptions){
if(engineOptions && engineOptions.constructor===Entropizer){
return engineOptions;}
return new Entropizer(engineOptions);},
namespace: function(events){
var namespaced=[];
$.each(events.split(' '), function(index, event){
namespaced.push(event + '.entropizer');});
return namespaced.join(' ');},
_destroy: function(){
this.target.off('.entropizer');
this.options.destroy(this.ui);},
_update: function(){
var password=this.target.val(),
entropy=this.entropizer.evaluate(password),
data=this.options.map(entropy, this.mapOptions);
this.options.update(data, this.ui);}};
$.entropizer={
classify: function(value, buckets){
var selectedBucket;
$.each(buckets, function(index, bucket){
if((!bucket.min || value >=bucket.min) && (!bucket.max || value < bucket.max)){
selectedBucket=bucket;
return false;}});
if(!selectedBucket){
return null;}
return cloneWithout(selectedBucket, ['min','max']);}};
$.fn.entropizer=function(options){
var key='entropizer';
if(options==='destroy'){
this.data(key)._destroy();
this.removeData(key);}
else {
return this.data(key, new Meter(this, options));}};}
(function(factory){
if(typeof define==='function' && define.amd){
define(['jquery','entropizer'], factory);}
else if(typeof module==='object' && typeof module.exports==='object'){
factory(require('jquery'), require('entropizer'));}
else {
factory(jQuery, Entropizer);}})(factory);})();