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.

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