You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
12 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. var urlapi = "http://192.168.1.41:3000/api/";
  2. var urlapi = "http://localhost:3000/api/";
  3. angular.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. var refreshTime=20000;
  13. $scope.getLoggedUser = function(){
  14. //get logged user
  15. $http.get(urlapi + 'users/byusername/' + $scope.user.username)
  16. .success(function(data, status, headers,config){
  17. console.log(data);
  18. $scope.user=data;
  19. })
  20. .error(function(data, status, headers,config){
  21. console.log("server not responding, data error");
  22. toastr.error("server not responding");
  23. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  24. })
  25. .then(function(result){
  26. users = result.data;
  27. });
  28. };
  29. /* DASHBOARD initialization */
  30. $scope.dashboardInit = function(){
  31. if(localStorage.getItem('owt_token')){// adding token to the headers
  32. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('owt_token');
  33. $http.defaults.headers.post['Content-Type'] = 'application/json';
  34. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('owt_token');
  35. }
  36. if(localStorage.getItem("owt_user")){
  37. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  38. }else{
  39. $scope.currentInclude="login.html";
  40. }
  41. $scope.getLoggedUser();
  42. //getting users
  43. $http.get(urlapi + 'users')
  44. .success(function(data, status, headers,config){
  45. if(data.success==false){
  46. localStorage.removeItem("owt_token");
  47. localStorage.removeItem("owt_user");
  48. $scope.currentInclude="login.html";
  49. console.log("token ended");
  50. }else{
  51. console.log(data);
  52. $scope.users=data;
  53. }
  54. })
  55. .error(function(data, status, headers,config){
  56. console.log("server not responding, data error");
  57. toastr.error("server not responding");
  58. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  59. })
  60. .then(function(result){
  61. users = result.data;
  62. });
  63. //getting projects
  64. $http.get(urlapi + 'projects')
  65. .success(function(data, status, headers,config){
  66. if(data.success==false){
  67. localStorage.removeItem("owt_token");
  68. localStorage.removeItem("owt_user");
  69. $scope.currentInclude="login.html";
  70. console.log("token ended");
  71. }else{
  72. for(var i=0; i<data.length; i++)
  73. {
  74. data[i].chart=translateWorkStrikes2Chart(data[i].workStrikes);
  75. }
  76. console.log(data);
  77. $scope.projects=data;
  78. }
  79. })
  80. .error(function(data, status, headers,config){
  81. console.log("server not responding, data error");
  82. toastr.error("server not responding");
  83. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  84. })
  85. .then(function(result){
  86. users = result.data;
  87. });
  88. };
  89. /* </DASHBOARD initialization */
  90. $scope.user={};
  91. $http.defaults.headers.post['Content-Type'] = 'application/json';
  92. if(localStorage.getItem("owt_user")){
  93. if(localStorage.getItem('owt_token')){// adding token to the headers
  94. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('owt_token');
  95. $http.defaults.headers.post['Content-Type'] = 'application/json';
  96. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('owt_token');
  97. }
  98. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  99. $scope.currentInclude="dashboard.html";
  100. $scope.getLoggedUser();
  101. $scope.dashboardInit();
  102. intervalGetData = $interval(function(){
  103. $scope.dashboardInit();
  104. }, refreshTime);
  105. }else{
  106. $scope.currentInclude="login.html";
  107. }
  108. /* LOGIN SIGNUP */
  109. $scope.showSignup = function(){
  110. $scope.currentInclude="signup.html";
  111. };
  112. $scope.hideSignup = function(){
  113. $scope.currentInclude="login.html";
  114. };
  115. $scope.onBtnSignup = function(){
  116. $scope.user.projects=[];
  117. $http({
  118. url: urlapi + 'users',
  119. method: "POST",
  120. data: $scope.user
  121. }).then(function(response) {
  122. $scope.currentInclude="login.html";
  123. },
  124. function(response) {// failed
  125. });
  126. };
  127. var intervalGetData;//out of internal scope
  128. $scope.onBtnLogin = function(){
  129. $http({
  130. url: urlapi + 'auth',
  131. method: "POST",
  132. data: $scope.user
  133. }).then(function(response) {
  134. if(response.data.success)
  135. {
  136. localStorage.setItem("owt_token", response.data.token);
  137. localStorage.setItem("owt_user", angular.toJson(response.data.user));
  138. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  139. $scope.currentInclude="dashboard.html";
  140. $scope.dashboardInit();
  141. intervalGetData = $interval(function(){
  142. $scope.dashboardInit();
  143. }, refreshTime);
  144. }else{
  145. toastr.error(response.data.message);
  146. }
  147. },
  148. function(response) {// failed
  149. });
  150. };
  151. $scope.onBtnLogout = function(){
  152. $http({
  153. url: urlapi + 'logout',
  154. method: "POST",
  155. data: $scope.user
  156. }).then(function(response) {
  157. localStorage.removeItem("owt_token");
  158. localStorage.removeItem("owt_user");
  159. $interval.cancel(intervalGetData);
  160. $scope.currentInclude="login.html";
  161. },
  162. function(response) {// failed
  163. });
  164. };
  165. /* </ LOGIN SIGNUP */
  166. //localStorage.clear();
  167. //$scope.working=false;
  168. $scope.currentproject={};
  169. $scope.newproject={};
  170. //$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
  171. $scope.addNewProject = function(){
  172. /*$scope.newproject.totaltime="0";
  173. $scope.projects.push($scope.newproject);
  174. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
  175. $scope.newproject={
  176. id: $scope.projects[$scope.projects.length-1].id+1,
  177. chart: {
  178. labels: [],
  179. series: ['Working time'],
  180. data: []
  181. }
  182. };*/
  183. $http({
  184. url: urlapi + 'projects',
  185. method: "POST",
  186. data: $scope.newproject
  187. }).then(function(response) {
  188. console.log("project posted");
  189. console.log(response);
  190. $scope.projects=response.data;
  191. toastr.success("project created at server");
  192. $scope.newproject={};
  193. },
  194. function(response) {// failed
  195. });
  196. };
  197. $scope.editingIndex="";
  198. $scope.editProject = function(index){
  199. $scope.editingIndex=index;
  200. $scope.editingProject=angular.copy($scope.projects[index]);
  201. };
  202. $scope.cancelEditProject = function(){
  203. $scope.editingProject=false;
  204. };
  205. $scope.updateProject = function(){
  206. $scope.projects[$scope.editingIndex]=angular.copy($scope.editingProject);
  207. $scope.currentproject=angular.copy($scope.editingProject);
  208. $scope.editingProject=false;
  209. };
  210. $scope.removeProject = function(index){
  211. /*$scope.projects.splice(index, 1);
  212. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));*/
  213. $http({
  214. url: urlapi + 'projects/' + $scope.projects[index]._id,
  215. method: "DELETE",
  216. data: $scope.user._id
  217. }).then(function(response) {
  218. console.log(response);
  219. $scope.projects=response.data;
  220. $scope.projectSelect(null);
  221. },
  222. function(response) {// failed
  223. });
  224. };
  225. $scope.currentprojectIndex;
  226. $scope.projectSelect = function(index){
  227. //$scope.btnStop();
  228. console.log(index);
  229. $scope.currentprojectIndex=index;
  230. $scope.currentproject=$scope.projects[index];
  231. };
  232. $scope.joinProject = function(){
  233. console.log($scope.user);
  234. $http({
  235. url: urlapi + 'projects/'+$scope.currentproject._id+'/adduser',
  236. method: "PUT",
  237. data: $scope.user
  238. }).then(function(response) {
  239. console.log("project joined");
  240. $scope.projects=response.data;
  241. //re select currentproject
  242. $scope.currentproject=$scope.projects[$scope.currentprojectIndex];
  243. toastr.success("project joined");
  244. },
  245. function(response) {// failed
  246. });
  247. };
  248. var interval;
  249. $scope.currentStrike=0;
  250. $scope.btnWork = function(){
  251. //$scope.editingProject=false;
  252. $scope.user.working=true;
  253. $http({
  254. url: urlapi + 'projects/' + $scope.currentproject._id + '/startworking',
  255. method: "PUT",
  256. data: $scope.user
  257. }).then(function(response) {
  258. console.log(response);
  259. $scope.projects=response.data;
  260. $scope.getLoggedUser();
  261. },
  262. function(response) {// failed
  263. });
  264. };
  265. $scope.btnStop = function(){
  266. if($scope.user.working==true)
  267. {
  268. //$scope.user.working=false;
  269. $http({
  270. url: urlapi + 'projects/' + $scope.currentproject._id + '/stopworking',
  271. method: "PUT",
  272. data: $scope.user
  273. }).then(function(response) {
  274. console.log(response);
  275. for(var i=0; i<response.data.length; i++)
  276. {
  277. response.data[i].chart=translateWorkStrikes2Chart(response.data[i].workStrikes);
  278. }
  279. $scope.projects=response.data;
  280. $scope.getLoggedUser();
  281. },
  282. function(response) {// failed
  283. });
  284. }
  285. }
  286. $scope.openCode = function(){
  287. toastr.info("Visiting code");
  288. var urlCode="https://github.com/idoctnef/openworktime";
  289. if(typeof process !== 'undefined'){
  290. console.log(process.versions['electron']);
  291. const {shell} = require('electron');
  292. shell.openExternal(urlCode);
  293. }else{
  294. window.open(urlCode);
  295. }
  296. };
  297. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  298. if(myArray)
  299. {
  300. for(var i = 0, len = myArray.length; i < len; i++) {
  301. if (myArray[i][property] === searchTerm){
  302. //console.log("i: " + i);
  303. return i;
  304. }
  305. }
  306. }
  307. //console.log("i: -1");
  308. return -1;
  309. }
  310. //chart
  311. /*$scope.chart={
  312. labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  313. series: ['Working time', 'Series B'],
  314. data: [
  315. [65, 59, 80, 81, 56, 55, 40],
  316. [28, 48, 40, 19, 86, 27, 90]
  317. ]
  318. };*/
  319. })
  320. .filter('secondsToDateTime', [function() {
  321. return function(seconds) {
  322. return new Date(2016, 0, 1).setSeconds(seconds);
  323. };
  324. }]);
  325. function translateWorkStrikes2Chart(workStrikes){
  326. var auxChart={
  327. labels: [],
  328. series: ['working'],
  329. data: []
  330. };
  331. if(workStrikes)
  332. {
  333. for(var i=0; i<workStrikes.length; i++)
  334. {
  335. auxChart.labels.push(workStrikes[i].username);
  336. auxChart.data.push(workStrikes[i].time);
  337. }
  338. }
  339. return(auxChart);
  340. }