]> BookStack Code Mirror - bookstack/blobdiff - resources/assets/js/vues/image-manager.js
Merge pull request #1072 from CliffyPrime/german_update
[bookstack] / resources / assets / js / vues / image-manager.js
index 9e3fa013eaadef83e6343421b183a83d0812795b..6bfc2662df250a66f6d221308dcd04ebb88d1500 100644 (file)
@@ -1,4 +1,5 @@
-const dropzone = require('./components/dropzone');
+import * as Dates from "../services/dates";
+import dropzone from "./components/dropzone";
 
 let page = 0;
 let previousClickTime = 0;
@@ -26,23 +27,32 @@ const data = {
 
     imageUpdateSuccess: false,
     imageDeleteSuccess: false,
+    deleteConfirm: false,
 };
 
 const methods = {
 
-    show(providedCallback) {
+    show(providedCallback, imageType = null) {
         callback = providedCallback;
         this.showing = true;
         this.$el.children[0].components.overlay.show();
 
         // Get initial images if they have not yet been loaded in.
-        if (dataLoaded) return;
+        if (dataLoaded && imageType === this.imageType) return;
+        if (imageType) {
+            this.imageType = imageType;
+            this.resetState();
+        }
         this.fetchData();
         dataLoaded = true;
     },
 
     hide() {
+        if (this.$refs.dropzone) {
+            this.$refs.dropzone.onClose();
+        }
         this.showing = false;
+        this.selectedImage = false;
         this.$el.children[0].components.overlay.hide();
     },
 
@@ -60,13 +70,18 @@ const methods = {
     },
 
     setView(viewName) {
+        this.view = viewName;
+        this.resetState();
+        this.fetchData();
+    },
+
+    resetState() {
         this.cancelSearch();
         this.images = [];
         this.hasMore = false;
+        this.deleteConfirm = false;
         page = 0;
-        this.view = viewName;
-        baseUrl = window.baseUrl(`/images/${this.imageType}/${viewName}/`);
-        this.fetchData();
+        baseUrl = window.baseUrl(`/images/${this.imageType}/${this.view}/`);
     },
 
     searchImages() {
@@ -87,6 +102,7 @@ const methods = {
     },
 
     cancelSearch() {
+        if (!this.searching) return;
         this.searching = false;
         this.searchTerm = '';
         this.images = preSearchImages;
@@ -103,6 +119,7 @@ const methods = {
             this.callbackAndHide(image);
         } else {
             this.selectedImage = image;
+            this.deleteConfirm = false;
             this.dependantPages = false;
         }
 
@@ -127,31 +144,32 @@ const methods = {
                     message += errors[key].join('\n');
                 });
                 this.$events.emit('error', message);
-            } else if (error.response.status === 403) {
-                this.$events.emit('error', error.response.data.error);
             }
         });
     },
 
     deleteImage() {
-        let force = this.dependantPages !== false;
-        let url = window.baseUrl('/images/' + this.selectedImage.id);
-        if (force) url += '?force=true';
-        this.$http.delete(url).then(response => {
+
+        if (!this.deleteConfirm) {
+            let url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
+            this.$http.get(url).then(resp => {
+                this.dependantPages = resp.data;
+            }).catch(console.error).then(() => {
+                this.deleteConfirm = true;
+            });
+            return;
+        }
+        let url = window.baseUrl(`/images/${this.selectedImage.id}`);
+        this.$http.delete(url).then(resp => {
             this.images.splice(this.images.indexOf(this.selectedImage), 1);
             this.selectedImage = false;
             this.$events.emit('success', trans('components.image_delete_success'));
-        }).catch(error=> {
-            if (error.response.status === 400) {
-                this.dependantPages = error.response.data;
-            } else if (error.response.status === 403) {
-                this.$events.emit('error', error.response.data.error);
-            }
+            this.deleteConfirm = false;
         });
     },
 
     getDate(stringDate) {
-        return new Date(stringDate);
+        return Dates.formatDateTime(new Date(stringDate));
     },
 
     uploadSuccess(event) {
@@ -173,10 +191,10 @@ function mounted() {
     baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
 }
 
-module.exports = {
+export default {
     mounted,
     methods,
     data,
     computed,
     components: {dropzone},
-};
\ No newline at end of file
+};