3 module.exports = function(ngApp) {
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;
20 $scope.getUploadUrl = function() {
21 return '/images/' + $scope.imageType + '/upload';
24 $scope.uploadSuccess = function(file, data) {
26 $scope.images.unshift(data);
30 function callbackAndHide(returnData) {
31 if (callback) callback(returnData);
32 $scope.showing = false;
35 $scope.imageSelect = function (image) {
36 var dblClickTime = 300;
37 var currentTime = Date.now();
38 var timeDiff = currentTime - previousClickTime;
40 if (timeDiff < dblClickTime) {
42 callbackAndHide(image);
45 $scope.selectedImage = image;
46 $scope.dependantPages = false;
48 previousClickTime = currentTime;
51 $scope.selectButtonClick = function() {
52 callbackAndHide($scope.selectedImage);
55 function show(doneCallback) {
56 callback = doneCallback;
57 $scope.showing = true;
58 // Get initial images if they have not yet been loaded in.
65 imageManagerService.show = show;
66 imageManagerService.showExternal = function(doneCallback) {
71 window.ImageManager = imageManagerService;
73 $scope.hide = function() {
74 $scope.showing = false;
77 function fetchData() {
78 var url = '/images/' + $scope.imageType + '/all/' + page;
79 $http.get(url).then((response) => {
80 $scope.images = $scope.images.concat(response.data.images);
81 $scope.hasMore = response.data.hasMore;
86 $scope.saveImageDetails = function(event) {
87 event.preventDefault();
88 var url = '/images/update/' + $scope.selectedImage.id;
89 $http.put(url, this.selectedImage).then((response) => {
90 $scope.imageUpdateSuccess = true;
92 $scope.imageUpdateSuccess = false;
95 var errors = response.data;
97 Object.keys(errors).forEach((key) => {
98 message += errors[key].join('\n');
100 $scope.imageUpdateFailure = message;
102 $scope.imageUpdateFailure = false;
107 $scope.deleteImage = function(event) {
108 event.preventDefault();
109 var force = $scope.dependantPages !== false;
110 var url = '/images/' + $scope.selectedImage.id;
111 if (force) url += '?force=true';
112 $http.delete(url).then((response) => {
113 $scope.images.splice($scope.images.indexOf($scope.selectedImage), 1);
114 $scope.selectedImage = false;
115 $scope.imageDeleteSuccess = true;
117 $scope.imageDeleteSuccess = false;
121 if (response.status === 400) {
122 $scope.dependantPages = response.data;
130 ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', function($scope, $http, $attrs) {
131 $scope.searching = false;
132 $scope.searchTerm = '';
133 $scope.searchResults = '';
135 $scope.searchBook = function (e) {
137 var term = $scope.searchTerm;
138 if (term.length == 0) return;
139 $scope.searching = true;
140 $scope.searchResults = '';
141 var searchUrl = '/search/book/' + $attrs.bookId;
142 searchUrl += '?term=' + encodeURIComponent(term);
143 $http.get(searchUrl).then((response) => {
144 $scope.searchResults = response.data;
148 $scope.checkSearchForm = function () {
149 if ($scope.searchTerm.length < 1) {
150 $scope.searching = false;
154 $scope.clearSearch = function() {
155 $scope.searching = false;
156 $scope.searchTerm = '';