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.

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