3 module.exports = function (ngApp, events) {
5 ngApp.controller('ImageManagerController', ['$scope', '$attrs', '$http', '$timeout', 'imageManagerService',
6 function ($scope, $attrs, $http, $timeout, imageManagerService) {
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;
16 var previousClickTime = 0;
17 var dataLoaded = false;
21 * Simple returns the appropriate upload url depending on the image type set.
24 $scope.getUploadUrl = function () {
25 return '/images/' + $scope.imageType + '/upload';
29 * Runs on image upload, Adds an image to local list of images
30 * and shows a success message to the user.
34 $scope.uploadSuccess = function (file, data) {
36 $scope.images.unshift(data);
38 events.emit('success', 'Image uploaded');
42 * Runs the callback and hides the image manager.
45 function callbackAndHide(returnData) {
46 if (callback) callback(returnData);
47 $scope.showing = false;
51 * Image select action. Checks if a double-click was fired.
54 $scope.imageSelect = function (image) {
55 var dblClickTime = 300;
56 var currentTime = Date.now();
57 var timeDiff = currentTime - previousClickTime;
59 if (timeDiff < dblClickTime) {
61 callbackAndHide(image);
64 $scope.selectedImage = image;
65 $scope.dependantPages = false;
67 previousClickTime = currentTime;
71 * Action that runs when the 'Select image' button is clicked.
72 * Runs the callback and hides the image manager.
74 $scope.selectButtonClick = function () {
75 callbackAndHide($scope.selectedImage);
79 * Show the image manager.
80 * Takes a callback to execute later on.
83 function show(doneCallback) {
84 callback = doneCallback;
85 $scope.showing = true;
86 // Get initial images if they have not yet been loaded in.
93 // Connects up the image manger so it can be used externally
94 // such as from TinyMCE.
95 imageManagerService.show = show;
96 imageManagerService.showExternal = function (doneCallback) {
101 window.ImageManager = imageManagerService;
104 * Hide the image manager
106 $scope.hide = function () {
107 $scope.showing = false;
111 * Fetch the list image data from the server.
113 function fetchData() {
114 var url = '/images/' + $scope.imageType + '/all/' + page;
115 $http.get(url).then((response) => {
116 $scope.images = $scope.images.concat(response.data.images);
117 $scope.hasMore = response.data.hasMore;
122 $scope.fetchData = fetchData;
125 * Save the details of an image.
128 $scope.saveImageDetails = function (event) {
129 event.preventDefault();
130 var url = '/images/update/' + $scope.selectedImage.id;
131 $http.put(url, this.selectedImage).then((response) => {
132 events.emit('success', 'Image details updated');
134 if (response.status === 422) {
135 var errors = response.data;
137 Object.keys(errors).forEach((key) => {
138 message += errors[key].join('\n');
140 events.emit('error', message);
141 } else if (response.status === 403) {
142 events.emit('error', response.data.error);
148 * Delete an image from system and notify of success.
149 * Checks if it should force delete when an image
150 * has dependant pages.
153 $scope.deleteImage = function (event) {
154 event.preventDefault();
155 var force = $scope.dependantPages !== false;
156 var url = '/images/' + $scope.selectedImage.id;
157 if (force) url += '?force=true';
158 $http.delete(url).then((response) => {
159 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
160 $scope.selectedImage = false;
161 events.emit('success', 'Image successfully deleted');
164 if (response.status === 400) {
165 $scope.dependantPages = response.data;
166 } else if (response.status === 403) {
167 events.emit('error', response.data.error);
173 * Simple date creator used to properly format dates.
177 $scope.getDate = function (stringDate) {
178 return new Date(stringDate);
184 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
185 $scope.searching = false;
186 $scope.searchTerm = '';
187 $scope.searchResults = '';
189 $scope.searchBook = function (e) {
191 var term = $scope.searchTerm;
192 if (term.length == 0) return;
193 $scope.searching = true;
194 $scope.searchResults = '';
195 var searchUrl = '/search/book/' + $attrs.bookId;
196 searchUrl += '?term=' + encodeURIComponent(term);
197 $http.get(searchUrl).then((response) => {
198 $scope.searchResults = $sce.trustAsHtml(response.data);
202 $scope.checkSearchForm = function () {
203 if ($scope.searchTerm.length < 1) {
204 $scope.searching = false;
208 $scope.clearSearch = function () {
209 $scope.searching = false;
210 $scope.searchTerm = '';