-module.exports = function (ngApp, events) {
-
- ngApp.controller('ImageManagerController', ['$scope', '$attrs', '$http', '$timeout', 'imageManagerService',
- function ($scope, $attrs, $http, $timeout, imageManagerService) {
- $scope.images = [];
- $scope.imageType = $attrs.imageType;
- $scope.selectedImage = false;
- $scope.dependantPages = false;
- $scope.showing = false;
- $scope.hasMore = false;
- $scope.imageUpdateSuccess = false;
- $scope.imageDeleteSuccess = false;
- var page = 0;
- var previousClickTime = 0;
- var dataLoaded = false;
- var callback = false;
-
- /**
- * Simple returns the appropriate upload url depending on the image type set.
- * @returns {string}
- */
- $scope.getUploadUrl = function () {
- return '/images/' + $scope.imageType + '/upload';
- };
-
- /**
- * Runs on image upload, Adds an image to local list of images
- * and shows a success message to the user.
- * @param file
- * @param data
- */
- $scope.uploadSuccess = function (file, data) {
- $scope.$apply(() => {
- $scope.images.unshift(data);
- });
- events.emit('success', 'Image uploaded');
- };
-
- /**
- * Runs the callback and hides the image manager.
- * @param returnData
- */
- function callbackAndHide(returnData) {
- if (callback) callback(returnData);
- $scope.showing = false;
- }
-
- /**
- * Image select action. Checks if a double-click was fired.
- * @param image
- */
- $scope.imageSelect = function (image) {
- var dblClickTime = 300;
- var currentTime = Date.now();
- var timeDiff = currentTime - previousClickTime;
-
- if (timeDiff < dblClickTime) {
- // If double click
- callbackAndHide(image);
- } else {
- // If single
- $scope.selectedImage = image;
- $scope.dependantPages = false;
- }
- previousClickTime = currentTime;
- };