2021-10-30 01:39:46 +00:00
|
|
|
<template>
|
2021-11-11 09:53:33 +00:00
|
|
|
<div class="custom-attribute">
|
2021-10-30 01:39:46 +00:00
|
|
|
<div class="title-wrap">
|
|
|
|
<h4 class="text-block-title title error">
|
|
|
|
<span class="attribute-name" :class="{ error: $v.editedValue.$error }">
|
|
|
|
{{ label }}
|
|
|
|
</span>
|
|
|
|
</h4>
|
|
|
|
</div>
|
|
|
|
<div v-show="isEditing">
|
|
|
|
<div class="input-group small">
|
|
|
|
<input
|
|
|
|
ref="inputfield"
|
|
|
|
v-model="editedValue"
|
|
|
|
:type="inputType"
|
|
|
|
class="input-group-field"
|
|
|
|
autofocus="true"
|
|
|
|
:class="{ error: $v.editedValue.$error }"
|
|
|
|
@blur="$v.editedValue.$touch"
|
|
|
|
@keyup.enter="onUpdate"
|
|
|
|
/>
|
|
|
|
<div class="input-group-button">
|
|
|
|
<woot-button size="small" icon="ion-checkmark" @click="onUpdate" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<span v-if="shouldShowErrorMessage" class="error-message">
|
|
|
|
{{ errorMessage }}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
v-show="!isEditing"
|
|
|
|
class="value--view"
|
|
|
|
:class="{ 'is-editable': showActions }"
|
|
|
|
>
|
|
|
|
<a
|
|
|
|
v-if="isAttributeTypeLink"
|
|
|
|
:href="value"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
class="value"
|
|
|
|
>
|
|
|
|
{{ value || '---' }}
|
|
|
|
</a>
|
|
|
|
<p v-else class="value">
|
2021-11-11 09:53:33 +00:00
|
|
|
{{ formattedValue || '---' }}
|
2021-10-30 01:39:46 +00:00
|
|
|
</p>
|
|
|
|
<woot-button
|
|
|
|
v-if="showActions"
|
|
|
|
v-tooltip="$t('CUSTOM_ATTRIBUTES.ACTIONS.COPY')"
|
|
|
|
variant="link"
|
|
|
|
size="small"
|
|
|
|
color-scheme="secondary"
|
|
|
|
icon="ion-clipboard"
|
|
|
|
class-names="edit-button"
|
|
|
|
@click="onCopy"
|
|
|
|
/>
|
|
|
|
<woot-button
|
|
|
|
v-if="showActions"
|
|
|
|
v-tooltip="$t('CUSTOM_ATTRIBUTES.ACTIONS.EDIT')"
|
|
|
|
variant="link"
|
|
|
|
size="small"
|
|
|
|
color-scheme="secondary"
|
|
|
|
icon="ion-compose"
|
|
|
|
class-names="edit-button"
|
|
|
|
@click="onEdit"
|
|
|
|
/>
|
|
|
|
<woot-button
|
|
|
|
v-if="showActions"
|
|
|
|
v-tooltip="$t('CUSTOM_ATTRIBUTES.ACTIONS.DELETE')"
|
|
|
|
variant="link"
|
|
|
|
size="small"
|
|
|
|
color-scheme="secondary"
|
|
|
|
icon="ion-trash-a"
|
|
|
|
class-names="edit-button"
|
|
|
|
@click="onDelete"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-11-11 09:53:33 +00:00
|
|
|
import format from 'date-fns/format';
|
2021-10-30 01:39:46 +00:00
|
|
|
import { required, url } from 'vuelidate/lib/validators';
|
|
|
|
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
|
|
|
|
2021-11-11 09:53:33 +00:00
|
|
|
const DATE_FORMAT = 'yyyy-MM-dd';
|
|
|
|
|
2021-10-30 01:39:46 +00:00
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
label: { type: String, required: true },
|
|
|
|
value: { type: [String, Number], default: '' },
|
|
|
|
showActions: { type: Boolean, default: false },
|
|
|
|
attributeType: { type: String, default: 'text' },
|
|
|
|
attributeKey: { type: String, required: true },
|
2021-11-11 09:53:33 +00:00
|
|
|
contactId: { type: Number, default: null },
|
2021-10-30 01:39:46 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isEditing: false,
|
2021-11-11 09:53:33 +00:00
|
|
|
editedValue:
|
|
|
|
this.attributeType === 'date'
|
2021-11-15 09:26:35 +00:00
|
|
|
? format(new Date(this.value || new Date()), DATE_FORMAT)
|
2021-11-11 09:53:33 +00:00
|
|
|
: this.value,
|
2021-10-30 01:39:46 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
validations() {
|
|
|
|
if (this.isAttributeTypeLink) {
|
|
|
|
return {
|
|
|
|
editedValue: { required, url },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
editedValue: { required },
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
isAttributeTypeLink() {
|
|
|
|
return this.attributeType === 'link';
|
|
|
|
},
|
|
|
|
inputType() {
|
|
|
|
return this.isAttributeTypeLink ? 'url' : this.attributeType;
|
|
|
|
},
|
|
|
|
shouldShowErrorMessage() {
|
|
|
|
return this.$v.editedValue.$error;
|
|
|
|
},
|
|
|
|
errorMessage() {
|
|
|
|
if (this.$v.editedValue.url) {
|
|
|
|
return this.$t('CUSTOM_ATTRIBUTES.VALIDATIONS.INVALID_URL');
|
|
|
|
}
|
|
|
|
return this.$t('CUSTOM_ATTRIBUTES.VALIDATIONS.REQUIRED');
|
|
|
|
},
|
2021-11-11 09:53:33 +00:00
|
|
|
formattedValue() {
|
|
|
|
if (this.attributeType === 'date') {
|
|
|
|
return format(new Date(this.editedValue), 'dd-MM-yyyy');
|
|
|
|
}
|
|
|
|
return this.editedValue;
|
|
|
|
},
|
2021-10-30 01:39:46 +00:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
bus.$on(BUS_EVENTS.FOCUS_CUSTOM_ATTRIBUTE, focusAttributeKey => {
|
|
|
|
if (this.attributeKey === focusAttributeKey) {
|
|
|
|
this.onEdit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
focusInput() {
|
|
|
|
if (this.$refs.inputfield) {
|
|
|
|
this.$refs.inputfield.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onEdit() {
|
|
|
|
this.isEditing = true;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.focusInput();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onUpdate() {
|
2021-11-11 09:53:33 +00:00
|
|
|
const updatedValue =
|
|
|
|
this.attributeType === 'date'
|
|
|
|
? format(new Date(this.editedValue), DATE_FORMAT)
|
|
|
|
: this.editedValue;
|
|
|
|
|
2021-10-30 01:39:46 +00:00
|
|
|
this.$v.$touch();
|
|
|
|
if (this.$v.$invalid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isEditing = false;
|
2021-11-11 09:53:33 +00:00
|
|
|
this.$emit('update', this.attributeKey, updatedValue);
|
2021-10-30 01:39:46 +00:00
|
|
|
},
|
|
|
|
onDelete() {
|
|
|
|
this.isEditing = false;
|
|
|
|
this.$emit('delete', this.attributeKey);
|
|
|
|
},
|
|
|
|
onCopy() {
|
|
|
|
this.$emit('copy', this.value);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-11-11 09:53:33 +00:00
|
|
|
.custom-attribute {
|
2021-10-30 01:39:46 +00:00
|
|
|
padding: var(--space-slab) var(--space-normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
.title-wrap {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
margin-bottom: var(--space-mini);
|
|
|
|
}
|
|
|
|
.title {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
.attribute-name {
|
|
|
|
&.error {
|
|
|
|
color: var(--r-400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.title--icon {
|
|
|
|
width: var(--space-two);
|
|
|
|
}
|
|
|
|
.edit-button {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
.value--view {
|
|
|
|
display: flex;
|
|
|
|
|
|
|
|
&.is-editable:hover {
|
|
|
|
.value {
|
|
|
|
background: var(--color-background);
|
|
|
|
}
|
|
|
|
.edit-button {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.value {
|
|
|
|
display: inline-block;
|
|
|
|
min-width: var(--space-mega);
|
|
|
|
border-radius: var(--border-radius-small);
|
|
|
|
word-break: break-all;
|
|
|
|
padding: var(--space-micro) var(--space-smaller);
|
|
|
|
}
|
|
|
|
.error-message {
|
|
|
|
color: var(--r-400);
|
|
|
|
display: block;
|
|
|
|
font-size: 1.4rem;
|
|
|
|
font-size: var(--font-size-small);
|
|
|
|
font-weight: 400;
|
|
|
|
margin-bottom: 1rem;
|
|
|
|
margin-top: -1.6rem;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
</style>
|