Files
jiapu/dist/public/js/lay-module/passwordIntensity/passwordIntensity.js
T

7 lines
3.8 KiB
JavaScript

/*!
* passwordIntensity v1.0
* author JerryZst
* qq 1309579432
* Date: 2021/01/20 0007
*/layui.define(["jquery","element"],function(c){var h=layui.jquery,d=layui.element,u="passwordIntensity",r=function(t){this.version="passwordIntensity-1.0",this.tmpId=new Date().getTime(),this.tmpId=t.uniqueId?t.uniqueId:this.tmpId+Math.round(Math.random()*1e3+9999),this.passwordLevel=["low","middle","midhigh","high"],this._level=null,this.options=h.extend(!0,{data:t.data||"",chang:t.chang||8,on:t.on||null},t),this.init(),this.bindEvents()};r.prototype.getComponents=function(){var t=this,e=h(t.options.elem),s=e.attr("lay-filter");return s||(s=t.options.elem.substring(1),e.attr("lay-filter",s)),{$elem:e,filter:s}},r.prototype.init=function(){for(var t=this.getComponents(),e="",s=0;s<this.passwordLevel.length;s++){var l="",a="",v=this.tmpId+this.passwordLevel[s];this.passwordLevel[s]==="low"?(a="弱",l="layui-bg-red"):this.passwordLevel[s]==="middle"?(a="中",l="layui-bg-orange"):this.passwordLevel[s]==="midhigh"?(a="中上",l="layui-bg-orange"):(a="强",l="layui-bg-green"),e+='<div class="layui-col-md3 layui-col-sm3 layui-col-xs3" style="text-align: center"><div class="layui-progress layui-progress-big" lay-filter="'+v+'"><div id="'+v+'" class="layui-progress-bar '+l+'" lay-percent="0%"></div></div><span style="background: white;font-size: 12px">'+a+"</span></div>"}if(e='<div id="'+this.tmpId+'" class="layui-row layui-col-space2" style="margin-top: 5px">'+e+"</div>",t.$elem.parent("div").append(e),this.options.data){t.$elem.val(this.options.data);var o=this,y=p(this.options.data,this.options.chang);switch(y){case 0:o.setLevel("low","100%");break;case 1:o.setLevel("low","100%");break;case 2:o.setLevel("middle","100%");break;case 3:o.setLevel("midhigh","100%");break;default:o.setLevel("high","100%");break}}return e},r.prototype.bindEvents=function(){var t=this.getComponents(),e=this;t.$elem.off("input propertychange").on("input propertychange",function(){var s=h(this).val();if(s==""||s==null)e.setLevel(""),e._level=null;else{var l=p(s,e.options.chang);switch(e._level=l,l){case 0:e.setLevel("low","100%");break;case 1:e.setLevel("low","100%");break;case 2:e.setLevel("middle","100%");break;case 3:e.setLevel("midhigh","100%");break;default:e.setLevel("high","100%");break}}}),t.$elem.off("blur").on("blur",function(){var s=h(this).val();if(s==""||s==null)e.setLevel(""),e._level=null;else{var l=p(s,e.options.chang);switch(e._level=l,l){case 0:e.setLevel("low","100%");break;case 1:e.setLevel("low","100%");break;case 2:e.setLevel("middle","100%");break;case 3:e.setLevel("midhigh","100%");break;default:e.setLevel("high","100%");break}}})},r.prototype.setLevel=function(t,e){e=e||"100%";for(var s=0;s<this.passwordLevel.length;s++)t===""?d.progress(this.tmpId+this.passwordLevel[s],"0%"):t===this.passwordLevel[s]?d.progress(this.tmpId+t,e):e==="100%"&&d.progress(this.tmpId+this.passwordLevel[s],"0%");return!0},r.prototype.getLevel=function(){return n(this._level)},r.prototype.isLow=function(){return n(this._level)==="low"||this._level===null},r.prototype.isMiddle=function(){return n(this._level)==="middle"},r.prototype.isHigh=function(){return n(this._level)==="high"},r.prototype.ismidHigh=function(){return n(this._level)==="midhigh"};function n(t){var e="low";switch(t){case 0:e="low";break;case 1:e="low";break;case 2:e="middle";break;case 3:e="midhigh";break;default:e="high";break}return e}r.prototype.on=function(t,e){return layui.onevent.call(this,u,t,e)};function f(t){return t>=48&&t<=57?1:t>=65&&t<=90?2:t>=97&&t<=122?4:8}function g(t){for(modes=0,i=0;i<4;i++)t&1&&modes++,t>>>=1;return modes}function p(t,e){if(t.length<=6||t.length<e)return 0;for(Modes=0,i=0;i<t.length;i++)Modes|=f(t.charCodeAt(i));return g(Modes)}var m={render:function(t){return new r(t)},on:function(t,e){return layui.onevent.call(this,u,t,e)}};c(u,m)});