|
6 | 6 | b-btn.ml-1(@click="restoreDefaultClasses", variant="outline-warning" size="sm") |
7 | 7 | icon(name="undo") |
8 | 8 | | Restore defaults |
| 9 | + label.btn.btn-sm.ml-1.btn-outline-primary(style="margin: 0") |
| 10 | + | Import |
| 11 | + input(type="file" @change="importCategories" hidden) |
| 12 | + b-btn.ml-1(@click="exportClasses", variant="outline-primary" size="sm") |
| 13 | + | Export |
9 | 14 | div |
10 | 15 | | Rules for categorizing events. An event can only have one category. If several categories match, the deepest one will be chosen. |
11 | 16 |
|
@@ -61,6 +66,41 @@ export default { |
61 | 66 | restoreDefaultClasses: async function () { |
62 | 67 | await this.$store.commit('categories/restoreDefaultClasses'); |
63 | 68 | }, |
| 69 | + exportClasses: function () { |
| 70 | + console.log('Exporting categories...'); |
| 71 | +
|
| 72 | + const export_data = { |
| 73 | + categories: JSON.parse(localStorage.classes), |
| 74 | + }; |
| 75 | + const text = JSON.stringify(export_data); |
| 76 | + const filename = 'aw-category-export.json'; |
| 77 | +
|
| 78 | + // Initiate downloading a file by creating a hidden button and clicking it |
| 79 | + const element = document.createElement('a'); |
| 80 | + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); |
| 81 | + element.setAttribute('download', filename); |
| 82 | + element.style.display = 'none'; |
| 83 | + document.body.appendChild(element); |
| 84 | + element.click(); |
| 85 | + document.body.removeChild(element); |
| 86 | + }, |
| 87 | + importCategories: async function (elem) { |
| 88 | + console.log('Importing categories...'); |
| 89 | +
|
| 90 | + // Get file from upload |
| 91 | + const file = elem.originalTarget.files[0]; |
| 92 | + if (file.type != 'application/json') { |
| 93 | + console.error('Only JSON files are possible to import'); |
| 94 | + return; |
| 95 | + } |
| 96 | +
|
| 97 | + // Read and parse import text to JSON |
| 98 | + const text = await file.text(); |
| 99 | + const import_obj = JSON.parse(text); |
| 100 | +
|
| 101 | + // Set import to categories as unsaved changes |
| 102 | + this.$store.commit('categories/import', import_obj.categories); |
| 103 | + }, |
64 | 104 | }, |
65 | 105 | }; |
66 | 106 | </script> |
0 commit comments