"use strict"; byo.directive("byoGalleryIndex", function() { return { restrict: "E", require: [], scope: { navigation_entry : "=navigationEntry", navigation_params : "=navigationParams" }, templateUrl: "/apps/gallery/gallery_index.html", controller: ["$scope", "$http", function($scope, $http) { $scope.albums = []; $scope.load = function() { console.log("Point"); $http.get("/api/gallery/get_albums/" + $scope.navigation_entry.id).success(function(data) { $scope.albums = data; console.log(data); }); }; $scope.go_album = function(album) { document.location = album.navigation_entry.url; }; $scope.load(); }] }; }); byo.directive("byoGalleryNew", function() { return { restrict: "E", require: [], scope: { navigation_entry : "=navigationEntry", navigation_params : "=navigationParams" }, templateUrl: "/apps/gallery/gallery_new.html", controller: ["$scope", "$http", "$rootScope", function($scope, $http, $rootScope) { var today = new Date(); $scope.album = { albumdate : today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate() }; $scope.create = function() { $http.post("/api/gallery/create_album/" + $scope.navigation_entry.id, $scope.album).success(function(album) { $rootScope.$broadcast("event:viewChanged", { "navigation_entry" : album.navigation_entry, "view" : "edit" }); }); }; }] }; }); byo.directive("byoGalleryEdit", function() { return { restrict: "E", require: [], scope: { navigation_entry : "=navigationEntry", navigation_params : "=navigationParams" }, templateUrl: "/apps/gallery/gallery_edit.html", controller: ["$scope", "$http", "Upload", "$alert", function($scope, $http, $upload, $alert) { $scope.queue = []; $scope.uploading = false; $scope.uploading_progress = 0; $scope.load = function() { $http.get("/api/gallery/get_album/" + $scope.navigation_entry.id).success(function(data) { $scope.album = data; }); }; $scope.$watch("queue", function() { console.log("queue changed!", angular.copy($scope.queue)); if ($scope.queue !== null && $scope.queue.length !== 0) { $scope.uploading = true; var uploading_num = $scope.queue.length; $scope.uploading_progress = 0; $scope.uploading_progresses = {}; angular.forEach($scope.queue, function(file) { file.uploaded = 0; $upload.upload({ url: "/api/resources/upload_file/images/" + $scope.navigation_entry.id + "/" + $scope.navigation_entry.lang, headers: {'Content-Type': file.type}, method: 'POST', data: file, fileFormDataName: 'files[]', file: file }).progress(function (evt) { var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); //file.uploaded = progressPercentage; //console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name); $scope.uploading_progresses[file.name] = { progress : progressPercentage, name : file.name, mime : file.type }; $scope.uploading_progress = 0; angular.forEach($scope.uploading_progresses, function(file) { $scope.uploading_progress += file.progress; }); $scope.uploading_progress += ($scope.queue.length - Object.keys($scope.uploading_progresses).length) * 100; $scope.uploading_progress = parseInt($scope.uploading_progress / $scope.queue.length); }).success(function (data, status, headers, config) { console.log('file ' + config.file.name + 'uploaded. Response: ' + JSON.stringify(data)); uploading_num = uploading_num - 1; if (uploading_num === 0) { $scope.uploading = false; } angular.forEach(data, function(picture_id) { $scope.album.pictures.unshift({ id : picture_id }); }); //$scope.select($scope.selected_entry.id); $scope.alert("picture uploaded"); delete $scope.uploading_progresses[file.name]; }).error(function() { uploading_num = uploading_num - 1; if (uploading_num === 0) { $scope.uploading = false; } //$scope.select($scope.selected_entry.id); delete $scope.uploading_progresses[file.name]; }); }); } }); $scope.delete = function(picture) { if (confirm("Are you sure you want to delete this picture?")) { if (!$scope.uploading) { $http.get("/api/resources/delete_file/images/" + picture.id + "/" + $scope.navigation_entry.lang + "/" + $scope.navigation_entry.id).success(function() { var found_index = false; angular.forEach($scope.album.pictures, function(album_picture, index) { if (album_picture.id === picture.id) { found_index = index; } }); if (found_index !== false) { $scope.album.pictures.splice(found_index, 1); } $scope.alert("picture deleted"); }); } } }; $scope.set_keypic = function(picture) { $http.get("/api/gallery/set_keypic/" + $scope.navigation_entry.id + "/" + picture.id).success(function() { $scope.album.keypic = picture.id; $scope.alert("key picture set"); }); }; $scope.save = function() { $http.post("/api/gallery/save_album/" + $scope.navigation_entry.id, $scope.album).success(function() { $scope.alert("data saved"); $scope.load(); }); }; $scope.save_picture_meta = function(picture) { var meta = angular.copy(picture); delete meta.name; delete meta.link; delete meta.mime; delete meta.uploaded; delete meta.uploader; $http.post("/api/gallery/save_picture_meta/" + $scope.navigation_entry.id + "/" + picture.id, meta).success(function() { $scope.alert("picture updated"); $scope.load(); }); }; $scope.save_pictures_meta = function(ids) { console.log("save!", ids); var metas = []; angular.forEach($scope.album.pictures, function(picture) { if (ids === undefined || ids.indexOf(picture.id) > -1) { var meta = angular.copy(picture); delete meta.name; delete meta.link; delete meta.mime; delete meta.uploaded; delete meta.uploader; metas.push(meta); } }); console.log("update!", metas); $http.post("/api/gallery/save_pictures_meta/" + $scope.navigation_entry.id + "/", metas).success(function() { $scope.alert("pictures updated"); $scope.load(); }); }; $scope.validate_file = function(file) { console.log("validating file", file); var name = file.name.toLowerCase(); if (name.substr(-3) === "jpg" || name.substr(-3) === "png" || name.substr(-4) === "jpeg") { return true; } return false; }; $scope.switch_pictures = function(indexa, indexb) { var tmp = angular.copy($scope.album.pictures[indexa].order_by); $scope.album.pictures[indexa].order_by = $scope.album.pictures[indexb].order_by; $scope.album.pictures[indexb].order_by = tmp; $scope.save_pictures_meta([$scope.album.pictures[indexa].id, $scope.album.pictures[indexb].id]); }; $scope.sort_album_by_filename = function() { var names = []; var orders = []; angular.forEach($scope.album.pictures, function(picture) { names.push(picture.name); orders.push(picture.order_by); }); console.log(angular.copy(names)); names.sort(); orders.sort(); console.log(angular.copy(names), orders); var ids = []; angular.forEach($scope.album.pictures, function(picture) { var index = names.indexOf(picture.name); if (index > -1 && picture.order_by != orders[index]) { picture.order_by = orders[index]; ids.push(picture.id); } }); $scope.save_pictures_meta(ids); }; $scope.alert = function(message) { $alert({title: 'action completed', content: message, container: '#alerts-container', type: 'info', show: true, duration : 2.5}); }; $scope.load(); }] }; });