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.

299 lines
9.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. var urlapi = "http://localhost:3000/api/";
  2. angular.module('workApp', ['chart.js'])
  3. .controller('workController', function(
  4. $scope,
  5. $interval,
  6. $http
  7. ) {
  8. $scope.users={};
  9. $scope.projects={};
  10. $scope.currentInclude='login.html';
  11. var refreshTime=20000;
  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.post['Content-Type'] = 'application/json';
  17. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('owt_token');
  18. }
  19. if(localStorage.getItem("owt_user")){
  20. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  21. }else{
  22. $scope.currentInclude="login.html";
  23. }
  24. //getting users
  25. $http.get(urlapi + 'users')
  26. .success(function(data, status, headers,config){
  27. if(data.success==false){
  28. localStorage.removeItem("owt_token");
  29. localStorage.removeItem("owt_user");
  30. $scope.currentInclude="login.html";
  31. console.log("token ended");
  32. }else{
  33. console.log(data);
  34. $scope.users=data;
  35. }
  36. })
  37. .error(function(data, status, headers,config){
  38. console.log("server not responding, data error");
  39. toastr.error("server not responding");
  40. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  41. })
  42. .then(function(result){
  43. users = result.data;
  44. });
  45. //getting projects
  46. $http.get(urlapi + 'projects')
  47. .success(function(data, status, headers,config){
  48. if(data.success==false){
  49. localStorage.removeItem("owt_token");
  50. localStorage.removeItem("owt_user");
  51. $scope.currentInclude="login.html";
  52. console.log("token ended");
  53. }else{
  54. console.log(data);
  55. $scope.projects=data;
  56. }
  57. })
  58. .error(function(data, status, headers,config){
  59. console.log("server not responding, data error");
  60. toastr.error("server not responding");
  61. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  62. })
  63. .then(function(result){
  64. users = result.data;
  65. });
  66. };
  67. /* </DASHBOARD initialization */
  68. $scope.user={};
  69. if(localStorage.getItem("owt_user")){
  70. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  71. $scope.currentInclude="dashboard.html";
  72. $scope.dashboardInit();
  73. intervalGetData = $interval(function(){
  74. $scope.dashboardInit();
  75. }, refreshTime);
  76. }else{
  77. $scope.currentInclude="login.html";
  78. }
  79. /* LOGIN SIGNUP */
  80. $scope.showSignup = function(){
  81. $scope.currentInclude="signup.html";
  82. };
  83. $scope.hideSignup = function(){
  84. $scope.currentInclude="login.html";
  85. };
  86. $scope.onBtnSignup = function(){
  87. $scope.user.projects=[];
  88. $http({
  89. url: urlapi + 'users',
  90. method: "POST",
  91. data: $scope.user
  92. }).then(function(response) {
  93. $scope.currentInclude="login.html";
  94. },
  95. function(response) {// failed
  96. });
  97. };
  98. var intervalGetData;//out of internal scope
  99. $scope.onBtnLogin = function(){
  100. $http({
  101. url: urlapi + 'auth',
  102. method: "POST",
  103. data: $scope.user
  104. }).then(function(response) {
  105. if(response.data.success)
  106. {
  107. localStorage.setItem("owt_token", response.data.token);
  108. localStorage.setItem("owt_user", angular.toJson(response.data.user));
  109. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  110. $scope.currentInclude="dashboard.html";
  111. $scope.dashboardInit();
  112. intervalGetData = $interval(function(){
  113. $scope.dashboardInit();
  114. }, refreshTime);
  115. }else{
  116. toastr.error(response.data.message);
  117. }
  118. },
  119. function(response) {// failed
  120. });
  121. };
  122. $scope.onBtnLogout = function(){
  123. $http({
  124. url: urlapi + 'logout',
  125. method: "POST",
  126. data: $scope.user
  127. }).then(function(response) {
  128. localStorage.removeItem("owt_token");
  129. localStorage.removeItem("owt_user");
  130. $interval.cancel(intervalGetData);
  131. $scope.currentInclude="login.html";
  132. },
  133. function(response) {// failed
  134. });
  135. };
  136. /* </ LOGIN SIGNUP */
  137. //localStorage.clear();
  138. $scope.working=false;
  139. $scope.currentproject={};
  140. $scope.newproject={};
  141. //$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
  142. $scope.addNewProject = function(){
  143. /*$scope.newproject.totaltime="0";
  144. $scope.projects.push($scope.newproject);
  145. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
  146. $scope.newproject={
  147. id: $scope.projects[$scope.projects.length-1].id+1,
  148. chart: {
  149. labels: [],
  150. series: ['Working time'],
  151. data: []
  152. }
  153. };*/
  154. $http({
  155. url: urlapi + 'projects',
  156. method: "POST",
  157. data: $scope.newproject
  158. }).then(function(response) {
  159. console.log("project posted");
  160. console.log(response);
  161. $scope.projects=response.data;
  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. $http({
  185. url: urlapi + 'projects/' + $scope.projects[index]._id,
  186. method: "DELETE",
  187. data: $scope.user._id
  188. }).then(function(response) {
  189. console.log(response);
  190. $scope.projects=response.data;
  191. $scope.projectSelect(null);
  192. },
  193. function(response) {// failed
  194. });
  195. };
  196. $scope.currentprojectIndex;
  197. $scope.projectSelect = function(index){
  198. $scope.btnStop();
  199. $scope.currentprojectIndex=index;
  200. $scope.currentproject=$scope.projects[index];
  201. };
  202. $scope.joinProject = function(){
  203. console.log($scope.user);
  204. $http({
  205. url: urlapi + 'projects/'+$scope.currentproject._id+'/adduser',
  206. method: "PUT",
  207. data: $scope.user
  208. }).then(function(response) {
  209. console.log("project joined");
  210. $scope.projects=response.data;
  211. //re select currentproject
  212. $scope.currentproject=$scope.projects[$scope.currentprojectIndex];
  213. toastr.success("project joined");
  214. },
  215. function(response) {// failed
  216. });
  217. };
  218. var interval;
  219. $scope.currentStrike=0;
  220. $scope.btnWork = function(){
  221. //$scope.editingProject=false;
  222. $scope.working=true;
  223. $http({
  224. url: urlapi + 'projects/' + $scope.currentproject._id + '/startworking',
  225. method: "PUT",
  226. data: $scope.user
  227. }).then(function(response) {
  228. console.log(response);
  229. $scope.projects=response.data;
  230. },
  231. function(response) {// failed
  232. });
  233. };
  234. $scope.btnStop = function(){
  235. if($scope.working==true)
  236. {
  237. $scope.working=false;
  238. $http({
  239. url: urlapi + 'projects/' + $scope.currentproject._id + '/stopworking',
  240. method: "PUT",
  241. data: $scope.user
  242. }).then(function(response) {
  243. console.log(response);
  244. $scope.projects=response.data;
  245. },
  246. function(response) {// failed
  247. });
  248. }
  249. }
  250. $scope.openCode = function(){
  251. toastr.info("Visiting code");
  252. var urlCode="https://github.com/idoctnef/openworktime";
  253. if(typeof process !== 'undefined'){
  254. console.log(process.versions['electron']);
  255. const {shell} = require('electron');
  256. shell.openExternal(urlCode);
  257. }else{
  258. window.open(urlCode);
  259. }
  260. };
  261. //chart
  262. /*$scope.chart={
  263. labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  264. series: ['Working time', 'Series B'],
  265. data: [
  266. [65, 59, 80, 81, 56, 55, 40],
  267. [28, 48, 40, 19, 86, 27, 90]
  268. ]
  269. };*/
  270. })
  271. .filter('secondsToDateTime', [function() {
  272. return function(seconds) {
  273. return new Date(2016, 0, 1).setSeconds(seconds);
  274. };
  275. }]);