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.

629 lines
20 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', ['pascalprecht.translate'])
  6. .controller('AppCtrl', function($scope, $ionicModal, $timeout, $http, $window, $ionicLoading) {
  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. if($scope.emptyParams($scope.signupData))
  78. {
  79. $http({
  80. url: urlapi + 'users',
  81. method: "POST",
  82. data: $scope.signupData
  83. })
  84. .then(function(response) {
  85. // success
  86. console.log("response: ");
  87. console.log(response.data);
  88. $timeout(function() {
  89. $scope.closeSignup();
  90. $scope.login();
  91. }, 1000);
  92. },
  93. function(response) { // optional
  94. // failed
  95. $ionicLoading.show({ template: 'Username already taken', noBackdrop: true, duration: 2000 });
  96. });
  97. }else{
  98. $ionicLoading.show({ template: 'First complete all parameters', noBackdrop: true, duration: 2000 });
  99. }
  100. };
  101. $scope.emptyParams = function(obj){
  102. if(obj.username==undefined)
  103. {
  104. return(false);
  105. }
  106. if(obj.password==undefined)
  107. {
  108. return(false);
  109. }
  110. if(obj.mail==undefined)
  111. {
  112. return(false);
  113. }
  114. if(obj.phone==undefined)
  115. {
  116. return(false);
  117. }
  118. if(obj.avatar==undefined)
  119. {
  120. return(false);
  121. }
  122. return(true);
  123. };
  124. $scope.avatars=[
  125. "turtle",
  126. "cat",
  127. "toucan",
  128. "racoon",
  129. "tiger",
  130. "squirrel",
  131. "sheep",
  132. "penguin",
  133. "panda",
  134. "owl",
  135. "pelican",
  136. "whale",
  137. "snake",
  138. "mouse",
  139. "giraffe",
  140. "macaw",
  141. "lion",
  142. "llama",
  143. "kangaroo",
  144. "hen",
  145. "frog",
  146. "clown-fish",
  147. "chameleon",
  148. "octopus"
  149. ];
  150. $scope.avatarSelect = function(avat){
  151. $scope.signupData.avatar=avat;
  152. //alert($scope.signupData.avatar);
  153. };
  154. $scope.logout = function(){
  155. localStorage.removeItem("c_username");
  156. localStorage.removeItem("c_token");
  157. localStorage.removeItem("c_avatar");
  158. localStorage.removeItem("c_userid");
  159. $window.location.reload(true);
  160. };
  161. })
  162. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout, $ionicLoading) {
  163. if(localStorage.getItem('c_token')){// adding token to the headers
  164. //console.log("token added to headers in run module");
  165. //console.log($http.defaults);
  166. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('c_token');
  167. //$http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";
  168. //console.log($http.defaults.headers);
  169. }
  170. /*if(localStorage.getItem('c_token')){// adding token to the headers
  171. $http.defaults.headers.token = localStorage.getItem('c_token');
  172. }*/
  173. $scope.travels="";
  174. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  175. $scope.doRefresh = function() {
  176. $http.get(urlapi + 'travels')
  177. .success(function(data, status, headers,config){
  178. console.log('data success');
  179. console.log(data); // for browser console
  180. $scope.travels = data; // for UI
  181. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  182. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  183. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  184. })
  185. .error(function(data, status, headers,config){
  186. console.log('data error');
  187. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  188. $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
  189. })
  190. .then(function(result){
  191. travels = result.data;
  192. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  193. });
  194. };
  195. $scope.newtravel={};
  196. /*$scope.newtravel={
  197. title: "prova",
  198. from: "prova",
  199. to: "prova",
  200. seats: 3,
  201. package: true,
  202. phone: 123,
  203. telegram: "telusr",
  204. description: "this is the description of prova"
  205. };*/
  206. // Create the login modal that we will use later
  207. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  208. scope: $scope
  209. }).then(function(modal) {
  210. $scope.modalOffering = modal;
  211. });
  212. // Create the login modal that we will use later
  213. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  214. scope: $scope
  215. }).then(function(modal) {
  216. $scope.modalAsking = modal;
  217. });
  218. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  219. scope: $scope
  220. }).then(function(modal) {
  221. $scope.modalPackage = modal;
  222. });
  223. // Triggered in the login modal to close it
  224. $scope.closeNewOfferingTravel = function() {
  225. $scope.modalOffering.hide();
  226. };
  227. // Triggered in the login modal to close it
  228. $scope.closeNewAskingTravel = function() {
  229. $scope.modalAsking.hide();
  230. };
  231. $scope.closeNewAskingPackage = function() {
  232. $scope.modalPackage.hide();
  233. };
  234. // Open the login modal
  235. $scope.showNewOfferingTravel = function() {
  236. $scope.modalOffering.show();
  237. };
  238. // Open the login modal
  239. $scope.showNewAskingTravel = function() {
  240. $scope.modalAsking.show();
  241. };
  242. $scope.showNewAskingPackage = function() {
  243. $scope.modalPackage.show();
  244. };
  245. // Perform the login action when the user submits the login form
  246. $scope.doNewOfferingTravel = function() {
  247. console.log('Doing new travel', $scope.newtravel);
  248. $scope.newtravel.icon="lorry";
  249. $scope.newtravel.generateddate=$scope.newtravel.date;
  250. $scope.newtravel.owner=localStorage.getItem("c_username");
  251. $scope.newtravel.modality="offering";
  252. //$scope.newtravel.token=localStorage.getItem("c_token");
  253. console.log($scope.newtravel);
  254. $http({
  255. url: urlapi + 'travels',
  256. method: "POST",
  257. data: $scope.newtravel
  258. })
  259. .then(function(response) {
  260. // success
  261. console.log("response: ");
  262. console.log(response);
  263. //$scope.newtravel._id=response.data._id;
  264. //$scope.travels.push($scope.newtravel);
  265. $scope.travels=response.data;
  266. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  267. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  268. $scope.newtravel={};
  269. if(response.data.success==false){
  270. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  271. }
  272. },
  273. function(response) { // optional
  274. // failed
  275. });
  276. // Simulate a login delay. Remove this and replace with your login
  277. // code if using a login system
  278. $timeout(function() {
  279. $scope.closeNewOfferingTravel();
  280. }, 1000);
  281. };
  282. $scope.doNewAskingTravel = function() {
  283. console.log('Doing new travel', $scope.newtravel);
  284. $scope.newtravel.icon="lorry";
  285. $scope.newtravel.generateddate=$scope.newtravel.date;
  286. $scope.newtravel.owner=localStorage.getItem("c_username");
  287. $scope.newtravel.modality="asking";
  288. console.log($scope.newtravel);
  289. $http({
  290. url: urlapi + 'travels',
  291. method: "POST",
  292. data: $scope.newtravel
  293. })
  294. .then(function(response) {
  295. // success
  296. console.log("response: ");
  297. console.log(response);
  298. //$scope.newtravel._id=response.data._id;
  299. //$scope.travels.push($scope.newtravel);
  300. $scope.travels=response.data;
  301. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  302. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  303. $scope.newtravel={};
  304. if(response.data.success==false){
  305. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  306. }
  307. },
  308. function(response) { // optional
  309. // failed
  310. });
  311. // Simulate a login delay. Remove this and replace with your login
  312. // code if using a login system
  313. $timeout(function() {
  314. $scope.closeNewAskingTravel();
  315. }, 1000);
  316. };
  317. $scope.doNewAskingPackage = function() {
  318. console.log('Doing new package', $scope.newtravel);
  319. $scope.newtravel.icon="lorry";
  320. $scope.newtravel.generateddate=$scope.newtravel.date;
  321. $scope.newtravel.owner=localStorage.getItem("c_username");
  322. $scope.newtravel.package=true;
  323. $scope.newtravel.modality="package";
  324. console.log($scope.newtravel);
  325. $http({
  326. url: urlapi + 'travels',
  327. method: "POST",
  328. data: $scope.newtravel
  329. })
  330. .then(function(response) {
  331. // success
  332. console.log("response: ");
  333. console.log(response);
  334. //$scope.newtravel._id=response.data._id;
  335. //$scope.travels.push($scope.newtravel);
  336. $scope.travels=response.data;
  337. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  338. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  339. $scope.newtravel={};
  340. if(response.data.success==false){
  341. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  342. }
  343. },
  344. function(response) { // optional
  345. // failed
  346. });
  347. // Simulate a login delay. Remove this and replace with your login
  348. // code if using a login system
  349. $timeout(function() {
  350. $scope.closeNewAskingPackage();
  351. }, 1000);
  352. };
  353. })
  354. .controller('TravelCtrl', function($scope, $stateParams, $http, $ionicModal, $ionicPopup, $filter) {
  355. if(localStorage.getItem('c_token')){// adding token to the headers
  356. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  357. }
  358. $scope.storageusername=localStorage.getItem("c_username");
  359. console.log($stateParams.travelId);
  360. $scope.travels= JSON.parse(localStorage.getItem('c_travels'));
  361. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  362. $http.get(urlapi + 'travels/comment/'+$stateParams.travelId)
  363. .success(function(data, status, headers,config){
  364. console.log(data); // for browser console
  365. $scope.comments = data; // for UI
  366. })
  367. .error(function(data, status, headers,config){
  368. console.log('data error');
  369. })
  370. .then(function(result){
  371. comments = result.data;
  372. });
  373. $scope.deleteTravel = function(){
  374. var confirmPopup = $ionicPopup.confirm({
  375. title: 'Deleting publication',
  376. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  377. });
  378. confirmPopup.then(function(res) {
  379. if(res) {
  380. console.log('You are sure');
  381. console.log("delete travel: " + $stateParams.travelId);
  382. $http({
  383. url: urlapi + 'travels/' + $stateParams.travelId,
  384. method: "DELETE"
  385. })
  386. .then(function(response) {
  387. console.log(response);
  388. },
  389. function(response) { // optional
  390. // failed
  391. });
  392. } else {
  393. console.log('You are not sure');
  394. }
  395. });
  396. };
  397. $scope.joinTravel = function(){
  398. $scope.newjoin={
  399. //travelId: $stateParams.travelId,
  400. joinedUserId: localStorage.getItem("c_userid"),
  401. joinedUsername: localStorage.getItem("c_username"),
  402. joinedAvatar: localStorage.getItem("c_avatar")
  403. };
  404. $http({
  405. url: urlapi + 'travels/'+ $stateParams.travelId+'/join',
  406. method: "POST",
  407. data: $scope.newjoin
  408. })
  409. .then(function(response) {
  410. // success
  411. console.log("response: ");
  412. console.log(response);
  413. $scope.travels=response.data;
  414. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  415. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  416. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  417. },
  418. function(response) { // optional
  419. // failed
  420. });
  421. };
  422. $scope.unjoinTravel = function(){
  423. console.log("unjoin");
  424. $scope.unjoin={
  425. travelId: $stateParams.travelId,
  426. joinedUserId: localStorage.getItem("c_userid"),
  427. joinedUsername: localStorage.getItem("c_username"),
  428. joinedAvatar: localStorage.getItem("c_avatar")
  429. };
  430. $http({
  431. url: urlapi + 'travels/'+ $stateParams.travelId+'/unjoin',
  432. method: "POST",
  433. data: $scope.unjoin
  434. })
  435. .then(function(response) {
  436. // success
  437. console.log("response: ");
  438. console.log(response);
  439. $scope.travels=response.data;
  440. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  441. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  442. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  443. },
  444. function(response) { // optional
  445. // failed
  446. });
  447. };
  448. /* adding comment */
  449. $scope.doingNewComment=false;
  450. $scope.newComment={};
  451. $scope.showNewComment = function() {
  452. $scope.doingNewComment=true;
  453. };
  454. $scope.closeNewComment = function() {
  455. $scope.doingNewComment=false;
  456. };
  457. $scope.doNewComment = function() {
  458. $scope.newComment.commentUserId=localStorage.getItem("c_userid");
  459. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  460. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");
  461. console.log($scope.newComment);
  462. $http({
  463. url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
  464. method: "POST",
  465. data: $scope.newComment
  466. })
  467. .then(function(response) {
  468. // success
  469. console.log("newComment added to server: " + response);
  470. console.log(response);
  471. $scope.travels=response.data;
  472. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  473. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  474. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  475. if(response.data.success==false){
  476. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  477. }
  478. },
  479. function(response) { // optional
  480. // failed
  481. });
  482. $scope.closeNewComment();
  483. };
  484. console.log("a");
  485. console.log($scope.storageusername);
  486. console.log($scope.travel.owner);
  487. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  488. //console.log(myArray+", "+searchTerm+", "+property);
  489. if(myArray)
  490. {
  491. for(var i = 0, len = myArray.length; i < len; i++) {
  492. if (myArray[i][property] === searchTerm){
  493. //console.log("i: " + i);
  494. return i;
  495. }
  496. }
  497. }
  498. //console.log("i: -1");
  499. return -1;
  500. }
  501. })
  502. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  503. $scope.users="";
  504. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  505. $scope.doRefresh = function() {
  506. $http.get(urlapi + 'users')
  507. .success(function(data, status, headers, config){
  508. console.log('data success');
  509. console.log(data); // for browser console
  510. $scope.users = data; // for UI
  511. localStorage.setItem('c_users', JSON.stringify($scope.users));
  512. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  513. })
  514. .error(function(data, status, headers,config){
  515. console.log('data error');
  516. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  517. })
  518. .then(function(result){
  519. users = result.data;
  520. });
  521. };
  522. })
  523. .controller('UserCtrl', function($scope, $stateParams, $http) {
  524. //$scope.user="";
  525. console.log($stateParams.username);
  526. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  527. .success(function(data, status, headers,config){
  528. console.log('data success');
  529. console.log(data); // for browser console
  530. $scope.user = data; // for UI
  531. })
  532. .error(function(data, status, headers,config){
  533. console.log('data error');
  534. })
  535. .then(function(result){
  536. user = result.data;
  537. });
  538. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  539. .success(function(data, status, headers,config){
  540. console.log('data success');
  541. console.log(data); // for browser console
  542. $scope.travels = data; // for UI
  543. })
  544. .error(function(data, status, headers,config){
  545. console.log('data error');
  546. })
  547. .then(function(result){
  548. travels = result.data;
  549. });
  550. })
  551. .controller('SettingsCtrl', function($scope, $stateParams, $translate) {
  552. if(localStorage.getItem('lang'))//initialization
  553. {
  554. $scope.lang=localStorage.getItem('lang');
  555. }else{
  556. localStorage.setItem('lang', 'english');
  557. $scope.lang=localStorage.getItem('lang');
  558. }
  559. $scope.langChange = function(lang){
  560. console.log(lang);
  561. window.localStorage.setItem('lang', lang);
  562. $translate.use(lang);
  563. };
  564. })
  565. .controller('HelpCtrl', function($scope, $stateParams, $translate) {
  566. console.log("help page");
  567. });