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.

291 lines
9.6 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. console.log(data);
  49. $scope.projects=data;
  50. })
  51. .error(function(data, status, headers,config){
  52. console.log("server not responding, data error");
  53. toastr.error("server not responding");
  54. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  55. })
  56. .then(function(result){
  57. users = result.data;
  58. });
  59. };
  60. /* </DASHBOARD initialization */
  61. $scope.user={};
  62. if(localStorage.getItem("owt_user")){
  63. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  64. $scope.currentInclude="dashboard.html";
  65. $scope.dashboardInit();
  66. intervalGetData = $interval(function(){
  67. $scope.dashboardInit();
  68. }, refreshTime);
  69. }else{
  70. $scope.currentInclude="login.html";
  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. var intervalGetData;//out of internal scope
  92. $scope.onBtnLogin = function(){
  93. $http({
  94. url: urlapi + 'auth',
  95. method: "POST",
  96. data: $scope.user
  97. }).then(function(response) {
  98. if(response.data.success)
  99. {
  100. localStorage.setItem("owt_token", response.data.token);
  101. localStorage.setItem("owt_user", angular.toJson(response.data.user));
  102. $scope.user=JSON.parse(localStorage.getItem("owt_user"));
  103. $scope.currentInclude="dashboard.html";
  104. $scope.dashboardInit();
  105. intervalGetData = $interval(function(){
  106. $scope.dashboardInit();
  107. }, refreshTime);
  108. }else{
  109. toastr.error(response.data.message);
  110. }
  111. },
  112. function(response) {// failed
  113. });
  114. };
  115. $scope.onBtnLogout = function(){
  116. $http({
  117. url: urlapi + 'logout',
  118. method: "POST",
  119. data: $scope.user
  120. }).then(function(response) {
  121. localStorage.removeItem("owt_token");
  122. localStorage.removeItem("owt_user");
  123. $interval.cancel(intervalGetData);
  124. $scope.currentInclude="login.html";
  125. },
  126. function(response) {// failed
  127. });
  128. };
  129. /* </ LOGIN SIGNUP */
  130. //localStorage.clear();
  131. $scope.working=false;
  132. $scope.currentproject={};
  133. $scope.newproject={};
  134. //$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
  135. $scope.addNewProject = function(){
  136. /*$scope.newproject.totaltime="0";
  137. $scope.projects.push($scope.newproject);
  138. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
  139. $scope.newproject={
  140. id: $scope.projects[$scope.projects.length-1].id+1,
  141. chart: {
  142. labels: [],
  143. series: ['Working time'],
  144. data: []
  145. }
  146. };*/
  147. $http({
  148. url: urlapi + 'projects',
  149. method: "POST",
  150. data: $scope.newproject
  151. }).then(function(response) {
  152. console.log("project posted");
  153. console.log(response);
  154. $scope.projects=response.data;
  155. toastr.success("project created at server");
  156. $scope.newproject={};
  157. },
  158. function(response) {// failed
  159. });
  160. };
  161. $scope.editingIndex="";
  162. $scope.editProject = function(index){
  163. $scope.editingIndex=index;
  164. $scope.editingProject=angular.copy($scope.projects[index]);
  165. };
  166. $scope.cancelEditProject = function(){
  167. $scope.editingProject=false;
  168. };
  169. $scope.updateProject = function(){
  170. $scope.projects[$scope.editingIndex]=angular.copy($scope.editingProject);
  171. $scope.currentproject=angular.copy($scope.editingProject);
  172. $scope.editingProject=false;
  173. };
  174. $scope.removeProject = function(index){
  175. /*$scope.projects.splice(index, 1);
  176. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));*/
  177. $http({
  178. url: urlapi + 'projects/' + $scope.projects[index]._id,
  179. method: "DELETE",
  180. data: $scope.user._id
  181. }).then(function(response) {
  182. console.log(response);
  183. $scope.projects=response.data;
  184. $scope.projectSelect(null);
  185. },
  186. function(response) {// failed
  187. });
  188. };
  189. $scope.currentprojectIndex;
  190. $scope.projectSelect = function(index){
  191. $scope.btnStop();
  192. $scope.currentprojectIndex=index;
  193. $scope.currentproject=$scope.projects[index];
  194. };
  195. $scope.joinProject = function(){
  196. console.log($scope.user);
  197. $http({
  198. url: urlapi + 'projects/'+$scope.currentproject._id+'/adduser',
  199. method: "PUT",
  200. data: $scope.user
  201. }).then(function(response) {
  202. console.log("project joined");
  203. $scope.projects=response.data;
  204. //re select currentproject
  205. $scope.currentproject=$scope.projects[$scope.currentprojectIndex];
  206. toastr.success("project joined");
  207. },
  208. function(response) {// failed
  209. });
  210. };
  211. var interval;
  212. $scope.currentStrike=0;
  213. $scope.btnWork = function(){
  214. $scope.editingProject=false;
  215. $scope.working=true;
  216. $scope.currentStrike=0;
  217. interval = $interval(function(){
  218. $scope.currentStrike++;
  219. $scope.currentproject.totaltime++;
  220. }, 1000);
  221. $http({
  222. url: urlapi + 'users',
  223. method: "POST",
  224. data: $scope.user
  225. }).then(function(response) {
  226. $scope.currentInclude="login.html";
  227. },
  228. function(response) {// failed
  229. });
  230. };
  231. $scope.btnStop = function(){
  232. $interval.cancel(interval);
  233. if($scope.working==true)
  234. {
  235. $scope.working=false;
  236. $scope.currentproject.chart.labels.push("work strike " + $scope.currentproject.chart.labels.length);
  237. $scope.currentproject.chart.data.push($scope.currentStrike);
  238. localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
  239. }
  240. }
  241. $scope.openCode = function(){
  242. toastr.info("Visiting code");
  243. var urlCode="https://github.com/idoctnef/openworktime";
  244. if(typeof process !== 'undefined'){
  245. console.log(process.versions['electron']);
  246. const {shell} = require('electron');
  247. shell.openExternal(urlCode);
  248. }else{
  249. window.open(urlCode);
  250. }
  251. };
  252. //chart
  253. /*$scope.chart={
  254. labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  255. series: ['Working time', 'Series B'],
  256. data: [
  257. [65, 59, 80, 81, 56, 55, 40],
  258. [28, 48, 40, 19, 86, 27, 90]
  259. ]
  260. };*/
  261. })
  262. .filter('secondsToDateTime', [function() {
  263. return function(seconds) {
  264. return new Date(2016, 0, 1).setSeconds(seconds);
  265. };
  266. }]);