]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/attachment-manager.js
Fixes a corner case with exclamation in the ID.
[bookstack] / resources / assets / js / vues / attachment-manager.js
1 const draggable = require('vuedraggable');
2 const dropzone = require('./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) return file.deleting = true;
56
57         this.$http.delete(window.baseUrl(`/attachments/${file.id}`)).then(resp => {
58             this.$events.emit('success', resp.data.message);
59             this.files.splice(this.files.indexOf(file), 1);
60         }).catch(err => {
61             this.checkValidationErrors('delete', err)
62         });
63     },
64
65     uploadSuccess(upload) {
66         this.files.push(upload.data);
67         this.$events.emit('success', trans('entities.attachments_file_uploaded'));
68     },
69
70     uploadSuccessUpdate(upload) {
71         let fileIndex = this.filesIndex(upload.data);
72         if (fileIndex === -1) {
73             this.files.push(upload.data)
74         } else {
75             this.files.splice(fileIndex, 1, upload.data);
76         }
77
78         if (this.fileToEdit && this.fileToEdit.id === upload.data.id) {
79             this.fileToEdit = Object.assign({}, upload.data);
80         }
81         this.$events.emit('success', trans('entities.attachments_file_updated'));
82     },
83
84     checkValidationErrors(groupName, err) {
85         if (typeof err.response.data === "undefined" && typeof err.response.data === "undefined") return;
86         this.errors[groupName] = err.response.data;
87     },
88
89     getUploadUrl(file) {
90         let url = window.baseUrl(`/attachments/upload`);
91         if (typeof file !== 'undefined') url += `/${file.id}`;
92         return url;
93     },
94
95     cancelEdit() {
96         this.fileToEdit = null;
97     },
98
99     attachNewLink(file) {
100         file.uploaded_to = this.pageId;
101         this.errors.link = {};
102         this.$http.post(window.baseUrl('/attachments/link'), file).then(resp => {
103             this.files.push(resp.data);
104             this.file = this.newFile();
105             this.$events.emit('success', trans('entities.attachments_link_attached'));
106         }).catch(err => {
107             this.checkValidationErrors('link', err);
108         });
109     },
110
111     updateFile(file) {
112         $http.put(window.baseUrl(`/attachments/${file.id}`), file).then(resp => {
113             let search = this.filesIndex(resp.data);
114             if (search === -1) {
115                 this.files.push(resp.data);
116             } else {
117                 this.files.splice(search, 1, resp.data);
118             }
119
120             if (this.fileToEdit && !file.external) this.fileToEdit.link = '';
121             this.fileToEdit = false;
122
123             this.$events.emit('success', trans('entities.attachments_updated_success'));
124         }).catch(err => {
125             this.checkValidationErrors('edit', err);
126         });
127     },
128
129     filesIndex(file) {
130         for (let i = 0, len = this.files.length; i < len; i++) {
131             if (this.files[i].id === file.id) return i;
132         }
133         return -1;
134     }
135
136 };
137
138 module.exports = {
139     data, methods, mounted, components,
140 };