2022-08-04 16:51:30 +08:00
|
|
|
<template>
|
2022-08-24 23:51:23 +08:00
|
|
|
<div class="label-templates-show">
|
|
|
|
<div class="header">
|
|
|
|
<div id="breadcrumbsWrapper">
|
|
|
|
<div class="breadcrumbs-container">
|
|
|
|
<a :href="labelTemplatesUrl" class="breadcrumbs-link">
|
|
|
|
{{ i18n.t('label_templates.show.breadcrumb_index') }}
|
|
|
|
</a>
|
|
|
|
<span class="delimiter">/</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-if="labelTemplate.id" class="title-row">
|
|
|
|
<img :src="labelTemplate.attributes.icon_url" class="label-template-icon"/>
|
|
|
|
<InlineEdit
|
2022-09-20 18:59:58 +08:00
|
|
|
v-if="canManage"
|
2022-08-24 23:51:23 +08:00
|
|
|
:value="labelTemplate.attributes.name"
|
|
|
|
:characterLimit="255"
|
|
|
|
:allowBlank="false"
|
|
|
|
:attributeName="i18n.t('label_templates.show.name_error_prefix')"
|
|
|
|
:autofocus="editingName"
|
|
|
|
:editOnload="newLabel"
|
|
|
|
@editingEnabled="editingName = true"
|
|
|
|
@editingDisabled="editingName = false"
|
|
|
|
@update="updateName"
|
|
|
|
/>
|
2022-09-20 18:59:58 +08:00
|
|
|
<template v-else>
|
|
|
|
{{ labelTemplate.attributes.name }}
|
|
|
|
</template>
|
2022-08-04 16:51:30 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-20 18:59:58 +08:00
|
|
|
<div v-if="labelTemplate.id && notFluics" class="template-descripiton">
|
2022-08-04 16:51:30 +08:00
|
|
|
<div class="title">
|
2022-08-04 19:36:14 +08:00
|
|
|
{{ i18n.t('label_templates.show.description_title') }}
|
2022-08-04 16:51:30 +08:00
|
|
|
</div>
|
2022-08-24 23:51:23 +08:00
|
|
|
<div class="description">
|
|
|
|
<InlineEdit
|
2022-09-20 18:59:58 +08:00
|
|
|
v-if="canManage"
|
2022-08-24 23:51:23 +08:00
|
|
|
:value="labelTemplate.attributes.description"
|
|
|
|
:characterLimit="255"
|
|
|
|
:allowBlank="true"
|
|
|
|
:attributeName="i18n.t('label_templates.show.description_error_prefix')"
|
|
|
|
:placeholder="i18n.t('label_templates.show.description_placeholder')"
|
|
|
|
:autofocus="editingDescription"
|
|
|
|
@editingEnabled="editingDescription = true"
|
|
|
|
@editingDisabled="editingDescription = false"
|
|
|
|
@update="updateDescription"
|
|
|
|
/>
|
2022-09-20 18:59:58 +08:00
|
|
|
<template v-else>
|
|
|
|
{{ labelTemplate.attributes.description }}
|
|
|
|
</template>
|
2022-08-24 23:51:23 +08:00
|
|
|
</div>
|
2022-08-04 16:51:30 +08:00
|
|
|
</div>
|
2022-08-04 19:36:14 +08:00
|
|
|
<div v-if="labelTemplate.id" class="label-template-container">
|
2022-09-20 18:59:58 +08:00
|
|
|
<div class="label-edit-container" v-if="notFluics">
|
2022-08-16 16:16:40 +08:00
|
|
|
<div class="label-edit-header">
|
|
|
|
<div class="title">
|
|
|
|
{{ i18n.t('label_templates.show.content_title', { format: labelTemplate.attributes.language_type.toUpperCase() }) }}
|
|
|
|
</div>
|
2022-08-25 19:25:34 +08:00
|
|
|
<InsertFieldDropdown
|
|
|
|
v-if="editingContent"
|
|
|
|
:labelTemplate="labelTemplate"
|
|
|
|
@insertField="insertField"
|
2022-08-24 23:51:23 +08:00
|
|
|
/>
|
2022-08-04 19:36:14 +08:00
|
|
|
</div>
|
|
|
|
<template v-if="editingContent">
|
2022-09-19 17:56:43 +08:00
|
|
|
<div class="label-textarea-container" :class="{'error': hasError }">
|
2022-08-04 19:36:14 +08:00
|
|
|
<textarea
|
|
|
|
ref="contentInput"
|
|
|
|
v-model="newContent"
|
|
|
|
class="label-textarea"
|
2022-09-19 17:56:43 +08:00
|
|
|
@blur="saveCursorPosition"
|
2022-08-04 19:36:14 +08:00
|
|
|
></textarea>
|
2022-09-19 17:56:43 +08:00
|
|
|
<div class="error-message">
|
|
|
|
{{ codeErrorMessage }}
|
|
|
|
</div>
|
2022-08-04 19:36:14 +08:00
|
|
|
</div>
|
|
|
|
<div class="button-container">
|
2022-09-19 17:56:43 +08:00
|
|
|
<div class="btn btn-secondary refresh-preview" @click="generatePreview(true)">
|
2022-08-04 19:36:14 +08:00
|
|
|
<i class="fas fa-sync"></i>
|
|
|
|
{{ i18n.t('label_templates.show.buttons.refresh') }}
|
|
|
|
</div>
|
2022-08-25 19:25:34 +08:00
|
|
|
<div class="btn btn-secondary" @mousedown="disableContentEdit">
|
2022-08-04 19:36:14 +08:00
|
|
|
{{ i18n.t('general.cancel') }}
|
|
|
|
</div>
|
2022-09-19 17:56:43 +08:00
|
|
|
<div class="btn btn-primary save-template" :disabled="hasError && previewValid" @click="generatePreview(false)">
|
2022-08-04 19:36:14 +08:00
|
|
|
<i class="fas fa-save"></i>
|
|
|
|
{{ i18n.t('label_templates.show.buttons.save') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
2022-09-20 18:59:58 +08:00
|
|
|
<div v-else-if="canManage" class="label-view-container" :title="i18n.t('label_templates.show.view_content_tooltip')" @click="enableContentEdit">
|
2022-08-24 23:51:23 +08:00
|
|
|
{{ labelTemplate.attributes.content}}
|
2022-08-04 19:36:14 +08:00
|
|
|
<i class="fas fa-pen"></i>
|
|
|
|
</div>
|
2022-09-20 18:59:58 +08:00
|
|
|
<div v-else class="label-view-container read-only" :title="i18n.t('label_templates.show.view_content_tooltip')">
|
|
|
|
{{ labelTemplate.attributes.content}}
|
|
|
|
</div>
|
2022-08-04 19:36:14 +08:00
|
|
|
</div>
|
|
|
|
<div class="label-preview-container">
|
2022-09-19 17:56:43 +08:00
|
|
|
<LabelPreview :zpl='previewContent' :previewUrl="previewUrl" @preview:valid="updateContent" @preview:invalid="invalidPreview" />
|
2022-08-04 19:36:14 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-08-04 16:51:30 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
import InlineEdit from 'vue/shared/inline_edit.vue'
|
2022-08-16 16:16:40 +08:00
|
|
|
import InsertFieldDropdown from 'vue/label_template/insert_field_dropdown.vue'
|
2022-08-24 19:55:54 +08:00
|
|
|
import LabelPreview from './components/label_preview.vue'
|
2022-08-04 16:51:30 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'LabelTemplateContainer',
|
|
|
|
props: {
|
|
|
|
labelTemplateUrl: String,
|
2022-08-04 19:36:14 +08:00
|
|
|
labelTemplatesUrl: String,
|
2022-08-24 19:55:54 +08:00
|
|
|
previewUrl: String,
|
2022-08-04 19:36:14 +08:00
|
|
|
newLabel: Boolean
|
2022-08-04 16:51:30 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
labelTemplate: {
|
|
|
|
attributes: {}
|
|
|
|
},
|
|
|
|
editingName: false,
|
2022-08-04 19:36:14 +08:00
|
|
|
editingDescription: false,
|
|
|
|
editingContent: false,
|
2022-08-17 19:40:35 +08:00
|
|
|
newContent: '',
|
2022-09-19 17:56:43 +08:00
|
|
|
previewContent: '',
|
|
|
|
previewValid: false,
|
|
|
|
skipSave: false,
|
|
|
|
codeErrorMessage: '',
|
2022-08-17 19:40:35 +08:00
|
|
|
cursorPos: 0
|
2022-08-04 16:51:30 +08:00
|
|
|
}
|
|
|
|
},
|
2022-09-19 17:56:43 +08:00
|
|
|
watch: {
|
|
|
|
newContent() {
|
|
|
|
this.showErrors();
|
|
|
|
},
|
|
|
|
previewValid() {
|
|
|
|
this.showErrors();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
hasError() {
|
|
|
|
return this.codeErrorMessage.length > 0
|
2022-09-20 18:59:58 +08:00
|
|
|
},
|
|
|
|
canManage() {
|
|
|
|
return this.labelTemplate.attributes.urls.update && this.notFluics
|
|
|
|
},
|
|
|
|
notFluics() {
|
|
|
|
return this.labelTemplate.attributes.type !== "FluicsLabelTemplate"
|
2022-09-19 17:56:43 +08:00
|
|
|
}
|
|
|
|
},
|
2022-08-24 23:48:48 +08:00
|
|
|
components: {InlineEdit, InsertFieldDropdown, LabelPreview},
|
2022-08-04 16:51:30 +08:00
|
|
|
created() {
|
|
|
|
$.get(this.labelTemplateUrl, (result) => {
|
|
|
|
this.labelTemplate = result.data
|
2022-08-04 19:36:14 +08:00
|
|
|
this.newContent = this.labelTemplate.attributes.content
|
2022-09-19 17:56:43 +08:00
|
|
|
this.previewContent = this.labelTemplate.attributes.content
|
2022-08-04 16:51:30 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
methods: {
|
2022-08-17 19:40:35 +08:00
|
|
|
enableContentEdit() {
|
2022-08-25 19:25:34 +08:00
|
|
|
this.editingContent = true;
|
2022-08-17 19:40:35 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs.contentInput.focus();
|
2022-08-25 19:25:34 +08:00
|
|
|
$(this.$refs.contentInput).prop('selectionStart', this.cursorPos);
|
|
|
|
$(this.$refs.contentInput).prop('selectionEnd', this.cursorPos);
|
2022-08-17 19:40:35 +08:00
|
|
|
});
|
|
|
|
},
|
2022-08-25 19:25:34 +08:00
|
|
|
disableContentEdit() {
|
|
|
|
this.editingContent = false;
|
|
|
|
this.newContent = this.labelTemplate.attributes.content;
|
2022-09-19 17:56:43 +08:00
|
|
|
this.previewContent = this.labelTemplate.attributes.content;
|
2022-08-25 19:25:34 +08:00
|
|
|
},
|
2022-08-04 16:51:30 +08:00
|
|
|
updateName(newName) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.labelTemplate.attributes.urls.update,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: {label_template: {name: newName}},
|
|
|
|
success: (result) => {
|
|
|
|
this.labelTemplate.attributes.name = result.data.attributes.name
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
updateDescription(newDescription) {
|
|
|
|
$.ajax({
|
|
|
|
url: this.labelTemplate.attributes.urls.update,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: {label_template: {description: newDescription}},
|
|
|
|
success: (result) => {
|
|
|
|
this.labelTemplate.attributes.description = result.data.attributes.description
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2022-08-04 19:36:14 +08:00
|
|
|
updateContent() {
|
2022-09-19 17:56:43 +08:00
|
|
|
this.previewValid = true;
|
|
|
|
|
|
|
|
if (!this.editingContent) return;
|
|
|
|
|
|
|
|
if (this.skipSave) {
|
|
|
|
this.skipSave = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.hasError) return;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: this.labelTemplate.attributes.urls.update,
|
|
|
|
type: 'PATCH',
|
|
|
|
data: {label_template: {content: this.newContent}},
|
|
|
|
success: (result) => {
|
|
|
|
this.labelTemplate.attributes.content = result.data.attributes.content;
|
|
|
|
this.editingContent = false;
|
|
|
|
}
|
|
|
|
});
|
2022-08-04 19:36:14 +08:00
|
|
|
});
|
|
|
|
},
|
2022-09-19 17:56:43 +08:00
|
|
|
generatePreview(skipSave = false) {
|
|
|
|
this.skipSave = skipSave;
|
|
|
|
if (!skipSave && this.previewContent === this.newContent && this.previewValid) {
|
|
|
|
this.updateContent();
|
|
|
|
} else {
|
|
|
|
this.previewContent = this.newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
invalidPreview() {
|
|
|
|
this.previewValid = false;
|
|
|
|
this.skipSave = false;
|
|
|
|
},
|
|
|
|
saveCursorPosition() {
|
|
|
|
this.cursorPos = $(this.$refs.contentInput).prop('selectionStart');
|
|
|
|
},
|
2022-08-17 19:40:35 +08:00
|
|
|
insertField(field) {
|
|
|
|
this.enableContentEdit();
|
|
|
|
let textBefore = this.newContent.substring(0, this.cursorPos);
|
|
|
|
let textAfter = this.newContent.substring(this.cursorPos, this.newContent.length);
|
|
|
|
this.newContent = textBefore + field + textAfter;
|
|
|
|
this.cursorPos = this.cursorPos + field.length;
|
2022-09-19 17:56:43 +08:00
|
|
|
},
|
|
|
|
showErrors() {
|
|
|
|
if (this.editingContent) {
|
|
|
|
if (this.newContent.length === 0) {
|
|
|
|
this.codeErrorMessage = this.i18n.t('label_templates.show.code_errors.empty')
|
|
|
|
} else if (this.newContent.length > 10000) {
|
|
|
|
this.codeErrorMessage = this.i18n.t('label_templates.show.code_errors.too_long')
|
|
|
|
} else if (!this.previewValid) {
|
|
|
|
this.codeErrorMessage = this.i18n.t('label_templates.show.code_errors.invalid')
|
|
|
|
} else {
|
|
|
|
this.codeErrorMessage = ''
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.codeErrorMessage = ''
|
|
|
|
}
|
2022-08-17 19:40:35 +08:00
|
|
|
}
|
2022-08-04 16:51:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|