3 module.exports = function (ngApp, events) {
5 ngApp.controller('ImageManagerController', ['$scope', '$attrs', '$http', '$timeout', 'imageManagerService',
6 function ($scope, $attrs, $http, $timeout, imageManagerService) {
9 $scope.imageType = $attrs.imageType;
10 $scope.selectedImage = false;
11 $scope.dependantPages = false;
12 $scope.showing = false;
13 $scope.hasMore = false;
14 $scope.imageUpdateSuccess = false;
15 $scope.imageDeleteSuccess = false;
16 $scope.uploadedTo = $attrs.uploadedTo;
19 var previousClickTime = 0;
20 var dataLoaded = false;
24 * Simple returns the appropriate upload url depending on the image type set.
27 $scope.getUploadUrl = function () {
28 return '/images/' + $scope.imageType + '/upload';
32 * Runs on image upload, Adds an image to local list of images
33 * and shows a success message to the user.
37 $scope.uploadSuccess = function (file, data) {
39 $scope.images.unshift(data);
41 events.emit('success', 'Image uploaded');
45 * Runs the callback and hides the image manager.
48 function callbackAndHide(returnData) {
49 if (callback) callback(returnData);
50 $scope.showing = false;
54 * Image select action. Checks if a double-click was fired.
57 $scope.imageSelect = function (image) {
58 var dblClickTime = 300;
59 var currentTime = Date.now();
60 var timeDiff = currentTime - previousClickTime;
62 if (timeDiff < dblClickTime) {
64 callbackAndHide(image);
67 $scope.selectedImage = image;
68 $scope.dependantPages = false;
70 previousClickTime = currentTime;
74 * Action that runs when the 'Select image' button is clicked.
75 * Runs the callback and hides the image manager.
77 $scope.selectButtonClick = function () {
78 callbackAndHide($scope.selectedImage);
82 * Show the image manager.
83 * Takes a callback to execute later on.
86 function show(doneCallback) {
87 callback = doneCallback;
88 $scope.showing = true;
89 // Get initial images if they have not yet been loaded in.
96 // Connects up the image manger so it can be used externally
97 // such as from TinyMCE.
98 imageManagerService.show = show;
99 imageManagerService.showExternal = function (doneCallback) {
100 $scope.$apply(() => {
104 window.ImageManager = imageManagerService;
107 * Hide the image manager
109 $scope.hide = function () {
110 $scope.showing = false;
114 * Fetch the list image data from the server.
116 function fetchData() {
117 var url = '/images/' + $scope.imageType + '/all/' + page;
118 $http.get(url).then((response) => {
119 $scope.images = $scope.images.concat(response.data.images);
120 $scope.hasMore = response.data.hasMore;
125 $scope.fetchData = fetchData;
128 * Save the details of an image.
131 $scope.saveImageDetails = function (event) {
132 event.preventDefault();
133 var url = '/images/update/' + $scope.selectedImage.id;
134 $http.put(url, this.selectedImage).then((response) => {
135 events.emit('success', 'Image details updated');
137 if (response.status === 422) {
138 var errors = response.data;
140 Object.keys(errors).forEach((key) => {
141 message += errors[key].join('\n');
143 events.emit('error', message);
144 } else if (response.status === 403) {
145 events.emit('error', response.data.error);
151 * Delete an image from system and notify of success.
152 * Checks if it should force delete when an image
153 * has dependant pages.
156 $scope.deleteImage = function (event) {
157 event.preventDefault();
158 var force = $scope.dependantPages !== false;
159 var url = '/images/' + $scope.selectedImage.id;
160 if (force) url += '?force=true';
161 $http.delete(url).then((response) => {
162 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
163 $scope.selectedImage = false;
164 events.emit('success', 'Image successfully deleted');
167 if (response.status === 400) {
168 $scope.dependantPages = response.data;
169 } else if (response.status === 403) {
170 events.emit('error', response.data.error);
176 * Simple date creator used to properly format dates.
180 $scope.getDate = function (stringDate) {
181 return new Date(stringDate);
187 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
188 $scope.searching = false;
189 $scope.searchTerm = '';
190 $scope.searchResults = '';
192 $scope.searchBook = function (e) {
194 var term = $scope.searchTerm;
195 if (term.length == 0) return;
196 $scope.searching = true;
197 $scope.searchResults = '';
198 var searchUrl = '/search/book/' + $attrs.bookId;
199 searchUrl += '?term=' + encodeURIComponent(term);
200 $http.get(searchUrl).then((response) => {
201 $scope.searchResults = $sce.trustAsHtml(response.data);
205 $scope.checkSearchForm = function () {
206 if ($scope.searchTerm.length < 1) {
207 $scope.searching = false;
211 $scope.clearSearch = function () {
212 $scope.searching = false;
213 $scope.searchTerm = '';
219 ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', '$timeout', function ($scope, $http, $attrs, $interval, $timeout) {
221 $scope.editorOptions = require('./pages/page-form');
222 $scope.editorHtml = '';
223 $scope.draftText = '';
224 var pageId = Number($attrs.pageId);
225 var isEdit = pageId !== 0;
226 var autosaveFrequency = 30; // AutoSave interval in seconds.
227 $scope.isUpdateDraft = Number($attrs.pageUpdateDraft) === 1;
228 $scope.isNewPageDraft = Number($attrs.pageNewDraft) === 1;
229 if ($scope.isUpdateDraft || $scope.isNewPageDraft) {
230 $scope.draftText = 'Editing Draft'
232 $scope.draftText = 'Editing Page'
235 var autoSave = false;
237 var currentContent = {
248 $scope.editorChange = function () {}
251 * Start the AutoSave loop, Checks for content change
252 * before performing the costly AJAX request.
254 function startAutoSave() {
255 currentContent.title = $('#name').val();
256 currentContent.html = $scope.editorHtml;
258 autoSave = $interval(() => {
259 var newTitle = $('#name').val();
260 var newHtml = $scope.editorHtml;
262 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
263 currentContent.html = newHtml;
264 currentContent.title = newTitle;
265 saveDraft(newTitle, newHtml);
267 }, 1000 * autosaveFrequency);
271 * Save a draft update into the system via an AJAX request.
275 function saveDraft(title, html) {
276 $http.put('/ajax/page/' + pageId + '/save-draft', {
279 }).then((responseData) => {
280 $scope.draftText = responseData.data.message;
281 if (!$scope.isNewPageDraft) $scope.isUpdateDraft = true;
285 $scope.forceDraftSave = function() {
286 var newTitle = $('#name').val();
287 var newHtml = $scope.editorHtml;
288 saveDraft(newTitle, newHtml);
292 * Discard the current draft and grab the current page
293 * content from the system via an AJAX request.
295 $scope.discardDraft = function () {
296 $http.get('/ajax/page/' + pageId).then((responseData) => {
297 if (autoSave) $interval.cancel(autoSave);
298 $scope.draftText = 'Editing Page';
299 $scope.isUpdateDraft = false;
300 $scope.$broadcast('html-update', responseData.data.html);
301 $('#name').val(responseData.data.name);
305 events.emit('success', 'Draft discarded, The editor has been updated with the current page content');