mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
5.1 KiB
184 lines
5.1 KiB
4 years ago
|
<template>
|
||
|
<div>
|
||
|
<v-text-field
|
||
|
v-bind="$attrs"
|
||
|
ref="input"
|
||
4 years ago
|
v-model="autocomplete"
|
||
4 years ago
|
:style="styles"
|
||
3 years ago
|
:class="[
|
||
|
classNames,
|
||
|
{ 'env-valid': isEnvFound && isEnvUsageValid, 'env-invalid': isEnvFound && !isEnvUsageValid },
|
||
|
]"
|
||
4 years ago
|
@input="onInput"
|
||
|
@keydown.native="onKeyup"
|
||
|
/>
|
||
4 years ago
|
|
||
3 years ago
|
<v-menu ref="autoMenu" v-model="show" :position-x="x" :position-y="y" dense :min-width="width">
|
||
4 years ago
|
<v-list dense>
|
||
|
<v-list-item
|
||
|
v-for="(item, index) in envValues"
|
||
|
:key="index"
|
||
|
@click="onSelect(item)"
|
||
4 years ago
|
@keyup.enter="onSelect(item)"
|
||
4 years ago
|
>
|
||
|
<v-list-item-title>{{ item }}</v-list-item-title>
|
||
|
</v-list-item>
|
||
|
</v-list>
|
||
|
</v-menu>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
4 years ago
|
// http://jsfiddle.net/pranavcbalan/qg5xyko6/
|
||
4 years ago
|
|
||
4 years ago
|
export default {
|
||
|
name: 'XAutoComplete',
|
||
|
props: {
|
||
|
value: String,
|
||
|
env: String,
|
||
3 years ago
|
styles: [Array, Object, String],
|
||
4 years ago
|
},
|
||
4 years ago
|
data() {
|
||
4 years ago
|
return {
|
||
|
show: false,
|
||
|
x: 0,
|
||
|
y: 0,
|
||
|
activeValue: '',
|
||
|
curPos: 0,
|
||
|
input: null,
|
||
3 years ago
|
el: null,
|
||
|
};
|
||
4 years ago
|
},
|
||
|
computed: {
|
||
|
// filtered list based on input
|
||
4 years ago
|
items() {
|
||
3 years ago
|
return this.envValues.filter(s => s.includes(this.activeValue));
|
||
4 years ago
|
},
|
||
4 years ago
|
// for setting menu width to textfield width
|
||
4 years ago
|
width() {
|
||
3 years ago
|
return this.input && this.input.clientWidth;
|
||
4 years ago
|
},
|
||
4 years ago
|
// extracting class names from root element
|
||
4 years ago
|
classNames() {
|
||
3 years ago
|
return this.el && this.el.className;
|
||
4 years ago
|
},
|
||
|
// v-model for the text filed
|
||
|
autocomplete: {
|
||
4 years ago
|
get() {
|
||
3 years ago
|
return this.value;
|
||
4 years ago
|
},
|
||
4 years ago
|
set(val) {
|
||
3 years ago
|
this.$emit('input', val);
|
||
|
},
|
||
4 years ago
|
},
|
||
4 years ago
|
// extracting current env keys
|
||
4 years ago
|
envValues() {
|
||
3 years ago
|
const envObj = this.isDashboard
|
||
|
? this.$store.getters['project/GtrApiEnvironment']
|
||
|
: this.$store.getters['project/GtrDefaultApiEnvironment'];
|
||
|
return envObj && envObj[this.env] ? Object.keys(envObj[this.env]) : [];
|
||
4 years ago
|
},
|
||
4 years ago
|
isEnvFound() {
|
||
3 years ago
|
return /{{\s*\w+\s*}}/.test(this.value);
|
||
4 years ago
|
},
|
||
4 years ago
|
isEnvUsageValid() {
|
||
3 years ago
|
const re = /{{\s*(\w+)\s*}}/g;
|
||
|
let m;
|
||
4 years ago
|
// eslint-disable-next-line no-cond-assign
|
||
3 years ago
|
while ((m = re.exec(this.value))) {
|
||
|
if (!this.envValues.includes(m[1])) {
|
||
|
return false;
|
||
|
}
|
||
4 years ago
|
}
|
||
3 years ago
|
return true;
|
||
|
},
|
||
4 years ago
|
},
|
||
4 years ago
|
mounted() {
|
||
3 years ago
|
this.el = this.$el;
|
||
4 years ago
|
// getting input element reference
|
||
3 years ago
|
this.input = this.$refs.input && this.$refs.input.$el.querySelector('input');
|
||
4 years ago
|
},
|
||
3 years ago
|
created() {},
|
||
4 years ago
|
methods: {
|
||
|
// handling input event
|
||
4 years ago
|
onInput(v) {
|
||
3 years ago
|
this.curPos = this.input.selectionStart || 0;
|
||
4 years ago
|
// extracting string from beginning to caret position
|
||
|
// then using regex to check and extract certain pattern like
|
||
|
// eg: {{ , {{ someWord,...
|
||
3 years ago
|
const m = v.slice(0, this.curPos).match(/{{\s?(\w*)$/);
|
||
4 years ago
|
if (m) {
|
||
3 years ago
|
this.activeValue = m[1] || '';
|
||
|
this.show = true;
|
||
4 years ago
|
// calculate menu position relative to input element
|
||
3 years ago
|
this.x = this.input.getBoundingClientRect().left; // + (this.curPos * 8) % this.$refs.input.$el.clientWidth;
|
||
|
this.y = this.input.getBoundingClientRect().top + this.input.clientHeight;
|
||
4 years ago
|
} else {
|
||
3 years ago
|
this.show = false;
|
||
4 years ago
|
}
|
||
|
},
|
||
4 years ago
|
onKeyup(e) {
|
||
3 years ago
|
const menu = this.$refs.autoMenu;
|
||
4 years ago
|
// handlig up and down keys
|
||
|
if (this.show && (e.which === 40 || e.which === 38)) {
|
||
3 years ago
|
e.preventDefault();
|
||
|
menu.onKeyDown(e);
|
||
|
return;
|
||
4 years ago
|
}
|
||
|
// handling enter and space
|
||
|
if (this.show && (e.which === 13 || e.which === 32)) {
|
||
3 years ago
|
menu.onKeyDown(e);
|
||
4 years ago
|
}
|
||
|
},
|
||
4 years ago
|
onSelect(item) {
|
||
4 years ago
|
// appending menu value to the input
|
||
|
this.input.setRangeText(
|
||
|
// add closing only when its necessary
|
||
3 years ago
|
item.slice(this.activeValue.length) +
|
||
|
(this.autocomplete.substr(this.input.selectionEnd, 2) === '}}' ? '' : '}}'),
|
||
4 years ago
|
this.input.selectionStart,
|
||
|
this.input.selectionEnd,
|
||
|
'end'
|
||
3 years ago
|
);
|
||
4 years ago
|
// trigger input event to sync with vue model
|
||
3 years ago
|
this.input.dispatchEvent(new Event('input'));
|
||
|
this.show = false;
|
||
|
},
|
||
|
},
|
||
|
};
|
||
4 years ago
|
</script>
|
||
|
|
||
|
<style scoped>
|
||
3 years ago
|
/deep/ .env-valid input {
|
||
|
color: var(--v-success-base) !important;
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
/deep/ .env-invalid input {
|
||
|
color: var(--v-error-base) !important;
|
||
|
}
|
||
4 years ago
|
</style>
|
||
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|