]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/image-manager.js
Updated assets and version for release v0.18.4
[bookstack] / resources / assets / js / vues / image-manager.js
1 const dropzone = require('./components/dropzone');
2
3 let page = 0;
4 let previousClickTime = 0;
5 let previousClickImage = 0;
6 let dataLoaded = false;
7 let callback = false;
8 let baseUrl = '';
9
10 let preSearchImages = [];
11 let preSearchHasMore = false;
12
13 const data = {
14     images: [],
15
16     imageType: false,
17     uploadedTo: false,
18
19     selectedImage: false,
20     dependantPages: false,
21     showing: false,
22     view: 'all',
23     hasMore: false,
24     searching: false,
25     searchTerm: '',
26
27     imageUpdateSuccess: false,
28     imageDeleteSuccess: false,
29 };
30
31 const methods = {
32
33     show(providedCallback) {
34         callback = providedCallback;
35         this.showing = true;
36         this.$el.children[0].components.overlay.show();
37
38         // Get initial images if they have not yet been loaded in.
39         if (dataLoaded) return;
40         this.fetchData();
41         dataLoaded = true;
42     },
43
44     hide() {
45         this.showing = false;
46         this.$el.children[0].components.overlay.hide();
47     },
48
49     fetchData() {
50         let url = baseUrl + page;
51         let query = {};
52         if (this.uploadedTo !== false) query.page_id = this.uploadedTo;
53         if (this.searching) query.term = this.searchTerm;
54
55         this.$http.get(url, {params: query}).then(response => {
56             this.images = this.images.concat(response.data.images);
57             this.hasMore = response.data.hasMore;
58             page++;
59         });
60     },
61
62     setView(viewName) {
63         this.cancelSearch();
64         this.images = [];
65         this.hasMore = false;
66         page = 0;
67         this.view = viewName;
68         baseUrl = window.baseUrl(`/images/${this.imageType}/${viewName}/`);
69         this.fetchData();
70     },
71
72     searchImages() {
73         if (this.searchTerm === '') return this.cancelSearch();
74
75         // Cache current settings for later
76         if (!this.searching) {
77             preSearchImages = this.images;
78             preSearchHasMore = this.hasMore;
79         }
80
81         this.searching = true;
82         this.images = [];
83         this.hasMore = false;
84         page = 0;
85         baseUrl = window.baseUrl(`/images/${this.imageType}/search/`);
86         this.fetchData();
87     },
88
89     cancelSearch() {
90         this.searching = false;
91         this.searchTerm = '';
92         this.images = preSearchImages;
93         this.hasMore = preSearchHasMore;
94     },
95
96     imageSelect(image) {
97         let dblClickTime = 300;
98         let currentTime = Date.now();
99         let timeDiff = currentTime - previousClickTime;
100         let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
101
102         if (isDblClick) {
103             this.callbackAndHide(image);
104         } else {
105             this.selectedImage = image;
106             this.dependantPages = false;
107         }
108
109         previousClickTime = currentTime;
110         previousClickImage = image.id;
111     },
112
113     callbackAndHide(imageResult) {
114         if (callback) callback(imageResult);
115         this.hide();
116     },
117
118     saveImageDetails() {
119         let url = window.baseUrl(`/images/update/${this.selectedImage.id}`);
120         this.$http.put(url, this.selectedImage).then(response => {
121             this.$events.emit('success', trans('components.image_update_success'));
122         }).catch(error => {
123             if (error.response.status === 422) {
124                 let errors = error.response.data;
125                 let message = '';
126                 Object.keys(errors).forEach((key) => {
127                     message += errors[key].join('\n');
128                 });
129                 this.$events.emit('error', message);
130             }
131         });
132     },
133
134     deleteImage() {
135         let force = this.dependantPages !== false;
136         let url = window.baseUrl('/images/' + this.selectedImage.id);
137         if (force) url += '?force=true';
138         this.$http.delete(url).then(response => {
139             this.images.splice(this.images.indexOf(this.selectedImage), 1);
140             this.selectedImage = false;
141             this.$events.emit('success', trans('components.image_delete_success'));
142         }).catch(error=> {
143             if (error.response.status === 400) {
144                 this.dependantPages = error.response.data;
145             }
146         });
147     },
148
149     getDate(stringDate) {
150         return new Date(stringDate);
151     },
152
153     uploadSuccess(event) {
154         this.images.unshift(event.data);
155         this.$events.emit('success', trans('components.image_upload_success'));
156     },
157 };
158
159 const computed = {
160     uploadUrl() {
161         return window.baseUrl(`/images/${this.imageType}/upload`);
162     }
163 };
164
165 function mounted() {
166     window.ImageManager = this;
167     this.imageType = this.$el.getAttribute('image-type');
168     this.uploadedTo = this.$el.getAttribute('uploaded-to');
169     baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
170 }
171
172 module.exports = {
173     mounted,
174     methods,
175     data,
176     computed,
177     components: {dropzone},
178 };