2018-12-26 19:02:41 +00:00
|
|
|
<template>
|
2020-09-27 18:10:38 +00:00
|
|
|
<input ref="input" type="password" :class="['p-inputtext p-component', {'p-filled': filled}]" :value="modelValue"
|
2020-09-21 11:43:12 +00:00
|
|
|
@input="onInput" @focus="onFocus" @blur="onBlur" @keyup="onKeyUp" />
|
2018-12-26 19:02:41 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-09-27 18:10:38 +00:00
|
|
|
import ConnectedOverlayScrollHandler from '../utils/ConnectedOverlayScrollHandler';
|
2018-12-26 19:02:41 +00:00
|
|
|
import DomHandler from '../utils/DomHandler';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2020-09-21 11:43:12 +00:00
|
|
|
modelValue: String,
|
2018-12-26 19:02:41 +00:00
|
|
|
promptLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'Enter a password'
|
|
|
|
},
|
2020-08-27 10:54:51 +00:00
|
|
|
mediumRegex: {
|
2020-08-11 09:48:02 +00:00
|
|
|
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: {
|
2020-08-11 09:48:02 +00:00
|
|
|
type: String,
|
|
|
|
default: '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})' // eslint-disable-line
|
|
|
|
},
|
2018-12-26 19:02:41 +00:00
|
|
|
weakLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'Weak'
|
|
|
|
},
|
|
|
|
mediumLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'Medium'
|
|
|
|
},
|
|
|
|
strongLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'Strong'
|
|
|
|
},
|
|
|
|
feedback: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
panel: null,
|
|
|
|
meter: null,
|
|
|
|
info: null,
|
2020-08-11 09:48:02 +00:00
|
|
|
mediumCheckRegExp: null,
|
|
|
|
strongCheckRegExp: null,
|
2020-09-27 18:10:38 +00:00
|
|
|
scrollHandler: null,
|
2020-08-11 09:48:02 +00:00
|
|
|
mounted() {
|
2020-08-27 10:54:51 +00:00
|
|
|
this.mediumCheckRegExp = new RegExp(this.mediumRegex);
|
|
|
|
this.strongCheckRegExp = new RegExp(this.strongRegex);
|
2020-08-11 09:48:02 +00:00
|
|
|
},
|
2020-09-27 18:10:38 +00:00
|
|
|
beforeUnmount() {
|
2020-09-28 10:58:09 +00:00
|
|
|
if (this.scrollHandler) {
|
|
|
|
this.scrollHandler.destroy();
|
|
|
|
this.scrollHandler = null;
|
|
|
|
}
|
2020-09-27 18:10:38 +00:00
|
|
|
},
|
2018-12-26 19:02:41 +00:00
|
|
|
methods: {
|
|
|
|
testStrength(str) {
|
2020-08-11 09:48:02 +00:00
|
|
|
let level = 0;
|
2018-12-26 19:02:41 +00:00
|
|
|
|
2020-08-11 09:48:02 +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
|
|
|
|
2020-08-11 09:48:02 +00:00
|
|
|
return level;
|
2018-12-26 19:02:41 +00:00
|
|
|
},
|
|
|
|
createPanel() {
|
|
|
|
this.panel = document.createElement('div');
|
2020-06-27 14:31:52 +00:00
|
|
|
this.panel.className = 'p-password-panel p-component p-password-panel-overlay p-connected-overlay';
|
2018-12-26 19:02:41 +00:00
|
|
|
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;
|
|
|
|
|
2020-09-21 11:43:12 +00:00
|
|
|
this.panel.style.minWidth = DomHandler.getOuterWidth(this.$el) + 'px';
|
2018-12-26 19:02:41 +00:00
|
|
|
this.panel.appendChild(this.meter);
|
|
|
|
this.panel.appendChild(this.info);
|
|
|
|
document.body.appendChild(this.panel);
|
2020-09-21 11:43:12 +00:00
|
|
|
},
|
2020-09-27 18:10:38 +00:00
|
|
|
onInput(event) {
|
|
|
|
this.$emit('update:modelValue', event.target.value)
|
2020-09-21 11:43:12 +00:00
|
|
|
},
|
2020-09-27 18:10:38 +00:00
|
|
|
showOverlay() {
|
2020-09-21 11:43:12 +00:00
|
|
|
if (this.feedback) {
|
|
|
|
if (!this.panel) {
|
|
|
|
this.createPanel();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.panel.style.zIndex = String(DomHandler.generateZIndex());
|
|
|
|
this.panel.style.display = 'block';
|
|
|
|
setTimeout(() => {
|
|
|
|
DomHandler.addClass(this.panel, 'p-connected-overlay-visible');
|
|
|
|
DomHandler.removeClass(this.panel, 'p-connected-overlay-hidden');
|
2020-09-27 18:10:38 +00:00
|
|
|
this.bindScrollListener();
|
2020-09-21 11:43:12 +00:00
|
|
|
}, 1);
|
|
|
|
DomHandler.absolutePosition(this.panel, this.$el);
|
|
|
|
}
|
|
|
|
},
|
2020-09-27 18:10:38 +00:00
|
|
|
hideOverlay() {
|
2020-09-21 11:43:12 +00:00
|
|
|
if (this.panel) {
|
|
|
|
DomHandler.addClass(this.panel, 'p-connected-overlay-hidden');
|
|
|
|
DomHandler.removeClass(this.panel, 'p-connected-overlay-visible');
|
2020-09-27 18:10:38 +00:00
|
|
|
this.unbindScrollListener();
|
2020-09-21 11:43:12 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.panel.style.display = 'none';
|
|
|
|
DomHandler.removeClass(this.panel, 'p-connected-overlay-hidden');
|
|
|
|
}, 150);
|
|
|
|
}
|
|
|
|
},
|
2020-09-27 18:10:38 +00:00
|
|
|
onFocus() {
|
|
|
|
this.showOverlay();
|
|
|
|
},
|
|
|
|
onBlur() {
|
|
|
|
this.hideOverlay();
|
|
|
|
},
|
2020-09-21 11:43:12 +00:00
|
|
|
onKeyUp(event) {
|
|
|
|
if (this.panel) {
|
|
|
|
let value = event.target.value;
|
|
|
|
let label = null;
|
|
|
|
let meterPos = null;
|
|
|
|
|
|
|
|
switch (this.testStrength(value)) {
|
|
|
|
case 1:
|
|
|
|
label = this.weakLabel;
|
|
|
|
meterPos = '0px -10px';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
label = this.mediumLabel;
|
|
|
|
meterPos = '0px -20px';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
label = this.strongLabel;
|
|
|
|
meterPos = '0px -30px';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
label = this.promptLabel;
|
|
|
|
meterPos = '0px 0px';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.meter.style.backgroundPosition = meterPos;
|
|
|
|
this.info.textContent = label;
|
2020-09-27 18:10:38 +00:00
|
|
|
|
|
|
|
if (!DomHandler.hasClass(this.panel, 'p-connected-overlay-visible')) {
|
|
|
|
this.showOverlay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
bindScrollListener() {
|
|
|
|
if (!this.scrollHandler) {
|
2020-09-28 10:58:09 +00:00
|
|
|
this.scrollHandler = new ConnectedOverlayScrollHandler(this.$refs.input, () => {
|
2020-09-27 18:10:38 +00:00
|
|
|
if (DomHandler.hasClass(this.panel, 'p-connected-overlay-visible')) {
|
|
|
|
this.hideOverlay();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scrollHandler.bindScrollListener();
|
|
|
|
},
|
|
|
|
unbindScrollListener() {
|
|
|
|
if (this.scrollHandler) {
|
|
|
|
this.scrollHandler.unbindScrollListener();
|
2020-09-21 11:43:12 +00:00
|
|
|
}
|
2018-12-26 19:02:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
filled() {
|
2020-09-21 11:43:12 +00:00
|
|
|
return (this.modelValue != null && this.modelValue.toString().length > 0)
|
2018-12-26 19:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-27 18:10:38 +00:00
|
|
|
</script>
|