/
FilesSection.vue
110 lines (105 loc) · 2.21 KB
/
FilesSection.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<script>
import ModelsSection from "@/components/Sections/ModelsSection.vue";
export default {
extends: ModelsSection,
computed: {
addIcon() {
return "upload";
},
canAdd() {
return (
this.$panel.permissions.files.create && this.options.upload !== false
);
},
canDrop() {
return this.canAdd !== false;
},
emptyProps() {
return {
icon: "image",
text: this.$t("files.empty")
};
},
items() {
return this.data.map((file) => {
file.sortable = this.options.sortable;
file.column = this.column;
file.options = this.$dropdown(file.link, {
query: {
view: "list",
update: this.options.sortable,
delete: this.data.length > this.options.min
}
});
// add data-attributes info for item
file.data = {
"data-id": file.id,
"data-template": file.template
};
return file;
});
},
type() {
return "files";
},
uploadOptions() {
return {
...this.options.upload,
url: this.$panel.urls.api + "/" + this.options.upload.api,
on: {
complete: () => {
this.$panel.notification.success({ context: "view" });
}
}
};
}
},
mounted() {
this.$events.on("model.update", this.reload);
this.$events.on("file.sort", this.reload);
},
destroyed() {
this.$events.off("model.update", this.reload);
this.$events.off("file.sort", this.reload);
},
methods: {
onAction(action, file) {
if (action === "replace") {
this.replace(file);
}
},
onAdd() {
if (this.canAdd) {
this.$panel.upload.pick(this.uploadOptions);
}
},
onDrop(files) {
if (this.canAdd) {
this.$panel.upload.open(files, this.uploadOptions);
}
},
async onSort(items) {
if (this.options.sortable === false) {
return false;
}
this.isProcessing = true;
try {
await this.$api.patch(this.options.apiUrl + "/sort", {
files: items.map((item) => item.id),
index: this.pagination.offset
});
this.$panel.notification.success();
this.$events.emit("file.sort");
} catch (error) {
this.$panel.error(error);
this.reload();
} finally {
this.isProcessing = false;
}
},
replace(file) {
this.$panel.upload.replace(file, this.uploadOptions);
}
}
};
</script>