]> BookStack Code Mirror - bookstack/blob - resources/js/vues/attachment-manager.js
Add feature to send test e-mails
[bookstack] / resources / js / vues / attachment-manager.js
1 import draggable from "vuedraggable";
2 import dropzone from "./components/dropzone";
3
4 function mounted() {
5     this.pageId = this.$el.getAttribute('page-id');
6     this.file = this.newFile();
7
8     this.$http.get(window.baseUrl(`/attachments/get/page/${this.pageId}`)).then(resp => {
9         this.files = resp.data;
10     }).catch(err => {
11         this.checkValidationErrors('get', err);
12     });
13 }
14
15 let data = {
16     pageId: null,
17     files: [],
18     fileToEdit: null,
19     file: {},
20     tab: 'list',
21     editTab: 'file',
22     errors: {link: {}, edit: {}, delete: {}}
23 };
24
25 const components = {dropzone, draggable};
26
27 let methods = {
28
29     newFile() {
30         return {page_id: this.pageId};
31     },
32
33     getFileUrl(file) {
34         if (file.external && file.path.indexOf('http') !== 0) {
35             return file.path;
36         }
37         return window.baseUrl(`/attachments/${file.id}`);
38     },
39
40     fileSortUpdate() {
41         this.$http.put(window.baseUrl(`/attachments/sort/page/${this.pageId}`), {files: this.files}).then(resp => {
42             this.$events.emit('success', resp.data.message);
43         }).catch(err => {
44             this.checkValidationErrors('sort', err);
45         });
46     },
47
48     startEdit(file) {
49         this.fileToEdit = Object.assign({}, file);
50         this.fileToEdit.link = file.external ? file.path : '';
51         this.editTab = file.external ? 'link' : 'file';
52     },
53
54     deleteFile(file) {
55         if (!file.deleting) {
56             return this.$set(file, 'deleting', true);
57         }
58
59         this.$http.delete(window.baseUrl(`/attachments/${file.id}`)).then(resp => {
60             this.$events.emit('success', resp.data.message);
61             this.files.splice(this.files.indexOf(file), 1);
62         }).catch(err => {
63             this.checkValidationErrors('delete', err)
64         });
65     },
66
67     uploadSuccess(upload) {
68         this.files.push(upload.data);
69         this.$events.emit('success', trans('entities.attachments_file_uploaded'));
70     },
71
72     uploadSuccessUpdate(upload) {
73         let fileIndex = this.filesIndex(upload.data);
74         if (fileIndex === -1) {
75             this.files.push(upload.data)
76         } else {
77             this.files.splice(fileIndex, 1, upload.data);
78         }
79
80         if (this.fileToEdit && this.fileToEdit.id === upload.data.id) {
81             this.fileToEdit = Object.assign({}, upload.data);
82         }
83         this.$events.emit('success', trans('entities.attachments_file_updated'));
84     },
85
86     checkValidationErrors(groupName, err) {
87         if (typeof err.response.data === "undefined" && typeof err.response.data === "undefined") return;
88         this.errors[groupName] = err.response.data;
89     },
90
91     getUploadUrl(file) {
92         let url = window.baseUrl(`/attachments/upload`);
93         if (typeof file !== 'undefined') url += `/${file.id}`;
94         return url;
95     },
96
97     cancelEdit() {
98         this.fileToEdit = null;
99     },
100
101     attachNewLink(file) {
102         file.uploaded_to = this.pageId;
103         this.errors.link = {};
104         this.$http.post(window.baseUrl('/attachments/link'), file).then(resp => {
105             this.files.push(resp.data);
106             this.file = this.newFile();
107             this.$events.emit('success', trans('entities.attachments_link_attached'));
108         }).catch(err => {
109             this.checkValidationErrors('link', err);
110         });
111     },
112
113     updateFile(file) {
114         $http.put(window.baseUrl(`/attachments/${file.id}`), file).then(resp => {
115             let search = this.filesIndex(resp.data);
116             if (search === -1) {
117                 this.files.push(resp.data);
118             } else {
119                 this.files.splice(search, 1, resp.data);
120             }
121
122             if (this.fileToEdit && !file.external) this.fileToEdit.link = '';
123             this.fileToEdit = false;
124
125             this.$events.emit('success', trans('entities.attachments_updated_success'));
126         }).catch(err => {
127             this.checkValidationErrors('edit', err);
128         });
129     },
130
131     filesIndex(file) {
132         for (let i = 0, len = this.files.length; i < len; i++) {
133             if (this.files[i].id === file.id) return i;
134         }
135         return -1;
136     }
137
138 };
139
140 export default {
141     data, methods, mounted, components,
142 };