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 $scope.searching = false;
20 $scope.searchTerm = '';
23 var previousClickTime = 0;
24 var previousClickImage = 0;
25 var dataLoaded = false;
28 var preSearchImages = [];
29 var preSearchHasMore = false;
32 * Used by dropzone to get the endpoint to upload to.
35 $scope.getUploadUrl = function () {
36 return '/images/' + $scope.imageType + '/upload';
40 * Cancel the current search operation.
42 function cancelSearch() {
43 $scope.searching = false;
44 $scope.searchTerm = '';
45 $scope.images = preSearchImages;
46 $scope.hasMore = preSearchHasMore;
48 $scope.cancelSearch = cancelSearch;
52 * Runs on image upload, Adds an image to local list of images
53 * and shows a success message to the user.
57 $scope.uploadSuccess = function (file, data) {
59 $scope.images.unshift(data);
61 events.emit('success', 'Image uploaded');
65 * Runs the callback and hides the image manager.
68 function callbackAndHide(returnData) {
69 if (callback) callback(returnData);
70 $scope.showing = false;
74 * Image select action. Checks if a double-click was fired.
77 $scope.imageSelect = function (image) {
78 var dblClickTime = 300;
79 var currentTime = Date.now();
80 var timeDiff = currentTime - previousClickTime;
82 if (timeDiff < dblClickTime && image.id === previousClickImage) {
84 callbackAndHide(image);
87 $scope.selectedImage = image;
88 $scope.dependantPages = false;
90 previousClickTime = currentTime;
91 previousClickImage = image.id;
95 * Action that runs when the 'Select image' button is clicked.
96 * Runs the callback and hides the image manager.
98 $scope.selectButtonClick = function () {
99 callbackAndHide($scope.selectedImage);
103 * Show the image manager.
104 * Takes a callback to execute later on.
105 * @param doneCallback
107 function show(doneCallback) {
108 callback = doneCallback;
109 $scope.showing = true;
110 // Get initial images if they have not yet been loaded in.
117 // Connects up the image manger so it can be used externally
118 // such as from TinyMCE.
119 imageManagerService.show = show;
120 imageManagerService.showExternal = function (doneCallback) {
121 $scope.$apply(() => {
125 window.ImageManager = imageManagerService;
128 * Hide the image manager
130 $scope.hide = function () {
131 $scope.showing = false;
134 var baseUrl = '/images/' + $scope.imageType + '/all/'
137 * Fetch the list image data from the server.
139 function fetchData() {
140 var url = baseUrl + page + '?';
142 if ($scope.uploadedTo) components['page_id'] = $scope.uploadedTo;
143 if ($scope.searching) components['term'] = $scope.searchTerm;
146 var urlQueryString = Object.keys(components).map((key) => {
147 return key + '=' + encodeURIComponent(components[key]);
149 url += urlQueryString;
151 $http.get(url).then((response) => {
152 $scope.images = $scope.images.concat(response.data.images);
153 $scope.hasMore = response.data.hasMore;
157 $scope.fetchData = fetchData;
160 * Start a search operation
163 $scope.searchImages = function() {
165 if ($scope.searchTerm === '') {
170 if (!$scope.searching) {
171 preSearchImages = $scope.images;
172 preSearchHasMore = $scope.hasMore;
175 $scope.searching = true;
177 $scope.hasMore = false;
179 baseUrl = '/images/' + $scope.imageType + '/search/';
184 * Set the current image listing view.
187 $scope.setView = function(viewName) {
190 $scope.hasMore = false;
192 $scope.view = viewName;
193 baseUrl = '/images/' + $scope.imageType + '/' + viewName + '/';
198 * Save the details of an image.
201 $scope.saveImageDetails = function (event) {
202 event.preventDefault();
203 var url = '/images/update/' + $scope.selectedImage.id;
204 $http.put(url, this.selectedImage).then((response) => {
205 events.emit('success', 'Image details updated');
207 if (response.status === 422) {
208 var errors = response.data;
210 Object.keys(errors).forEach((key) => {
211 message += errors[key].join('\n');
213 events.emit('error', message);
214 } else if (response.status === 403) {
215 events.emit('error', response.data.error);
221 * Delete an image from system and notify of success.
222 * Checks if it should force delete when an image
223 * has dependant pages.
226 $scope.deleteImage = function (event) {
227 event.preventDefault();
228 var force = $scope.dependantPages !== false;
229 var url = '/images/' + $scope.selectedImage.id;
230 if (force) url += '?force=true';
231 $http.delete(url).then((response) => {
232 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
233 $scope.selectedImage = false;
234 events.emit('success', 'Image successfully deleted');
237 if (response.status === 400) {
238 $scope.dependantPages = response.data;
239 } else if (response.status === 403) {
240 events.emit('error', response.data.error);
246 * Simple date creator used to properly format dates.
250 $scope.getDate = function (stringDate) {
251 return new Date(stringDate);
257 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
258 $scope.searching = false;
259 $scope.searchTerm = '';
260 $scope.searchResults = '';
262 $scope.searchBook = function (e) {
264 var term = $scope.searchTerm;
265 if (term.length == 0) return;
266 $scope.searching = true;
267 $scope.searchResults = '';
268 var searchUrl = '/search/book/' + $attrs.bookId;
269 searchUrl += '?term=' + encodeURIComponent(term);
270 $http.get(searchUrl).then((response) => {
271 $scope.searchResults = $sce.trustAsHtml(response.data);
275 $scope.checkSearchForm = function () {
276 if ($scope.searchTerm.length < 1) {
277 $scope.searching = false;
281 $scope.clearSearch = function () {
282 $scope.searching = false;
283 $scope.searchTerm = '';
289 ngApp.controller('PageEditController', ['$scope', '$http', '$attrs', '$interval', '$timeout', '$sce',
290 function ($scope, $http, $attrs, $interval, $timeout, $sce) {
292 $scope.editorOptions = require('./pages/page-form');
293 $scope.editContent = '';
294 $scope.draftText = '';
295 var pageId = Number($attrs.pageId);
296 var isEdit = pageId !== 0;
297 var autosaveFrequency = 30; // AutoSave interval in seconds.
298 var isMarkdown = $attrs.editorType === 'markdown';
299 $scope.isUpdateDraft = Number($attrs.pageUpdateDraft) === 1;
300 $scope.isNewPageDraft = Number($attrs.pageNewDraft) === 1;
302 // Set inital header draft text
303 if ($scope.isUpdateDraft || $scope.isNewPageDraft) {
304 $scope.draftText = 'Editing Draft'
306 $scope.draftText = 'Editing Page'
309 var autoSave = false;
311 var currentContent = {
322 // Actions specifically for the markdown editor
324 $scope.displayContent = '';
325 // Editor change event
326 $scope.editorChange = function (content) {
327 $scope.displayContent = $sce.trustAsHtml(content);
332 $scope.editorChange = function() {};
336 * Start the AutoSave loop, Checks for content change
337 * before performing the costly AJAX request.
339 function startAutoSave() {
340 currentContent.title = $('#name').val();
341 currentContent.html = $scope.editContent;
343 autoSave = $interval(() => {
344 var newTitle = $('#name').val();
345 var newHtml = $scope.editContent;
347 if (newTitle !== currentContent.title || newHtml !== currentContent.html) {
348 currentContent.html = newHtml;
349 currentContent.title = newTitle;
353 }, 1000 * autosaveFrequency);
357 * Save a draft update into the system via an AJAX request.
361 function saveDraft() {
363 name: $('#name').val(),
364 html: isMarkdown ? $sce.getTrustedHtml($scope.displayContent) : $scope.editContent
367 if (isMarkdown) data.markdown = $scope.editContent;
369 $http.put('/ajax/page/' + pageId + '/save-draft', data).then((responseData) => {
370 $scope.draftText = responseData.data.message;
371 if (!$scope.isNewPageDraft) $scope.isUpdateDraft = true;
375 $scope.forceDraftSave = function() {
380 * Discard the current draft and grab the current page
381 * content from the system via an AJAX request.
383 $scope.discardDraft = function () {
384 $http.get('/ajax/page/' + pageId).then((responseData) => {
385 if (autoSave) $interval.cancel(autoSave);
386 $scope.draftText = 'Editing Page';
387 $scope.isUpdateDraft = false;
388 $scope.$broadcast('html-update', responseData.data.html);
389 $scope.$broadcast('markdown-update', responseData.data.markdown || responseData.data.html);
390 $('#name').val(responseData.data.name);
394 events.emit('success', 'Draft discarded, The editor has been updated with the current page content');