openworktime/webapp/controllers.js
webapp/controllers.jsRaw
1var urlapi = "http://localhost:3000/api/";
2
3angular.module('workApp', ['chart.js'])
4 .controller('workController', function(
5 $scope,
6 $interval,
7 $http
8 ) {
9 $scope.users={};
10 $scope.projects={};
11 $scope.currentInclude='login.html';
12 /* DASHBOARD initialization */
13 $scope.dashboardInit = function(){
14 if(localStorage.getItem('owt_token')){// adding token to the headers
15 $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('owt_token');
16 $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('owt_token');
17 }
18
19 //getting users
20 $http.get(urlapi + 'users')
21 .success(function(data, status, headers,config){
22 if(data.success==false){
23 localStorage.removeItem("owt_token");
24 localStorage.removeItem("owt_user");
25 $scope.currentInclude="login.html";
26 console.log("token ended");
27 }else{
28 console.log(data);
29 $scope.users=data;
30 }
31 })
32 .error(function(data, status, headers,config){
33 console.log("server not responding, data error");
34 toastr.error("server not responding");
35 $scope.$broadcast('scroll.refreshComplete');//refresher stop
36 })
37 .then(function(result){
38 users = result.data;
39 });
40
41 //getting projects
42 $http.get(urlapi + 'projects')
43 .success(function(data, status, headers,config){
44 console.log(data);
45 $scope.projects=data;
46 })
47 .error(function(data, status, headers,config){
48 console.log("server not responding, data error");
49 toastr.error("server not responding");
50 $scope.$broadcast('scroll.refreshComplete');//refresher stop
51 })
52 .then(function(result){
53 users = result.data;
54 });
55
56 };
57 /* </DASHBOARD initialization */
58
59
60 $scope.user={};
61 if(localStorage.getItem("owt_user")){
62 $scope.user=JSON.parse(localStorage.getItem("owt_user"));
63 $scope.currentInclude="dashboard.html";
64 $scope.dashboardInit();
65 intervalGetData = $interval(function(){
66 $scope.dashboardInit();
67 }, 2000);
68 }else{
69 $scope.currentInclude="login.html";
70 }
71
72 /* LOGIN SIGNUP */
73 $scope.showSignup = function(){
74 $scope.currentInclude="signup.html";
75 };
76 $scope.hideSignup = function(){
77 $scope.currentInclude="login.html";
78 };
79 $scope.onBtnSignup = function(){
80 $scope.user.projects=[];
81 $http({
82 url: urlapi + 'users',
83 method: "POST",
84 data: $scope.user
85 }).then(function(response) {
86 $scope.currentInclude="login.html";
87 },
88 function(response) {// failed
89 });
90
91 };
92 var intervalGetData;//out of internal scope
93 $scope.onBtnLogin = function(){
94 $http({
95 url: urlapi + 'auth',
96 method: "POST",
97 data: $scope.user
98 }).then(function(response) {
99 if(response.data.success)
100 {
101 localStorage.setItem("owt_token", response.data.token);
102 localStorage.setItem("owt_user", angular.toJson(response.data.user));
103 $scope.user=JSON.parse(localStorage.getItem("owt_user"));
104
105 $scope.currentInclude="dashboard.html";
106 $scope.dashboardInit();
107
108 intervalGetData = $interval(function(){
109 $scope.dashboardInit();
110 }, 2000);
111 }else{
112 toastr.error(response.data.message);
113 }
114 },
115 function(response) {// failed
116 });
117 };
118 $scope.onBtnLogout = function(){
119 $http({
120 url: urlapi + 'logout',
121 method: "POST",
122 data: $scope.user
123 }).then(function(response) {
124 localStorage.removeItem("owt_token");
125 localStorage.removeItem("owt_user");
126 $interval.cancel(intervalGetData);
127 $scope.currentInclude="login.html";
128 },
129 function(response) {// failed
130 });
131 };
132 /* </ LOGIN SIGNUP */
133
134
135
136
137 //localStorage.clear();
138 $scope.working=false;
139 $scope.currentproject={};
140
141 $scope.newproject={};
142 //$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
143 $scope.addNewProject = function(){
144 /*$scope.newproject.totaltime="0";
145 $scope.projects.push($scope.newproject);
146 localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
147 $scope.newproject={
148 id: $scope.projects[$scope.projects.length-1].id+1,
149 chart: {
150 labels: [],
151 series: ['Working time'],
152 data: []
153 }
154 };*/
155 $http({
156 url: urlapi + 'projects',
157 method: "POST",
158 data: $scope.newproject
159 }).then(function(response) {
160 console.log("project posted");
161 console.log(response);
162 toastr.success("project created at server");
163 $scope.newproject={};
164 },
165 function(response) {// failed
166 });
167 };
168 $scope.editingIndex="";
169 $scope.editProject = function(index){
170 $scope.editingIndex=index;
171 $scope.editingProject=angular.copy($scope.projects[index]);
172 };
173 $scope.cancelEditProject = function(){
174 $scope.editingProject=false;
175 };
176 $scope.updateProject = function(){
177 $scope.projects[$scope.editingIndex]=angular.copy($scope.editingProject);
178 $scope.currentproject=angular.copy($scope.editingProject);
179 $scope.editingProject=false;
180 };
181 $scope.removeProject = function(index){
182 $scope.projects.splice(index, 1);
183 localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
184 $scope.projectSelect(0);
185 };
186 $scope.projectSelect = function(index){
187 $scope.btnStop();
188 $scope.currentproject=$scope.projects[index];
189 };
190 var interval;
191 $scope.currentStrike=0;
192 $scope.btnWork = function(){
193 $scope.editingProject=false;
194 $scope.working=true;
195 $scope.currentStrike=0;
196 interval = $interval(function(){
197 $scope.currentStrike++;
198 $scope.currentproject.totaltime++;
199 }, 1000);
200 };
201 $scope.btnStop = function(){
202 $interval.cancel(interval);
203 if($scope.working==true)
204 {
205 $scope.working=false;
206 $scope.currentproject.chart.labels.push("work strike " + $scope.currentproject.chart.labels.length);
207 $scope.currentproject.chart.data.push($scope.currentStrike);
208
209 localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
210 }
211
212
213 }
214
215 $scope.openCode = function(){
216 toastr.info("Visiting code");
217 var urlCode="https://github.com/idoctnef/openworktime";
218 if(typeof process !== 'undefined'){
219 console.log(process.versions['electron']);
220 const {shell} = require('electron');
221
222 shell.openExternal(urlCode);
223 }else{
224 window.open(urlCode);
225 }
226 };
227
228 //chart
229 /*$scope.chart={
230 labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
231 series: ['Working time', 'Series B'],
232 data: [
233 [65, 59, 80, 81, 56, 55, 40],
234 [28, 48, 40, 19, 86, 27, 90]
235 ]
236 };*/
237
238 })
239
240
241 .filter('secondsToDateTime', [function() {
242 return function(seconds) {
243 return new Date(2016, 0, 1).setSeconds(seconds);
244 };
245}]);