Files
web2.0-backend/infoscreen/static/js/infoadmin_controllers.js
T
2017-03-29 13:53:48 +03:00

184 lines
6.5 KiB
JavaScript

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){
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){
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) {
});
}
}]);
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){
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_externalimage_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_external_image", $scope.item).then(ItemList.loadItems)
}
});
app.controller('infoadmin_abbitem_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_abbitem", $scope.item).then(ItemList.loadItems)
}
});
app.controller('infoadmin_sossoitem_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_sossoitem", $scope.item).then(ItemList.loadItems)
}
});
app.controller('infoadmin_eventitem_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_eventitem", $scope.item).then(ItemList.loadItems)
}
});
app.controller('infoadmin_hslitem_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_hslitem", $scope.item).then(ItemList.loadItems)
}
});
app.controller('infoadmin_websiteitem_create', function($scope, $http,ItemList){
$scope.item = {}
$scope.send = function(){
$http.post("/infoscreen/create_websiteitem", $scope.item).then(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));
});
}
}]);