This commit is contained in:
2024-09-28 17:37:29 +02:00
parent 8dfafa9404
commit fb4d566007
64 changed files with 32325 additions and 573 deletions
@@ -6,10 +6,10 @@
<div class="card-body">
<ItemList
:crypto_key="crypto_key"
:items="items"
:items_headers="items_headers"
:items_relations="{'type': types}"
:hidden_fields="[]"
group_by="type__name"
@deleteItem="deleteItem"
@createItem="createItem"
@@ -24,10 +24,10 @@
<div class="card-body">
<ItemList
:crypto_key="crypto_key"
:items="types"
:items_headers="types_headers"
:items_relations="{}"
:hidden_fields="[]"
group_by="[]"
@deleteItem="deleteType"
@createItem="createType"
+107 -172
View File
@@ -1,17 +1,15 @@
ItemView = {
template: "#ItemView",
router_path: "/ItemView",
delimiters: ["[[", "]]"],
props: ["crypto_key"],
props: [],
data: function() {
return {
search: "",
items: [],
items_headers: [],
items_relations: [],
types: [],
types_headers: [],
types_relations: [],
}
},
@@ -20,262 +18,199 @@ ItemView = {
},
computed: {
items_encrypted_fields: function() {
items_efields: function() {
return this.items_headers.filter(e => e.encrypted).map(e => e.value)
},
types_encrypted_fields: function() {
types_efields: function() {
return this.types_headers.filter(e => e.encrypted).map(e => e.value)
},
},
methods: {
reload () {
async reload () {
var self = this
try {
const response = await this.$http.get(Urls["items:list"]())
this.$http.get(Urls["items:list"]()).then(response => {
this.items_headers = response.data.result.items_headers
this.types_headers = response.data.result.types_headers
Object.keys(response.data.result).forEach(name => {
self.$set(self, name, response.data.result[name])
// Decrypt all item the push
response.data.result.items.forEach(async item => {
const new_item = await this.decryptObject(this.items_efields, item)
this.items.push(new_item)
})
self.items.forEach(item => {
self.decryptObject(self.items_encrypted_fields, item)
// Decrypt all type the push
response.data.result.types.forEach(async type => {
const new_type = await this.decryptObject(this.types_efields, type)
this.types.push(new_type)
})
self.types.forEach(type => {
self.decryptObject(self.types_encrypted_fields, type)
})
}).catch(err => {
} catch (err) {
Swal.fire({title: "{{_('Error during loading of items') | escapejs}}", icon: "error", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
}
},
async aDecryptObject (encrypted_fields, obj) {
async object_edit(url_edit, url_create, encrypted_fields, method, obj) {
var self = this
let url = null
return new Promise((resolve) => {
if (obj.id == undefined || obj.id == null) {
url = Urls[url_create]()
} else {
url = Urls[url_edit](obj.id)
}
let promises = encrypted_fields.map(field => {
// Encrypt all necessary fields
if (obj[field] == null) {
return null
}
try {
return new Promise((resolve) => {
return decryptWithKey(self.crypto_key, obj[field]).then(dec => {
resolve({field: field, value: dec})
})
})
const newobj = await this.encryptObject(encrypted_fields, obj)
const response = await this.$http[method](url, newobj)
}).filter(e => e != null)
Promise.all(promises).then(values => {
values.forEach(value => {
obj[value.field] = value.value
})
resolve(obj)
})
})
},
decryptObject (encrypted_fields, obj) {
var self = this
return new Promise((resolve) => {
let promises = encrypted_fields.map(field => {
// Encrypt all necessary fields
if (obj[field] == null) {
return null
}
return new Promise((resolve) => {
return decryptWithKey(self.crypto_key, obj[field]).then(dec => {
resolve({field: field, value: dec})
})
})
}).filter(e => e != null)
Promise.all(promises).then(values => {
values.forEach(value => {
obj[value.field] = value.value
})
resolve(obj)
})
})
},
object_edition (url_edit, url_create, encrypted_fields, method, obj) {
// Return a Promise
var self = this
return new Promise((resolve) => {
let url = Urls[url_edit](obj.id)
if (obj.id == undefined || obj.id == null) {
url = Urls[url_create]()
if (method != "delete") {
return await this.decryptObject(encrypted_fields, response.data.object)
}
let promises = encrypted_fields.map(field => {
// Encrypt all necessary fields
if (obj[field] == null) {
return null
}
} catch (err) {
return new Promise((resolve) => {
return encryptWithKey(self.crypto_key, obj[field]).then(enc => {
resolve({field: field, value: enc})
})
})
let msg = "{{_('Error during edition') | escapejs}}"
if (method == "delete") {
msg = "{{_('Error during deletion') | escapejs}}"
}
}).filter(e => e != null)
Swal.fire({title: msg, icon: "error", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
Promise.all(promises).then(values => {
throw err
values.forEach(value => {
obj[value.field] = value.value
})
}
self.$http[method](url, obj).then(response => {
if (method == "delete") {
resolve()
} else {
self.decryptObject(encrypted_fields, response.data.object).then(new_obj => {
resolve(new_obj)
})
}
}).catch(err => {
let msg = "{{_('Error during edition') | escapejs}}"
if (method == "delete") {
msg = "{{_('Error during deletion') | escapejs}}"
}
Swal.fire({title: msg, icon: "error", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
})
})
},
item_edition (method, item) {
return this.object_edition("items:edit", "items:create", this.items_encrypted_fields, method, item)
return this.object_edit("items:edit", "items:create", this.items_efields, method, item)
},
type_edition (method, item) {
return this.object_edition("items:type.edit", "items:type.create", this.items_encrypted_fields, method, item)
return this.object_edit("items:type.edit", "items:type.create", this.types_efields, method, item)
},
createItem (item) {
async createItem (item) {
var self = this
try {
this.item_edition("post", item).then(new_item => {
self.items.push(new_item)
const new_item = await this.item_edition("post", item)
this.items.push(new_item)
Swal.fire({title: "{{_('Item successfully created!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
}
},
editItem (index, item) {
async editItem (index, item) {
var self = this
try {
this.item_edition("post", item).then(new_item => {
// Remove the item
this.items.splice(index, 1)
// Remove the 'current' (non edited) item from the list
self.items.splice(index, 1)
self.items.push(new_item)
const new_item = await this.item_edition("post", item)
// Add the new item
this.items.push(new_item)
Swal.fire({title: "{{_('Item successfully edited') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
this.items.push(item)
}
},
deleteItem (index) {
async deleteItem (index) {
var self = this
var item = this.items[index]
this.item_edition("delete", item).then(() => {
self.items.splice(this.items.indexOf(item), 1)
try {
// Remove the item
this.items.splice(index, 1)
await this.item_edition("delete", item)
Swal.fire({title: "{{_('Item successfully deleted!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
this.items.push(item)
}
},
createType (type) {
async createType (type) {
var self = this
try {
this.type_edition("post", type).then(new_type => {
self.types.push(new_type)
const new_type = await this.type_edition("post", type)
this.types.push(new_type)
Swal.fire({title: "{{_('Type successfully created!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
}
},
editType (index, type) {
async editType (index, type) {
var self = this
try {
this.type_edition("post", type).then(new_type => {
this.types.splice(index, 1)
// Remove the 'current' (non edited) item from the list
self.types.splice(index, 1)
self.types.push(new_type)
const new_type = await this.type_edition("post", type)
this.types.push(new_type)
Swal.fire({title: "{{_('Type successfully edited') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
this.types.push(type)
}
},
deleteType (index) {
async deleteType (index) {
var self = this
var type = this.types[index]
this.type_edition("delete", type).then(() => {
self.types.splice(this.types.indexOf(type), 1)
try {
// Remove the type
this.types.splice(index, 1)
await this.type_edition("delete", type)
Swal.fire({title: "{{_('Type successfully deleted!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000})
})
} catch (err) {
this.types.push(type)
}
},
},