primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 7.21 kB
JavaScript
import { Directive, ElementRef, NgZone, Input, HostListener, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DomHandler, ConnectedOverlayScrollHandler } from 'primeng/dom';
class Password {
constructor(el, zone) {
this.el = el;
this.zone = zone;
this.promptLabel = 'Enter a password';
this.weakLabel = 'Weak';
this.mediumLabel = 'Medium';
this.strongLabel = 'Strong';
this.feedback = true;
}
set showPassword(show) {
this.el.nativeElement.type = show ? 'text' : 'password';
}
ngDoCheck() {
this.updateFilledState();
}
onInput(e) {
this.updateFilledState();
}
updateFilledState() {
this.filled = this.el.nativeElement.value && this.el.nativeElement.value.length;
}
createPanel() {
this.panel = document.createElement('div');
this.panel.className = 'p-password-panel p-component p-password-panel-overlay p-connected-overlay';
this.meter = document.createElement('div');
this.meter.className = 'p-password-meter';
this.info = document.createElement('div');
this.info.className = 'p-password-info';
this.info.textContent = this.promptLabel;
this.panel.appendChild(this.meter);
this.panel.appendChild(this.info);
this.panel.style.minWidth = DomHandler.getOuterWidth(this.el.nativeElement) + 'px';
document.body.appendChild(this.panel);
}
showOverlay() {
if (this.feedback) {
if (!this.panel) {
this.createPanel();
}
this.panel.style.zIndex = String(++DomHandler.zindex);
this.panel.style.display = 'block';
this.zone.runOutsideAngular(() => {
setTimeout(() => {
DomHandler.addClass(this.panel, 'p-connected-overlay-visible');
this.bindScrollListener();
this.bindDocumentResizeListener();
}, 1);
});
DomHandler.absolutePosition(this.panel, this.el.nativeElement);
}
}
hideOverlay() {
if (this.feedback && this.panel) {
DomHandler.addClass(this.panel, 'p-connected-overlay-hidden');
DomHandler.removeClass(this.panel, 'p-connected-overlay-visible');
this.unbindScrollListener();
this.unbindDocumentResizeListener();
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.ngOnDestroy();
}, 150);
});
}
}
onFocus() {
this.showOverlay();
}
onBlur() {
this.hideOverlay();
}
onKeyup(e) {
if (this.feedback) {
let value = e.target.value, label = null, meterPos = null;
if (value.length === 0) {
label = this.promptLabel;
meterPos = '0px 0px';
}
else {
var score = this.testStrength(value);
if (score < 30) {
label = this.weakLabel;
meterPos = '0px -10px';
}
else if (score >= 30 && score < 80) {
label = this.mediumLabel;
meterPos = '0px -20px';
}
else if (score >= 80) {
label = this.strongLabel;
meterPos = '0px -30px';
}
}
if (!this.panel || !DomHandler.hasClass(this.panel, 'p-connected-overlay-visible')) {
this.showOverlay();
}
this.meter.style.backgroundPosition = meterPos;
this.info.textContent = label;
}
}
testStrength(str) {
let grade = 0;
let val;
val = str.match('[0-9]');
grade += this.normalize(val ? val.length : 1 / 4, 1) * 25;
val = str.match('[a-zA-Z]');
grade += this.normalize(val ? val.length : 1 / 2, 3) * 10;
val = str.match('[!@#$%^&*?_~.,;=]');
grade += this.normalize(val ? val.length : 1 / 6, 1) * 35;
val = str.match('[A-Z]');
grade += this.normalize(val ? val.length : 1 / 6, 1) * 30;
grade *= str.length / 8;
return grade > 100 ? 100 : grade;
}
normalize(x, y) {
let diff = x - y;
if (diff <= 0)
return x / y;
else
return 1 + 0.5 * (x / (x + y / 4));
}
get disabled() {
return this.el.nativeElement.disabled;
}
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.el.nativeElement, () => {
if (DomHandler.hasClass(this.panel, 'p-connected-overlay-visible')) {
this.hideOverlay();
}
});
}
this.scrollHandler.bindScrollListener();
}
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
}
bindDocumentResizeListener() {
this.documentResizeListener = this.onWindowResize.bind(this);
window.addEventListener('resize', this.documentResizeListener);
}
unbindDocumentResizeListener() {
if (this.documentResizeListener) {
window.removeEventListener('resize', this.documentResizeListener);
this.documentResizeListener = null;
}
}
onWindowResize() {
this.hideOverlay();
}
ngOnDestroy() {
if (this.panel) {
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
this.unbindDocumentResizeListener();
document.body.removeChild(this.panel);
this.panel = null;
this.meter = null;
this.info = null;
}
}
}
Password.decorators = [
{ type: Directive, args: [{
selector: '[pPassword]',
host: {
'[class.p-inputtext]': 'true',
'[class.p-component]': 'true',
'[class.p-filled]': 'filled'
}
},] }
];
Password.ctorParameters = () => [
{ type: ElementRef },
{ type: NgZone }
];
Password.propDecorators = {
promptLabel: [{ type: Input }],
weakLabel: [{ type: Input }],
mediumLabel: [{ type: Input }],
strongLabel: [{ type: Input }],
feedback: [{ type: Input }],
showPassword: [{ type: Input }],
onInput: [{ type: HostListener, args: ['input', ['$event'],] }],
onFocus: [{ type: HostListener, args: ['focus',] }],
onBlur: [{ type: HostListener, args: ['blur',] }],
onKeyup: [{ type: HostListener, args: ['keyup', ['$event'],] }]
};
class PasswordModule {
}
PasswordModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
exports: [Password],
declarations: [Password]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
export { Password, PasswordModule };
//# sourceMappingURL=primeng-password.js.map