-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
99 lines (87 loc) · 2.53 KB
/
project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function Project(id,name,description,site) {
this.$id = id;
this.name = name;
this.description = description;
this.site = site;
}
function Projects() {
projects = [];
this.projects = projects;
this.loaded = 0;
this.add = function(prj) {
projects.splice(projects.length,0,prj);
}
this.get = function(id) {
for(var i=0;i<projects.length;i++) {
var prj = projects[i];
if(prj.$id == id)
return prj;
}
}
this.remove = function(id) {
for(var i=0;i<projects.length;i++) {
if(projects[i].$id == id) {
projects.splice(i,1);
return;
}
}
}
this.update = function(itemOrId) {
alert(itemOrId);
}
}
angular.projects = new Projects();
angular.module('project',[]).
factory('Projects', function() {
return angular.projects;
}).
config(function($routeProvider) {
$routeProvider.
when('/', {controller:ListCtrl, templateUrl:'list.html'}).
when('/edit/:projectId', {controller:EditCtrl, templateUrl:'detail.html'}).
when('/new', {controller:CreateCtrl, templateUrl:'detail.html'}).
otherwise({redirectTo:'/'});
});
function ListCtrl($scope, $http, Projects) {
if(Projects.loaded == 0) {
$http.get("projects.json").success(function(data) {
for(var i = 0;i<data.length;i++) {
var itm = data[i];
Projects.add(new Project(itm.$id,itm.name,itm.description,itm.site));
}
});
Projects.loaded = 1;
}
$scope.projects = Projects;
}
function CreateCtrl($scope, $location, $timeout, Projects) {
$scope.project = new Project();
$scope.save = function() {
$scope.project.$id = randomString(5,"abcdefghijklmnopqrstuvwxyz0123456789");
Projects.add(angular.copy($scope.project));
$location.path('/');
}
}
function EditCtrl($scope, $location, $routeParams, Projects) {
$scope.project = angular.copy(Projects.get($routeParams.projectId));
$scope.isClean = function() {
return angular.equals(Projects.get($routeParams.projectId), $scope.project);
}
$scope.destroy = function() {
Projects.remove($routeParams.projectId);
$location.path('/');
};
$scope.save = function() {
var prj = Projects.get($routeParams.projectId);
prj.name = $scope.project.name;
prj.description = $scope.project.description;
prj.site = $scope.project.site;
$location.path('/');
};
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i)
result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}