-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/javascript/vue/components/custom_attributes/predicates/components/predicateRow.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<template> | ||
<div | ||
class="field" | ||
> | ||
<label v-html="predicateObject.object_tag" /> | ||
<input | ||
type="text" | ||
v-model="data_attribute.value" | ||
@change="updatePredicate" | ||
> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
predicateObject: { | ||
type: Object, | ||
required: true | ||
}, | ||
objectId: { | ||
required: true, | ||
validator(value) { | ||
return value === undefined || typeof value === 'string' || typeof value === 'number' | ||
} | ||
}, | ||
objectType: { | ||
type: String, | ||
required: true | ||
}, | ||
existing: { | ||
type: Object, | ||
required: false | ||
} | ||
}, | ||
data() { | ||
return { | ||
data_attribute: { | ||
type: 'InternalAttribute', | ||
controlled_vocabulary_term_id: this.predicateObject.id, | ||
attribute_subject_id: this.objectId, | ||
attribute_subject_type: this.objectType, | ||
value: this.value | ||
} | ||
} | ||
}, | ||
watch: { | ||
existing(newVal) { | ||
if(newVal) { | ||
this.data_attribute = newVal | ||
} | ||
} | ||
}, | ||
methods: { | ||
updatePredicate() { | ||
this.$emit('onUpdate', this.data_attribute) | ||
} | ||
} | ||
} | ||
</script> |
126 changes: 126 additions & 0 deletions
126
app/javascript/vue/components/custom_attributes/predicates/predicates.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<template> | ||
<fieldset> | ||
<spinner-component | ||
v-if="loading" | ||
/> | ||
<legend>Custom attributes</legend> | ||
<predicate-row | ||
v-for="item in predicatesList" | ||
:key="item.id" | ||
:object-id="objectId" | ||
:object-type="objectType" | ||
:predicate-object="item" | ||
:existing="findExisting(item.id)" | ||
@onUpdate="addDataAttribute" | ||
/> | ||
</fieldset> | ||
</template> | ||
|
||
<script> | ||
import SpinnerComponent from 'components/spinner' | ||
import PredicateRow from './components/predicateRow' | ||
import { GetPredicates, GetPredicatesCreated, GetProjectPreferences } from './request/resources.js' | ||
export default { | ||
components: { | ||
PredicateRow, | ||
SpinnerComponent | ||
}, | ||
props: { | ||
model: { | ||
type: String, | ||
required: true | ||
}, | ||
objectId: { | ||
required: true | ||
}, | ||
objectType: { | ||
type: String, | ||
required: true | ||
}, | ||
modelPreferences: { | ||
type: [Array], | ||
required: false | ||
} | ||
}, | ||
data() { | ||
return { | ||
loading: true, | ||
createdList: [], | ||
predicatesList: [], | ||
list: [], | ||
data_attributes: [], | ||
modelPreferencesIds: undefined | ||
} | ||
}, | ||
watch: { | ||
objectId(newVal) { | ||
if(newVal && this.objectType) { | ||
this.loading = true | ||
GetPredicatesCreated(this.objectType, this.objectId).then(response => { | ||
this.createdList = response.body | ||
this.loading = false | ||
}) | ||
} | ||
} | ||
}, | ||
mounted() { | ||
this.loadPreferences() | ||
}, | ||
methods: { | ||
loadPreferences() { | ||
if(Array.isArray(this.modelPreferences) && this.modelPreferences.length) { | ||
this.loadPredicates(this.modelPreferences) | ||
} | ||
else { | ||
GetProjectPreferences().then(response => { | ||
this.modelPreferencesIds = response.body.model_predicate_sets[this.model] | ||
this.loadPredicates(this.modelPreferencesIds) | ||
}) | ||
} | ||
}, | ||
loadPredicates(ids) { | ||
let promises = [] | ||
promises.push(GetPredicates(ids).then(response => { | ||
this.predicatesList = response.body | ||
})) | ||
if(this.objectId) { | ||
promises.push(GetPredicatesCreated(this.objectType, this.objectId).then(response => { | ||
this.createdList = response.body | ||
})) | ||
} | ||
Promise.all(promises).then(() => { | ||
this.loading = false | ||
}) | ||
}, | ||
findExisting(id) { | ||
return this.createdList.find(item => { | ||
return item.controlled_vocabulary_term_id == id | ||
}) | ||
}, | ||
addDataAttribute(dataAttribute) { | ||
let index = this.data_attributes.findIndex(item => { | ||
return item.controlled_vocabulary_term_id == dataAttribute.controlled_vocabulary_term_id | ||
}) | ||
if(index > -1) { | ||
this.$set(this.data_attributes, index, dataAttribute) | ||
} | ||
else { | ||
this.data_attributes.push(dataAttribute) | ||
} | ||
this.$emit('onUpdate', this.data_attributes) | ||
} | ||
} | ||
} | ||
</script> | ||
<style scoped> | ||
input { | ||
width: 100%; | ||
} | ||
</style> | ||
|
19 changes: 19 additions & 0 deletions
19
app/javascript/vue/components/custom_attributes/predicates/request/resources.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import ajaxCall from 'helpers/ajaxCall' | ||
|
||
const GetProjectPreferences = function () { | ||
return ajaxCall('get', `/project_preferences.json`) | ||
} | ||
|
||
const GetPredicates = function (ids) { | ||
return ajaxCall('get', '/controlled_vocabulary_terms', { params: { 'of_type[]': 'Predicate', 'id': ids } }) | ||
} | ||
|
||
const GetPredicatesCreated = function (objectType, objectId) { | ||
return ajaxCall('get', `/data_attributes?attribute_subject_type=${objectType}&attribute_subject_id=${objectId}&type=InternalAttribute`) | ||
} | ||
|
||
export { | ||
GetPredicates, | ||
GetPredicatesCreated, | ||
GetProjectPreferences | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Vue from 'vue' | ||
import VueResource from 'vue-resource' | ||
|
||
Vue.use(VueResource) | ||
|
||
const ajaxCall = function (type, url, data = null) { | ||
Vue.http.headers.common['X-CSRF-Token'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content') | ||
return new Promise(function (resolve, reject) { | ||
Vue.http[type](url, data).then(response => { | ||
console.log(response) | ||
return resolve(response) | ||
}, response => { | ||
console.log(response) | ||
handleError(response) | ||
return reject(response) | ||
}) | ||
}) | ||
} | ||
|
||
const handleError = function (json) { | ||
if (typeof json !== 'object') return | ||
let errors = Object.keys(json) | ||
let errorMessage = '' | ||
|
||
errors.forEach(function (item) { | ||
errorMessage += json[item].join('<br>') | ||
}) | ||
|
||
TW.workbench.alert.create(errorMessage, 'error') | ||
} | ||
|
||
export default ajaxCall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ascript/vue/tasks/digitize/components/collectionEvent/components/geography/predicates.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<template> | ||
<predicates-component | ||
:object-id="collectingEvent.id" | ||
object-type="CollectingEvent" | ||
model="CollectingEvent" | ||
@onUpdate="setAttributes" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import PredicatesComponent from 'components/custom_attributes/predicates/predicates' | ||
import { GetterNames } from '../../../../store/getters/getters.js' | ||
import { MutationNames } from '../../../../store/mutations/mutations.js' | ||
export default { | ||
components: { | ||
PredicatesComponent | ||
}, | ||
computed: { | ||
collectingEvent() { | ||
return this.$store.getters[GetterNames.GetCollectionEvent] | ||
} | ||
}, | ||
methods: { | ||
setAttributes(dataAttributes) { | ||
this.$store.commit(MutationNames.SetCollectionEventDataAttributes, dataAttributes) | ||
console.log(this.collectingEvent) | ||
} | ||
} | ||
} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
...pt/vue/tasks/digitize/store/mutations/CollectionEvent/setCollectionEventDataAttributes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function(state, value) { | ||
state.collection_event.data_attributes_attributes = value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters