File: D:/HostingSpaces/SBogers101/cranendonckactief.nl/wwwroot/packages/komma/kms/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.attributeKey = '';
/**
* 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: {
//Sent the data and the options
file: file,
uploadSizes: $scope.uploadSizes,
dynamic: $scope.dynamic,
subFolder: $scope.subFolder,
attribute_key: $scope.attributeKey
}
});
//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()
}
};
}]);