3 var moment = require('moment');
5 module.exports = function (ngApp, events) {
7 ngApp.controller('ImageManagerController', ['$scope', '$attrs', '$http', '$timeout', 'imageManagerService',
8 function ($scope, $attrs, $http, $timeout, imageManagerService) {
11 $scope.imageType = $attrs.imageType;
12 $scope.selectedImage = false;
13 $scope.dependantPages = false;
14 $scope.showing = false;
15 $scope.hasMore = false;
16 $scope.imageUpdateSuccess = false;
17 $scope.imageDeleteSuccess = false;
18 $scope.uploadedTo = $attrs.uploadedTo;
21 $scope.searching = false;
22 $scope.searchTerm = '';
25 var previousClickTime = 0;
26 var previousClickImage = 0;
27 var dataLoaded = false;
30 var preSearchImages = [];
31 var preSearchHasMore = false;
34 * Used by dropzone to get the endpoint to upload to.
37 $scope.getUploadUrl = function () {
38 return '/images/' + $scope.imageType + '/upload';
42 * Cancel the current search operation.
44 function cancelSearch() {
45 $scope.searching = false;
46 $scope.searchTerm = '';
47 $scope.images = preSearchImages;
48 $scope.hasMore = preSearchHasMore;
50 $scope.cancelSearch = cancelSearch;
54 * Runs on image upload, Adds an image to local list of images
55 * and shows a success message to the user.
59 $scope.uploadSuccess = function (file, data) {
61 $scope.images.unshift(data);
63 events.emit('success', 'Image uploaded');
67 * Runs the callback and hides the image manager.
70 function callbackAndHide(returnData) {
71 if (callback) callback(returnData);
72 $scope.showing = false;
76 * Image select action. Checks if a double-click was fired.
79 $scope.imageSelect = function (image) {
80 var dblClickTime = 300;
81 var currentTime = Date.now();
82 var timeDiff = currentTime - previousClickTime;
84 if (timeDiff < dblClickTime && image.id === previousClickImage) {
86 callbackAndHide(image);
89 $scope.selectedImage = image;
90 $scope.dependantPages = false;
92 previousClickTime = currentTime;
93 previousClickImage = image.id;
97 * Action that runs when the 'Select image' button is clicked.
98 * Runs the callback and hides the image manager.
100 $scope.selectButtonClick = function () {
101 callbackAndHide($scope.selectedImage);
105 * Show the image manager.
106 * Takes a callback to execute later on.
107 * @param doneCallback
109 function show(doneCallback) {
110 callback = doneCallback;
111 $scope.showing = true;
112 // Get initial images if they have not yet been loaded in.
119 // Connects up the image manger so it can be used externally
120 // such as from TinyMCE.
121 imageManagerService.show = show;
122 imageManagerService.showExternal = function (doneCallback) {
123 $scope.$apply(() => {
127 window.ImageManager = imageManagerService;
130 * Hide the image manager
132 $scope.hide = function () {
133 $scope.showing = false;
136 var baseUrl = '/images/' + $scope.imageType + '/all/'
139 * Fetch the list image data from the server.
141 function fetchData() {
142 var url = baseUrl + page + '?';
144 if ($scope.uploadedTo) components['page_id'] = $scope.uploadedTo;
145 if ($scope.searching) components['term'] = $scope.searchTerm;
148 var urlQueryString = Object.keys(components).map((key) => {
149 return key + '=' + encodeURIComponent(components[key]);
151 url += urlQueryString;
153 $http.get(url).then((response) => {
154 $scope.images = $scope.images.concat(response.data.images);
155 $scope.hasMore = response.data.hasMore;
159 $scope.fetchData = fetchData;
162 * Start a search operation
165 $scope.searchImages = function() {
167 if ($scope.searchTerm === '') {
172 if (!$scope.searching) {
173 preSearchImages = $scope.images;
174 preSearchHasMore = $scope.hasMore;
177 $scope.searching = true;
179 $scope.hasMore = false;
181 baseUrl = '/images/' + $scope.imageType + '/search/';
186 * Set the current image listing view.
189 $scope.setView = function(viewName) {
192 $scope.hasMore = false;
194 $scope.view = viewName;
195 baseUrl = '/images/' + $scope.imageType + '/' + viewName + '/';
200 * Save the details of an image.
203 $scope.saveImageDetails = function (event) {
204 event.preventDefault();
205 var url = '/images/update/' + $scope.selectedImage.id;
206 $http.put(url, this.selectedImage).then((response) => {
207 events.emit('success', 'Image details updated');
209 if (response.status === 422) {
210 var errors = response.data;
212 Object.keys(errors).forEach((key) => {
213 message += errors[key].join('\n');
215 events.emit('error', message);
216 } else if (response.status === 403) {
217 events.emit('error', response.data.error);
223 * Delete an image from system and notify of success.
224 * Checks if it should force delete when an image
225 * has dependant pages.
228 $scope.deleteImage = function (event) {
229 event.preventDefault();
230 var force = $scope.dependantPages !== false;
231 var url = '/images/' + $scope.selectedImage.id;
232 if (force) url += '?force=true';
233 $http.delete(url).then((response) => {
234 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
235 $scope.selectedImage = false;
236 events.emit('success', 'Image successfully deleted');
239 if (response.status === 400) {
240 $scope.dependantPages = response.data;
241 } else if (response.status === 403) {
242 events.emit('error', response.data.error);
248 * Simple date creator used to properly format dates.
252 $scope.getDate = function (stringDate) {
253 return new Date(stringDate);
259 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
260 $scope.searching = false;
261 $scope.searchTerm = '';
262 $scope.searchResults = '';
264 $scope.searchBook = function (e) {
266 var term = $scope.searchTerm;
267 if (term.length == 0) return;
268 $scope.searching = true;
269 $scope.searchResults = '';
270 var searchUrl = '/search/book/' + $attrs.bookId;
271 searchUrl += '?term=' + encodeURIComponent(term);
272 $http.get(searchUrl).then((response) => {
273 $scope.searchResults = $sce.trustAsHtml(response.data);
277 $scope.checkSearchForm = function () {
278 if ($scope.searchTerm.length < 1) {
279 $scope.searching = false;
283 $scope.clearSearch = function () {
284 $scope.searching = false;
285 $scope.searchTerm = '';
291 ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', '$timeout', '$sce',
292 function ($scope, $http, $attrs, $interval, $timeout, $sce) {
294 $scope.editorOptions = require('./pages/page-form');
295 $scope.editContent = '';
296 $scope.draftText = '';
297 var pageId = Number($attrs.pageId);
298 var isEdit = pageId !== 0;
299 var autosaveFrequency = 30; // AutoSave interval in seconds.
300 var isMarkdown = $attrs.editorType === 'markdown';
301 $scope.isUpdateDraft = Number($attrs.pageUpdateDraft) === 1;
302 $scope.isNewPageDraft = Number($attrs.pageNewDraft) === 1;
304 // Set inital header draft text
305 if ($scope.isUpdateDraft || $scope.isNewPageDraft) {
306 $scope.draftText = 'Editing Draft'
308 $scope.draftText = 'Editing Page'
311 var autoSave = false;
313 var currentContent = {
324 // Actions specifically for the markdown editor
326 $scope.displayContent = '';
327 // Editor change event
328 $scope.editorChange = function (content) {
329 $scope.displayContent = $sce.trustAsHtml(content);
334 $scope.editorChange = function() {};
338 * Start the AutoSave loop, Checks for content change
339 * before performing the costly AJAX request.
341 function startAutoSave() {
342 currentContent.title = $('#name').val();
343 currentContent.html = $scope.editContent;
345 autoSave = $interval(() => {
346 var newTitle = $('#name').val();
347 var newHtml = $scope.editContent;
349 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
350 currentContent.html = newHtml;
351 currentContent.title = newTitle;
355 }, 1000 * autosaveFrequency);
359 * Save a draft update into the system via an AJAX request.
363 function saveDraft() {
365 name: $('#name').val(),
366 html: isMarkdown ? $sce.getTrustedHtml($scope.displayContent) : $scope.editContent
369 if (isMarkdown) data.markdown = $scope.editContent;
371 $http.put('/ajax/page/' + pageId + '/save-draft', data).then((responseData) => {
372 var updateTime = moment.utc(moment.unix(responseData.data.timestamp)).toDate();
373 $scope.draftText = responseData.data.message + moment(updateTime).format('HH:mm');
374 if (!$scope.isNewPageDraft) $scope.isUpdateDraft = true;
378 $scope.forceDraftSave = function() {
383 * Discard the current draft and grab the current page
384 * content from the system via an AJAX request.
386 $scope.discardDraft = function () {
387 $http.get('/ajax/page/' + pageId).then((responseData) => {
388 if (autoSave) $interval.cancel(autoSave);
389 $scope.draftText = 'Editing Page';
390 $scope.isUpdateDraft = false;
391 $scope.$broadcast('html-update', responseData.data.html);
392 $scope.$broadcast('markdown-update', responseData.data.markdown || responseData.data.html);
393 $('#name').val(responseData.data.name);
397 events.emit('success', 'Draft discarded, The editor has been updated with the current page content');
403 ngApp.controller('PageTagController', ['$scope', '$http', '$attrs',
404 function ($scope, $http, $attrs) {
406 const pageId = Number($attrs.pageId);
409 $scope.sortOptions = {
412 containment: "parent",
417 * Push an empty tag to the end of the scope tags.
419 function addEmptyTag() {
425 $scope.addEmptyTag = addEmptyTag;
428 * Get all tags for the current book and add into scope.
431 $http.get('/ajax/tags/get/page/' + pageId).then((responseData) => {
432 $scope.tags = responseData.data;
439 * Set the order property on all tags.
441 function setTagOrder() {
442 for (let i = 0; i < $scope.tags.length; i++) {
443 $scope.tags[i].order = i;
448 * When an tag changes check if another empty editable
449 * field needs to be added onto the end.
452 $scope.tagChange = function(tag) {
453 let cPos = $scope.tags.indexOf(tag);
454 if (cPos !== $scope.tags.length-1) return;
456 if (tag.name !== '' || tag.value !== '') {
462 * When an tag field loses focus check the tag to see if its
463 * empty and therefore could be removed from the list.
466 $scope.tagBlur = function(tag) {
467 let isLast = $scope.tags.length - 1 === $scope.tags.indexOf(tag);
468 if (tag.name === '' && tag.value === '' && !isLast) {
469 let cPos = $scope.tags.indexOf(tag);
470 $scope.tags.splice(cPos, 1);
475 * Save the tags to the current page.
477 $scope.saveTags = function() {
479 let postData = {tags: $scope.tags};
480 $http.post('/ajax/tags/update/page/' + pageId, postData).then((responseData) => {
481 $scope.tags = responseData.data.tags;
483 events.emit('success', responseData.data.message);
488 * Remove a tag from the current list.
491 $scope.removeTag = function(tag) {
492 let cIndex = $scope.tags.indexOf(tag);
493 $scope.tags.splice(cIndex, 1);