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.

267 lines
8.1 KiB

  1. angular.module('starter.controllers', [])
  2. .controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  3. // With the new view caching in Ionic, Controllers are only called
  4. // when they are recreated or on app start, instead of every page change.
  5. // To listen for when this page is active (for example, to refresh data),
  6. // listen for the $ionicView.enter event:
  7. //$scope.$on('$ionicView.enter', function(e) {
  8. //});
  9. // Form data for the login modal
  10. $scope.loginData = {};
  11. // Create the login modal that we will use later
  12. $ionicModal.fromTemplateUrl('templates/login.html', {
  13. scope: $scope
  14. }).then(function(modal) {
  15. $scope.modal = modal;
  16. });
  17. // Triggered in the login modal to close it
  18. $scope.closeLogin = function() {
  19. $scope.modal.hide();
  20. };
  21. // Open the login modal
  22. $scope.login = function() {
  23. $scope.modal.show();
  24. };
  25. // Perform the login action when the user submits the login form
  26. $scope.doLogin = function() {
  27. console.log('Doing login', $scope.loginData);
  28. // Simulate a login delay. Remove this and replace with your login
  29. // code if using a login system
  30. $timeout(function() {
  31. $scope.closeLogin();
  32. }, 1000);
  33. };
  34. })
  35. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout) {
  36. $scope.travels="";
  37. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  38. $scope.doRefresh = function() {
  39. $http.get('http://localhost:3000/api/travels')
  40. .success(function(data, status, headers,config){
  41. console.log('data success');
  42. console.log(data); // for browser console
  43. $scope.travels = data; // for UI
  44. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  45. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  46. })
  47. .error(function(data, status, headers,config){
  48. console.log('data error');
  49. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  50. })
  51. .then(function(result){
  52. travels = result.data;
  53. });
  54. };
  55. $scope.newtravel={};
  56. // Create the login modal that we will use later
  57. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  58. scope: $scope
  59. }).then(function(modal) {
  60. $scope.modalOffering = modal;
  61. });
  62. // Create the login modal that we will use later
  63. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  64. scope: $scope
  65. }).then(function(modal) {
  66. $scope.modalAsking = modal;
  67. });
  68. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  69. scope: $scope
  70. }).then(function(modal) {
  71. $scope.modalPackage = modal;
  72. });
  73. // Triggered in the login modal to close it
  74. $scope.closeNewOfferingTravel = function() {
  75. $scope.modalOffering.hide();
  76. };
  77. // Triggered in the login modal to close it
  78. $scope.closeNewAskingTravel = function() {
  79. $scope.modalAsking.hide();
  80. };
  81. $scope.closeNewAskingPackage = function() {
  82. $scope.modalPackage.hide();
  83. };
  84. // Open the login modal
  85. $scope.showNewOfferingTravel = function() {
  86. $scope.modalOffering.show();
  87. };
  88. // Open the login modal
  89. $scope.showNewAskingTravel = function() {
  90. $scope.modalAsking.show();
  91. };
  92. $scope.showNewAskingPackage = function() {
  93. $scope.modalPackage.show();
  94. };
  95. // Perform the login action when the user submits the login form
  96. $scope.doNewOfferingTravel = function() {
  97. console.log('Doing new travel', $scope.newtravel);
  98. $scope.newtravel.icon="lorry";
  99. $scope.newtravel.generateddate=$scope.newtravel.date;
  100. $scope.newtravel.owner="user";
  101. $scope.newtravel.modality="offering";
  102. console.log($scope.newtravel);
  103. $http({
  104. url: 'http://localhost:3000/api/travels',
  105. method: "POST",
  106. data: $scope.newtravel
  107. })
  108. .then(function(response) {
  109. // success
  110. console.log("response: ");
  111. console.log(response);
  112. $scope.newtravel._id=response.data._id;
  113. $scope.travels.push($scope.newtravel);
  114. $scope.newtravel={};
  115. },
  116. function(response) { // optional
  117. // failed
  118. });
  119. // Simulate a login delay. Remove this and replace with your login
  120. // code if using a login system
  121. $timeout(function() {
  122. $scope.closeNewOfferingTravel();
  123. }, 1000);
  124. };
  125. $scope.doNewAskingTravel = function() {
  126. console.log('Doing new travel', $scope.newtravel);
  127. $scope.newtravel.icon="lorry";
  128. $scope.newtravel.generateddate=$scope.newtravel.date;
  129. $scope.newtravel.owner="user";
  130. $scope.newtravel.modality="asking";
  131. console.log($scope.newtravel);
  132. $http({
  133. url: 'http://localhost:3000/api/travels',
  134. method: "POST",
  135. data: $scope.newtravel
  136. })
  137. .then(function(response) {
  138. // success
  139. console.log("response: ");
  140. console.log(response);
  141. $scope.newtravel._id=response.data._id;
  142. $scope.travels.push($scope.newtravel);
  143. },
  144. function(response) { // optional
  145. // failed
  146. });
  147. // Simulate a login delay. Remove this and replace with your login
  148. // code if using a login system
  149. $timeout(function() {
  150. $scope.closeNewAskingTravel();
  151. }, 1000);
  152. };
  153. $scope.doNewAskingPackage = function() {
  154. console.log('Doing new package', $scope.newtravel);
  155. $scope.newtravel.icon="lorry";
  156. $scope.newtravel.generateddate=$scope.newtravel.date;
  157. $scope.newtravel.owner="user";
  158. $scope.newtravel.package=true;
  159. $scope.newtravel.modality="package";
  160. console.log($scope.newtravel);
  161. $http({
  162. url: 'http://localhost:3000/api/travels',
  163. method: "POST",
  164. data: $scope.newtravel
  165. })
  166. .then(function(response) {
  167. // success
  168. console.log("response: ");
  169. console.log(response);
  170. $scope.newtravel._id=response.data._id;
  171. $scope.travels.push($scope.newtravel);
  172. },
  173. function(response) { // optional
  174. // failed
  175. });
  176. // Simulate a login delay. Remove this and replace with your login
  177. // code if using a login system
  178. $timeout(function() {
  179. $scope.closeNewAskingPackage();
  180. }, 1000);
  181. };
  182. })
  183. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  184. $scope.travel="";
  185. console.log($stateParams.travelId);
  186. $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
  187. .success(function(data, status, headers,config){
  188. console.log('data success');
  189. console.log(data); // for browser console
  190. $scope.travel = data; // for UI
  191. })
  192. .error(function(data, status, headers,config){
  193. console.log('data error');
  194. })
  195. .then(function(result){
  196. travels = result.data;
  197. });
  198. })
  199. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  200. $scope.users="";
  201. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  202. $scope.doRefresh = function() {
  203. $http.get('http://localhost:3000/api/users')
  204. .success(function(data, status, headers, config){
  205. console.log('data success');
  206. console.log(data); // for browser console
  207. $scope.users = data; // for UI
  208. localStorage.setItem('c_users', JSON.stringify($scope.users));
  209. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  210. })
  211. .error(function(data, status, headers,config){
  212. console.log('data error');
  213. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  214. })
  215. .then(function(result){
  216. users = result.data;
  217. });
  218. };
  219. })
  220. .controller('UserCtrl', function($scope, $stateParams, $http) {
  221. $scope.travel="";
  222. console.log($stateParams.username);
  223. $http.get('http://localhost:3000/api/users/byusername/'+$stateParams.username)
  224. .success(function(data, status, headers,config){
  225. console.log('data success');
  226. console.log(data); // for browser console
  227. $scope.user = data; // for UI
  228. })
  229. .error(function(data, status, headers,config){
  230. console.log('data error');
  231. })
  232. .then(function(result){
  233. travels = result.data;
  234. });
  235. });