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.

618 lines
19 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. },
  417. function(response) { // optional
  418. // failed
  419. });
  420. };
  421. $scope.unjoinTravel = function(){
  422. console.log("unjoin");
  423. $scope.unjoin={
  424. travelId: $stateParams.travelId,
  425. joinedUserId: localStorage.getItem("c_userid"),
  426. joinedUsername: localStorage.getItem("c_username"),
  427. joinedAvatar: localStorage.getItem("c_avatar")
  428. };
  429. $http({
  430. url: urlapi + 'travels/'+ $stateParams.travelId+'/unjoin',
  431. method: "POST",
  432. data: $scope.unjoin
  433. })
  434. .then(function(response) {
  435. // success
  436. console.log("response: ");
  437. console.log(response);
  438. $scope.travels=response.data;
  439. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  440. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  441. },
  442. function(response) { // optional
  443. // failed
  444. });
  445. };
  446. /* adding comment */
  447. $scope.doingNewComment=false;
  448. $scope.newComment={};
  449. $scope.showNewComment = function() {
  450. $scope.doingNewComment=true;
  451. };
  452. $scope.closeNewComment = function() {
  453. $scope.doingNewComment=false;
  454. };
  455. $scope.doNewComment = function() {
  456. $scope.newComment.commentUserId=localStorage.getItem("c_userid");
  457. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  458. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");
  459. console.log($scope.newComment);
  460. $http({
  461. url: urlapi + 'travels/comment/' + $stateParams.travelId,
  462. method: "POST",
  463. data: $scope.newComment
  464. })
  465. .then(function(response) {
  466. // success
  467. console.log("newComment added to server: " + response);
  468. console.log(response);
  469. if(response.data.success==false){
  470. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  471. }
  472. },
  473. function(response) { // optional
  474. // failed
  475. });
  476. $scope.closeNewComment();
  477. };
  478. console.log("a");
  479. console.log($scope.storageusername);
  480. console.log($scope.travel.owner);
  481. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  482. //console.log(myArray+", "+searchTerm+", "+property);
  483. if(myArray)
  484. {
  485. for(var i = 0, len = myArray.length; i < len; i++) {
  486. if (myArray[i][property] === searchTerm){
  487. //console.log("i: " + i);
  488. return i;
  489. }
  490. }
  491. }
  492. //console.log("i: -1");
  493. return -1;
  494. }
  495. })
  496. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  497. $scope.users="";
  498. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  499. $scope.doRefresh = function() {
  500. $http.get(urlapi + 'users')
  501. .success(function(data, status, headers, config){
  502. console.log('data success');
  503. console.log(data); // for browser console
  504. $scope.users = data; // for UI
  505. localStorage.setItem('c_users', JSON.stringify($scope.users));
  506. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  507. })
  508. .error(function(data, status, headers,config){
  509. console.log('data error');
  510. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  511. })
  512. .then(function(result){
  513. users = result.data;
  514. });
  515. };
  516. })
  517. .controller('UserCtrl', function($scope, $stateParams, $http) {
  518. //$scope.user="";
  519. console.log($stateParams.username);
  520. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  521. .success(function(data, status, headers,config){
  522. console.log('data success');
  523. console.log(data); // for browser console
  524. $scope.user = data; // for UI
  525. })
  526. .error(function(data, status, headers,config){
  527. console.log('data error');
  528. })
  529. .then(function(result){
  530. user = result.data;
  531. });
  532. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  533. .success(function(data, status, headers,config){
  534. console.log('data success');
  535. console.log(data); // for browser console
  536. $scope.travels = data; // for UI
  537. })
  538. .error(function(data, status, headers,config){
  539. console.log('data error');
  540. })
  541. .then(function(result){
  542. travels = result.data;
  543. });
  544. }).controller('SettingsCtrl', function($scope, $stateParams, $translate) {
  545. if(localStorage.getItem('lang'))//initialization
  546. {
  547. $scope.lang=localStorage.getItem('lang');
  548. }else{
  549. localStorage.setItem('lang', 'english');
  550. $scope.lang=localStorage.getItem('lang');
  551. }
  552. $scope.langChange = function(lang){
  553. console.log(lang);
  554. window.localStorage.setItem('lang', lang);
  555. $translate.use(lang);
  556. };
  557. });