primevue-mirror/src/components/password/Password.vue

341 lines
10 KiB
Vue
Raw Normal View History

2018-12-26 19:02:41 +00:00
<template>
2021-02-03 09:42:05 +00:00
<div :class="containerClass" :style="style">
<PInputText ref="input" :class="inputFieldClass" :style="inputStyle" :type="inputType" :value="modelValue" @input="onInput" @focus="onFocus" @blur="onBlur" @keyup="onKeyUp" v-bind="$attrs" />
2021-02-03 09:42:05 +00:00
<i v-if="toggleMask" :class="toggleIconClass" @click="onMaskToggle" />
2021-04-16 09:17:19 +00:00
<Teleport :to="appendTarget" :disabled="appendDisabled">
<transition name="p-connected-overlay" @enter="onOverlayEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave">
<div :ref="overlayRef" :class="panelStyleClass" v-if="overlayVisible" @click="onOverlayClick">
2021-03-01 20:54:17 +00:00
<slot name="header"></slot>
<slot name="content">
<div class="p-password-meter">
<div :class="strengthClass" :style="{'width': meter ? meter.width : ''}"></div>
</div>
<div class="p-password-info">{{infoText}}</div>
2021-03-01 20:54:17 +00:00
</slot>
<slot name="footer"></slot>
</div>
</transition>
</Teleport>
2021-02-03 09:42:05 +00:00
</div>
2018-12-26 19:02:41 +00:00
</template>
<script>
import {ConnectedOverlayScrollHandler,DomHandler,ZIndexUtils} from 'primevue/utils';
2021-03-03 10:30:19 +00:00
import OverlayEventBus from 'primevue/overlayeventbus';
2021-02-03 09:42:05 +00:00
import InputText from 'primevue/inputtext';
2018-12-26 19:02:41 +00:00
export default {
name: 'Password',
emits: ['update:modelValue'],
inheritAttrs: false,
2018-12-26 19:02:41 +00:00
props: {
2020-09-21 11:43:12 +00:00
modelValue: String,
2018-12-26 19:02:41 +00:00
promptLabel: {
type: String,
2020-12-09 12:24:11 +00:00
default: null
2018-12-26 19:02:41 +00:00
},
2020-08-27 10:54:51 +00:00
mediumRegex: {
type: String,
default: '^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})' // eslint-disable-line
},
2020-08-27 10:54:51 +00:00
strongRegex: {
type: String,
default: '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})' // eslint-disable-line
},
2018-12-26 19:02:41 +00:00
weakLabel: {
type: String,
2020-12-09 12:24:11 +00:00
default: null
2018-12-26 19:02:41 +00:00
},
mediumLabel: {
type: String,
2020-12-09 12:24:11 +00:00
default: null
2018-12-26 19:02:41 +00:00
},
strongLabel: {
type: String,
2020-12-09 12:24:11 +00:00
default: null
2018-12-26 19:02:41 +00:00
},
feedback: {
type: Boolean,
default: true
2021-02-03 09:42:05 +00:00
},
appendTo: {
type: String,
2021-03-01 20:54:17 +00:00
default: 'body'
2021-02-03 09:42:05 +00:00
},
toggleMask: {
type: Boolean,
default: false
},
hideIcon: {
type: String,
default: 'pi pi-eye-slash'
},
showIcon: {
type: String,
default: 'pi pi-eye'
},
inputClass: String,
inputStyle: null,
2021-02-03 09:42:05 +00:00
style: null,
class: String,
panelClass: String
2018-12-26 19:02:41 +00:00
},
2020-09-28 11:04:30 +00:00
data() {
return {
overlayVisible: false,
meter: null,
2021-02-03 09:42:05 +00:00
infoText: null,
focused: false,
unmasked: false
2020-09-28 11:04:30 +00:00
};
},
mediumCheckRegExp: null,
strongCheckRegExp: null,
2020-09-28 11:27:22 +00:00
resizeListener: null,
2020-09-27 18:10:38 +00:00
scrollHandler: null,
2020-09-28 11:04:30 +00:00
overlay: null,
mounted() {
2021-02-03 09:42:05 +00:00
this.infoText = this.promptText;
2020-08-27 10:54:51 +00:00
this.mediumCheckRegExp = new RegExp(this.mediumRegex);
this.strongCheckRegExp = new RegExp(this.strongRegex);
},
2020-09-27 18:10:38 +00:00
beforeUnmount() {
2020-09-28 11:27:22 +00:00
this.unbindResizeListener();
2020-09-28 10:58:09 +00:00
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
if (this.overlay) {
ZIndexUtils.clear(this.overlay);
this.overlay = null;
}
2020-09-27 18:10:38 +00:00
},
2018-12-26 19:02:41 +00:00
methods: {
onOverlayEnter(el) {
ZIndexUtils.set('overlay', el, this.$primevue.config.zIndex.overlay);
2021-02-03 09:42:05 +00:00
this.alignOverlay();
2020-09-28 11:04:30 +00:00
this.bindScrollListener();
2020-09-28 11:27:22 +00:00
this.bindResizeListener();
2020-09-28 11:04:30 +00:00
},
onOverlayLeave() {
this.unbindScrollListener();
2020-09-28 11:27:22 +00:00
this.unbindResizeListener();
2020-09-28 11:04:30 +00:00
this.overlay = null;
},
onOverlayAfterLeave(el) {
ZIndexUtils.clear(el);
},
2021-02-03 09:42:05 +00:00
alignOverlay() {
2021-04-16 09:17:19 +00:00
if (this.appendDisabled) {
DomHandler.relativePosition(this.overlay, this.$refs.input.$el);
}
else {
this.overlay.style.minWidth = DomHandler.getOuterWidth(this.$refs.input.$el) + 'px';
DomHandler.absolutePosition(this.overlay, this.$refs.input.$el);
}
2021-02-03 09:42:05 +00:00
},
2018-12-26 19:02:41 +00:00
testStrength(str) {
let level = 0;
2018-12-26 19:02:41 +00:00
if (this.strongCheckRegExp.test(str))
level = 3;
else if (this.mediumCheckRegExp.test(str))
level = 2;
else if (str.length)
level = 1;
2018-12-26 19:02:41 +00:00
return level;
2018-12-26 19:02:41 +00:00
},
2020-09-27 18:10:38 +00:00
onInput(event) {
2022-03-22 07:56:12 +00:00
this.$emit('update:modelValue', event.target.value)
2020-09-21 11:43:12 +00:00
},
2020-09-28 11:04:30 +00:00
onFocus() {
2021-02-03 09:42:05 +00:00
this.focused = true;
2020-09-21 11:43:12 +00:00
if (this.feedback) {
2020-09-28 11:04:30 +00:00
this.overlayVisible = true;
2020-09-21 11:43:12 +00:00
}
},
2020-09-27 18:10:38 +00:00
onBlur() {
2021-02-03 09:42:05 +00:00
this.focused = false;
2020-09-28 11:04:30 +00:00
if (this.feedback) {
this.overlayVisible = false;
}
2020-09-27 18:10:38 +00:00
},
2020-09-21 11:43:12 +00:00
onKeyUp(event) {
2020-09-28 11:04:30 +00:00
if (this.feedback) {
const value = event.target.value;
2020-09-21 11:43:12 +00:00
let label = null;
let meter = null;
2020-09-21 11:43:12 +00:00
switch (this.testStrength(value)) {
case 1:
2020-12-09 12:24:11 +00:00
label = this.weakText;
meter = {
strength: 'weak',
width: '33.33%'
};
2020-09-21 11:43:12 +00:00
break;
case 2:
2020-12-09 12:24:11 +00:00
label = this.mediumText;
meter = {
strength: 'medium',
width: '66.66%'
};
2020-09-21 11:43:12 +00:00
break;
case 3:
2020-12-09 12:24:11 +00:00
label = this.strongText;
meter = {
strength: 'strong',
width: '100%'
};
2020-09-21 11:43:12 +00:00
break;
default:
2020-12-09 12:24:11 +00:00
label = this.promptText;
meter = null;
2020-09-21 11:43:12 +00:00
break;
}
this.meter = meter;
2020-09-28 11:04:30 +00:00
this.infoText = label;
2020-09-27 18:10:38 +00:00
2020-09-28 11:04:30 +00:00
if (!this.overlayVisible) {
this.overlayVisible = true;
2020-09-27 18:10:38 +00:00
}
}
},
bindScrollListener() {
if (!this.scrollHandler) {
2021-02-03 09:42:05 +00:00
this.scrollHandler = new ConnectedOverlayScrollHandler(this.$refs.input.$el, () => {
2020-09-28 11:04:30 +00:00
if (this.overlayVisible) {
this.overlayVisible = false;
2020-09-27 18:10:38 +00:00
}
});
}
this.scrollHandler.bindScrollListener();
},
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
2020-09-21 11:43:12 +00:00
}
2020-09-28 11:04:30 +00:00
},
2020-09-28 11:27:22 +00:00
bindResizeListener() {
if (!this.resizeListener) {
this.resizeListener = () => {
if (this.overlayVisible) {
this.overlayVisible = false;
}
};
window.addEventListener('resize', this.resizeListener);
}
},
unbindResizeListener() {
if (this.resizeListener) {
window.removeEventListener('resize', this.resizeListener);
this.resizeListener = null;
}
},
2020-09-28 11:04:30 +00:00
overlayRef(el) {
this.overlay = el;
2021-02-03 09:42:05 +00:00
},
onMaskToggle() {
this.unmasked = !this.unmasked;
},
onOverlayClick(event) {
OverlayEventBus.emit('overlay-click', {
originalEvent: event,
target: this.$el
});
2018-12-26 19:02:41 +00:00
}
},
computed: {
2021-02-03 09:42:05 +00:00
containerClass() {
return ['p-password p-component p-inputwrapper', this.class, {
'p-inputwrapper-filled': this.filled,
'p-inputwrapper-focus': this.focused,
'p-input-icon-right': this.toggleMask
}];
},
inputFieldClass() {
return ['p-password-input', this.inputClass, {
2021-02-03 09:42:05 +00:00
'p-disabled': this.$attrs.disabled
}];
},
panelStyleClass() {
return ['p-password-panel p-component', this.panelClass, {
'p-input-filled': this.$primevue.config.inputStyle === 'filled',
'p-ripple-disabled': this.$primevue.config.ripple === false
}];
},
2021-02-03 09:42:05 +00:00
toggleIconClass() {
return this.unmasked ? this.hideIcon : this.showIcon;
2021-02-03 09:42:05 +00:00
},
strengthClass() {
return `p-password-strength ${this.meter ? this.meter.strength : ''}`;
},
2021-02-03 09:42:05 +00:00
inputType() {
return this.unmasked ? 'text' : 'password';
},
2018-12-26 19:02:41 +00:00
filled() {
2020-09-21 11:43:12 +00:00
return (this.modelValue != null && this.modelValue.toString().length > 0)
2020-12-09 12:24:11 +00:00
},
weakText() {
return this.weakLabel || this.$primevue.config.locale.weak;
},
mediumText() {
return this.mediumLabel || this.$primevue.config.locale.medium;
},
strongText() {
return this.strongLabel || this.$primevue.config.locale.strong;
},
promptText() {
return this.promptLabel || this.$primevue.config.locale.passwordPrompt;
2021-04-16 09:17:19 +00:00
},
appendDisabled() {
return this.appendTo === 'self';
},
appendTarget() {
return this.appendDisabled ? null : this.appendTo;
2018-12-26 19:02:41 +00:00
}
2021-02-03 09:42:05 +00:00
},
components: {
'PInputText': InputText
2018-12-26 19:02:41 +00:00
}
}
2020-09-27 18:10:38 +00:00
</script>
2021-02-03 09:42:05 +00:00
<style>
.p-password {
position: relative;
display: inline-flex;
}
.p-password-panel {
position: absolute;
top: 0;
left: 0;
2021-02-03 09:42:05 +00:00
}
.p-password .p-password-panel {
min-width: 100%;
}
.p-password-meter {
height: 10px;
}
2021-02-03 09:56:35 +00:00
.p-password-strength {
height: 100%;
width: 0;
transition: width 1s ease-in-out;
}
2021-02-03 09:56:35 +00:00
.p-fluid .p-password {
display: flex;
}
2021-02-03 09:42:05 +00:00
</style>