File: D:/HostingSpaces/SBogers87/basephotography.nl/workbench/komma/kms/public/js/fileupload.ng.js
'use strict';
//inject directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('fileUploadController', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
//The files list
//Todo, extend with the files form the database
$scope.files = [];
//These are the fileIds, used for saving
$scope.fileIds = [];
$scope.dynamic = false;
$scope.uploadSizes = '';
$scope.maxImages;
$scope.errorMsg = {};
$scope.subFolder = false;
$scope.tagIds = {};
/**
* This function uploads the new files
* @param files
* @param errFiles
*/
$scope.uploadFiles = function (files, errFiles, maximages) {
if (typeof $scope.files == 'undefined') $scope.files = []
if ($scope.files.length >= maximages) return false;
//Set the files
$scope.errFiles = errFiles;
//Cut the files before uploading when the added files exeed the maxImages
if ($scope.getFileCount() + files.length > maximages) {
//From is maximages minus current count
var from = maximages - $scope.getFileCount()
//fomany is maxmages minus from
var howmany = maximages - from
//Splice the files array
files.splice(from, 10000000)
$scope.errorMsg.toMany = 'Maximum of ' + maximages + ' files reached. Only ' + from + ' file(s) uploaded';
}
//Foreach each file
angular.forEach(files, function (file) {
//Push to true adds image at the back, false at the front
var push = true
//Add the placeholder
var loadingImage = {loading: true, thumb_image_url: '/packages/komma/kms/img/loading.gif'}
if (push == true) {
$scope.files.push(loadingImage);
var imageKey = $scope.files.length - 1
} else {
//Add at the front
$scope.files.unshift(loadingImage)
var imageKey = 0
}
//Upload to the server
file.upload = Upload.upload({
url: '/kms/upload',
data: {
file: file,
uploadSizes: $scope.uploadSizes,
dynamic: $scope.dynamic,
subFolder: $scope.subFolder
}
});
//After uploading
file.upload.then(function (response) {
$timeout(function () {
// delete $scope.files.loading
//Set the respons data
file.result = response.data;
//Add the file to the current files
$scope.files[imageKey] = file.result;
//Reverse push
//Add the file to the current files
//$scope.files.unshift(file.result);
$scope.setFileIds()
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
file.progress = Math.min(100, parseInt(100.0 *
evt.loaded / evt.total));
});
});
}
$scope.deleteImage = function (index) {
//Delete the image
$scope.files.splice(index, 1)
//Delete the image id
$scope.setFileIds()
delete $scope.errorMsg.toMany;
}
$scope.moveImage = function (index, direction) {
var currentFile = $scope.files[index]
var currentFileId = $scope.fileIds[index]
if (direction == 'left') {
$scope.files[index] = $scope.files[index - 1];
$scope.files[index - 1] = currentFile;
} else {
$scope.files[index] = $scope.files[index + 1];
$scope.files[index + 1] = currentFile;
}
$scope.setFileIds()
}
$scope.initFiles = function (files, maxImages) {
$scope.files = files
$scope.setFileIds()
if (typeof maxImages != 'undefined') {
$scope.maxImages = maxImages
}
}
$scope.setFileIds = function () {
var local_fileIds = [];
angular.forEach($scope.files, function (file) {
local_fileIds.push(file.id);
});
$scope.fileIds = local_fileIds
}
$scope.getFileCount = function () {
return $scope.fileIds.length;
}
$scope.sortableOptions = {
update: function (e, ui) {
ui.item.sortable.cancel();
var originalIndex = ui.item.sortable.index;
var newIndex = ui.item.sortable.dropindex
var oldImage = $scope.files[originalIndex]
$scope.files[originalIndex] = $scope.files[newIndex]
$scope.files[newIndex] = oldImage
$scope.setFileIds()
}
};
/**
* Add a tag to the scope tagIds
* when a select is chosen.
*
* @param item
* @param id
*/
$scope.addToTags = function (item, id) {
//Get the id of the tag
var tagId = item.fullValue
//Start with a empty object
var currentTags = {}
//is there already an taglist for the current id
if (id in $scope.tagIds) {
//Load this in the currentTags
currentTags = $scope.tagIds[id]
}
//Add the tag with the tagId as key and value (easy for double values, and delete afterwards)
currentTags[tagId] = tagId
//Add the current tags to the taglist with the image id
$scope.tagIds[id] = currentTags
}
/**
* Remove a tag from the scope tagIds
* When a tag is removed
* @param item
* @param id
*/
$scope.removeFromTags = function (item, id) {
//Delete the item based on the image id and the tag id
delete $scope.tagIds[id][item.fullValue];
}
/**
* Set the tagIds on the initial load
*
* @param files
*/
$scope.setTagIds = function (files) {
//Loop trough all the files
angular.forEach(files, function (file) {
//Create empty tag list
var tags = {};
//Loop trough the object of tagIds
angular.forEach(JSON.parse(file.tagIds), function (tagId) {
//Add tags with key,value to object
tags[tagId] = tagId
})
// Add object to $scope.tagIds
$scope.tagIds[file.id] = tags;
})
}
}]);