var app = angular.module('infoAdmin', [ 'ngFileUpload' ]); app.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.xsrfCookieName = 'csrftoken'; $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; }]); app.config(function($interpolateProvider) { $interpolateProvider.startSymbol('{$'); $interpolateProvider.endSymbol('$}'); }); app.service("InstanceList", ["$http", function($http){ var self = this; this.selected_rot = {} this.rotations = []; this.listInstances = function() { return self.selected_rot; }; this.listRotations = function(){ return self.rotations; } this.createInstance = function(rotation_id, item_id, item_type, duration){ var data = { 'rotation_id': rotation_id, 'item_id': item_id, 'item_type': item_type, 'duration': duration, } $http.post("/infoscreen/instance", data).then(function(response) { //eslint-disable-line no-unused-vars self.getRotation(rotation_id); }); }; this.getRotation = function(id){ $http.get("/infoscreen/rotation/"+id).then(function(response){ self.selected_rot = response.data; }); }; this.getRotations = function(){ $http.get("/infoscreen/rotations").then(function(response){ self.rotations = response.data; // select first rotation by default if we have any if (self.rotations.length > 0){ self.getRotation(self.rotations[0].id); } }); } this.deleteInstance = function(id){ $http.delete("/infoscreen/instance/"+id).then(function(response) { //eslint-disable-line no-unused-vars self.getRotation(self.selected_rot.id); }); } this.createRotation = function(name) { var data = {'name': name} $http.post("/infoscreen/create_rotation", data); } this.deleteRotation = function(id) { $http.delete("/infoscreen/delete_rotation/" + id).then(function(response) { //eslint-disable-line no-unused-vars }); } }]); app.service("ItemList", ["$http",'InstanceList', function($http, InstanceList){ var self = this; self.items = [] this.loadItems = function(){ $http.get("/infoscreen/items").then(function(response){ self.items = response.data; }); }; this.getItems = function(){ return self.items; }; this.deleteItem = function(type_id, item_id){ $http.delete("/infoscreen/delete_item/"+type_id+"/"+item_id).then(function(){ self.loadItems(); // refresh instances because deleting item may cascade to instances InstanceList.getRotation(InstanceList.selected_rot.id); }); }; this.refreshCB = function(response) { //eslint-disable-line no-unused-vars self.loadItems(); }; }]); app.controller('infoadmin_ctrl', function($scope, $http, $window, ItemList, InstanceList){ // init items $scope.rotations = []; $scope.infoitems = []; $scope.selected_rot= {}; // helpers $scope.deleteItem = ItemList.deleteItem; $scope.selectRotation = InstanceList.getRotation; /* Create a new rotation from with the user-provided name * We also want to reload the page afterwards to refresh the table * If the table were implemented with ng-table we could just refresh that but at the moment * it's not so we just fullblast the whole thing */ $scope.createRotation = function(name) { InstanceList.createRotation(name); $window.location.reload(); }; $scope.deleteRotation = function(id) { InstanceList.deleteRotation(id); $window.location.reload(); } $scope.createInstance = InstanceList.createInstance; $scope.deleteInstance = InstanceList.deleteInstance; // fetch data $scope.$watch(InstanceList.listInstances, function(instances){ $scope.selected_rot = instances; }); $scope.$watch(InstanceList.listRotations, function(rotations){ $scope.rotations = rotations; }); InstanceList.getRotations(); $http.get("/infoscreen/types").then(function(response){ $scope.infotypes = response.data; }); $scope.$watch(ItemList.getItems, function(items){ $scope.infoitems = items }); ItemList.loadItems(); }); app.controller('infoadmin_image_create', ['$scope', 'Upload', '$timeout',"ItemList", function ($scope, Upload, $timeout,ItemList) { $scope.send = function(file) { file.upload = Upload.upload({ url: '/infoscreen/create_image', data: {name: $scope.imagename, image: file}, }).then(function (response) { $timeout(function () { file.result = response.data; ItemList.loadItems(); $scope.name= "" $scope.img = {} }); },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)); }); } }]); app.controller('infoadmin_video_create', ['$scope', 'Upload', '$timeout',"ItemList", function ($scope, Upload, $timeout,ItemList) { $scope.send = function(file) { file.upload = Upload.upload({ url: '/infoscreen/create_video', data: {name: $scope.name, video: file}, }).then(function (response) { $timeout(function () { file.result = response.data; ItemList.loadItems(); $scope.name= "" $scope.video = {} }); },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)); }); } }]); function itemGenerator(create_url){ return function($scope, $http, ItemList){ $scope.item = {} $scope.send = function(){ $http.post(create_url, $scope.item).then(ItemList.loadItems) } } } function controllerGenerator(name){ app.controller("infoadmin_" + name + "_create", itemGenerator("/infoscreen/create_" + name)); } var simple_controllers = [ "external_image", "abbitem", "sossoitem", "eventitem", "hslitem", "websiteitem", "apyitem", ]; _.each(simple_controllers, controllerGenerator);