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.

349 lines
10 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. });
  70. };
  71. $scope.doSignup = function() {
  72. console.log('Doing signup', $scope.signupData);
  73. $http({
  74. url: urlapi + 'users',
  75. method: "POST",
  76. data: $scope.signupData
  77. })
  78. .then(function(response) {
  79. // success
  80. console.log("response: ");
  81. console.log(response.data);
  82. $timeout(function() {
  83. $scope.closeSignup();
  84. }, 1000);
  85. },
  86. function(response) { // optional
  87. // failed
  88. });
  89. };
  90. $scope.logout = function(){
  91. localStorage.removeItem("c_username");
  92. localStorage.removeItem("c_token");
  93. $window.location.reload(true);
  94. };
  95. })
  96. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout) {
  97. $scope.travels="";
  98. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  99. $scope.doRefresh = function() {
  100. $http.get(urlapi + 'travels')
  101. .success(function(data, status, headers,config){
  102. console.log('data success');
  103. console.log(data); // for browser console
  104. $scope.travels = data; // for UI
  105. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  106. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  107. })
  108. .error(function(data, status, headers,config){
  109. console.log('data error');
  110. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  111. })
  112. .then(function(result){
  113. travels = result.data;
  114. });
  115. };
  116. $scope.newtravel={};
  117. // Create the login modal that we will use later
  118. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  119. scope: $scope
  120. }).then(function(modal) {
  121. $scope.modalOffering = modal;
  122. });
  123. // Create the login modal that we will use later
  124. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  125. scope: $scope
  126. }).then(function(modal) {
  127. $scope.modalAsking = modal;
  128. });
  129. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  130. scope: $scope
  131. }).then(function(modal) {
  132. $scope.modalPackage = modal;
  133. });
  134. // Triggered in the login modal to close it
  135. $scope.closeNewOfferingTravel = function() {
  136. $scope.modalOffering.hide();
  137. };
  138. // Triggered in the login modal to close it
  139. $scope.closeNewAskingTravel = function() {
  140. $scope.modalAsking.hide();
  141. };
  142. $scope.closeNewAskingPackage = function() {
  143. $scope.modalPackage.hide();
  144. };
  145. // Open the login modal
  146. $scope.showNewOfferingTravel = function() {
  147. $scope.modalOffering.show();
  148. };
  149. // Open the login modal
  150. $scope.showNewAskingTravel = function() {
  151. $scope.modalAsking.show();
  152. };
  153. $scope.showNewAskingPackage = function() {
  154. $scope.modalPackage.show();
  155. };
  156. // Perform the login action when the user submits the login form
  157. $scope.doNewOfferingTravel = function() {
  158. console.log('Doing new travel', $scope.newtravel);
  159. $scope.newtravel.icon="lorry";
  160. $scope.newtravel.generateddate=$scope.newtravel.date;
  161. $scope.newtravel.owner=localStorage.getItem("c_username");
  162. $scope.newtravel.modality="offering";
  163. console.log($scope.newtravel);
  164. $http({
  165. url: urlapi + 'travels',
  166. method: "POST",
  167. data: $scope.newtravel
  168. })
  169. .then(function(response) {
  170. // success
  171. console.log("response: ");
  172. console.log(response);
  173. $scope.newtravel._id=response.data._id;
  174. $scope.travels.push($scope.newtravel);
  175. $scope.newtravel={};
  176. },
  177. function(response) { // optional
  178. // failed
  179. });
  180. // Simulate a login delay. Remove this and replace with your login
  181. // code if using a login system
  182. $timeout(function() {
  183. $scope.closeNewOfferingTravel();
  184. }, 1000);
  185. };
  186. $scope.doNewAskingTravel = function() {
  187. console.log('Doing new travel', $scope.newtravel);
  188. $scope.newtravel.icon="lorry";
  189. $scope.newtravel.generateddate=$scope.newtravel.date;
  190. $scope.newtravel.owner=localStorage.getItem("c_username");
  191. $scope.newtravel.modality="asking";
  192. console.log($scope.newtravel);
  193. $http({
  194. url: urlapi + 'travels',
  195. method: "POST",
  196. data: $scope.newtravel
  197. })
  198. .then(function(response) {
  199. // success
  200. console.log("response: ");
  201. console.log(response);
  202. $scope.newtravel._id=response.data._id;
  203. $scope.travels.push($scope.newtravel);
  204. },
  205. function(response) { // optional
  206. // failed
  207. });
  208. // Simulate a login delay. Remove this and replace with your login
  209. // code if using a login system
  210. $timeout(function() {
  211. $scope.closeNewAskingTravel();
  212. }, 1000);
  213. };
  214. $scope.doNewAskingPackage = function() {
  215. console.log('Doing new package', $scope.newtravel);
  216. $scope.newtravel.icon="lorry";
  217. $scope.newtravel.generateddate=$scope.newtravel.date;
  218. $scope.newtravel.owner=localStorage.getItem("c_username");
  219. $scope.newtravel.package=true;
  220. $scope.newtravel.modality="package";
  221. console.log($scope.newtravel);
  222. $http({
  223. url: urlapi + 'travels',
  224. method: "POST",
  225. data: $scope.newtravel
  226. })
  227. .then(function(response) {
  228. // success
  229. console.log("response: ");
  230. console.log(response);
  231. $scope.newtravel._id=response.data._id;
  232. $scope.travels.push($scope.newtravel);
  233. },
  234. function(response) { // optional
  235. // failed
  236. });
  237. // Simulate a login delay. Remove this and replace with your login
  238. // code if using a login system
  239. $timeout(function() {
  240. $scope.closeNewAskingPackage();
  241. }, 1000);
  242. };
  243. })
  244. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  245. $scope.storageusername=localStorage.getItem("c_username");
  246. $scope.travel="";
  247. console.log($stateParams.travelId);
  248. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  249. .success(function(data, status, headers,config){
  250. console.log('data success');
  251. console.log(data); // for browser console
  252. $scope.travel = data; // for UI
  253. })
  254. .error(function(data, status, headers,config){
  255. console.log('data error');
  256. })
  257. .then(function(result){
  258. travels = result.data;
  259. });
  260. })
  261. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  262. $scope.users="";
  263. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  264. $scope.doRefresh = function() {
  265. $http.get(urlapi + 'users')
  266. .success(function(data, status, headers, config){
  267. console.log('data success');
  268. console.log(data); // for browser console
  269. $scope.users = data; // for UI
  270. localStorage.setItem('c_users', JSON.stringify($scope.users));
  271. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  272. })
  273. .error(function(data, status, headers,config){
  274. console.log('data error');
  275. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  276. })
  277. .then(function(result){
  278. users = result.data;
  279. });
  280. };
  281. })
  282. .controller('UserCtrl', function($scope, $stateParams, $http) {
  283. //$scope.user="";
  284. console.log($stateParams.username);
  285. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  286. .success(function(data, status, headers,config){
  287. console.log('data success');
  288. console.log(data); // for browser console
  289. $scope.user = data; // for UI
  290. })
  291. .error(function(data, status, headers,config){
  292. console.log('data error');
  293. })
  294. .then(function(result){
  295. user = result.data;
  296. });
  297. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  298. .success(function(data, status, headers,config){
  299. console.log('data success');
  300. console.log(data); // for browser console
  301. $scope.travels = data; // for UI
  302. })
  303. .error(function(data, status, headers,config){
  304. console.log('data error');
  305. })
  306. .then(function(result){
  307. travels = result.data;
  308. });
  309. });