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.

624 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. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  183. })
  184. .error(function(data, status, headers,config){
  185. console.log('data error');
  186. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  187. $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
  188. })
  189. .then(function(result){
  190. travels = result.data;
  191. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  192. });
  193. };
  194. $scope.newtravel={};
  195. /*$scope.newtravel={
  196. title: "prova",
  197. from: "prova",
  198. to: "prova",
  199. seats: 3,
  200. package: true,
  201. phone: 123,
  202. telegram: "telusr",
  203. description: "this is the description of prova"
  204. };*/
  205. // Create the login modal that we will use later
  206. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  207. scope: $scope
  208. }).then(function(modal) {
  209. $scope.modalOffering = modal;
  210. });
  211. // Create the login modal that we will use later
  212. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  213. scope: $scope
  214. }).then(function(modal) {
  215. $scope.modalAsking = modal;
  216. });
  217. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  218. scope: $scope
  219. }).then(function(modal) {
  220. $scope.modalPackage = modal;
  221. });
  222. // Triggered in the login modal to close it
  223. $scope.closeNewOfferingTravel = function() {
  224. $scope.modalOffering.hide();
  225. };
  226. // Triggered in the login modal to close it
  227. $scope.closeNewAskingTravel = function() {
  228. $scope.modalAsking.hide();
  229. };
  230. $scope.closeNewAskingPackage = function() {
  231. $scope.modalPackage.hide();
  232. };
  233. // Open the login modal
  234. $scope.showNewOfferingTravel = function() {
  235. $scope.modalOffering.show();
  236. };
  237. // Open the login modal
  238. $scope.showNewAskingTravel = function() {
  239. $scope.modalAsking.show();
  240. };
  241. $scope.showNewAskingPackage = function() {
  242. $scope.modalPackage.show();
  243. };
  244. // Perform the login action when the user submits the login form
  245. $scope.doNewOfferingTravel = function() {
  246. console.log('Doing new travel', $scope.newtravel);
  247. $scope.newtravel.icon="lorry";
  248. $scope.newtravel.generateddate=$scope.newtravel.date;
  249. $scope.newtravel.owner=localStorage.getItem("c_username");
  250. $scope.newtravel.modality="offering";
  251. //$scope.newtravel.token=localStorage.getItem("c_token");
  252. console.log($scope.newtravel);
  253. $http({
  254. url: urlapi + 'travels',
  255. method: "POST",
  256. data: $scope.newtravel
  257. })
  258. .then(function(response) {
  259. // success
  260. console.log("response: ");
  261. console.log(response);
  262. $scope.newtravel._id=response.data._id;
  263. $scope.travels.push($scope.newtravel);
  264. $scope.newtravel={};
  265. if(response.data.success==false){
  266. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  267. }
  268. },
  269. function(response) { // optional
  270. // failed
  271. });
  272. // Simulate a login delay. Remove this and replace with your login
  273. // code if using a login system
  274. $timeout(function() {
  275. $scope.closeNewOfferingTravel();
  276. }, 1000);
  277. };
  278. $scope.doNewAskingTravel = function() {
  279. console.log('Doing new travel', $scope.newtravel);
  280. $scope.newtravel.icon="lorry";
  281. $scope.newtravel.generateddate=$scope.newtravel.date;
  282. $scope.newtravel.owner=localStorage.getItem("c_username");
  283. $scope.newtravel.modality="asking";
  284. console.log($scope.newtravel);
  285. $http({
  286. url: urlapi + 'travels',
  287. method: "POST",
  288. data: $scope.newtravel
  289. })
  290. .then(function(response) {
  291. // success
  292. console.log("response: ");
  293. console.log(response);
  294. $scope.newtravel._id=response.data._id;
  295. $scope.travels.push($scope.newtravel);
  296. if(response.data.success==false){
  297. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  298. }
  299. },
  300. function(response) { // optional
  301. // failed
  302. });
  303. // Simulate a login delay. Remove this and replace with your login
  304. // code if using a login system
  305. $timeout(function() {
  306. $scope.closeNewAskingTravel();
  307. }, 1000);
  308. };
  309. $scope.doNewAskingPackage = function() {
  310. console.log('Doing new package', $scope.newtravel);
  311. $scope.newtravel.icon="lorry";
  312. $scope.newtravel.generateddate=$scope.newtravel.date;
  313. $scope.newtravel.owner=localStorage.getItem("c_username");
  314. $scope.newtravel.package=true;
  315. $scope.newtravel.modality="package";
  316. console.log($scope.newtravel);
  317. $http({
  318. url: urlapi + 'travels',
  319. method: "POST",
  320. data: $scope.newtravel
  321. })
  322. .then(function(response) {
  323. // success
  324. console.log("response: ");
  325. console.log(response);
  326. $scope.newtravel._id=response.data._id;
  327. $scope.travels.push($scope.newtravel);
  328. if(response.data.success==false){
  329. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  330. }
  331. },
  332. function(response) { // optional
  333. // failed
  334. });
  335. // Simulate a login delay. Remove this and replace with your login
  336. // code if using a login system
  337. $timeout(function() {
  338. $scope.closeNewAskingPackage();
  339. }, 1000);
  340. };
  341. })
  342. .controller('TravelCtrl', function($scope, $stateParams, $http, $ionicModal, $ionicPopup, $filter) {
  343. if(localStorage.getItem('c_token')){// adding token to the headers
  344. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  345. }
  346. $scope.storageusername=localStorage.getItem("c_username");
  347. $scope.travel="";
  348. console.log($stateParams.travelId);
  349. /*$http.get(urlapi + 'travels/'+$stateParams.travelId)
  350. .success(function(data, status, headers,config){
  351. console.log('data success');
  352. console.log(data); // for browser console
  353. $scope.travel = data; // for UI
  354. console.log("b");
  355. console.log($scope.travel);
  356. })
  357. .error(function(data, status, headers,config){
  358. console.log('data error');
  359. })
  360. .then(function(result){
  361. travel = result.data;
  362. });*/
  363. $scope.travels= JSON.parse(localStorage.getItem('c_travels'));
  364. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  365. $scope.joins="";
  366. $http.get(urlapi + 'travels/join/'+$stateParams.travelId)
  367. .success(function(data, status, headers,config){
  368. console.log('data success');
  369. console.log(data); // for browser console
  370. $scope.joins = data; // for UI
  371. })
  372. .error(function(data, status, headers,config){
  373. console.log('data error');
  374. })
  375. .then(function(result){
  376. joins = result.data;
  377. });
  378. $http.get(urlapi + 'travels/comment/'+$stateParams.travelId)
  379. .success(function(data, status, headers,config){
  380. console.log(data); // for browser console
  381. $scope.comments = data; // for UI
  382. })
  383. .error(function(data, status, headers,config){
  384. console.log('data error');
  385. })
  386. .then(function(result){
  387. comments = result.data;
  388. });
  389. $scope.deleteTravel = function(){
  390. var confirmPopup = $ionicPopup.confirm({
  391. title: 'Deleting publication',
  392. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  393. });
  394. confirmPopup.then(function(res) {
  395. if(res) {
  396. console.log('You are sure');
  397. console.log("delete travel: " + $stateParams.travelId);
  398. $http({
  399. url: urlapi + 'travels/' + $stateParams.travelId,
  400. method: "DELETE"
  401. })
  402. .then(function(response) {
  403. console.log(response);
  404. },
  405. function(response) { // optional
  406. // failed
  407. });
  408. } else {
  409. console.log('You are not sure');
  410. }
  411. });
  412. };
  413. $scope.joinTravel = function(){
  414. $scope.newjoin={
  415. travelId: $stateParams.travelId,
  416. joinedUserId: localStorage.getItem("c_userid"),
  417. joinedUsername: localStorage.getItem("c_username"),
  418. joinedAvatar: localStorage.getItem("c_avatar")
  419. };
  420. $http({
  421. url: urlapi + 'travels/join/' + $stateParams.travelId,
  422. method: "POST",
  423. data: $scope.newjoin
  424. })
  425. .then(function(response) {
  426. // success
  427. console.log("response: ");
  428. console.log(response);
  429. },
  430. function(response) { // optional
  431. // failed
  432. });
  433. };
  434. $scope.unjoinTravel = function(){
  435. console.log("unjoin");
  436. $scope.unjoin={
  437. travelId: $stateParams.travelId,
  438. joinedUserId: localStorage.getItem("c_userid"),
  439. joinedUsername: localStorage.getItem("c_username"),
  440. joinedAvatar: localStorage.getItem("c_avatar")
  441. };
  442. $http({
  443. url: urlapi + 'travels/unjoin/' + $stateParams.travelId,
  444. method: "POST",
  445. data: $scope.unjoin
  446. })
  447. .then(function(response) {
  448. // success
  449. console.log("response: ");
  450. console.log(response);
  451. },
  452. function(response) { // optional
  453. // failed
  454. });
  455. };
  456. /* adding comment */
  457. $scope.doingNewComment=false;
  458. $scope.newComment={};
  459. $scope.showNewComment = function() {
  460. $scope.doingNewComment=true;
  461. };
  462. $scope.closeNewComment = function() {
  463. $scope.doingNewComment=false;
  464. };
  465. $scope.doNewComment = function() {
  466. $scope.newComment.commentUserId=localStorage.getItem("c_userid");
  467. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  468. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");
  469. console.log($scope.newComment);
  470. $http({
  471. url: urlapi + 'travels/comment/' + $stateParams.travelId,
  472. method: "POST",
  473. data: $scope.newComment
  474. })
  475. .then(function(response) {
  476. // success
  477. console.log("newComment added to server: " + response);
  478. console.log(response);
  479. if(response.data.success==false){
  480. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  481. }
  482. },
  483. function(response) { // optional
  484. // failed
  485. });
  486. $scope.closeNewComment();
  487. };
  488. console.log("a");
  489. console.log($scope.storageusername);
  490. console.log($scope.travel.owner);
  491. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  492. console.log(myArray+", "+searchTerm+", "+property);
  493. if(myArray)
  494. {
  495. for(var i = 0, len = myArray.length; i < len; i++) {
  496. if (myArray[i][property] === searchTerm){
  497. //console.log("i: " + i);
  498. return i;
  499. }
  500. }
  501. }
  502. //console.log("i: -1");
  503. return -1;
  504. }
  505. })
  506. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  507. $scope.users="";
  508. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  509. $scope.doRefresh = function() {
  510. $http.get(urlapi + 'users')
  511. .success(function(data, status, headers, config){
  512. console.log('data success');
  513. console.log(data); // for browser console
  514. $scope.users = data; // for UI
  515. localStorage.setItem('c_users', JSON.stringify($scope.users));
  516. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  517. })
  518. .error(function(data, status, headers,config){
  519. console.log('data error');
  520. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  521. })
  522. .then(function(result){
  523. users = result.data;
  524. });
  525. };
  526. })
  527. .controller('UserCtrl', function($scope, $stateParams, $http) {
  528. //$scope.user="";
  529. console.log($stateParams.username);
  530. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  531. .success(function(data, status, headers,config){
  532. console.log('data success');
  533. console.log(data); // for browser console
  534. $scope.user = data; // for UI
  535. })
  536. .error(function(data, status, headers,config){
  537. console.log('data error');
  538. })
  539. .then(function(result){
  540. user = result.data;
  541. });
  542. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  543. .success(function(data, status, headers,config){
  544. console.log('data success');
  545. console.log(data); // for browser console
  546. $scope.travels = data; // for UI
  547. })
  548. .error(function(data, status, headers,config){
  549. console.log('data error');
  550. })
  551. .then(function(result){
  552. travels = result.data;
  553. });
  554. }).controller('SettingsCtrl', function($scope, $stateParams, $translate) {
  555. if(localStorage.getItem('lang'))//initialization
  556. {
  557. $scope.lang=localStorage.getItem('lang');
  558. }else{
  559. localStorage.setItem('lang', 'english');
  560. $scope.lang=localStorage.getItem('lang');
  561. }
  562. $scope.langChange = function(lang){
  563. console.log(lang);
  564. window.localStorage.setItem('lang', lang);
  565. $translate.use(lang);
  566. };
  567. });