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.

363 lines
11 KiB

  1. var urlapi="http://localhost:3000/api/";
  2. //var urlapi="http://192.168.1.40:3000/api/";
  3. //localStorage.setItem("c_username", "user2");
  4. //localStorage.setItem("c_token", "");
  5. angular.module('starter.controllers', [])
  6. .controller('AppCtrl', function($scope, $ionicModal, $timeout, $http, $window) {
  7. // With the new view caching in Ionic, Controllers are only called
  8. // when they are recreated or on app start, instead of every page change.
  9. // To listen for when this page is active (for example, to refresh data),
  10. // listen for the $ionicView.enter event:
  11. //$scope.$on('$ionicView.enter', function(e) {
  12. //});
  13. // Form data for the login modal
  14. $scope.loginData = {};
  15. $scope.signupData= {};
  16. $scope.storageusername=localStorage.getItem("c_username");
  17. // Create the login modal that we will use later
  18. $ionicModal.fromTemplateUrl('templates/login.html', {
  19. scope: $scope
  20. }).then(function(modal) {
  21. $scope.modalLogin = modal;
  22. });
  23. $ionicModal.fromTemplateUrl('templates/signup.html', {
  24. scope: $scope
  25. }).then(function(modal) {
  26. $scope.modalSignup = modal;
  27. });
  28. // Triggered in the login modal to close it
  29. $scope.closeLogin = function() {
  30. $scope.modalLogin.hide();
  31. };
  32. $scope.closeSignup = function() {
  33. $scope.modalSignup.hide();
  34. };
  35. // Open the login modal
  36. $scope.login = function() {
  37. $scope.modalLogin.show();
  38. };
  39. $scope.signup = function() {
  40. $scope.modalSignup.show();
  41. };
  42. // Perform the login action when the user submits the login form
  43. $scope.doLogin = function() {
  44. console.log('Doing login', $scope.loginData);
  45. $http({
  46. url: urlapi + 'auth',
  47. method: "POST",
  48. data: $scope.loginData
  49. })
  50. .then(function(response) {
  51. // success
  52. console.log("response: ");
  53. console.log(response.data);
  54. if(response.data.success==true)
  55. {
  56. console.log("login successful");
  57. localStorage.setItem("c_username", $scope.loginData.username);
  58. localStorage.setItem("c_token", response.data.token);
  59. }else{
  60. console.log("login failed");
  61. }
  62. $timeout(function() {
  63. $scope.closeLogin();
  64. $window.location.reload(true);
  65. }, 1000);
  66. },
  67. function(response) { // optional
  68. // failed
  69. console.log(response);
  70. });
  71. };
  72. $scope.doSignup = function() {
  73. console.log('Doing signup', $scope.signupData);
  74. $http({
  75. url: urlapi + 'users',
  76. method: "POST",
  77. data: $scope.signupData
  78. })
  79. .then(function(response) {
  80. // success
  81. console.log("response: ");
  82. console.log(response.data);
  83. $timeout(function() {
  84. $scope.closeSignup();
  85. }, 1000);
  86. },
  87. function(response) { // optional
  88. // failed
  89. });
  90. };
  91. $scope.logout = function(){
  92. localStorage.removeItem("c_username");
  93. localStorage.removeItem("c_token");
  94. $window.location.reload(true);
  95. };
  96. })
  97. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout, $ionicLoading) {
  98. $scope.travels="";
  99. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  100. $scope.doRefresh = function() {
  101. $http.get(urlapi + 'travels')
  102. .success(function(data, status, headers,config){
  103. console.log('data success');
  104. console.log(data); // for browser console
  105. $scope.travels = data; // for UI
  106. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  107. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  108. })
  109. .error(function(data, status, headers,config){
  110. console.log('data error');
  111. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  112. })
  113. .then(function(result){
  114. travels = result.data;
  115. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  116. });
  117. };
  118. $scope.newtravel={};
  119. // Create the login modal that we will use later
  120. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  121. scope: $scope
  122. }).then(function(modal) {
  123. $scope.modalOffering = modal;
  124. });
  125. // Create the login modal that we will use later
  126. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  127. scope: $scope
  128. }).then(function(modal) {
  129. $scope.modalAsking = modal;
  130. });
  131. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  132. scope: $scope
  133. }).then(function(modal) {
  134. $scope.modalPackage = modal;
  135. });
  136. // Triggered in the login modal to close it
  137. $scope.closeNewOfferingTravel = function() {
  138. $scope.modalOffering.hide();
  139. };
  140. // Triggered in the login modal to close it
  141. $scope.closeNewAskingTravel = function() {
  142. $scope.modalAsking.hide();
  143. };
  144. $scope.closeNewAskingPackage = function() {
  145. $scope.modalPackage.hide();
  146. };
  147. // Open the login modal
  148. $scope.showNewOfferingTravel = function() {
  149. $scope.modalOffering.show();
  150. };
  151. // Open the login modal
  152. $scope.showNewAskingTravel = function() {
  153. $scope.modalAsking.show();
  154. };
  155. $scope.showNewAskingPackage = function() {
  156. $scope.modalPackage.show();
  157. };
  158. // Perform the login action when the user submits the login form
  159. $scope.doNewOfferingTravel = function() {
  160. console.log('Doing new travel', $scope.newtravel);
  161. $scope.newtravel.icon="lorry";
  162. $scope.newtravel.generateddate=$scope.newtravel.date;
  163. $scope.newtravel.owner=localStorage.getItem("c_username");
  164. $scope.newtravel.modality="offering";
  165. console.log($scope.newtravel);
  166. $http({
  167. url: urlapi + 'travels',
  168. method: "POST",
  169. data: $scope.newtravel
  170. })
  171. .then(function(response) {
  172. // success
  173. console.log("response: ");
  174. console.log(response);
  175. $scope.newtravel._id=response.data._id;
  176. $scope.travels.push($scope.newtravel);
  177. $scope.newtravel={};
  178. if(response.data.success==false){
  179. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  180. }
  181. },
  182. function(response) { // optional
  183. // failed
  184. });
  185. // Simulate a login delay. Remove this and replace with your login
  186. // code if using a login system
  187. $timeout(function() {
  188. $scope.closeNewOfferingTravel();
  189. }, 1000);
  190. };
  191. $scope.doNewAskingTravel = function() {
  192. console.log('Doing new travel', $scope.newtravel);
  193. $scope.newtravel.icon="lorry";
  194. $scope.newtravel.generateddate=$scope.newtravel.date;
  195. $scope.newtravel.owner=localStorage.getItem("c_username");
  196. $scope.newtravel.modality="asking";
  197. console.log($scope.newtravel);
  198. $http({
  199. url: urlapi + 'travels',
  200. method: "POST",
  201. data: $scope.newtravel
  202. })
  203. .then(function(response) {
  204. // success
  205. console.log("response: ");
  206. console.log(response);
  207. $scope.newtravel._id=response.data._id;
  208. $scope.travels.push($scope.newtravel);
  209. if(response.data.success==false){
  210. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  211. }
  212. },
  213. function(response) { // optional
  214. // failed
  215. });
  216. // Simulate a login delay. Remove this and replace with your login
  217. // code if using a login system
  218. $timeout(function() {
  219. $scope.closeNewAskingTravel();
  220. }, 1000);
  221. };
  222. $scope.doNewAskingPackage = function() {
  223. console.log('Doing new package', $scope.newtravel);
  224. $scope.newtravel.icon="lorry";
  225. $scope.newtravel.generateddate=$scope.newtravel.date;
  226. $scope.newtravel.owner=localStorage.getItem("c_username");
  227. $scope.newtravel.package=true;
  228. $scope.newtravel.modality="package";
  229. console.log($scope.newtravel);
  230. $http({
  231. url: urlapi + 'travels',
  232. method: "POST",
  233. data: $scope.newtravel
  234. })
  235. .then(function(response) {
  236. // success
  237. console.log("response: ");
  238. console.log(response);
  239. $scope.newtravel._id=response.data._id;
  240. $scope.travels.push($scope.newtravel);
  241. if(response.data.success==false){
  242. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  243. }
  244. },
  245. function(response) { // optional
  246. // failed
  247. });
  248. // Simulate a login delay. Remove this and replace with your login
  249. // code if using a login system
  250. $timeout(function() {
  251. $scope.closeNewAskingPackage();
  252. }, 1000);
  253. };
  254. })
  255. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  256. $scope.storageusername=localStorage.getItem("c_username");
  257. $scope.travel="";
  258. console.log($stateParams.travelId);
  259. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  260. .success(function(data, status, headers,config){
  261. console.log('data success');
  262. console.log(data); // for browser console
  263. $scope.travel = data; // for UI
  264. })
  265. .error(function(data, status, headers,config){
  266. console.log('data error');
  267. })
  268. .then(function(result){
  269. travels = result.data;
  270. });
  271. })
  272. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  273. $scope.users="";
  274. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  275. $scope.doRefresh = function() {
  276. $http.get(urlapi + 'users')
  277. .success(function(data, status, headers, config){
  278. console.log('data success');
  279. console.log(data); // for browser console
  280. $scope.users = data; // for UI
  281. localStorage.setItem('c_users', JSON.stringify($scope.users));
  282. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  283. })
  284. .error(function(data, status, headers,config){
  285. console.log('data error');
  286. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  287. })
  288. .then(function(result){
  289. users = result.data;
  290. });
  291. };
  292. })
  293. .controller('UserCtrl', function($scope, $stateParams, $http) {
  294. //$scope.user="";
  295. console.log($stateParams.username);
  296. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  297. .success(function(data, status, headers,config){
  298. console.log('data success');
  299. console.log(data); // for browser console
  300. $scope.user = data; // for UI
  301. })
  302. .error(function(data, status, headers,config){
  303. console.log('data error');
  304. })
  305. .then(function(result){
  306. user = result.data;
  307. });
  308. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  309. .success(function(data, status, headers,config){
  310. console.log('data success');
  311. console.log(data); // for browser console
  312. $scope.travels = data; // for UI
  313. })
  314. .error(function(data, status, headers,config){
  315. console.log('data error');
  316. })
  317. .then(function(result){
  318. travels = result.data;
  319. });
  320. });