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.

540 lines
17 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. $scope.storageavatar=localStorage.getItem("c_avatar");
  18. // Create the login modal that we will use later
  19. $ionicModal.fromTemplateUrl('templates/login.html', {
  20. scope: $scope
  21. }).then(function(modal) {
  22. $scope.modalLogin = modal;
  23. });
  24. $ionicModal.fromTemplateUrl('templates/signup.html', {
  25. scope: $scope
  26. }).then(function(modal) {
  27. $scope.modalSignup = modal;
  28. });
  29. // Triggered in the login modal to close it
  30. $scope.closeLogin = function() {
  31. $scope.modalLogin.hide();
  32. };
  33. $scope.closeSignup = function() {
  34. $scope.modalSignup.hide();
  35. };
  36. // Open the login modal
  37. $scope.login = function() {
  38. $scope.modalLogin.show();
  39. };
  40. $scope.signup = function() {
  41. $scope.modalSignup.show();
  42. };
  43. // Perform the login action when the user submits the login form
  44. $scope.doLogin = function() {
  45. console.log('Doing login', $scope.loginData);
  46. $http({
  47. url: urlapi + 'auth',
  48. method: "POST",
  49. data: $scope.loginData
  50. })
  51. .then(function(response) {
  52. // success
  53. console.log("response: ");
  54. console.log(response.data);
  55. if(response.data.success==true)
  56. {
  57. console.log("login successful");
  58. localStorage.setItem("c_username", $scope.loginData.username);
  59. localStorage.setItem("c_token", response.data.token);
  60. localStorage.setItem("c_userid", response.data.userid);
  61. localStorage.setItem("c_avatar", response.data.avatar);
  62. }else{
  63. console.log("login failed");
  64. }
  65. $timeout(function() {
  66. $scope.closeLogin();
  67. $window.location.reload(true);
  68. }, 1000);
  69. },
  70. function(response) { // optional
  71. // failed
  72. console.log(response);
  73. });
  74. };
  75. $scope.doSignup = function() {
  76. console.log('Doing signup', $scope.signupData);
  77. $http({
  78. url: urlapi + 'users',
  79. method: "POST",
  80. data: $scope.signupData
  81. })
  82. .then(function(response) {
  83. // success
  84. console.log("response: ");
  85. console.log(response.data);
  86. $timeout(function() {
  87. $scope.closeSignup();
  88. }, 1000);
  89. },
  90. function(response) { // optional
  91. // failed
  92. });
  93. };
  94. $scope.logout = function(){
  95. localStorage.removeItem("c_username");
  96. localStorage.removeItem("c_token");
  97. localStorage.removeItem("c_avatar");
  98. localStorage.removeItem("c_userid");
  99. $window.location.reload(true);
  100. };
  101. })
  102. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout, $ionicLoading) {
  103. if(localStorage.getItem('c_token')){// adding token to the headers
  104. //console.log("token added to headers in run module");
  105. //console.log($http.defaults);
  106. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('c_token');
  107. //$http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";
  108. //console.log($http.defaults.headers);
  109. }
  110. /*if(localStorage.getItem('c_token')){// adding token to the headers
  111. $http.defaults.headers.token = localStorage.getItem('c_token');
  112. }*/
  113. $scope.travels="";
  114. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  115. $scope.doRefresh = function() {
  116. $http.get(urlapi + 'travels')
  117. .success(function(data, status, headers,config){
  118. console.log('data success');
  119. console.log(data); // for browser console
  120. $scope.travels = data; // for UI
  121. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  122. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  123. })
  124. .error(function(data, status, headers,config){
  125. console.log('data error');
  126. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  127. })
  128. .then(function(result){
  129. travels = result.data;
  130. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  131. });
  132. };
  133. $scope.newtravel={};
  134. /*$scope.newtravel={
  135. title: "prova",
  136. from: "prova",
  137. to: "prova",
  138. seats: 3,
  139. package: true,
  140. phone: 123,
  141. telegram: "telusr",
  142. description: "this is the description of prova"
  143. };*/
  144. // Create the login modal that we will use later
  145. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  146. scope: $scope
  147. }).then(function(modal) {
  148. $scope.modalOffering = modal;
  149. });
  150. // Create the login modal that we will use later
  151. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  152. scope: $scope
  153. }).then(function(modal) {
  154. $scope.modalAsking = modal;
  155. });
  156. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  157. scope: $scope
  158. }).then(function(modal) {
  159. $scope.modalPackage = modal;
  160. });
  161. // Triggered in the login modal to close it
  162. $scope.closeNewOfferingTravel = function() {
  163. $scope.modalOffering.hide();
  164. };
  165. // Triggered in the login modal to close it
  166. $scope.closeNewAskingTravel = function() {
  167. $scope.modalAsking.hide();
  168. };
  169. $scope.closeNewAskingPackage = function() {
  170. $scope.modalPackage.hide();
  171. };
  172. // Open the login modal
  173. $scope.showNewOfferingTravel = function() {
  174. $scope.modalOffering.show();
  175. };
  176. // Open the login modal
  177. $scope.showNewAskingTravel = function() {
  178. $scope.modalAsking.show();
  179. };
  180. $scope.showNewAskingPackage = function() {
  181. $scope.modalPackage.show();
  182. };
  183. // Perform the login action when the user submits the login form
  184. $scope.doNewOfferingTravel = function() {
  185. console.log('Doing new travel', $scope.newtravel);
  186. $scope.newtravel.icon="lorry";
  187. $scope.newtravel.generateddate=$scope.newtravel.date;
  188. $scope.newtravel.owner=localStorage.getItem("c_username");
  189. $scope.newtravel.modality="offering";
  190. //$scope.newtravel.token=localStorage.getItem("c_token");
  191. console.log($scope.newtravel);
  192. $http({
  193. url: urlapi + 'travels',
  194. method: "POST",
  195. data: $scope.newtravel
  196. })
  197. .then(function(response) {
  198. // success
  199. console.log("response: ");
  200. console.log(response);
  201. $scope.newtravel._id=response.data._id;
  202. $scope.travels.push($scope.newtravel);
  203. $scope.newtravel={};
  204. if(response.data.success==false){
  205. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  206. }
  207. },
  208. function(response) { // optional
  209. // failed
  210. });
  211. // Simulate a login delay. Remove this and replace with your login
  212. // code if using a login system
  213. $timeout(function() {
  214. $scope.closeNewOfferingTravel();
  215. }, 1000);
  216. };
  217. $scope.doNewAskingTravel = function() {
  218. console.log('Doing new travel', $scope.newtravel);
  219. $scope.newtravel.icon="lorry";
  220. $scope.newtravel.generateddate=$scope.newtravel.date;
  221. $scope.newtravel.owner=localStorage.getItem("c_username");
  222. $scope.newtravel.modality="asking";
  223. console.log($scope.newtravel);
  224. $http({
  225. url: urlapi + 'travels',
  226. method: "POST",
  227. data: $scope.newtravel
  228. })
  229. .then(function(response) {
  230. // success
  231. console.log("response: ");
  232. console.log(response);
  233. $scope.newtravel._id=response.data._id;
  234. $scope.travels.push($scope.newtravel);
  235. if(response.data.success==false){
  236. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  237. }
  238. },
  239. function(response) { // optional
  240. // failed
  241. });
  242. // Simulate a login delay. Remove this and replace with your login
  243. // code if using a login system
  244. $timeout(function() {
  245. $scope.closeNewAskingTravel();
  246. }, 1000);
  247. };
  248. $scope.doNewAskingPackage = function() {
  249. console.log('Doing new package', $scope.newtravel);
  250. $scope.newtravel.icon="lorry";
  251. $scope.newtravel.generateddate=$scope.newtravel.date;
  252. $scope.newtravel.owner=localStorage.getItem("c_username");
  253. $scope.newtravel.package=true;
  254. $scope.newtravel.modality="package";
  255. console.log($scope.newtravel);
  256. $http({
  257. url: urlapi + 'travels',
  258. method: "POST",
  259. data: $scope.newtravel
  260. })
  261. .then(function(response) {
  262. // success
  263. console.log("response: ");
  264. console.log(response);
  265. $scope.newtravel._id=response.data._id;
  266. $scope.travels.push($scope.newtravel);
  267. if(response.data.success==false){
  268. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  269. }
  270. },
  271. function(response) { // optional
  272. // failed
  273. });
  274. // Simulate a login delay. Remove this and replace with your login
  275. // code if using a login system
  276. $timeout(function() {
  277. $scope.closeNewAskingPackage();
  278. }, 1000);
  279. };
  280. })
  281. .controller('TravelCtrl', function($scope, $stateParams, $http, $ionicModal, $ionicPopup) {
  282. if(localStorage.getItem('c_token')){// adding token to the headers
  283. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  284. }
  285. $scope.storageusername=localStorage.getItem("c_username");
  286. $scope.travel="";
  287. console.log($stateParams.travelId);
  288. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  289. .success(function(data, status, headers,config){
  290. console.log('data success');
  291. console.log(data); // for browser console
  292. $scope.travel = data; // for UI
  293. })
  294. .error(function(data, status, headers,config){
  295. console.log('data error');
  296. })
  297. .then(function(result){
  298. travel = result.data;
  299. });
  300. $http.get(urlapi + 'travels/join/'+$stateParams.travelId)
  301. .success(function(data, status, headers,config){
  302. console.log('data success');
  303. console.log(data); // for browser console
  304. $scope.joins = data; // for UI
  305. })
  306. .error(function(data, status, headers,config){
  307. console.log('data error');
  308. })
  309. .then(function(result){
  310. joins = result.data;
  311. });
  312. $http.get(urlapi + 'travels/comment/'+$stateParams.travelId)
  313. .success(function(data, status, headers,config){
  314. console.log(data); // for browser console
  315. $scope.comments = data; // for UI
  316. })
  317. .error(function(data, status, headers,config){
  318. console.log('data error');
  319. })
  320. .then(function(result){
  321. comments = result.data;
  322. });
  323. $scope.deleteTravel = function(){
  324. var confirmPopup = $ionicPopup.confirm({
  325. title: 'Deleting publication',
  326. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  327. });
  328. confirmPopup.then(function(res) {
  329. if(res) {
  330. console.log('You are sure');
  331. console.log("delete travel: " + $stateParams.travelId);
  332. $http({
  333. url: urlapi + 'travels/' + $stateParams.travelId,
  334. method: "DELETE"
  335. })
  336. .then(function(response) {
  337. console.log(response);
  338. },
  339. function(response) { // optional
  340. // failed
  341. });
  342. } else {
  343. console.log('You are not sure');
  344. }
  345. });
  346. };
  347. $scope.joinTravel = function(){
  348. $scope.newjoin={
  349. travelId: $stateParams.travelId,
  350. joinedUserId: localStorage.getItem("c_userid"),
  351. joinedUsername: localStorage.getItem("c_username"),
  352. joinedAvatar: localStorage.getItem("c_avatar")
  353. };
  354. $http({
  355. url: urlapi + 'travels/join/' + $stateParams.travelId,
  356. method: "POST",
  357. data: $scope.newjoin
  358. })
  359. .then(function(response) {
  360. // success
  361. console.log("response: ");
  362. console.log(response);
  363. },
  364. function(response) { // optional
  365. // failed
  366. });
  367. };
  368. $scope.unjoinTravel = function(){
  369. console.log("unjoin");
  370. $scope.unjoin={
  371. travelId: $stateParams.travelId,
  372. joinedUserId: localStorage.getItem("c_userid"),
  373. joinedUsername: localStorage.getItem("c_username"),
  374. joinedAvatar: localStorage.getItem("c_avatar")
  375. };
  376. $http({
  377. url: urlapi + 'travels/unjoin/' + $stateParams.travelId,
  378. method: "POST",
  379. data: $scope.unjoin
  380. })
  381. .then(function(response) {
  382. // success
  383. console.log("response: ");
  384. console.log(response);
  385. },
  386. function(response) { // optional
  387. // failed
  388. });
  389. };
  390. /* adding comment */
  391. $scope.doingNewComment=false;
  392. $scope.newComment={};
  393. $scope.showNewComment = function() {
  394. $scope.doingNewComment=true;
  395. };
  396. $scope.closeNewComment = function() {
  397. $scope.doingNewComment=false;
  398. };
  399. $scope.doNewComment = function() {
  400. $scope.newComment.commentUserId=localStorage.getItem("c_userid");
  401. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  402. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");
  403. console.log($scope.newComment);
  404. $http({
  405. url: urlapi + 'travels/comment/' + $stateParams.travelId,
  406. method: "POST",
  407. data: $scope.newComment
  408. })
  409. .then(function(response) {
  410. // success
  411. console.log("newComment added to server: " + response);
  412. console.log(response);
  413. if(response.data.success==false){
  414. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  415. }
  416. },
  417. function(response) { // optional
  418. // failed
  419. });
  420. $scope.closeNewComment();
  421. };
  422. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  423. if(myArray)
  424. {
  425. for(var i = 0, len = myArray.length; i < len; i++) {
  426. if (myArray[i][property] === searchTerm){
  427. //console.log("i: " + i);
  428. return i;
  429. }
  430. }
  431. }
  432. //console.log("i: -1");
  433. return -1;
  434. }
  435. })
  436. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  437. $scope.users="";
  438. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  439. $scope.doRefresh = function() {
  440. $http.get(urlapi + 'users')
  441. .success(function(data, status, headers, config){
  442. console.log('data success');
  443. console.log(data); // for browser console
  444. $scope.users = data; // for UI
  445. localStorage.setItem('c_users', JSON.stringify($scope.users));
  446. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  447. })
  448. .error(function(data, status, headers,config){
  449. console.log('data error');
  450. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  451. })
  452. .then(function(result){
  453. users = result.data;
  454. });
  455. };
  456. })
  457. .controller('UserCtrl', function($scope, $stateParams, $http) {
  458. //$scope.user="";
  459. console.log($stateParams.username);
  460. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  461. .success(function(data, status, headers,config){
  462. console.log('data success');
  463. console.log(data); // for browser console
  464. $scope.user = data; // for UI
  465. })
  466. .error(function(data, status, headers,config){
  467. console.log('data error');
  468. })
  469. .then(function(result){
  470. user = result.data;
  471. });
  472. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  473. .success(function(data, status, headers,config){
  474. console.log('data success');
  475. console.log(data); // for browser console
  476. $scope.travels = data; // for UI
  477. })
  478. .error(function(data, status, headers,config){
  479. console.log('data error');
  480. })
  481. .then(function(result){
  482. travels = result.data;
  483. });
  484. });