104 lines
2.8 KiB
Vue
104 lines
2.8 KiB
Vue
<script>
|
|
|
|
import REST from '@api/rest-axios'
|
|
|
|
import { mapActions, mapState } from 'pinia'
|
|
import { ConcernementsStore } from '@stores/concernements'
|
|
import { UserStore } from '@stores/user'
|
|
|
|
|
|
export default {
|
|
props: {
|
|
tag: String,
|
|
value: String,
|
|
contenteditable : {
|
|
type : [Boolean, String],
|
|
default : true,
|
|
},
|
|
html : {
|
|
type : [Boolean, String],
|
|
default : false,
|
|
},
|
|
data: Object
|
|
},
|
|
emits: ['updated'],
|
|
data(){
|
|
return {
|
|
|
|
}
|
|
},
|
|
computed: {
|
|
...mapState(UserStore,['csrf_token']),
|
|
},
|
|
created () {
|
|
console.log('ContentEditable created');
|
|
},
|
|
mounted () {
|
|
|
|
},
|
|
methods: {
|
|
...mapActions(ConcernementsStore, ['reloadConcernements']),
|
|
onContentEditableFocusOut(e){
|
|
console.log('onContentEditableFocusOut', e);
|
|
// console.log('onContentEditableFocusOut data', this.data);
|
|
let new_field_content = this.html ? e.target.innerHTML : e.target.innerText;
|
|
// console.log('onContentEditableFocusOut', new_field_content);
|
|
this.save(new_field_content)
|
|
},
|
|
save(content){
|
|
// console.log('save csrf_token', this.csrf_token);
|
|
const params = {
|
|
type: this.data.bundle,
|
|
[this.data.field.field_name]: [{[this.data.field.value]: content}]
|
|
};
|
|
if (this.data.entitytype === 'node') {
|
|
params.nid = [{"value":this.data.id}];
|
|
} else {
|
|
params.id = [{"value":this.data.id}];
|
|
}
|
|
// we need additional values for image alt for example
|
|
// console.log('additional_values', this.data.field.additional_values);
|
|
if (this.data.field.additional_values) {
|
|
for (const key in this.data.field.additional_values) {
|
|
if (Object.hasOwnProperty.call(this.data.field.additional_values, key)) {
|
|
params[this.data.field.field_name][0][key] = this.data.field.additional_values[key]
|
|
}
|
|
}
|
|
}
|
|
|
|
const configs = {
|
|
headers: {'X-CSRF-Token': this.csrf_token}
|
|
};
|
|
|
|
let url_base = `/${this.data.entitytype === 'node' ? '' : 'entity/'}${this.data.entitytype}`;
|
|
REST.patch(`${url_base}/${this.data.id}?_format=json`, params, configs)
|
|
.then(({ data }) => {
|
|
console.log('user REST post node data', data)
|
|
// TODO if success update the data in pinia
|
|
// this.reloadConcernements();
|
|
this.$emit('updated');
|
|
})
|
|
.catch(error => {
|
|
console.warn(`Issue with patch node ${this.data.bundle}`, error)
|
|
Promise.reject(error)
|
|
})
|
|
}
|
|
},
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<component
|
|
v-if="!html"
|
|
:is="tag"
|
|
:contenteditable="contenteditable"
|
|
@focusout="onContentEditableFocusOut"
|
|
>{{ value }}</component>
|
|
<component
|
|
v-else
|
|
:is="tag"
|
|
v-html="value"
|
|
:contenteditable="contenteditable"
|
|
@focusout="onContentEditableFocusOut"
|
|
/>
|
|
</template> |