-
Notifications
You must be signed in to change notification settings - Fork 19
/
jquery.strengthify.min.js
1 lines (1 loc) · 3.18 KB
/
jquery.strengthify.min.js
1
(function($){$.fn.strengthify=function(paramOptions){"use strict";var defaults={zxcvbn:'zxcvbn/zxcvbn.js',userInputs:[],titles:['Weakest','Weak','So-so','Good','Perfect'],tilesOptions:{tooltip:true,element:false},drawTitles:false,drawMessage:false,drawBars:true,$addAfter:null,nonce:null};return this.each(function(){var options=$.extend(defaults,paramOptions);if(!options.drawTitles&&!options.drawMessage&&!options.drawBars){console.warn("expect at least one of 'drawTitles', 'drawMessage', or 'drawBars' to be true")}function getWrapperFor(id){return $('div[data-strengthifyFor="'+id+'"]')};function drawStrengthify(){var password=$(this).val().substring(0,100),elemId=$(this).attr('id'),opacity=(password==='')?0:1,result=zxcvbn(password,options.userInputs),css='',bsLevel='',message='',$wrapper=getWrapperFor(elemId),$container=$wrapper.find('.strengthify-container'),$message=$wrapper.find('[data-strengthifyMessage]');$wrapper.children().css('opacity',opacity).css('-ms-filter','"progid:DXImageTransform.Microsoft.Alpha(Opacity='+opacity*100+')"');if(options.onResult){options.onResult(result)}switch(result.score){case 0:case 1:css='password-bad';bsLevel='danger';message=result.feedback?result.feedback.suggestions.join('<br/>'):"";break;case 2:bsLevel='warning';message=result.feedback?result.feedback.suggestions.join('<br/>'):"";css='password-medium';break;case 3:css='password-good';bsLevel='info';message="Getting better.";break;case 4:css='password-good';bsLevel='success';message="Looks good.";break}if($message){$message.removeAttr('class');$message.addClass('bg-'+bsLevel);if(password===''){message=''}$message.html(message)}if($container){$container.attr('class',css+' strengthify-container').css('width',((result.score===0?1:result.score)*25)+'%');if(password===''){$container.css('width',0)}}if(options.drawTitles){if(options.tilesOptions.tooltip){$wrapper.attr('title',options.titles[result.score]).tooltip({placement:'bottom',trigger:'manual'}).tooltip('fixTitle').tooltip('show');if(opacity===0){$wrapper.tooltip('hide')}}if(options.tilesOptions.element){$wrapper.find(".strengthify-tiles").text(options.titles[result.score])}}};function init(){var $elem=$(this),elemId=$elem.attr('id');var drawSelf=drawStrengthify.bind(this);var $addAfter=options.$addAfter;if(!$addAfter){$addAfter=$elem}$addAfter.after('<div class="strengthify-wrapper" data-strengthifyFor="'+$elem.attr('id')+'"></div>');if(options.drawBars){getWrapperFor(elemId).append('<div class="strengthify-bg" />').append('<div class="strengthify-container" />').append('<div class="strengthify-separator" style="left: 25%" />').append('<div class="strengthify-separator" style="left: 50%" />').append('<div class="strengthify-separator" style="left: 75%" />')}if(options.drawMessage){getWrapperFor(elemId).append('<div data-strengthifyMessage></div>')}if(options.drawTitles&&options.tilesOptions){getWrapperFor(elemId).append('<div class="strengthify-tiles"></div>')}var script=document.createElement("script");script.src=options.zxcvbn;if(options.nonce!==null){script.setAttribute('nonce',options.nonce)}script.onload=function(){$elem.parent().on('scroll',drawSelf);$elem.bind('keyup input change',drawSelf)};document.head.appendChild(script)};init.call(this);})}}(jQuery));