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.

405 lines
12 KiB

  1. var urlapi="http://localhost:3000/api/";
  2. //var urlapi="https://collectivecar.paas.primustech.io/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. if(localStorage.getItem('c_token')){// adding token to the headers
  99. //console.log("token added to headers in run module");
  100. //console.log($http.defaults);
  101. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('c_token');
  102. //$http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";
  103. //console.log($http.defaults.headers);
  104. }
  105. /*if(localStorage.getItem('c_token')){// adding token to the headers
  106. $http.defaults.headers.token = localStorage.getItem('c_token');
  107. }*/
  108. $scope.travels="";
  109. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  110. $scope.doRefresh = function() {
  111. $http.get(urlapi + 'travels')
  112. .success(function(data, status, headers,config){
  113. console.log('data success');
  114. console.log(data); // for browser console
  115. $scope.travels = data; // for UI
  116. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  117. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  118. })
  119. .error(function(data, status, headers,config){
  120. console.log('data error');
  121. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  122. })
  123. .then(function(result){
  124. travels = result.data;
  125. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  126. });
  127. };
  128. $scope.newtravel={};
  129. /*$scope.newtravel={
  130. title: "prova",
  131. from: "prova",
  132. to: "prova",
  133. seats: 3,
  134. package: true,
  135. phone: 123,
  136. telegram: "telusr",
  137. description: "this is the description of prova"
  138. };*/
  139. // Create the login modal that we will use later
  140. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  141. scope: $scope
  142. }).then(function(modal) {
  143. $scope.modalOffering = modal;
  144. });
  145. // Create the login modal that we will use later
  146. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  147. scope: $scope
  148. }).then(function(modal) {
  149. $scope.modalAsking = modal;
  150. });
  151. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  152. scope: $scope
  153. }).then(function(modal) {
  154. $scope.modalPackage = modal;
  155. });
  156. // Triggered in the login modal to close it
  157. $scope.closeNewOfferingTravel = function() {
  158. $scope.modalOffering.hide();
  159. };
  160. // Triggered in the login modal to close it
  161. $scope.closeNewAskingTravel = function() {
  162. $scope.modalAsking.hide();
  163. };
  164. $scope.closeNewAskingPackage = function() {
  165. $scope.modalPackage.hide();
  166. };
  167. // Open the login modal
  168. $scope.showNewOfferingTravel = function() {
  169. $scope.modalOffering.show();
  170. };
  171. // Open the login modal
  172. $scope.showNewAskingTravel = function() {
  173. $scope.modalAsking.show();
  174. };
  175. $scope.showNewAskingPackage = function() {
  176. $scope.modalPackage.show();
  177. };
  178. // Perform the login action when the user submits the login form
  179. $scope.doNewOfferingTravel = function() {
  180. console.log('Doing new travel', $scope.newtravel);
  181. $scope.newtravel.icon="lorry";
  182. $scope.newtravel.generateddate=$scope.newtravel.date;
  183. $scope.newtravel.owner=localStorage.getItem("c_username");
  184. $scope.newtravel.modality="offering";
  185. //$scope.newtravel.token=localStorage.getItem("c_token");
  186. console.log($scope.newtravel);
  187. $http({
  188. url: urlapi + 'travels',
  189. method: "POST",
  190. data: $scope.newtravel
  191. })
  192. .then(function(response) {
  193. // success
  194. console.log("response: ");
  195. console.log(response);
  196. $scope.newtravel._id=response.data._id;
  197. $scope.travels.push($scope.newtravel);
  198. $scope.newtravel={};
  199. if(response.data.success==false){
  200. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  201. }
  202. },
  203. function(response) { // optional
  204. // failed
  205. });
  206. // Simulate a login delay. Remove this and replace with your login
  207. // code if using a login system
  208. $timeout(function() {
  209. $scope.closeNewOfferingTravel();
  210. }, 1000);
  211. };
  212. $scope.doNewAskingTravel = function() {
  213. console.log('Doing new travel', $scope.newtravel);
  214. $scope.newtravel.icon="lorry";
  215. $scope.newtravel.generateddate=$scope.newtravel.date;
  216. $scope.newtravel.owner=localStorage.getItem("c_username");
  217. $scope.newtravel.modality="asking";
  218. console.log($scope.newtravel);
  219. $http({
  220. url: urlapi + 'travels',
  221. method: "POST",
  222. data: $scope.newtravel
  223. })
  224. .then(function(response) {
  225. // success
  226. console.log("response: ");
  227. console.log(response);
  228. $scope.newtravel._id=response.data._id;
  229. $scope.travels.push($scope.newtravel);
  230. if(response.data.success==false){
  231. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  232. }
  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.closeNewAskingTravel();
  241. }, 1000);
  242. };
  243. $scope.doNewAskingPackage = function() {
  244. console.log('Doing new package', $scope.newtravel);
  245. $scope.newtravel.icon="lorry";
  246. $scope.newtravel.generateddate=$scope.newtravel.date;
  247. $scope.newtravel.owner=localStorage.getItem("c_username");
  248. $scope.newtravel.package=true;
  249. $scope.newtravel.modality="package";
  250. console.log($scope.newtravel);
  251. $http({
  252. url: urlapi + 'travels',
  253. method: "POST",
  254. data: $scope.newtravel
  255. })
  256. .then(function(response) {
  257. // success
  258. console.log("response: ");
  259. console.log(response);
  260. $scope.newtravel._id=response.data._id;
  261. $scope.travels.push($scope.newtravel);
  262. if(response.data.success==false){
  263. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  264. }
  265. },
  266. function(response) { // optional
  267. // failed
  268. });
  269. // Simulate a login delay. Remove this and replace with your login
  270. // code if using a login system
  271. $timeout(function() {
  272. $scope.closeNewAskingPackage();
  273. }, 1000);
  274. };
  275. })
  276. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  277. if(localStorage.getItem('c_token')){// adding token to the headers
  278. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  279. }
  280. $scope.storageusername=localStorage.getItem("c_username");
  281. $scope.travel="";
  282. console.log($stateParams.travelId);
  283. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  284. .success(function(data, status, headers,config){
  285. console.log('data success');
  286. console.log(data); // for browser console
  287. $scope.travel = data; // for UI
  288. })
  289. .error(function(data, status, headers,config){
  290. console.log('data error');
  291. })
  292. .then(function(result){
  293. travels = result.data;
  294. });
  295. $scope.deleteTravel = function(){
  296. console.log("delete travel: " + $stateParams.travelId);
  297. $http({
  298. url: urlapi + 'travels/' + $stateParams.travelId,
  299. method: "DELETE"
  300. })
  301. .then(function(response) {
  302. console.log(response);
  303. },
  304. function(response) { // optional
  305. // failed
  306. });
  307. };
  308. $scope.joinTravel = function(){
  309. };
  310. })
  311. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  312. $scope.users="";
  313. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  314. $scope.doRefresh = function() {
  315. $http.get(urlapi + 'users')
  316. .success(function(data, status, headers, config){
  317. console.log('data success');
  318. console.log(data); // for browser console
  319. $scope.users = data; // for UI
  320. localStorage.setItem('c_users', JSON.stringify($scope.users));
  321. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  322. })
  323. .error(function(data, status, headers,config){
  324. console.log('data error');
  325. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  326. })
  327. .then(function(result){
  328. users = result.data;
  329. });
  330. };
  331. })
  332. .controller('UserCtrl', function($scope, $stateParams, $http) {
  333. //$scope.user="";
  334. console.log($stateParams.username);
  335. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  336. .success(function(data, status, headers,config){
  337. console.log('data success');
  338. console.log(data); // for browser console
  339. $scope.user = data; // for UI
  340. })
  341. .error(function(data, status, headers,config){
  342. console.log('data error');
  343. })
  344. .then(function(result){
  345. user = result.data;
  346. });
  347. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  348. .success(function(data, status, headers,config){
  349. console.log('data success');
  350. console.log(data); // for browser console
  351. $scope.travels = data; // for UI
  352. })
  353. .error(function(data, status, headers,config){
  354. console.log('data error');
  355. })
  356. .then(function(result){
  357. travels = result.data;
  358. });
  359. });