2022-04-26 20:13:17 +08:00
|
|
|
<template>
|
2022-07-08 19:49:22 +08:00
|
|
|
<div class="sci-inline-edit" :class="{ 'editing': editing }" tabindex="0" @keyup.enter="enableEdit($event)">
|
2022-07-11 17:23:54 +08:00
|
|
|
<div class="sci-inline-edit__content" :class="{ 'error': error }">
|
2022-05-11 21:51:26 +08:00
|
|
|
<textarea
|
|
|
|
ref="input"
|
|
|
|
rows="1"
|
|
|
|
v-if="editing"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
v-model="newValue"
|
|
|
|
@input="handleInput"
|
|
|
|
@keydown="handleKeypress"
|
|
|
|
@paste="handlePaste"
|
|
|
|
@blur="handleBlur"
|
2022-07-12 16:41:58 +08:00
|
|
|
@keyup.escape="cancelEdit"
|
2022-05-11 21:51:26 +08:00
|
|
|
></textarea>
|
2023-03-09 18:38:47 +08:00
|
|
|
<div v-else-if="smartAnnotation" @click="enableEdit($event)" class="sci-inline-edit__view" v-html="sa_value || placeholder" :class="{ 'blank': isBlank }"></div>
|
|
|
|
<div v-else @click="enableEdit($event)" class="sci-inline-edit__view" :class="{ 'blank': isBlank }">{{newValue || placeholder}}</div>
|
2022-04-26 20:13:17 +08:00
|
|
|
<div v-if="editing && error" class="sci-inline-edit__error">
|
|
|
|
{{ error }}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-09 20:40:45 +08:00
|
|
|
<template v-if="editing">
|
2022-07-11 16:44:27 +08:00
|
|
|
<div :class="{ 'btn-primary': !error, 'btn-disabled': error }" class="sci-inline-edit__control btn icon-btn" @click="update">
|
2022-05-11 21:51:26 +08:00
|
|
|
<i class="fas fa-check"></i>
|
2022-04-26 20:13:17 +08:00
|
|
|
</div>
|
2022-08-10 21:34:17 +08:00
|
|
|
<div class="sci-inline-edit__control btn btn-light icon-btn" @mousedown="cancelEdit">
|
2022-05-11 21:51:26 +08:00
|
|
|
<i class="fas fa-times"></i>
|
2022-04-26 20:13:17 +08:00
|
|
|
</div>
|
2022-06-09 20:40:45 +08:00
|
|
|
</template>
|
2022-04-26 20:13:17 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-08-04 17:06:25 +08:00
|
|
|
import UtilsMixin from 'vue/mixins/utils.js';
|
|
|
|
|
2022-04-26 20:13:17 +08:00
|
|
|
export default {
|
|
|
|
name: 'InlineEdit',
|
|
|
|
props: {
|
|
|
|
value: { type: String, default: '' },
|
2022-07-08 18:51:43 +08:00
|
|
|
sa_value: { type: String},
|
2022-04-26 20:13:17 +08:00
|
|
|
allowBlank: { type: Boolean, default: true },
|
|
|
|
attributeName: { type: String, required: true },
|
|
|
|
characterLimit: { type: Number },
|
2023-02-03 22:36:32 +08:00
|
|
|
characterMinLimit: { type: Number },
|
2022-04-26 20:13:17 +08:00
|
|
|
placeholder: { type: String },
|
2022-05-11 21:51:26 +08:00
|
|
|
autofocus: { type: Boolean, default: false },
|
2022-07-25 17:21:31 +08:00
|
|
|
saveOnEnter: { type: Boolean, default: true },
|
|
|
|
allowNewLine: { type: Boolean, default: false },
|
2022-07-07 20:33:38 +08:00
|
|
|
multilinePaste: { type: Boolean, default: false },
|
2022-07-08 18:51:43 +08:00
|
|
|
smartAnnotation: { type: Boolean, default: false },
|
2022-08-08 22:50:34 +08:00
|
|
|
editOnload: { type: Boolean, default: false },
|
|
|
|
defaultValue: { type: String, default: '' }
|
2022-04-26 20:13:17 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
editing: false,
|
2022-08-10 20:43:23 +08:00
|
|
|
dirty: false,
|
2022-04-26 20:13:17 +08:00
|
|
|
newValue: ''
|
|
|
|
}
|
|
|
|
},
|
2022-08-04 17:06:25 +08:00
|
|
|
mixins: [UtilsMixin],
|
2022-04-26 20:13:17 +08:00
|
|
|
created( ){
|
2022-04-26 20:30:07 +08:00
|
|
|
this.newValue = this.value || '';
|
2022-04-26 20:13:17 +08:00
|
|
|
},
|
|
|
|
mounted() {
|
2022-05-05 18:56:31 +08:00
|
|
|
this.handleAutofocus();
|
2022-07-07 20:33:38 +08:00
|
|
|
if (this.editOnload) {
|
|
|
|
this.enableEdit();
|
|
|
|
}
|
2022-05-05 18:56:31 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
autofocus() {
|
|
|
|
this.handleAutofocus();
|
2022-08-08 17:32:10 +08:00
|
|
|
},
|
|
|
|
value() {
|
|
|
|
this.newValue = this.value;
|
2022-04-26 20:13:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
isBlank() {
|
|
|
|
return this.newValue.length === 0
|
|
|
|
},
|
|
|
|
error() {
|
|
|
|
if(!this.allowBlank && this.isBlank) {
|
|
|
|
return this.i18n.t('inline_edit.errors.blank', { attribute: this.attributeName })
|
|
|
|
}
|
|
|
|
if(this.characterLimit && this.newValue.length > this.characterLimit) {
|
|
|
|
return(
|
|
|
|
this.i18n.t('inline_edit.errors.over_limit',
|
2022-08-04 17:06:25 +08:00
|
|
|
{
|
|
|
|
attribute: this.attributeName,
|
|
|
|
limit: this.numberWithSpaces(this.characterLimit)
|
|
|
|
}
|
2022-04-26 20:13:17 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2023-02-03 22:36:32 +08:00
|
|
|
if (this.characterMinLimit && this.newValue.length < this.characterMinLimit) {
|
|
|
|
return (
|
|
|
|
this.i18n.t('inline_edit.errors.below_limit',
|
|
|
|
{
|
|
|
|
attribute: this.attributeName,
|
|
|
|
limit: this.numberWithSpaces(this.characterMinLimit)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2022-04-26 20:13:17 +08:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2022-05-05 18:56:31 +08:00
|
|
|
handleAutofocus() {
|
2022-07-11 17:52:38 +08:00
|
|
|
if (this.autofocus || !this.placeholder && this.isBlank || this.editOnload && this.isBlank) {
|
2022-05-11 21:51:26 +08:00
|
|
|
this.enableEdit();
|
2022-05-05 18:56:31 +08:00
|
|
|
setTimeout(this.focus, 50);
|
|
|
|
}
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
handleBlur() {
|
2022-07-08 19:49:22 +08:00
|
|
|
if ($('.atwho-view:visible').length) return;
|
2022-07-08 18:51:43 +08:00
|
|
|
|
2022-07-04 19:55:26 +08:00
|
|
|
if (this.allowBlank || !this.isBlank) {
|
2022-05-11 21:51:26 +08:00
|
|
|
this.$nextTick(this.update);
|
|
|
|
} else {
|
|
|
|
this.$emit('delete');
|
|
|
|
}
|
|
|
|
},
|
2022-04-26 20:13:17 +08:00
|
|
|
focus() {
|
|
|
|
this.$nextTick(() => {
|
2022-06-09 20:40:45 +08:00
|
|
|
if (!this.$refs.input) return;
|
|
|
|
|
2022-04-26 20:13:17 +08:00
|
|
|
this.$refs.input.focus();
|
|
|
|
this.resize();
|
|
|
|
});
|
|
|
|
},
|
2022-07-08 19:49:22 +08:00
|
|
|
enableEdit(e) {
|
2022-09-09 17:15:33 +08:00
|
|
|
if (e && $(e.target).hasClass('atwho-user-popover')) return;
|
|
|
|
if (e && $(e.target).hasClass('sa-link')) return;
|
|
|
|
if (e && $(e.target).parent().hasClass('atwho-inserted')) return;
|
2022-08-05 17:23:36 +08:00
|
|
|
|
2022-04-26 20:13:17 +08:00
|
|
|
this.editing = true;
|
|
|
|
this.focus();
|
2022-07-08 18:51:43 +08:00
|
|
|
this.$nextTick(() => {
|
2022-08-08 22:50:34 +08:00
|
|
|
if (this.$refs.input.value === this.defaultValue) {
|
|
|
|
this.$refs.input.select();
|
|
|
|
}
|
2022-07-08 18:51:43 +08:00
|
|
|
if (this.smartAnnotation) {
|
|
|
|
SmartAnnotation.init($(this.$refs.input));
|
|
|
|
}
|
|
|
|
})
|
2022-05-05 18:56:31 +08:00
|
|
|
this.$emit('editingEnabled');
|
2022-04-26 20:13:17 +08:00
|
|
|
},
|
|
|
|
cancelEdit() {
|
|
|
|
this.editing = false;
|
|
|
|
this.newValue = this.value || '';
|
2022-05-05 18:56:31 +08:00
|
|
|
this.$emit('editingDisabled');
|
|
|
|
},
|
2022-05-11 21:51:26 +08:00
|
|
|
handlePaste(e) {
|
2022-08-10 20:43:23 +08:00
|
|
|
this.dirty = true;
|
|
|
|
|
2022-05-11 21:51:26 +08:00
|
|
|
if (!this.multilinePaste) return;
|
2022-07-19 22:44:35 +08:00
|
|
|
let lines = (e.originalEvent || e).clipboardData.getData('text/plain').split(/[\n\r]/);
|
|
|
|
lines = lines.filter((l) => l).map((l) => l.trim());
|
|
|
|
|
|
|
|
if (lines.length > 1) {
|
|
|
|
this.newValue = lines[0];
|
|
|
|
this.$emit('multilinePaste', lines);
|
|
|
|
}
|
2022-05-11 21:51:26 +08:00
|
|
|
},
|
2022-05-05 18:56:31 +08:00
|
|
|
handleInput() {
|
2022-08-10 20:43:23 +08:00
|
|
|
this.dirty = true;
|
2022-07-25 17:21:31 +08:00
|
|
|
if (!this.allowNewLine) {
|
|
|
|
this.newValue = this.newValue.replace(/^[\n\r]+|[\n\r]+$/g, '');
|
|
|
|
}
|
2022-05-05 18:56:31 +08:00
|
|
|
this.$nextTick(this.resize);
|
|
|
|
},
|
|
|
|
handleKeypress(e) {
|
2022-07-25 17:21:31 +08:00
|
|
|
if (e.key == 'Escape') {
|
|
|
|
this.cancelEdit();
|
|
|
|
} else if (e.key == 'Enter' && this.saveOnEnter) {
|
|
|
|
this.update();
|
2022-08-10 20:43:23 +08:00
|
|
|
} else {
|
|
|
|
this.dirty = true;
|
2022-05-05 18:56:31 +08:00
|
|
|
}
|
2022-04-26 20:13:17 +08:00
|
|
|
},
|
|
|
|
resize() {
|
2022-07-19 22:44:35 +08:00
|
|
|
if (!this.$refs.input) return;
|
|
|
|
|
2022-04-26 20:13:17 +08:00
|
|
|
this.$refs.input.style.height = "auto";
|
2022-05-05 18:56:31 +08:00
|
|
|
this.$refs.input.style.height = (this.$refs.input.scrollHeight) + "px";
|
2022-04-26 20:13:17 +08:00
|
|
|
},
|
|
|
|
update() {
|
2022-08-10 20:43:23 +08:00
|
|
|
if (!this.dirty && !this.isBlank) {
|
|
|
|
this.editing = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-10 21:34:17 +08:00
|
|
|
if(this.error) return;
|
|
|
|
if(!this.$refs.input) return;
|
|
|
|
this.newValue = this.$refs.input.value // Fix for smart annotation
|
|
|
|
this.newValue = this.newValue.trim();
|
|
|
|
this.editing = false;
|
|
|
|
this.$emit('editingDisabled');
|
|
|
|
this.$emit('update', this.newValue);
|
2022-04-26 20:13:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|