All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.primefaces.forms.forms.password.js Maven / Gradle / Ivy

Go to download

PrimeFaces is one of the most popular UI libraries in Java EE Ecosystem and widely used by software companies, world renowned brands, banks, financial institutions, insurance companies, universities and more.

There is a newer version: 14.0.1
Show newest version
/**
 * PrimeFaces Password
 */
PrimeFaces.widget.Password = PrimeFaces.widget.BaseWidget.extend({

    init: function(cfg) {
        this._super(cfg);

        if(!this.jq.is(':disabled')) {
            if(this.cfg.feedback) {
                this.setupFeedback();
            }

            PrimeFaces.skinInput(this.jq);
        }
    },

    setupFeedback: function() {
        var _self = this;

        //remove previous panel if any
        var oldPanel = $(this.jqId + '_panel');
        if(oldPanel.length == 1) {
            oldPanel.remove();
        }

        //config
        this.cfg.promptLabel = this.cfg.promptLabel||'Please enter a password';
        this.cfg.weakLabel = this.cfg.weakLabel||'Weak';
        this.cfg.goodLabel = this.cfg.goodLabel||'Medium';
        this.cfg.strongLabel = this.cfg.strongLabel||'Strong';

        var panelStyle = this.cfg.inline ? 'ui-password-panel-inline' : 'ui-password-panel-overlay';

        //create panel element
        var panelMarkup = '
'; panelMarkup += '
 
'; panelMarkup += '
' + this.cfg.promptLabel + '
'; panelMarkup += '
'; this.panel = $(panelMarkup).insertAfter(this.jq); this.meter = this.panel.children('div.ui-password-meter'); this.infoText = this.panel.children('div.ui-password-info'); if(!this.cfg.inline) { this.panel.addClass('ui-shadow'); } //events this.jq.focus(function() { _self.show(); }) .blur(function() { _self.hide(); }) .keyup(function() { var value = _self.jq.val(), label = null, meterPos = null; if(value.length == 0) { label = _self.cfg.promptLabel; meterPos = '0px 0px'; } else { var score = _self.testStrength(_self.jq.val()); if(score < 30) { label = _self.cfg.weakLabel; meterPos = '0px -10px'; } else if(score >= 30 && score < 80) { label = _self.cfg.goodLabel; meterPos = '0px -20px'; } else if(score >= 80) { label = _self.cfg.strongLabel; meterPos = '0px -30px'; } } //update meter and info text _self.meter.css('background-position', meterPos); _self.infoText.text(label); }); //overlay setting if(!this.cfg.inline) { this.panel.appendTo('body'); //Hide overlay on resize var resizeNS = 'resize.' + this.id; $(window).unbind(resizeNS).bind(resizeNS, function() { if(_self.panel.is(':visible')) { _self.align(); } }); } }, testStrength: function(str) { var grade = 0, val = 0, _self = this; val = str.match('[0-9]'); grade += _self.normalize(val ? val.length : 1/4, 1) * 25; val = str.match('[a-zA-Z]'); grade += _self.normalize(val ? val.length : 1/2, 3) * 10; val = str.match('[!@#$%^&*?_~.,;=]'); grade += _self.normalize(val ? val.length : 1/6, 1) * 35; val = str.match('[A-Z]'); grade += _self.normalize(val ? val.length : 1/6, 1) * 30; grade *= str.length / 8; return grade > 100 ? 100 : grade; }, normalize: function(x, y) { var diff = x - y; if(diff <= 0) { return x / y; } else { return 1 + 0.5 * (x / (x + y/4)); } }, align: function() { this.panel.css({ left:'', top:'', 'z-index': ++PrimeFaces.zindex }) .position({ my: 'left top', at: 'right top', of: this.jq }); }, show: function() { if(!this.cfg.inline) { this.align(); this.panel.fadeIn(); } else { this.panel.slideDown(); } }, hide: function() { if(this.cfg.inline) this.panel.slideUp(); else this.panel.fadeOut(); } });




© 2015 - 2024 Weber Informatics LLC | Privacy Policy