forked from nextcloud/strengthify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.strengthify.min.js
1 lines (1 loc) · 2.87 KB
/
jquery.strengthify.min.js
1
(function($){$.fn.strengthify=function(paramOptions){var defaults={zxcvbn:"zxcvbn/zxcvbn.js",titles:["Weakest","Weak","So-so","Good","Perfect"],tilesOptions:{tooltip:true,element:false},drawTitles:false,drawMessage:false,drawBars:true};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(),elemId=$(this).attr("id"),opacity=(password==="")?0:1,result=zxcvbn(password),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+')"');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);$elem.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>')}$elem.parent().on("scroll",drawSelf);$.ajax({cache:true,url:options.zxcvbn}).done(function(content){eval(content);$elem.bind("keyup input change",drawSelf)})}init.call(this)})}}(jQuery));