UNPKG

primeng

Version:

PrimeNG is an open source UI library for Angular featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeB

1,240 lines (1,220 loc) 59 kB
import { trigger, transition, style, animate } from '@angular/animations'; import * as i1 from '@angular/common'; import { isPlatformBrowser, CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { Injectable, inject, signal, booleanAttribute, Directive, Input, HostListener, Pipe, forwardRef, EventEmitter, numberAttribute, Component, ChangeDetectionStrategy, ViewEncapsulation, Output, ViewChild, ContentChild, ContentChildren, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { addClass, absolutePosition, removeClass, hasClass, isTouchDevice, getOuterWidth, relativePosition } from '@primeuix/utils'; import { OverlayService, TranslationKeys, PrimeTemplate, SharedModule } from 'primeng/api'; import { AutoFocus } from 'primeng/autofocus'; import { BaseComponent } from 'primeng/basecomponent'; import { DomHandler, ConnectedOverlayScrollHandler } from 'primeng/dom'; import { TimesIcon, EyeSlashIcon, EyeIcon } from 'primeng/icons'; import { InputText } from 'primeng/inputtext'; import { ZIndexUtils } from 'primeng/utils'; import { BaseStyle } from 'primeng/base'; const theme = ({ dt }) => ` .p-password { display: inline-flex; position: relative; } .p-password .p-password-overlay { min-width: 100%; } .p-password-meter { height: ${dt('password.meter.height')}; background: ${dt('password.meter.background')}; border-radius: ${dt('password.meter.border.radius')}; } .p-password-meter-label { height: 100%; width: 0; transition: width 1s ease-in-out; border-radius: ${dt('password.meter.border.radius')}; } .p-password-meter-weak { background: ${dt('password.strength.weak.background')}; } .p-password-meter-medium { background: ${dt('password.strength.medium.background')}; } .p-password-meter-strong { background: ${dt('password.strength.strong.background')}; } .p-password-fluid { display: flex; } .p-password-fluid .p-password-input { width: 100%; } .p-password-input::-ms-reveal, .p-password-input::-ms-clear { display: none; } .p-password-overlay { position: absolute; padding: ${dt('password.overlay.padding')}; background: ${dt('password.overlay.background')}; color: ${dt('password.overlay.color')}; border: 1px solid ${dt('password.overlay.border.color')}; box-shadow: ${dt('password.overlay.shadow')}; border-radius: ${dt('password.overlay.border.radius')}; } .p-password-content { display: flex; flex-direction: column; gap: ${dt('password.content.gap')}; } .p-password-toggle-mask-icon { inset-inline-end: ${dt('form.field.padding.x')}; color: ${dt('password.icon.color')}; position: absolute; top: 50%; margin-top: calc(-1 * calc(${dt('icon.size')} / 2)); width: ${dt('icon.size')}; height: ${dt('icon.size')}; } .p-password:has(.p-password-toggle-mask-icon) .p-password-input { padding-inline-end: calc((${dt('form.field.padding.x')} * 2) + ${dt('icon.size')}); } /* For PrimeNG */ p-password.ng-invalid.ng-dirty .p-inputtext { border-color: ${dt('inputtext.invalid.border.color')}; } p-password.ng-invalid.ng-dirty .p-inputtext:enabled:focus { border-color: ${dt('inputtext.focus.border.color')}; } p-password.ng-invalid.ng-dirty .p-inputtext::placeholder { color: ${dt('inputtext.invalid.placeholder.color')}; } .p-password-clear-icon { position: absolute; top: 50%; margin-top: -0.5rem; cursor: pointer; inset-inline-end: ${dt('form.field.padding.x')}; color: ${dt('form.field.icon.color')}; } .p-password-fluid-directive { width:100% } `; const inlineStyles = { root: ({ instance }) => ({ position: instance.appendTo === 'self' ? 'relative' : undefined }) }; const classes = { root: ({ instance }) => ({ 'p-password p-component p-inputwrapper': true, 'p-inputwrapper-filled': instance.filled(), 'p-inputwrapper-focus': instance.focused, 'p-password-fluid': instance.hasFluid }), pcInput: 'p-password-input', maskIcon: 'p-password-toggle-mask-icon p-password-mask-icon', unmaskIcon: 'p-password-toggle-mask-icon p-password-unmask-icon', overlay: 'p-password-overlay p-component', content: 'p-password-content', meter: 'p-password-meter', meterLabel: ({ instance }) => `p-password-meter-label ${instance.meter ? 'p-password-meter-' + instance.meter.strength : ''}`, meterText: 'p-password-meter-text' }; class PasswordStyle extends BaseStyle { name = 'password'; theme = theme; classes = classes; inlineStyles = inlineStyles; static ɵfac = /*@__PURE__*/ (() => { let ɵPasswordStyle_BaseFactory; return function PasswordStyle_Factory(__ngFactoryType__) { return (ɵPasswordStyle_BaseFactory || (ɵPasswordStyle_BaseFactory = i0.ɵɵgetInheritedFactory(PasswordStyle)))(__ngFactoryType__ || PasswordStyle); }; })(); static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: PasswordStyle, factory: PasswordStyle.ɵfac }); } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PasswordStyle, [{ type: Injectable }], null, null); })(); /** * * Password displays strength indicator for password fields. * * [Live Demo](https://www.primeng.org/password/) * * @module passwordstyle * */ var PasswordClasses; (function (PasswordClasses) { /** * Class name of the root element */ PasswordClasses["root"] = "p-password"; /** * Class name of the pt input element */ PasswordClasses["pcInput"] = "p-password-input"; /** * Class name of the mask icon element */ PasswordClasses["maskIcon"] = "p-password-mask-icon"; /** * Class name of the unmask icon element */ PasswordClasses["unmaskIcon"] = "p-password-unmask-icon"; /** * Class name of the overlay element */ PasswordClasses["overlay"] = "p-password-overlay"; /** * Class name of the meter element */ PasswordClasses["meter"] = "p-password-meter"; /** * Class name of the meter label element */ PasswordClasses["meterLabel"] = "p-password-meter-label"; /** * Class name of the meter text element */ PasswordClasses["meterText"] = "p-password-meter-text"; })(PasswordClasses || (PasswordClasses = {})); const _c0 = ["content"]; const _c1 = ["footer"]; const _c2 = ["header"]; const _c3 = ["clearicon"]; const _c4 = ["headericon"]; const _c5 = ["showicon"]; const _c6 = ["input"]; const _c7 = () => ({ class: "p-password-toggle-mask-icon p-password-mask-icon" }); const _c8 = (a0, a1) => ({ showTransitionParams: a0, hideTransitionParams: a1 }); const _c9 = a0 => ({ value: "visible", params: a0 }); const _c10 = a0 => ({ width: a0 }); function Password_ng_container_5_TimesIcon_1_Template(rf, ctx) { if (rf & 1) { const _r3 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "TimesIcon", 8); i0.ɵɵlistener("click", function Password_ng_container_5_TimesIcon_1_Template_TimesIcon_click_0_listener() { i0.ɵɵrestoreView(_r3); const ctx_r3 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r3.clear()); }); i0.ɵɵelementEnd(); } if (rf & 2) { i0.ɵɵattribute("data-pc-section", "clearIcon"); } } function Password_ng_container_5_3_ng_template_0_Template(rf, ctx) { } function Password_ng_container_5_3_Template(rf, ctx) { if (rf & 1) { i0.ɵɵtemplate(0, Password_ng_container_5_3_ng_template_0_Template, 0, 0, "ng-template"); } } function Password_ng_container_5_Template(rf, ctx) { if (rf & 1) { const _r2 = i0.ɵɵgetCurrentView(); i0.ɵɵelementContainerStart(0); i0.ɵɵtemplate(1, Password_ng_container_5_TimesIcon_1_Template, 1, 1, "TimesIcon", 7); i0.ɵɵelementStart(2, "span", 8); i0.ɵɵlistener("click", function Password_ng_container_5_Template_span_click_2_listener() { i0.ɵɵrestoreView(_r2); const ctx_r3 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r3.clear()); }); i0.ɵɵtemplate(3, Password_ng_container_5_3_Template, 1, 0, null, 9); i0.ɵɵelementEnd(); i0.ɵɵelementContainerEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", !ctx_r3.clearIconTemplate && !ctx_r3._clearIconTemplate); i0.ɵɵadvance(); i0.ɵɵattribute("data-pc-section", "clearIcon"); i0.ɵɵadvance(); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.clearIconTemplate || ctx_r3._clearIconTemplate); } } function Password_ng_container_6_ng_container_1_EyeSlashIcon_1_Template(rf, ctx) { if (rf & 1) { const _r5 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "EyeSlashIcon", 12); i0.ɵɵlistener("click", function Password_ng_container_6_ng_container_1_EyeSlashIcon_1_Template_EyeSlashIcon_click_0_listener() { i0.ɵɵrestoreView(_r5); const ctx_r3 = i0.ɵɵnextContext(3); return i0.ɵɵresetView(ctx_r3.onMaskToggle()); }); i0.ɵɵelementEnd(); } if (rf & 2) { i0.ɵɵattribute("data-pc-section", "hideIcon"); } } function Password_ng_container_6_ng_container_1_span_2_1_ng_template_0_Template(rf, ctx) { } function Password_ng_container_6_ng_container_1_span_2_1_Template(rf, ctx) { if (rf & 1) { i0.ɵɵtemplate(0, Password_ng_container_6_ng_container_1_span_2_1_ng_template_0_Template, 0, 0, "ng-template"); } } function Password_ng_container_6_ng_container_1_span_2_Template(rf, ctx) { if (rf & 1) { const _r6 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "span", 13); i0.ɵɵlistener("click", function Password_ng_container_6_ng_container_1_span_2_Template_span_click_0_listener() { i0.ɵɵrestoreView(_r6); const ctx_r3 = i0.ɵɵnextContext(3); return i0.ɵɵresetView(ctx_r3.onMaskToggle()); }); i0.ɵɵtemplate(1, Password_ng_container_6_ng_container_1_span_2_1_Template, 1, 0, null, 14); i0.ɵɵelementEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(3); i0.ɵɵadvance(); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.hideIconTemplate || ctx_r3._hideIconTemplate)("ngTemplateOutletContext", i0.ɵɵpureFunction0(2, _c7)); } } function Password_ng_container_6_ng_container_1_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainerStart(0); i0.ɵɵtemplate(1, Password_ng_container_6_ng_container_1_EyeSlashIcon_1_Template, 1, 1, "EyeSlashIcon", 10)(2, Password_ng_container_6_ng_container_1_span_2_Template, 2, 3, "span", 11); i0.ɵɵelementContainerEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(2); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", !ctx_r3.hideIconTemplate && !ctx_r3._hideIconTemplate); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx_r3.hideIconTemplate || ctx_r3._hideIconTemplate); } } function Password_ng_container_6_ng_container_2_EyeIcon_1_Template(rf, ctx) { if (rf & 1) { const _r7 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "EyeIcon", 12); i0.ɵɵlistener("click", function Password_ng_container_6_ng_container_2_EyeIcon_1_Template_EyeIcon_click_0_listener() { i0.ɵɵrestoreView(_r7); const ctx_r3 = i0.ɵɵnextContext(3); return i0.ɵɵresetView(ctx_r3.onMaskToggle()); }); i0.ɵɵelementEnd(); } if (rf & 2) { i0.ɵɵattribute("data-pc-section", "showIcon"); } } function Password_ng_container_6_ng_container_2_span_2_1_ng_template_0_Template(rf, ctx) { } function Password_ng_container_6_ng_container_2_span_2_1_Template(rf, ctx) { if (rf & 1) { i0.ɵɵtemplate(0, Password_ng_container_6_ng_container_2_span_2_1_ng_template_0_Template, 0, 0, "ng-template"); } } function Password_ng_container_6_ng_container_2_span_2_Template(rf, ctx) { if (rf & 1) { const _r8 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "span", 13); i0.ɵɵlistener("click", function Password_ng_container_6_ng_container_2_span_2_Template_span_click_0_listener() { i0.ɵɵrestoreView(_r8); const ctx_r3 = i0.ɵɵnextContext(3); return i0.ɵɵresetView(ctx_r3.onMaskToggle()); }); i0.ɵɵtemplate(1, Password_ng_container_6_ng_container_2_span_2_1_Template, 1, 0, null, 9); i0.ɵɵelementEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(3); i0.ɵɵadvance(); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.showIconTemplate || ctx_r3._showIconTemplate); } } function Password_ng_container_6_ng_container_2_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainerStart(0); i0.ɵɵtemplate(1, Password_ng_container_6_ng_container_2_EyeIcon_1_Template, 1, 1, "EyeIcon", 10)(2, Password_ng_container_6_ng_container_2_span_2_Template, 2, 1, "span", 11); i0.ɵɵelementContainerEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(2); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", !ctx_r3.showIconTemplate && !ctx_r3._showIconTemplate); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx_r3.showIconTemplate || ctx_r3._showIconTemplate); } } function Password_ng_container_6_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainerStart(0); i0.ɵɵtemplate(1, Password_ng_container_6_ng_container_1_Template, 3, 2, "ng-container", 5)(2, Password_ng_container_6_ng_container_2_Template, 3, 2, "ng-container", 5); i0.ɵɵelementContainerEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx_r3.unmasked); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", !ctx_r3.unmasked); } } function Password_div_7_ng_container_2_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainer(0); } } function Password_div_7_ng_container_3_ng_container_1_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainer(0); } } function Password_div_7_ng_container_3_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainerStart(0); i0.ɵɵtemplate(1, Password_div_7_ng_container_3_ng_container_1_Template, 1, 0, "ng-container", 9); i0.ɵɵelementContainerEnd(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(2); i0.ɵɵadvance(); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.contentTemplate || ctx_r3._contentTemplate); } } function Password_div_7_ng_template_4_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementStart(0, "div", 17)(1, "div", 18); i0.ɵɵelement(2, "div", 3); i0.ɵɵpipe(3, "mapper"); i0.ɵɵelementEnd(); i0.ɵɵelementStart(4, "div", 19); i0.ɵɵtext(5); i0.ɵɵelementEnd()(); } if (rf & 2) { const ctx_r3 = i0.ɵɵnextContext(2); i0.ɵɵadvance(); i0.ɵɵattribute("data-pc-section", "meter"); i0.ɵɵadvance(); i0.ɵɵproperty("ngClass", i0.ɵɵpipeBind2(3, 6, ctx_r3.meter, ctx_r3.strengthClass))("ngStyle", i0.ɵɵpureFunction1(9, _c10, ctx_r3.meter ? ctx_r3.meter.width : "")); i0.ɵɵattribute("data-pc-section", "meterLabel"); i0.ɵɵadvance(2); i0.ɵɵattribute("data-pc-section", "info"); i0.ɵɵadvance(); i0.ɵɵtextInterpolate(ctx_r3.infoText); } } function Password_div_7_ng_container_6_Template(rf, ctx) { if (rf & 1) { i0.ɵɵelementContainer(0); } } function Password_div_7_Template(rf, ctx) { if (rf & 1) { const _r9 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 15, 1); i0.ɵɵlistener("click", function Password_div_7_Template_div_click_0_listener($event) { i0.ɵɵrestoreView(_r9); const ctx_r3 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r3.onOverlayClick($event)); })("@overlayAnimation.start", function Password_div_7_Template_div_animation_overlayAnimation_start_0_listener($event) { i0.ɵɵrestoreView(_r9); const ctx_r3 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r3.onAnimationStart($event)); })("@overlayAnimation.done", function Password_div_7_Template_div_animation_overlayAnimation_done_0_listener($event) { i0.ɵɵrestoreView(_r9); const ctx_r3 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r3.onAnimationEnd($event)); }); i0.ɵɵtemplate(2, Password_div_7_ng_container_2_Template, 1, 0, "ng-container", 9)(3, Password_div_7_ng_container_3_Template, 2, 1, "ng-container", 16)(4, Password_div_7_ng_template_4_Template, 6, 11, "ng-template", null, 2, i0.ɵɵtemplateRefExtractor)(6, Password_div_7_ng_container_6_Template, 1, 0, "ng-container", 9); i0.ɵɵelementEnd(); } if (rf & 2) { const content_r10 = i0.ɵɵreference(5); const ctx_r3 = i0.ɵɵnextContext(); i0.ɵɵproperty("@overlayAnimation", i0.ɵɵpureFunction1(9, _c9, i0.ɵɵpureFunction2(6, _c8, ctx_r3.showTransitionOptions, ctx_r3.hideTransitionOptions))); i0.ɵɵattribute("data-pc-section", "panel"); i0.ɵɵadvance(2); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.headerTemplate || ctx_r3._headerTemplate); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx_r3.contentTemplate || ctx_r3._contentTemplate)("ngIfElse", content_r10); i0.ɵɵadvance(3); i0.ɵɵproperty("ngTemplateOutlet", ctx_r3.footerTemplate || ctx_r3._footerTemplate); } } /** * Password directive. * @group Components */ // strengthClass(meter: any) { // return `p-password-meter-label p-password-meter${meter?.strength ? `-${meter.strength}` : ''}`; // } class PasswordDirective extends BaseComponent { zone; /** * Text to prompt password entry. Defaults to PrimeNG I18N API configuration. * @group Props */ promptLabel = 'Enter a password'; /** * Text for a weak password. Defaults to PrimeNG I18N API configuration. * @group Props */ weakLabel = 'Weak'; /** * Text for a medium password. Defaults to PrimeNG I18N API configuration. * @group Props */ mediumLabel = 'Medium'; /** * Text for a strong password. Defaults to PrimeNG I18N API configuration. * @group Props */ strongLabel = 'Strong'; /** * Whether to show the strength indicator or not. * @group Props */ feedback = true; /** * Sets the visibility of the password field. * @group Props */ set showPassword(show) { this.el.nativeElement.type = show ? 'text' : 'password'; } /** * Specifies the input variant of the component. * @group Props */ variant = 'outlined'; /** * Spans 100% width of the container when enabled. * @group Props */ fluid = false; panel; meter; info; filled; content; label; scrollHandler; documentResizeListener; _componentStyle = inject(PasswordStyle); get hasFluid() { const nativeElement = this.el.nativeElement; const fluidComponent = nativeElement.closest('p-fluid'); return this.fluid || !!fluidComponent; } constructor(zone) { super(); this.zone = zone; } ngDoCheck() { this.updateFilledState(); } onInput(e) { this.updateFilledState(); } updateFilledState() { this.filled = this.el.nativeElement.value && this.el.nativeElement.value.length; } createPanel() { if (isPlatformBrowser(this.platformId)) { this.panel = this.renderer.createElement('div'); this.renderer.addClass(this.panel, 'p-password-overlay'); this.renderer.addClass(this.panel, 'p-component'); this.content = this.renderer.createElement('div'); this.renderer.addClass(this.content, 'p-password-content'); this.renderer.appendChild(this.panel, this.content); this.meter = this.renderer.createElement('div'); this.renderer.addClass(this.meter, 'p-password-meter'); this.renderer.appendChild(this.content, this.meter); this.label = this.renderer.createElement('div'); this.renderer.addClass(this.label, 'p-password-meter-label'); this.renderer.appendChild(this.meter, this.label); this.info = this.renderer.createElement('div'); this.renderer.addClass(this.info, 'p-password-meter-text'); this.renderer.setProperty(this.info, 'textContent', this.promptLabel); this.renderer.appendChild(this.content, this.info); this.renderer.setStyle(this.panel, 'minWidth', `${this.el.nativeElement.offsetWidth}px`); this.renderer.appendChild(document.body, this.panel); this.updateMeter(); } } showOverlay() { if (this.feedback) { if (!this.panel) { this.createPanel(); } this.renderer.setStyle(this.panel, 'zIndex', String(++DomHandler.zindex)); this.renderer.setStyle(this.panel, 'display', 'block'); this.zone.runOutsideAngular(() => { setTimeout(() => { addClass(this.panel, 'p-connected-overlay-visible'); this.bindScrollListener(); this.bindDocumentResizeListener(); }, 1); }); absolutePosition(this.panel, this.el.nativeElement); } } hideOverlay() { if (this.feedback && this.panel) { addClass(this.panel, 'p-connected-overlay-hidden'); removeClass(this.panel, 'p-connected-overlay-visible'); this.unbindScrollListener(); this.unbindDocumentResizeListener(); this.zone.runOutsideAngular(() => { setTimeout(() => { this.ngOnDestroy(); }, 150); }); } } onFocus() { this.showOverlay(); } onBlur() { this.hideOverlay(); } labelSignal = signal(''); 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'; } this.labelSignal.set(label); this.updateMeter(); } if (!this.panel || !hasClass(this.panel, 'p-connected-overlay-visible')) { this.showOverlay(); } this.renderer.setStyle(this.meter, 'backgroundPosition', meterPos); this.info.textContent = label; } } updateMeter() { if (this.labelSignal()) { const label = this.labelSignal(); const strengthClass = this.strengthClass(label.toLowerCase()); const width = this.getWidth(label.toLowerCase()); this.renderer.addClass(this.meter, strengthClass); this.renderer.setStyle(this.meter, 'width', width); this.info.textContent = label; } } getWidth(label) { return label === 'weak' ? '33.33%' : label === 'medium' ? '66.66%' : label === 'strong' ? '100%' : ''; } strengthClass(label) { return `p-password-meter${label ? `-${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 (hasClass(this.panel, 'p-connected-overlay-visible')) { this.hideOverlay(); } }); } this.scrollHandler.bindScrollListener(); } unbindScrollListener() { if (this.scrollHandler) { this.scrollHandler.unbindScrollListener(); } } bindDocumentResizeListener() { if (isPlatformBrowser(this.platformId)) { if (!this.documentResizeListener) { const window = this.document.defaultView; this.documentResizeListener = this.renderer.listen(window, 'resize', this.onWindowResize.bind(this)); } } } unbindDocumentResizeListener() { if (this.documentResizeListener) { this.documentResizeListener(); this.documentResizeListener = null; } } onWindowResize() { if (!isTouchDevice()) { this.hideOverlay(); } } ngOnDestroy() { if (this.panel) { if (this.scrollHandler) { this.scrollHandler.destroy(); this.scrollHandler = null; } this.unbindDocumentResizeListener(); this.renderer.removeChild(this.document.body, this.panel); this.panel = null; this.meter = null; this.info = null; } super.ngOnDestroy(); } static ɵfac = function PasswordDirective_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || PasswordDirective)(i0.ɵɵdirectiveInject(i0.NgZone)); }; static ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: PasswordDirective, selectors: [["", "pPassword", ""]], hostAttrs: [1, "p-password", "p-inputtext", "p-component", "p-inputwrapper"], hostVars: 6, hostBindings: function PasswordDirective_HostBindings(rf, ctx) { if (rf & 1) { i0.ɵɵlistener("input", function PasswordDirective_input_HostBindingHandler($event) { return ctx.onInput($event); })("focus", function PasswordDirective_focus_HostBindingHandler() { return ctx.onFocus(); })("blur", function PasswordDirective_blur_HostBindingHandler() { return ctx.onBlur(); })("keyup", function PasswordDirective_keyup_HostBindingHandler($event) { return ctx.onKeyup($event); }); } if (rf & 2) { i0.ɵɵclassProp("p-inputwrapper-filled", ctx.filled)("p-variant-filled", ctx.variant === "filled" || ctx.config.inputStyle() === "filled" || ctx.config.inputVariant() === "filled")("p-password-fluid-directive", ctx.hasFluid); } }, inputs: { promptLabel: "promptLabel", weakLabel: "weakLabel", mediumLabel: "mediumLabel", strongLabel: "strongLabel", feedback: [2, "feedback", "feedback", booleanAttribute], showPassword: "showPassword", variant: "variant", fluid: [2, "fluid", "fluid", booleanAttribute] }, features: [i0.ɵɵProvidersFeature([PasswordStyle]), i0.ɵɵInputTransformsFeature, i0.ɵɵInheritDefinitionFeature] }); } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PasswordDirective, [{ type: Directive, args: [{ selector: '[pPassword]', standalone: true, host: { class: 'p-password p-inputtext p-component p-inputwrapper', '[class.p-inputwrapper-filled]': 'filled', '[class.p-variant-filled]': 'variant === "filled" || config.inputStyle() === "filled" || config.inputVariant() === "filled"', '[class.p-password-fluid-directive]': 'hasFluid' }, providers: [PasswordStyle] }] }], () => [{ type: i0.NgZone }], { promptLabel: [{ type: Input }], weakLabel: [{ type: Input }], mediumLabel: [{ type: Input }], strongLabel: [{ type: Input }], feedback: [{ type: Input, args: [{ transform: booleanAttribute }] }], showPassword: [{ type: Input }], variant: [{ type: Input }], fluid: [{ type: Input, args: [{ transform: booleanAttribute }] }], onInput: [{ type: HostListener, args: ['input', ['$event']] }], onFocus: [{ type: HostListener, args: ['focus'] }], onBlur: [{ type: HostListener, args: ['blur'] }], onKeyup: [{ type: HostListener, args: ['keyup', ['$event']] }] }); })(); class MapperPipe { transform(value, mapper, ...args) { return mapper(value, ...args); } static ɵfac = function MapperPipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MapperPipe)(); }; static ɵpipe = /*@__PURE__*/ i0.ɵɵdefinePipe({ name: "mapper", type: MapperPipe, pure: true }); } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MapperPipe, [{ type: Pipe, args: [{ name: 'mapper', pure: true, standalone: true }] }], null, null); })(); const Password_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Password), multi: true }; /** * Password displays strength indicator for password fields. * @group Components */ class Password extends BaseComponent { /** * Defines a string that labels the input for accessibility. * @group Props */ ariaLabel; /** * Whether the component should span the full width of its parent. * @group Props */ fluid; /** * Specifies one or more IDs in the DOM that labels the input field. * @group Props */ ariaLabelledBy; /** * Label of the input for accessibility. * @group Props */ label; /** * Indicates whether the component is disabled or not. * @group Props */ disabled; /** * Text to prompt password entry. Defaults to PrimeNG I18N API configuration. * @group Props */ promptLabel; /** * Regex value for medium regex. * @group Props */ mediumRegex = '^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})'; /** * Regex value for strong regex. * @group Props */ strongRegex = '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})'; /** * Text for a weak password. Defaults to PrimeNG I18N API configuration. * @group Props */ weakLabel; /** * Text for a medium password. Defaults to PrimeNG I18N API configuration. * @group Props */ mediumLabel; /** * specifies the maximum number of characters allowed in the input element. * @group Props */ maxLength; /** * Text for a strong password. Defaults to PrimeNG I18N API configuration. * @group Props */ strongLabel; /** * Identifier of the accessible input element. * @group Props */ inputId; /** * Whether to show the strength indicator or not. * @group Props */ feedback = true; /** * Id of the element or "body" for document where the overlay should be appended to. * @group Props */ appendTo; /** * Whether to show an icon to display the password as plain text. * @group Props */ toggleMask; /** * Defines the size of the component. * @group Props */ size; /** * Style class of the input field. * @group Props */ inputStyleClass; /** * Style class of the element. * @group Props */ styleClass; /** * Inline style of the component. * @group Props */ style; /** * Inline style of the input field. * @group Props */ inputStyle; /** * Transition options of the show animation. * @group Props */ showTransitionOptions = '.12s cubic-bezier(0, 0, 0.2, 1)'; /** * Transition options of the hide animation. * @group Props */ hideTransitionOptions = '.1s linear'; /** * Specify automated assistance in filling out password by browser. * @group Props */ autocomplete; /** * Advisory information to display on input. * @group Props */ placeholder; /** * When enabled, a clear icon is displayed to clear the value. * @group Props */ showClear = false; /** * When present, it specifies that the component should automatically get focus on load. * @group Props */ autofocus; /** * Specifies the input variant of the component. * @group Props */ variant = 'outlined'; /** * Callback to invoke when the component receives focus. * @param {Event} event - Browser event. * @group Emits */ onFocus = new EventEmitter(); /** * Callback to invoke when the component loses focus. * @param {Event} event - Browser event. * @group Emits */ onBlur = new EventEmitter(); /** * Callback to invoke when clear button is clicked. * @group Emits */ onClear = new EventEmitter(); input; contentTemplate; footerTemplate; headerTemplate; clearIconTemplate; hideIconTemplate; showIconTemplate; templates; _contentTemplate; _footerTemplate; _headerTemplate; _clearIconTemplate; _hideIconTemplate; _showIconTemplate; overlayVisible = false; meter; infoText; focused = false; unmasked = false; mediumCheckRegExp; strongCheckRegExp; resizeListener; scrollHandler; overlay; value = null; onModelChange = () => { }; onModelTouched = () => { }; translationSubscription; _componentStyle = inject(PasswordStyle); get hasFluid() { const nativeElement = this.el.nativeElement; const fluidComponent = nativeElement.closest('p-fluid'); return this.fluid || !!fluidComponent; } overlayService = inject(OverlayService); ngOnInit() { super.ngOnInit(); this.infoText = this.promptText(); this.mediumCheckRegExp = new RegExp(this.mediumRegex); this.strongCheckRegExp = new RegExp(this.strongRegex); this.translationSubscription = this.config.translationObserver.subscribe(() => { this.updateUI(this.value || ''); }); } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'content': this._contentTemplate = item.template; break; case 'header': this._headerTemplate = item.template; break; case 'footer': this._footerTemplate = item.template; break; case 'clearicon': this._clearIconTemplate = item.template; break; case 'hideicon': this._hideIconTemplate = item.template; break; case 'showicon': this._showIconTemplate = item.template; break; default: this._contentTemplate = item.template; break; } }); } onAnimationStart(event) { switch (event.toState) { case 'visible': this.overlay = event.element; ZIndexUtils.set('overlay', this.overlay, this.config.zIndex.overlay); this.appendContainer(); this.alignOverlay(); this.bindScrollListener(); this.bindResizeListener(); break; case 'void': this.unbindScrollListener(); this.unbindResizeListener(); this.overlay = null; break; } } onAnimationEnd(event) { switch (event.toState) { case 'void': ZIndexUtils.clear(event.element); break; } } appendContainer() { if (this.appendTo) { if (this.appendTo === 'body') this.renderer.appendChild(this.document.body, this.overlay); else this.document.getElementById(this.appendTo).appendChild(this.overlay); } } alignOverlay() { if (this.appendTo) { this.overlay.style.minWidth = getOuterWidth(this.input.nativeElement) + 'px'; absolutePosition(this.overlay, this.input.nativeElement); } else { relativePosition(this.overlay, this.input.nativeElement); } } onInput(event) { this.value = event.target.value; this.onModelChange(this.value); } onInputFocus(event) { this.focused = true; if (this.feedback) { this.overlayVisible = true; } this.onFocus.emit(event); } onInputBlur(event) { this.focused = false; if (this.feedback) { this.overlayVisible = false; } this.onModelTouched(); this.onBlur.emit(event); } onKeyUp(event) { if (this.feedback) { let value = event.target.value; this.updateUI(value); if (event.code === 'Escape') { this.overlayVisible && (this.overlayVisible = false); return; } if (!this.overlayVisible) { this.overlayVisible = true; } } } updateUI(value) { let label = null; let meter = null; switch (this.testStrength(value)) { case 1: label = this.weakText(); meter = { strength: 'weak', width: '33.33%' }; break; case 2: label = this.mediumText(); meter = { strength: 'medium', width: '66.66%' }; break; case 3: label = this.strongText(); meter = { strength: 'strong', width: '100%' }; break; default: label = this.promptText(); meter = null; break; } this.meter = meter; this.infoText = label; } onMaskToggle() { this.unmasked = !this.unmasked; } onOverlayClick(event) { this.overlayService.add({ originalEvent: event, target: this.el.nativeElement }); } testStrength(str) { let level = 0; if (this.strongCheckRegExp.test(str)) level = 3; else if (this.mediumCheckRegExp.test(str)) level = 2; else if (str.length) level = 1; return level; } writeValue(value) { if (value === undefined) this.value = null; else this.value = value; if (this.feedback) this.updateUI(this.value || ''); this.cd.markForCheck(); } registerOnChange(fn) { this.onModelChange = fn; } registerOnTouched(fn) { this.onModelTouched = fn; } setDisabledState(val) { this.disabled = val; this.cd.markForCheck(); } bindScrollListener() { if (isPlatformBrowser(this.platformId)) { if (!this.scrollHandler) { this.scrollHandler = new ConnectedOverlayScrollHandler(this.input.nativeElement, () => { if (this.overlayVisible) { this.overlayVisible = false; } }); } this.scrollHandler.bindScrollListener(); } } bindResizeListener() { if (isPlatformBrowser(this.platformId)) { if (!this.resizeListener) { const window = this.document.defaultView; this.resizeListener = this.renderer.listen(window, 'resize', () => { if (this.overlayVisible && !isTouchDevice()) { this.overlayVisible = false; } }); } } } unbindScrollListener() { if (this.scrollHandler) { this.scrollHandler.unbindScrollListener(); } } unbindResizeListener() { if (this.resizeListener) { this.resizeListener(); this.resizeListener = null; } } containerClass(toggleMask) { return { 'p-password p-component p-inputwrapper': true, 'p-input-icon-right': toggleMask }; } get rootClass() { return this._componentStyle.classes.root({ instance: this }); } inputFieldClass(disabled) { return { 'p-password-input': true, 'p-disabled': disabled }; } strengthClass(meter) { return `p-password-meter-label p-password-meter${meter?.strength ? `-${meter.strength}` : ''}`; } filled() { return this.value != null && this.value.toString().length > 0; } promptText() { return this.promptLabel || this.getTranslation(TranslationKeys.PASSWORD_PROMPT); } weakText() { return this.weakLabel || this.getTranslation(TranslationKeys.WEAK); } mediumText() { return this.mediumLabel || this.getTranslation(TranslationKeys.MEDIUM); } strongText() { return this.strongLabel || this.getTranslation(TranslationKeys.STRONG); } restoreAppend() { if (this.overlay && this.appendTo) { if (this.appendTo === 'body') this.renderer.removeChild(this.document.body, this.overlay); else this.document.getElementById(this.appendTo).removeChild(this.overlay); } } inputType(unmasked) { return unmasked ? 'text' : 'password'; } getTranslation(option) { return this.config.getTranslation(option); } clear() { this.value = null; this.onModelChange(this.value); this.writeValue(this.value); this.onClear.emit(); } ngOnDestroy() { if (this.overlay) { ZIndexUtils.clear(this.overlay); this.overlay = null; } this.restoreAppend(); this.unbindResizeListener(); if (this.scrollHandler) { this.scrollHandler.destroy(); this.scrollHandler = null; } if (this.translationSubscription) { this.translationSubscription.unsubscribe(); } super.ngOnDestroy(); } static ɵfac = /*@__PURE__*/ (() => { let ɵPassword_BaseFactory; return function Password_Factory(__ngFactoryType__) { return (ɵPassword_BaseFactory || (ɵPassword_BaseFactory = i0.ɵɵgetInheritedFactory(Password)))(__ngFactoryType__ || Password); }; })(); static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: Password, selectors: [["p-password"]], contentQueries: function Password_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) { i0.ɵɵcontentQuery(dirIndex, _c0, 4); i0.ɵɵcontentQuery(dirIndex, _c1, 4); i0.ɵɵcontentQuery(dirIndex, _c2, 4); i0.ɵɵcontentQuery(dirIndex, _c3, 4); i0.ɵɵcontentQuery(dirIndex, _c4, 4); i0.ɵɵcontentQuery(dirIndex, _c5, 4); i0.ɵɵcontentQuery(dirIndex, PrimeTemplate, 4); } if (rf & 2) { let _t; i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.contentTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.footerTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.headerTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.clearIconTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.hideIconTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.showIconTemplate = _t.first); i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.templates = _t); } }, viewQuery: function Password_Query(rf, ctx) { if (rf & 1) { i0.ɵɵviewQuery(_c6, 5); } if (rf & 2) { let _t; i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.input = _t.first); } }, inputs: { ariaLabel: "ariaLabel", fluid: [2, "fluid", "fluid", booleanAttribute], ariaLabelledBy: "ariaLabelledBy", label: "label", disabled: [2, "disabled", "disabled", booleanAttribute], promptLabel: "promptLabel", mediumRegex: "mediumRegex", strongRegex: "strongRegex", weakLabel: "weakLabel", mediumLabel: "mediumLabel", maxLength: [2, "maxLength", "maxLength", numberAttribute], strongLabel: "strongLabel", inputId: "inputId", feedback: [2, "feedback", "feedback", booleanAttribute], appendTo: "appendTo", toggleMask: [2, "toggleMask", "toggleMask", booleanAttribute], size: "size", inputStyleClass: "inputStyleClass", styleClass: "styleClass", style: "style", inputStyle: "inputStyle", showTransitionOptions: "showTransitionOptions", hideTransitionOptions: "hideTransitionOptions", autocomplete: "autocomplete", placeholder: "placeholder", showClear: [2, "showClear", "showClear", booleanAttribute], autofocus: [2, "autofocus", "autofocus", booleanAttribute], variant: "variant" }, outputs: { onFocus: "onFocus", onBlur: "onBlur", onClear: "onClear" }, features: [i0.ɵɵProvidersFeature([Password_VALUE_ACCESSOR, PasswordStyle]), i0.ɵɵInputTransformsFeature, i0.ɵɵInheritDefinitionFeature], decls: 8, vars: 33, consts: [["input", ""], ["overlay", ""], ["content", ""], [3, "ngClass", "ngStyle"], ["pInputText", "", 3, "input", "focus", "blur", "keyup", "disabled", "pSize", "ngClass", "ngStyle", "value", "variant", "pAutoFocus"], [4, "ngIf"], ["class", "p-password-overlay p-component", 3, "click", 4, "ngIf"], ["class", "p-password-clear-icon", 3, "click", 4, "ngIf"], [1, "p-password-clear-icon", 3, "click"], [4, "ngTemplateOutlet"], ["class", "p-password-toggle-mask-icon p-password-mask-icon", 3, "click", 4, "ngIf"], [3, "click", 4, "ngIf"], [1, "p-password-toggle-mask-icon", "p-password-mask-icon", 3, "click"], [3, "click"], [4, "ngTemplateOutlet", "ngTemplateOutletContext"], [1, "p-password-overlay", "p-component", 3, "click"], [4, "ngIf", "ngIfElse"], [1, "p-password-content"], [1, "p-password-meter"], [1, "p-password-meter-text"]], template: function Password_Template(rf, ctx) { if (rf & 1) { const _r1 = i0.ɵɵgetCurrentView(); i0.ɵɵelementStart(0, "div", 3)(1, "input", 4, 0); i0.ɵɵpipe(3, "mapper"); i0.ɵɵpipe(4, "mapper"); i0.ɵɵlistener("input", function Password_Template_input_input_1_listener($event) { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.onInput($event)); })("focus", function Password_Template_input_focus_1_listener($event) { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.onInputFocus($event)); })("blur", function Password_Template_input_blur_1_listener($event) { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.onInputBlur($event)); })("keyup", function Password_Template_input_keyup_1_listener($event) { i0.ɵɵrestoreView(_r1); return i0.ɵɵresetView(ctx.onKeyUp($event)); }); i0.ɵɵelementEnd(); i0.ɵɵtemplate(5, Password_ng_container_5_Template, 4, 3, "ng-container", 5)(6, Password_ng_container_6_Template, 3, 2, "ng-container", 5)(7, Password_div_7_Template, 7, 11, "div", 6); i0.ɵɵelementEnd(); } if (rf & 2) { i0.ɵɵclassMap(ctx.styleClass); i0.ɵɵproperty("ngClass", ctx.rootClass)("ngStyle", ctx.style); i0.ɵɵattribute("data-pc-name", "password")("data-pc-section", "root"); i0.ɵɵadvance(); i0.ɵɵclassMap(ctx.inputStyleClass); i0.ɵɵproperty("disabled", ctx.disabled)("pSize", ctx.size)("ngClass", i0.ɵɵpipeBind2(3, 27, ctx.disabled, ctx.inputFieldClass))("ngStyle", ctx.inputStyle)("value", ctx.value)("variant", ctx.variant)("pAutoFocus", ctx.autofocus); i0.ɵɵattribute("label", ctx.label)("aria-label", ctx.ariaLabel)("aria-labelledBy", ctx.ariaLabelledBy)("id", ctx.inputId)("type", i0.ɵɵpipeBind2(4, 30, ctx.unmasked, ctx.inputType))("placeholder", ctx.placeholder)("autocomplete", ctx.autocomplete)("maxlength", ctx.maxLength)("data-pc-section", "input"); i0.ɵɵadvance(4); i0.ɵɵproperty("ngIf", ctx.showClear && ctx.value != null); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx.toggleMask); i0.ɵɵadvance(); i0.ɵɵproperty("ngIf", ctx.overlayVisible); } }, dependencies: [CommonModule, i1.NgClass, i1.NgIf, i1.NgTemplateOutlet, i1.NgStyle, InputText, AutoFocus, TimesIcon, EyeSlashIcon, EyeIcon, MapperPipe, SharedModule], encapsulation: 2, data: { animation: [trigger('overlayAnimation', [transition(':enter', [style({ opacity: 0, transform: 'scaleY(0.8)' }), animate('{{showTransitionParams}}')]), transition(':leave', [animate('{{hideTransitionParams}}', style({ opacity: 0 }))])])] }, changeDetection: 0 }); } (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(Password, [{ type: Component, args: [{ selector: 'p-password', standalone: true, imports: [CommonModule, InputText, AutoFocus, TimesIcon, EyeSlashIcon, EyeIcon, MapperPipe, SharedModule], template: ` <div [ngClass]="rootClass" [ngStyle]="style" [class]="styleClass" [attr.data-pc-name]="'password'" [attr.data-pc-section]="'root'"> <input #input [attr.label]="label" [attr.aria-label]="ariaLabel" [attr.aria-labelledBy]="ariaLabelledBy" [attr.id]="inputId" pInputText [disabled]="disabled" [pSize]="size" [ngClass]="disabled | mapper