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;
121 $scope.fetchData = fetchData;
124 * Save the details of an image.
127 $scope.saveImageDetails = function (event) {
128 event.preventDefault();
129 var url = '/images/update/' + $scope.selectedImage.id;
130 $http.put(url, this.selectedImage).then((response) => {
131 events.emit('success', 'Image details updated');
133 var errors = response.data;
135 Object.keys(errors).forEach((key) => {
136 message += errors[key].join('\n');
138 events.emit('error', message);
143 * Delete an image from system and notify of success.
144 * Checks if it should force delete when an image
145 * has dependant pages.
148 $scope.deleteImage = function (event) {
149 event.preventDefault();
150 var force = $scope.dependantPages !== false;
151 var url = '/images/' + $scope.selectedImage.id;
152 if (force) url += '?force=true';
153 $http.delete(url).then((response) => {
154 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
155 $scope.selectedImage = false;
156 events.emit('success', 'Image successfully deleted');
159 if (response.status === 400) {
160 $scope.dependantPages = response.data;
166 * Simple date creator used to properly format dates.
170 $scope.getDate = function(stringDate) {
171 return new Date(stringDate);
177 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
178 $scope.searching = false;
179 $scope.searchTerm = '';
180 $scope.searchResults = '';
182 $scope.searchBook = function (e) {
184 var term = $scope.searchTerm;
185 if (term.length == 0) return;
186 $scope.searching = true;
187 $scope.searchResults = '';
188 var searchUrl = '/search/book/' + $attrs.bookId;
189 searchUrl += '?term=' + encodeURIComponent(term);
190 $http.get(searchUrl).then((response) => {
191 $scope.searchResults = $sce.trustAsHtml(response.data);
195 $scope.checkSearchForm = function () {
196 if ($scope.searchTerm.length < 1) {
197 $scope.searching = false;
201 $scope.clearSearch = function () {
202 $scope.searching = false;
203 $scope.searchTerm = '';