ItemView = { template: "#ItemView", delimiters: ["[[", "]]"], props: ["crypto_key"], data: function() { return { search: "", items: [], items_headers: [], items_relations: [], types: [], types_headers: [], types_relations: [], } }, mounted: function() { this.reload() }, computed: { items_encrypted_fields: function() { return this.items_headers.filter(e => e.encrypted).map(e => e.value) }, types_encrypted_fields: function() { return this.types_headers.filter(e => e.encrypted).map(e => e.value) }, }, methods: { reload () { var self = this this.$http.get(Urls["items:list"]()).then(response => { Object.keys(response.data.result).forEach(name => { self.$set(self, name, response.data.result[name]) }) self.items.forEach(item => { self.decryptObject(self.items_encrypted_fields, item) }) self.types.forEach(type => { self.decryptObject(self.types_encrypted_fields, type) }) }).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) { 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) }) }) }, 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]() } let promises = encrypted_fields.map(field => { // Encrypt all necessary fields if (obj[field] == null) { return null } return new Promise((resolve) => { return encryptWithKey(self.crypto_key, obj[field]).then(enc => { resolve({field: field, value: enc}) }) }) }).filter(e => e != null) Promise.all(promises).then(values => { 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) }, type_edition (method, item) { return this.object_edition("items:type.edit", "items:type.create", this.items_encrypted_fields, method, item) }, createItem (item) { var self = this this.item_edition("post", item).then(new_item => { self.items.push(new_item) Swal.fire({title: "{{_('Item successfully created!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, editItem (index, item) { var self = this this.item_edition("post", item).then(new_item => { // Remove the 'current' (non edited) item from the list self.items.splice(index, 1) self.items.push(new_item) Swal.fire({title: "{{_('Item successfully edited') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, deleteItem (index) { var self = this var item = this.items[index] this.item_edition("delete", item).then(() => { self.items.splice(this.items.indexOf(item), 1) Swal.fire({title: "{{_('Item successfully deleted!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, createType (type) { var self = this this.type_edition("post", type).then(new_type => { self.types.push(new_type) Swal.fire({title: "{{_('Type successfully created!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, editType (index, type) { var self = this this.type_edition("post", type).then(new_type => { // Remove the 'current' (non edited) item from the list self.types.splice(index, 1) self.types.push(new_type) Swal.fire({title: "{{_('Type successfully edited') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, deleteType (index) { var self = this var type = this.types[index] this.type_edition("delete", type).then(() => { self.types.splice(this.types.indexOf(type), 1) Swal.fire({title: "{{_('Type successfully deleted!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000}) }) }, }, }