visgraph: post, patch and delete relationship link works!

This commit is contained in:
Mathieu Jaumotte 2021-11-09 19:54:01 +01:00
parent d6da6a5d9d
commit b392bc9e65
4 changed files with 78 additions and 39 deletions

View File

@ -108,10 +108,10 @@
> >
<label class="form-check-label" for="reverse">{{ $t('visgraph.reverse_relation') }}</label> <label class="form-check-label" for="reverse">{{ $t('visgraph.reverse_relation') }}</label>
</div> </div>
<pre class="debug"> <!--pre class="debug">
modal.data modal.data
{{ modal.data }} {{ modal.data }}
</pre> </pre-->
</form> </form>
</div> </div>
</template> </template>
@ -147,8 +147,8 @@ export default {
container: '', container: '',
checkedLayers: [], checkedLayers: [],
relations: [], relations: [],
relation: null, //relation: null,
reverse: false, //reverse: false,
displayHelpMessage: false, displayHelpMessage: false,
listenPersonFlag: 'normal', listenPersonFlag: 'normal',
newEdgeData: {}, newEdgeData: {},
@ -157,7 +157,13 @@ export default {
modalDialogClass: "modal-md", modalDialogClass: "modal-md",
title: null, title: null,
action: null, action: null,
data: {}, data: {
type: 'relationship',
from: null,
to: null,
relation: null,
reverse: false
},
button: { button: {
class: null, class: null,
text: null text: null
@ -216,26 +222,24 @@ export default {
}, },
/*
relation: { relation: {
get() { get() {
return this.relation return this.modal.data.relation
}, },
set(value) { set(value) {
//console.log('setter relation', value) // <=== InternalError: too much recursion this.modal.data.relation = value
this.relation = value
} }
}, },
reverse: { reverse: {
get() { get() {
return this.reverse return this.modal.data.reverse
}, },
set(newValue) { set(value) {
//console.log('setter reverse', newValue) // <=== InternalError: too much recursion this.modal.data.reverse = value
this.reverse = newValue
} }
}, },
/*
*/ */
}, },
@ -377,8 +381,9 @@ export default {
/// control Modal /// control Modal
addRelationshipModal(edgeData) { addRelationshipModal(edgeData) {
this.modal.data = edgeData console.log('==- addRelationshipModal', edgeData)
console.log('==- addRelationshipModal', edgeData) // { from: "person_1617", to: "person_1614" } this.modal.data.from = edgeData.from
this.modal.data.to = edgeData.to
this.modal.action = 'create' this.modal.action = 'create'
this.modal.title = 'visgraph.add_relationship_link' this.modal.title = 'visgraph.add_relationship_link'
this.modal.button.class = 'btn-create' this.modal.button.class = 'btn-create'
@ -410,7 +415,13 @@ export default {
resetForm() { resetForm() {
console.log('==- reset Form') console.log('==- reset Form')
this.modal.data = {} this.modal.data = {
type: 'relationship',
from: null,
to: null,
relation: null,
reverse: false
}
this.modal.action = null this.modal.action = null
this.modal.title = null this.modal.title = null
this.modal.button.class = null this.modal.button.class = null
@ -428,6 +439,7 @@ export default {
//console.log('customLabel', value) //console.log('customLabel', value)
return (value.title && value.reverseTitle) ? `${value.title.fr}${value.reverseTitle.fr}` : '' return (value.title && value.reverseTitle) ? `${value.title.fr}${value.reverseTitle.fr}` : ''
}, },
getPerson(idtext) { getPerson(idtext) {
let person = this.persons.filter(p => p.id === idtext) let person = this.persons.filter(p => p.id === idtext)
return person[0] return person[0]
@ -439,8 +451,9 @@ export default {
console.log(' @@> switch listener to create link mode:', this.listenPersonFlag) console.log(' @@> switch listener to create link mode:', this.listenPersonFlag)
}, },
dropRelationship() { dropRelationship() {
console.log('delete') console.log('delete', this.modal.data)
deleteRelationship(relationship) /// param ? deleteRelationship(this.modal.data)
this.$store.commit('removeLink', this.modal.data.id)
this.modal.showModal = false this.modal.showModal = false
this.resetForm() this.resetForm()
}, },
@ -449,9 +462,7 @@ export default {
switch (this.modal.action) { switch (this.modal.action) {
case 'create': case 'create':
return postRelationship( return postRelationship(this.modal.data)
this.getPerson(this.modal.data.from), this.getPerson(this.modal.data.to), this.relation, this.reverse
)
.then(relationship => new Promise(resolve => { .then(relationship => new Promise(resolve => {
console.log('post response', relationship) console.log('post response', relationship)
this.$store.dispatch('addLinkFromRelationship', relationship) this.$store.dispatch('addLinkFromRelationship', relationship)
@ -462,10 +473,10 @@ export default {
.catch() .catch()
case 'edit': case 'edit':
return patchRelationship(relationship) return patchRelationship(this.modal.data)
.then(relationship => new Promise(resolve => { .then(relationship => new Promise(resolve => {
console.log('patch relationship', relationship) console.log('patch relationship', relationship)
this.$store.dispatch('updateLinkFromRelationship', relationship) this.$store.commit('updateLink', relationship)
this.modal.showModal = false this.modal.showModal = false
this.resetForm() this.resetForm()
resolve() resolve()

View File

@ -1,3 +1,5 @@
import { splitId } from './vis-network'
/** /**
* @function makeFetch * @function makeFetch
* @param method * @param method
@ -130,21 +132,19 @@ const getRelationsList = () => {
/** /**
* @function postRelationship * @function postRelationship
* @param fromPerson * @param relationship
* @param toPerson
* @param relation
* @param reverse
* @returns {Promise<Response>} * @returns {Promise<Response>}
*/ */
const postRelationship = (fromPerson, toPerson, relation, reverse) => { const postRelationship = (relationship) => {
console.log(relationship)
return postFetch( return postFetch(
`/api/1.0/relations/relationship.json`, `/api/1.0/relations/relationship.json`,
{ {
type: 'relationship', type: 'relationship',
fromPerson: { type: 'person', id: fromPerson._id }, fromPerson: { type: 'person', id: splitId(relationship.from, 'id') },
toPerson: { type: 'person', id: toPerson._id }, toPerson: { type: 'person', id: splitId(relationship.to, 'id') },
relation: { type: 'relation', id: relation.id }, relation: { type: 'relation', id: relationship.relation.id },
reverse: reverse reverse: relationship.reverse
} }
) )
} }
@ -156,12 +156,14 @@ const postRelationship = (fromPerson, toPerson, relation, reverse) => {
*/ */
const patchRelationship = (relationship) => { const patchRelationship = (relationship) => {
console.log(relationship) console.log(relationship)
let linkType = splitId(relationship.id, 'link')
let id = splitId(linkType, 'id')
return patchFetch( return patchFetch(
`/api/1.0/relations/relationship/${relationship.id}.json`, `/api/1.0/relations/relationship/${id}.json`,
{ {
type: 'relationship', type: 'relationship',
fromPerson: { type: 'person', id: relationship.fromPerson.id }, fromPerson: { type: 'person', id: splitId(relationship.from, 'id') },
toPerson: { type: 'person', id: relationship.toPerson.id }, toPerson: { type: 'person', id: splitId(relationship.to, 'id') },
relation: { type: 'relation', id: relationship.relation.id }, relation: { type: 'relation', id: relationship.relation.id },
reverse: relationship.reverse reverse: relationship.reverse
} }
@ -174,8 +176,11 @@ const patchRelationship = (relationship) => {
* @returns {Promise<Response>} * @returns {Promise<Response>}
*/ */
const deleteRelationship = (relationship) => { const deleteRelationship = (relationship) => {
console.log(relationship)
let linkType = splitId(relationship.id, 'link')
let id = splitId(linkType, 'id')
return deleteFetch( return deleteFetch(
`/api/1.0/relations/relationship/${relationship.id}.json` `/api/1.0/relations/relationship/${id}.json`
) )
} }

View File

@ -160,6 +160,30 @@ const store = createStore({
addLink(state, link) { addLink(state, link) {
state.links.push(link) state.links.push(link)
}, },
updateLink(state, link) {
console.log('updateLink', link)
let link_ = {
from: `person_${link.fromPerson.id}`,
to: `person_${link.toPerson.id}`,
id: 'relationship_' + splitId(link.id,'id')
+ '-person_' + link.fromPerson.id + '-person_' + link.toPerson.id,
arrows: getRelationshipDirection(link),
color: 'lightblue',
font: { color: '#33839d' },
dashes: true,
label: getRelationshipLabel(link),
title: getRelationshipTitle(link),
relation: link.relation,
reverse: link.reverse
}
// find row position and replace by updatedLink
state.links.splice(
state.links.findIndex(item => item.id === link_.id), 1, link_
)
},
removeLink(state, link_id) {
state.links = state.links.filter(l => l.id !== link_id)
},
//// id markers //// id markers
markInWhitelist(state, person) { markInWhitelist(state, person) {

View File

@ -266,13 +266,12 @@ const getRelationshipTitle = (relationship) => {
* @param position * @param position
* @returns string|integer * @returns string|integer
*/ */
const splitId = (id, position) => { const splitId = (id, position) => { console.log(id, position)
switch (position) { switch (position) {
case 'type': // return 'accompanying_period' case 'type': // return 'accompanying_period'
return /(.+)_/.exec(id)[1] return /(.+)_/.exec(id)[1]
case 'id': // return 124 case 'id': // return 124
return parseInt(id return parseInt(id.toString()
.toString()
.split("_") .split("_")
.pop()) .pop())
case 'link': case 'link':