First commit

This commit is contained in:
2024-09-26 23:59:03 +02:00
commit f3116a6876
53 changed files with 1822 additions and 0 deletions
@@ -0,0 +1,5 @@
{% load i18n %}
<div>
<input class="form-control" type="text" v-model="item.name">
</div>
@@ -0,0 +1,4 @@
item = {
template: "#item",
props: ["crypto_key", "item"],
}
@@ -0,0 +1,88 @@
{% load i18n %}
<div>
<div class="card mt-4 pt-2 ps-lg-2">
<h5 class="card-header">{% trans "Your items" %}</h5>
<div class="card-body">
<v-data-table
:headers="data.items_headers"
:items="data.items"
:items-per-page="50"
:search="search"
group-by="type"
loading
dense>
<template v-slot:top>
<v-toolbar flat>
<v-text-field v-model="search" append-icon="mdi-magnify" label="Search" single-line hide-details></v-text-field>
<v-divider class="mx-4" insert vertical></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="500px">
<template v-slot:activator="{ on, attrs }">
<v-btn color="primary" dark class="mb-2" v-bind="attrs" v-on="on">{% trans "New item" %}</v-btn>
</template>
<v-card>
<v-card-text>
<v-container>
<v-row>
<v-text-field v-model="editedItem.name" label="Name"></v-text-field>
<v-select
v-model="editedItem.type"
:items="data.types"
label="Type"
item-text="name"
item-value="id"
persistent-hint
>
<template slot="item" slot-scope="data">
[[ data.item.name ]] - [[ data.item.custom_identifier ]]
</template>
</v-select>
<v-textarea v-model="editedItem.description" label="Description"></v-textarea>
<v-text-field v-model="editedItem.custom_identifier" label="Identifier"></v-text-field>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="close">{% trans "Cancel" %}</v-btn>
<v-btn color="blue darken-1" text @click="save">{% trans "Save" %}</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<v-dialog v-model="dialogDelete" max-width="500px">
<v-card>
<v-card-title class="text-h5">{% trans "Are you sure you want to delete this item?" %}</v-card-title>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="closeDelete">{% trans "Cancel" %}</v-btn>
<v-btn color="blue darken-1" text @click="deleteItemConfirm">{% trans "OK" %}</v-btn>
<v-spacer></v-spacer>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.actions="{ item }">
<v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
<v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" @click="initialize">{% trans "Reset" %}</v-btn>
</template>
</v-data-table>
</div>
</div>
</div>
@@ -0,0 +1,191 @@
item_list = {
template: "#item_list",
delimiters: ["[[", "]]"],
props: ["crypto_key", "locked"],
data: function() {
return {
dialog: false,
dialogDelete: false,
editedIndex: -1,
defaultItem: {},
editedItem: {},
search: null,
data: {},
}
},
mounted: function() {
var self = this;
this.$http.get(Urls["items:list"]()).then(response => {
Object.keys(response.data.result).forEach(name => {
self.$set(self.data, name, response.data.result[name]);
});
self.data.items.forEach(item => {
self.decryptItem(item);
});
}).catch(err => {
Swal.fire({title: "{{_('Error during loading of items.') | escapejs}}", icon: "error", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
});
},
computed: {
formTitle () {
return this.editedIndex === -1 ? "{{_('New item') | escapejs}}" : "{{_('Edit item') | escapejs}}"
},
},
watch: {
dialog (val) {
val || this.close()
},
dialogDelete (val) {
val || this.closeDelete()
},
},
methods: {
decryptItem (item) {
this.data.items_encrypted.forEach(field => {
decryptWithKey(this.crypto_key, item[field]).then(dec => {
item[field] = dec;
})
});
return item;
},
editItem (item) {
this.editedIndex = this.data.items.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialog = true
},
deleteItem (item) {
this.editedIndex = this.data.items.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialogDelete = true
},
deleteItemConfirm () {
var item = this.data.items[this.editedIndex];
this.item_edition("delete", item).then(response => {
this.data.items.splice(this.data.items.indexOf(item), 1)
Swal.fire({title: "{{_('Item successfully deleted!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
});
this.closeDelete()
},
close () {
this.dialog = false
this.$nextTick(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
})
},
closeDelete () {
this.dialogDelete = false
this.$nextTick(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
})
},
item_edition (method, item) {
// Return a Promise
var self = this;
return new Promise((resolve) => {
let url = Urls["items:edit"](item.id)
if (item.id == undefined || item.id == null) {
url = Urls["items:create"]()
}
let promises = self.data.items_encrypted.map(field => {
// Encrypt all necessary fields
if (item[field] == null) {
return null;
}
return new Promise((resolve) => {
return encryptWithKey(self.crypto_key, item[field]).then(enc => {
resolve({field: field, value: enc});
});
});
}).filter(e => e != null);
Promise.all(promises).then(values => {
values.forEach(value => {
item[value.field] = value.value;
});
self.$http[method](url, item).then(response => {
resolve(response.data);
}).catch(err => {
let msg = "{{_('Error during edition of item') | escapejs}}";
if (method == "delete") {
msg = "{{_('Error during deletion of item') | escapejs}}";
}
Swal.fire({title: msg, icon: "error", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
});
});
});
},
save () {
if (this.editedIndex > -1) {
var self = this;
this.item_edition("post", this.editedItem).then(data => {
self.data.items.splice(this.data.items.indexOf(self.editedItem), 1)
console.log('pre edit', data.item)
new_item = self.decryptItem(data.item);
console.log('edited item', new_item);
// self.data.items.push(self.decryptItem(data.item));
Swal.fire({title: "{{_('Item successfully edited') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
});
} else {
var self = this;
this.item_edition("post", this.editedItem).then(data => {
new_item = self.decryptItem(data.item);
console.log('new item', new_item);
// self.data.items.push(self.decryptItem(data.item));
// console.log(data.item);
Swal.fire({title: "{{_('Item successfully created!') | escapejs}}", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
});
}
this.close()
},
}
}