- var baseUrl = '/images/' + $scope.imageType + '/all/'
-
- /**
- * Fetch the list image data from the server.
- */
- function fetchData() {
- var url = baseUrl + page + '?';
- var components = {};
- if ($scope.uploadedTo) components['page_id'] = $scope.uploadedTo;
- if ($scope.searching) components['term'] = $scope.searchTerm;
-
-
- var urlQueryString = Object.keys(components).map((key) => {
- return key + '=' + encodeURIComponent(components[key]);
- }).join('&');
- url += urlQueryString;
-
- $http.get(url).then((response) => {
- $scope.images = $scope.images.concat(response.data.images);
- $scope.hasMore = response.data.hasMore;
- page++;
- });
- }
- $scope.fetchData = fetchData;
-
- /**
- * Start a search operation
- * @param searchTerm
- */
- $scope.searchImages = function() {
-
- if ($scope.searchTerm === '') {
- cancelSearch();
- return;
- }
-
- if (!$scope.searching) {
- preSearchImages = $scope.images;
- preSearchHasMore = $scope.hasMore;
- }
-
- $scope.searching = true;
- $scope.images = [];
- $scope.hasMore = false;
- page = 0;
- baseUrl = '/images/' + $scope.imageType + '/search/';
- fetchData();
- };
-
- /**
- * Set the current image listing view.
- * @param viewName
- */
- $scope.setView = function(viewName) {
- cancelSearch();
- $scope.images = [];
- $scope.hasMore = false;
- page = 0;
- $scope.view = viewName;
- baseUrl = '/images/' + $scope.imageType + '/' + viewName + '/';
- fetchData();
- }
-
- /**
- * Save the details of an image.
- * @param event
- */
- $scope.saveImageDetails = function (event) {
- event.preventDefault();
- var url = '/images/update/' + $scope.selectedImage.id;
- $http.put(url, this.selectedImage).then((response) => {
- events.emit('success', 'Image details updated');
- }, (response) => {
- if (response.status === 422) {
- var errors = response.data;
- var message = '';
- Object.keys(errors).forEach((key) => {
- message += errors[key].join('\n');
- });
- events.emit('error', message);
- } else if (response.status === 403) {
- events.emit('error', response.data.error);
- }
- });
- };
-
- /**
- * Delete an image from system and notify of success.
- * Checks if it should force delete when an image
- * has dependant pages.
- * @param event
- */
- $scope.deleteImage = function (event) {
- event.preventDefault();
- var force = $scope.dependantPages !== false;
- var url = '/images/' + $scope.selectedImage.id;
- if (force) url += '?force=true';
- $http.delete(url).then((response) => {
- $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
- $scope.selectedImage = false;
- events.emit('success', 'Image successfully deleted');
- }, (response) => {
- // Pages failure
- if (response.status === 400) {
- $scope.dependantPages = response.data;
- } else if (response.status === 403) {
- events.emit('error', response.data.error);
- }
- });
- };
-
- /**
- * Simple date creator used to properly format dates.
- * @param stringDate
- * @returns {Date}
- */
- $scope.getDate = function (stringDate) {
- return new Date(stringDate);
- };
-
- }]);
-
-
- ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
- $scope.searching = false;
- $scope.searchTerm = '';
- $scope.searchResults = '';
-
- $scope.searchBook = function (e) {
- e.preventDefault();
- var term = $scope.searchTerm;
- if (term.length == 0) return;
- $scope.searching = true;
- $scope.searchResults = '';
- var searchUrl = '/search/book/' + $attrs.bookId;
- searchUrl += '?term=' + encodeURIComponent(term);
- $http.get(searchUrl).then((response) => {
- $scope.searchResults = $sce.trustAsHtml(response.data);
- });
- };
-
- $scope.checkSearchForm = function () {
- if ($scope.searchTerm.length < 1) {
- $scope.searching = false;
- }
- };
-
- $scope.clearSearch = function () {
- $scope.searching = false;
- $scope.searchTerm = '';
- };
-
- }]);