]> BookStack Code Mirror - bookstack/blob - resources/assets/js/controllers.js
5d2d4bbcad5e23640adf8a1a222aca729f1fffaf
[bookstack] / resources / assets / js / controllers.js
1 "use strict";
2
3 module.exports = function(ngApp) {
4
5     ngApp.controller('ImageManagerController', ['$scope', '$attrs', '$http', '$timeout','imageManagerService',
6     function($scope, $attrs, $http, $timeout, imageManagerService) {
7         $scope.images = [];
8         $scope.imageType = $attrs.imageType;
9         $scope.selectedImage = false;
10         $scope.dependantPages = false;
11         $scope.showing = false;
12         $scope.hasMore = false;
13         $scope.imageUpdateSuccess = false;
14         $scope.imageDeleteSuccess = false;
15         var page = 0;
16         var previousClickTime = 0;
17         var dataLoaded = false;
18         var callback = false;
19
20         $scope.getUploadUrl = function() {
21             return '/images/' + $scope.imageType + '/upload';
22         };
23
24         $scope.uploadSuccess = function(file, data) {
25             $scope.$apply(() => {
26                 $scope.images.unshift(data);
27             });
28         };
29
30         function callbackAndHide(returnData) {
31             if (callback) callback(returnData);
32             $scope.showing = false;
33         }
34
35         $scope.imageSelect = function (image) {
36             var dblClickTime = 300;
37             var currentTime = Date.now();
38             var timeDiff = currentTime - previousClickTime;
39
40             if (timeDiff < dblClickTime) {
41                 // If double click
42                 callbackAndHide(image);
43             } else {
44                 // If single
45                 $scope.selectedImage = image;
46                 $scope.dependantPages = false;
47             }
48             previousClickTime = currentTime;
49         };
50
51         $scope.selectButtonClick = function() {
52             callbackAndHide($scope.selectedImage);
53         };
54
55         function show(doneCallback) {
56             callback = doneCallback;
57             $scope.showing = true;
58             // Get initial images if they have not yet been loaded in.
59             if (!dataLoaded) {
60                 fetchData();
61                 dataLoaded = true;
62             }
63         }
64
65         imageManagerService.show = show;
66         imageManagerService.showExternal = function(doneCallback) {
67             $scope.$apply(() => {
68                 show(doneCallback);
69             });
70         };
71         window.ImageManager = imageManagerService;
72
73         $scope.hide = function() {
74             $scope.showing = false;
75         };
76
77         function fetchData() {
78             var url = '/images/' + $scope.imageType + '/all/' + page;
79             $http.get(url).then((response) => {
80                 $scope.images = $scope.images.concat(response.data.images);
81                 $scope.hasMore = response.data.hasMore;
82                 page++;
83             });
84         }
85
86         $scope.saveImageDetails = function(event) {
87             event.preventDefault();
88             var url = '/images/update/' + $scope.selectedImage.id;
89             $http.put(url, this.selectedImage).then((response) => {
90                 $scope.imageUpdateSuccess = true;
91                 $timeout(() => {
92                     $scope.imageUpdateSuccess = false;
93                 }, 3000);
94             }, (response) => {
95                 var errors = response.data;
96                 var message = '';
97                 Object.keys(errors).forEach((key) => {
98                     message += errors[key].join('\n');
99                 });
100                 $scope.imageUpdateFailure = message;
101                 $timeout(() => {
102                     $scope.imageUpdateFailure = false;
103                 }, 5000);
104             });
105         };
106
107         $scope.deleteImage = function(event) {
108             event.preventDefault();
109             var force = $scope.dependantPages !== false;
110             var url = '/images/' + $scope.selectedImage.id;
111             if (force) url += '?force=true';
112             $http.delete(url).then((response) => {
113                 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
114                 $scope.selectedImage = false;
115                 $scope.imageDeleteSuccess = true;
116                 $timeout(() => {
117                     $scope.imageDeleteSuccess = false;
118                 }, 3000);
119             }, (response) => {
120                 // Pages failure
121                 if (response.status === 400) {
122                     $scope.dependantPages = response.data;
123                 }
124             });
125         };
126
127     }]);
128
129
130     ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', function($scope, $http, $attrs) {
131         $scope.searching = false;
132         $scope.searchTerm = '';
133         $scope.searchResults = '';
134
135         $scope.searchBook = function (e) {
136             e.preventDefault();
137             var term = $scope.searchTerm;
138             if (term.length == 0) return;
139             $scope.searching = true;
140             $scope.searchResults = '';
141             var searchUrl = '/search/book/' + $attrs.bookId;
142             searchUrl += '?term=' + encodeURIComponent(term);
143             $http.get(searchUrl).then((response) => {
144                 $scope.searchResults = response.data;
145             });
146         };
147
148         $scope.checkSearchForm = function () {
149             if ($scope.searchTerm.length < 1) {
150                 $scope.searching = false;
151             }
152         };
153
154         $scope.clearSearch = function() {
155             $scope.searching = false;
156             $scope.searchTerm = '';
157         };
158
159     }]);
160
161
162 };