checkHome/static/js/ckhome.js

243 lines
5.9 KiB
JavaScript

angular.module("CheckHomeApp", ["ngRoute", "ngResource"])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/items", {
templateUrl: "views/items.html",
controller: "RoomsController"
})
.when("/rooms", {
templateUrl: "views/rooms.html",
controller: "RoomsController"
})
.when("/tags", {
templateUrl: "views/tags.html",
controller: "TagsController"
})
.when("/users", {
templateUrl: "views/users.html",
controller: "UsersController"
})
.when("/", {
templateUrl: "views/home.html",
controller: "RoomsController"
});
$locationProvider.html5Mode(true);
});
angular.module("CheckHomeApp")
.factory("Version", function($resource) {
return $resource("/api/version")
})
.factory("Room", function($resource) {
return $resource("/api/rooms/:roomId", { roomId: '@id' }, {
'update': {method: 'PUT'},
})
})
.factory("ItemChecks", function($resource) {
return $resource("/api/items/:itemId/checks/:checkId", { itemId: '@id', checkId: '@id' }, {
'update': {method: 'PUT'},
})
})
.factory("ItemRoom", function($resource) {
return $resource("/api/rooms/:roomId/items/:itemId", { roomId: '@id', itemId: '@id' }, {
'update': {method: 'PUT'},
})
})
.factory("User", function($resource) {
return $resource("/api/users/:userId", { userId: '@id' }, {
'update': {method: 'PUT'},
})
})
.factory("TagsItem", function($resource) {
return $resource("/api/items/:itemId/tags/:tagId", { itemId: '@id', tagId: '@id' }, {
'save': {method: 'PUT'},
'update': {method: 'PUT'},
})
})
.factory("Tag", function($resource) {
return $resource("/api/tags/:tagId", { tagId: '@id' }, {
'update': {method: 'PUT'},
})
});
angular.module("CheckHomeApp")
.controller("VersionController", function($scope, Version) {
$scope.v = Version.get();
})
.controller("RoomsController", function($scope, Room) {
$scope.rooms = Room.query();
$scope.newRoom = function() {
var t = new Room();
t.edit = true;
$scope.rooms.push(t);
}
$scope.editRoom = function() {
this.room.edit = true;
}
$scope.saveRoom = function() {
if (this.room.id === undefined)
this.room.$save()
else
this.room.$update();
}
$scope.delRoom = function(rk) {
this.room.$delte().then(function() {
$scope.room.splice(rk, 1);
});
}
})
.controller("ItemsRoomController", function($scope, ItemRoom) {
$scope.items = ItemRoom.query({roomId: $scope.room.id});
$scope.newItem = function() {
var t = new ItemRoom();
t.edit = true;
$scope.items.push(t);
}
$scope.editItem = function() {
this.item.edit = true;
}
$scope.saveItem = function() {
if (this.item.id === undefined)
this.item.$save({roomId: $scope.room.id})
else
this.item.$update();
}
$scope.delItem = function(ik) {
this.item.$delete().then(function() {
$scope.items.splice(ik, 1);
});
}
})
.controller("ChecksItemController", function($scope, ItemChecks) {
$scope.ncomment = "";
$scope.checks = ItemChecks.query({itemId: $scope.item.id});
$scope.min_checks = function() {
function state2int(state) {
switch(state) {
case "yes":
return 4;
case "yesbut":
return 3;
case "nobut":
return 2;
case "no":
return 1;
default:
return 5;
}
}
var min = "N/A";
angular.forEach($scope.checks, function(check) {
if (state2int(min) > state2int(check["passed"]))
min = check["passed"];
});
return min;
}
$scope.newCheck = function(passed) {
var c = new ItemChecks();
c.passed = passed;
c.comment = $scope.ncomment;
c.$save({itemId: $scope.item.id}, function(res) {
$scope.ncomment = "";
$scope.checks.push(res)
});
}
$scope.checkOk = function() {
$scope.newCheck("yes");
}
$scope.checkMok = function() {
$scope.newCheck("yesbut");
}
$scope.checkMko = function() {
$scope.newCheck("nobut");
}
$scope.checkKo = function() {
$scope.newCheck("no");
}
$scope.delCheck = function(ck) {
this.check.$delete({itemId: $scope.item.id}).then(function() {
$scope.checks.splice(ck, 1);
});
}
})
.controller("TagsController", function($scope, Tag) {
$scope.tags = Tag.query();
$scope.newTag = function() {
var t = new Tag();
t.edit = true;
$scope.tags.push(t);
}
$scope.editTag = function() {
this.tag.edit = true;
}
$scope.saveTag = function() {
if (this.tag.id === undefined)
this.tag.$save()
else
this.tag.$update();
}
$scope.delTag = function(tk) {
this.tag.$delete().then(function() {
$scope.tags.splice(tk, 1);
});
}
})
.controller("TagsItemController", function($scope, TagsItem) {
$scope.itags = TagsItem.query({itemId: $scope.item.id});
$scope.newItemTag = function() {
var t = new TagsItem();
t.edit = true;
$scope.itags.push(t);
}
$scope.editItemTag = function() {
this.tag.edit = true;
}
$scope.editItemTagPass = function() {
this.tag.editpass = true;
}
$scope.saveItemTag = function() {
if (this.tag.id === undefined)
this.tag.$save({itemId: $scope.item.id, tagId: this.tag.val})
else
this.tag.$update();
}
$scope.delItemTag = function(tk) {
this.tag.$delete({itemId: $scope.item.id, tagId: this.tag.id}).then(function() {
$scope.itags.splice(tk, 1);
});
}
})
.controller("UsersController", function($scope, User) {
$scope.users = User.query();
$scope.newUser = function() {
var t = new User();
t.edit = true;
t.editpass = true;
$scope.users.push(t);
}
$scope.editUser = function() {
this.user.edit = true;
}
$scope.editUserPass = function() {
this.user.editpass = true;
}
$scope.saveUser = function() {
if (this.user.id === undefined)
this.user.$save()
else
this.user.$update();
}
$scope.delUser = function(uk) {
this.user.$delete().then(function(){
$scope.users.splice(uk, 1);
});
}
});