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.

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