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.

287 lines
8.7 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) {
  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. // Create the login modal that we will use later
  16. $ionicModal.fromTemplateUrl('templates/login.html', {
  17. scope: $scope
  18. }).then(function(modal) {
  19. $scope.modal = modal;
  20. });
  21. // Triggered in the login modal to close it
  22. $scope.closeLogin = function() {
  23. $scope.modal.hide();
  24. };
  25. // Open the login modal
  26. $scope.login = function() {
  27. $scope.modal.show();
  28. };
  29. // Perform the login action when the user submits the login form
  30. $scope.doLogin = function() {
  31. console.log('Doing login', $scope.loginData);
  32. // Simulate a login delay. Remove this and replace with your login
  33. // code if using a login system
  34. $timeout(function() {
  35. $scope.closeLogin();
  36. }, 1000);
  37. };
  38. })
  39. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout) {
  40. $scope.travels="";
  41. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  42. $scope.doRefresh = function() {
  43. $http.get(urlapi + 'travels')
  44. .success(function(data, status, headers,config){
  45. console.log('data success');
  46. console.log(data); // for browser console
  47. $scope.travels = data; // for UI
  48. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  49. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  50. })
  51. .error(function(data, status, headers,config){
  52. console.log('data error');
  53. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  54. })
  55. .then(function(result){
  56. travels = result.data;
  57. });
  58. };
  59. $scope.newtravel={};
  60. // Create the login modal that we will use later
  61. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  62. scope: $scope
  63. }).then(function(modal) {
  64. $scope.modalOffering = modal;
  65. });
  66. // Create the login modal that we will use later
  67. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  68. scope: $scope
  69. }).then(function(modal) {
  70. $scope.modalAsking = modal;
  71. });
  72. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  73. scope: $scope
  74. }).then(function(modal) {
  75. $scope.modalPackage = modal;
  76. });
  77. // Triggered in the login modal to close it
  78. $scope.closeNewOfferingTravel = function() {
  79. $scope.modalOffering.hide();
  80. };
  81. // Triggered in the login modal to close it
  82. $scope.closeNewAskingTravel = function() {
  83. $scope.modalAsking.hide();
  84. };
  85. $scope.closeNewAskingPackage = function() {
  86. $scope.modalPackage.hide();
  87. };
  88. // Open the login modal
  89. $scope.showNewOfferingTravel = function() {
  90. $scope.modalOffering.show();
  91. };
  92. // Open the login modal
  93. $scope.showNewAskingTravel = function() {
  94. $scope.modalAsking.show();
  95. };
  96. $scope.showNewAskingPackage = function() {
  97. $scope.modalPackage.show();
  98. };
  99. // Perform the login action when the user submits the login form
  100. $scope.doNewOfferingTravel = function() {
  101. console.log('Doing new travel', $scope.newtravel);
  102. $scope.newtravel.icon="lorry";
  103. $scope.newtravel.generateddate=$scope.newtravel.date;
  104. $scope.newtravel.owner=localStorage.getItem("c_username");
  105. $scope.newtravel.modality="offering";
  106. console.log($scope.newtravel);
  107. $http({
  108. url: urlapi + 'travels',
  109. method: "POST",
  110. data: $scope.newtravel
  111. })
  112. .then(function(response) {
  113. // success
  114. console.log("response: ");
  115. console.log(response);
  116. $scope.newtravel._id=response.data._id;
  117. $scope.travels.push($scope.newtravel);
  118. $scope.newtravel={};
  119. },
  120. function(response) { // optional
  121. // failed
  122. });
  123. // Simulate a login delay. Remove this and replace with your login
  124. // code if using a login system
  125. $timeout(function() {
  126. $scope.closeNewOfferingTravel();
  127. }, 1000);
  128. };
  129. $scope.doNewAskingTravel = function() {
  130. console.log('Doing new travel', $scope.newtravel);
  131. $scope.newtravel.icon="lorry";
  132. $scope.newtravel.generateddate=$scope.newtravel.date;
  133. $scope.newtravel.owner=localStorage.getItem("c_username");
  134. $scope.newtravel.modality="asking";
  135. console.log($scope.newtravel);
  136. $http({
  137. url: urlapi + 'travels',
  138. method: "POST",
  139. data: $scope.newtravel
  140. })
  141. .then(function(response) {
  142. // success
  143. console.log("response: ");
  144. console.log(response);
  145. $scope.newtravel._id=response.data._id;
  146. $scope.travels.push($scope.newtravel);
  147. },
  148. function(response) { // optional
  149. // failed
  150. });
  151. // Simulate a login delay. Remove this and replace with your login
  152. // code if using a login system
  153. $timeout(function() {
  154. $scope.closeNewAskingTravel();
  155. }, 1000);
  156. };
  157. $scope.doNewAskingPackage = function() {
  158. console.log('Doing new package', $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.package=true;
  163. $scope.newtravel.modality="package";
  164. console.log($scope.newtravel);
  165. $http({
  166. url: urlapi + 'travels',
  167. method: "POST",
  168. data: $scope.newtravel
  169. })
  170. .then(function(response) {
  171. // success
  172. console.log("response: ");
  173. console.log(response);
  174. $scope.newtravel._id=response.data._id;
  175. $scope.travels.push($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.closeNewAskingPackage();
  184. }, 1000);
  185. };
  186. })
  187. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  188. $scope.storageusername=localStorage.getItem("c_username");
  189. $scope.travel="";
  190. console.log($stateParams.travelId);
  191. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  192. .success(function(data, status, headers,config){
  193. console.log('data success');
  194. console.log(data); // for browser console
  195. $scope.travel = data; // for UI
  196. })
  197. .error(function(data, status, headers,config){
  198. console.log('data error');
  199. })
  200. .then(function(result){
  201. travels = result.data;
  202. });
  203. })
  204. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  205. $scope.users="";
  206. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  207. $scope.doRefresh = function() {
  208. $http.get(urlapi + 'users')
  209. .success(function(data, status, headers, config){
  210. console.log('data success');
  211. console.log(data); // for browser console
  212. $scope.users = data; // for UI
  213. localStorage.setItem('c_users', JSON.stringify($scope.users));
  214. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  215. })
  216. .error(function(data, status, headers,config){
  217. console.log('data error');
  218. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  219. })
  220. .then(function(result){
  221. users = result.data;
  222. });
  223. };
  224. })
  225. .controller('UserCtrl', function($scope, $stateParams, $http) {
  226. //$scope.user="";
  227. console.log($stateParams.username);
  228. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  229. .success(function(data, status, headers,config){
  230. console.log('data success');
  231. console.log(data); // for browser console
  232. $scope.user = data; // for UI
  233. })
  234. .error(function(data, status, headers,config){
  235. console.log('data error');
  236. })
  237. .then(function(result){
  238. user = result.data;
  239. });
  240. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  241. .success(function(data, status, headers,config){
  242. console.log('data success');
  243. console.log(data); // for browser console
  244. $scope.travels = data; // for UI
  245. })
  246. .error(function(data, status, headers,config){
  247. console.log('data error');
  248. })
  249. .then(function(result){
  250. travels = result.data;
  251. });
  252. });