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.

505 lines
16 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', [])
  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. // Create the login modal that we will use later
  18. $ionicModal.fromTemplateUrl('templates/login.html', {
  19. scope: $scope
  20. }).then(function(modal) {
  21. $scope.modalLogin = modal;
  22. });
  23. $ionicModal.fromTemplateUrl('templates/signup.html', {
  24. scope: $scope
  25. }).then(function(modal) {
  26. $scope.modalSignup = modal;
  27. });
  28. // Triggered in the login modal to close it
  29. $scope.closeLogin = function() {
  30. $scope.modalLogin.hide();
  31. };
  32. $scope.closeSignup = function() {
  33. $scope.modalSignup.hide();
  34. };
  35. // Open the login modal
  36. $scope.login = function() {
  37. $scope.modalLogin.show();
  38. };
  39. $scope.signup = function() {
  40. $scope.modalSignup.show();
  41. };
  42. // Perform the login action when the user submits the login form
  43. $scope.doLogin = function() {
  44. console.log('Doing login', $scope.loginData);
  45. $http({
  46. url: urlapi + 'auth',
  47. method: "POST",
  48. data: $scope.loginData
  49. })
  50. .then(function(response) {
  51. // success
  52. console.log("response: ");
  53. console.log(response.data);
  54. if(response.data.success==true)
  55. {
  56. console.log("login successful");
  57. localStorage.setItem("c_username", $scope.loginData.username);
  58. localStorage.setItem("c_token", response.data.token);
  59. localStorage.setItem("c_userid", response.data.userid);
  60. localStorage.setItem("c_avatar", response.data.avatar);
  61. }else{
  62. console.log("login failed");
  63. }
  64. $timeout(function() {
  65. $scope.closeLogin();
  66. $window.location.reload(true);
  67. }, 1000);
  68. },
  69. function(response) { // optional
  70. // failed
  71. console.log(response);
  72. });
  73. };
  74. $scope.doSignup = function() {
  75. console.log('Doing signup', $scope.signupData);
  76. $http({
  77. url: urlapi + 'users',
  78. method: "POST",
  79. data: $scope.signupData
  80. })
  81. .then(function(response) {
  82. // success
  83. console.log("response: ");
  84. console.log(response.data);
  85. $timeout(function() {
  86. $scope.closeSignup();
  87. }, 1000);
  88. },
  89. function(response) { // optional
  90. // failed
  91. });
  92. };
  93. $scope.logout = function(){
  94. localStorage.removeItem("c_username");
  95. localStorage.removeItem("c_token");
  96. localStorage.removeItem("c_avatar");
  97. localStorage.removeItem("c_userid");
  98. $window.location.reload(true);
  99. };
  100. })
  101. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout, $ionicLoading) {
  102. if(localStorage.getItem('c_token')){// adding token to the headers
  103. //console.log("token added to headers in run module");
  104. //console.log($http.defaults);
  105. $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('c_token');
  106. //$http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";
  107. //console.log($http.defaults.headers);
  108. }
  109. /*if(localStorage.getItem('c_token')){// adding token to the headers
  110. $http.defaults.headers.token = localStorage.getItem('c_token');
  111. }*/
  112. $scope.travels="";
  113. $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
  114. $scope.doRefresh = function() {
  115. $http.get(urlapi + 'travels')
  116. .success(function(data, status, headers,config){
  117. console.log('data success');
  118. console.log(data); // for browser console
  119. $scope.travels = data; // for UI
  120. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  121. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  122. })
  123. .error(function(data, status, headers,config){
  124. console.log('data error');
  125. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  126. })
  127. .then(function(result){
  128. travels = result.data;
  129. $ionicLoading.show({ template: 'Travels actualized from server!', noBackdrop: true, duration: 2000 });
  130. });
  131. };
  132. $scope.newtravel={};
  133. /*$scope.newtravel={
  134. title: "prova",
  135. from: "prova",
  136. to: "prova",
  137. seats: 3,
  138. package: true,
  139. phone: 123,
  140. telegram: "telusr",
  141. description: "this is the description of prova"
  142. };*/
  143. // Create the login modal that we will use later
  144. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  145. scope: $scope
  146. }).then(function(modal) {
  147. $scope.modalOffering = modal;
  148. });
  149. // Create the login modal that we will use later
  150. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  151. scope: $scope
  152. }).then(function(modal) {
  153. $scope.modalAsking = modal;
  154. });
  155. $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
  156. scope: $scope
  157. }).then(function(modal) {
  158. $scope.modalPackage = modal;
  159. });
  160. // Triggered in the login modal to close it
  161. $scope.closeNewOfferingTravel = function() {
  162. $scope.modalOffering.hide();
  163. };
  164. // Triggered in the login modal to close it
  165. $scope.closeNewAskingTravel = function() {
  166. $scope.modalAsking.hide();
  167. };
  168. $scope.closeNewAskingPackage = function() {
  169. $scope.modalPackage.hide();
  170. };
  171. // Open the login modal
  172. $scope.showNewOfferingTravel = function() {
  173. $scope.modalOffering.show();
  174. };
  175. // Open the login modal
  176. $scope.showNewAskingTravel = function() {
  177. $scope.modalAsking.show();
  178. };
  179. $scope.showNewAskingPackage = function() {
  180. $scope.modalPackage.show();
  181. };
  182. // Perform the login action when the user submits the login form
  183. $scope.doNewOfferingTravel = function() {
  184. console.log('Doing new travel', $scope.newtravel);
  185. $scope.newtravel.icon="lorry";
  186. $scope.newtravel.generateddate=$scope.newtravel.date;
  187. $scope.newtravel.owner=localStorage.getItem("c_username");
  188. $scope.newtravel.modality="offering";
  189. //$scope.newtravel.token=localStorage.getItem("c_token");
  190. console.log($scope.newtravel);
  191. $http({
  192. url: urlapi + 'travels',
  193. method: "POST",
  194. data: $scope.newtravel
  195. })
  196. .then(function(response) {
  197. // success
  198. console.log("response: ");
  199. console.log(response);
  200. $scope.newtravel._id=response.data._id;
  201. $scope.travels.push($scope.newtravel);
  202. $scope.newtravel={};
  203. if(response.data.success==false){
  204. $ionicLoading.show({ template: 'failed to generate new travel', noBackdrop: true, duration: 2000 });
  205. }
  206. },
  207. function(response) { // optional
  208. // failed
  209. });
  210. // Simulate a login delay. Remove this and replace with your login
  211. // code if using a login system
  212. $timeout(function() {
  213. $scope.closeNewOfferingTravel();
  214. }, 1000);
  215. };
  216. $scope.doNewAskingTravel = function() {
  217. console.log('Doing new travel', $scope.newtravel);
  218. $scope.newtravel.icon="lorry";
  219. $scope.newtravel.generateddate=$scope.newtravel.date;
  220. $scope.newtravel.owner=localStorage.getItem("c_username");
  221. $scope.newtravel.modality="asking";
  222. console.log($scope.newtravel);
  223. $http({
  224. url: urlapi + 'travels',
  225. method: "POST",
  226. data: $scope.newtravel
  227. })
  228. .then(function(response) {
  229. // success
  230. console.log("response: ");
  231. console.log(response);
  232. $scope.newtravel._id=response.data._id;
  233. $scope.travels.push($scope.newtravel);
  234. if(response.data.success==false){
  235. $ionicLoading.show({ template: 'failed to generate new asking travel', noBackdrop: true, duration: 2000 });
  236. }
  237. },
  238. function(response) { // optional
  239. // failed
  240. });
  241. // Simulate a login delay. Remove this and replace with your login
  242. // code if using a login system
  243. $timeout(function() {
  244. $scope.closeNewAskingTravel();
  245. }, 1000);
  246. };
  247. $scope.doNewAskingPackage = function() {
  248. console.log('Doing new package', $scope.newtravel);
  249. $scope.newtravel.icon="lorry";
  250. $scope.newtravel.generateddate=$scope.newtravel.date;
  251. $scope.newtravel.owner=localStorage.getItem("c_username");
  252. $scope.newtravel.package=true;
  253. $scope.newtravel.modality="package";
  254. console.log($scope.newtravel);
  255. $http({
  256. url: urlapi + 'travels',
  257. method: "POST",
  258. data: $scope.newtravel
  259. })
  260. .then(function(response) {
  261. // success
  262. console.log("response: ");
  263. console.log(response);
  264. $scope.newtravel._id=response.data._id;
  265. $scope.travels.push($scope.newtravel);
  266. if(response.data.success==false){
  267. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  268. }
  269. },
  270. function(response) { // optional
  271. // failed
  272. });
  273. // Simulate a login delay. Remove this and replace with your login
  274. // code if using a login system
  275. $timeout(function() {
  276. $scope.closeNewAskingPackage();
  277. }, 1000);
  278. };
  279. })
  280. .controller('TravelCtrl', function($scope, $stateParams, $http, $ionicModal) {
  281. if(localStorage.getItem('c_token')){// adding token to the headers
  282. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  283. }
  284. $scope.storageusername=localStorage.getItem("c_username");
  285. $scope.travel="";
  286. console.log($stateParams.travelId);
  287. $http.get(urlapi + 'travels/'+$stateParams.travelId)
  288. .success(function(data, status, headers,config){
  289. console.log('data success');
  290. console.log(data); // for browser console
  291. $scope.travel = data; // for UI
  292. })
  293. .error(function(data, status, headers,config){
  294. console.log('data error');
  295. })
  296. .then(function(result){
  297. travel = result.data;
  298. });
  299. $http.get(urlapi + 'travels/join/'+$stateParams.travelId)
  300. .success(function(data, status, headers,config){
  301. console.log('data success');
  302. console.log(data); // for browser console
  303. $scope.joins = data; // for UI
  304. })
  305. .error(function(data, status, headers,config){
  306. console.log('data error');
  307. })
  308. .then(function(result){
  309. joins = result.data;
  310. });
  311. $http.get(urlapi + 'travels/comment/'+$stateParams.travelId)
  312. .success(function(data, status, headers,config){
  313. console.log(data); // for browser console
  314. $scope.comments = data; // for UI
  315. })
  316. .error(function(data, status, headers,config){
  317. console.log('data error');
  318. })
  319. .then(function(result){
  320. comments = result.data;
  321. });
  322. $scope.deleteTravel = function(){
  323. console.log("delete travel: " + $stateParams.travelId);
  324. $http({
  325. url: urlapi + 'travels/' + $stateParams.travelId,
  326. method: "DELETE"
  327. })
  328. .then(function(response) {
  329. console.log(response);
  330. },
  331. function(response) { // optional
  332. // failed
  333. });
  334. };
  335. $scope.joinTravel = function(){
  336. $scope.newjoin={
  337. travelId: $stateParams.travelId,
  338. joinedUserId: localStorage.getItem("c_userid"),
  339. joinedUsername: localStorage.getItem("c_username"),
  340. joinedAvatar: localStorage.getItem("c_avatar")
  341. };
  342. $http({
  343. url: urlapi + 'travels/join/' + $stateParams.travelId,
  344. method: "POST",
  345. data: $scope.newjoin
  346. })
  347. .then(function(response) {
  348. // success
  349. console.log("response: ");
  350. console.log(response);
  351. },
  352. function(response) { // optional
  353. // failed
  354. });
  355. };
  356. $scope.unjoinTravel = function(){
  357. console.log("unjoin");
  358. };
  359. /* adding comment */
  360. $scope.doingNewComment=false;
  361. $scope.newComment={};
  362. $scope.showNewComment = function() {
  363. $scope.doingNewComment=true;
  364. };
  365. $scope.closeNewComment = function() {
  366. $scope.doingNewComment=false;
  367. };
  368. $scope.doNewComment = function() {
  369. $scope.newComment.commentUserId=localStorage.getItem("c_userid");
  370. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  371. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");
  372. console.log($scope.newComment);
  373. $http({
  374. url: urlapi + 'travels/comment/' + $stateParams.travelId,
  375. method: "POST",
  376. data: $scope.newComment
  377. })
  378. .then(function(response) {
  379. // success
  380. console.log("newComment added to server: " + response);
  381. console.log(response);
  382. if(response.data.success==false){
  383. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  384. }
  385. },
  386. function(response) { // optional
  387. // failed
  388. });
  389. $scope.closeNewComment();
  390. };
  391. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  392. if(myArray)
  393. {
  394. for(var i = 0, len = myArray.length; i < len; i++) {
  395. if (myArray[i][property] === searchTerm){
  396. //console.log("i: " + i);
  397. return i;
  398. }
  399. }
  400. }
  401. //console.log("i: -1");
  402. return -1;
  403. }
  404. })
  405. .controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
  406. $scope.users="";
  407. $scope.users=JSON.parse(localStorage.getItem('c_users'));
  408. $scope.doRefresh = function() {
  409. $http.get(urlapi + 'users')
  410. .success(function(data, status, headers, config){
  411. console.log('data success');
  412. console.log(data); // for browser console
  413. $scope.users = data; // for UI
  414. localStorage.setItem('c_users', JSON.stringify($scope.users));
  415. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  416. })
  417. .error(function(data, status, headers,config){
  418. console.log('data error');
  419. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  420. })
  421. .then(function(result){
  422. users = result.data;
  423. });
  424. };
  425. })
  426. .controller('UserCtrl', function($scope, $stateParams, $http) {
  427. //$scope.user="";
  428. console.log($stateParams.username);
  429. $http.get(urlapi + 'users/byusername/'+$stateParams.username)
  430. .success(function(data, status, headers,config){
  431. console.log('data success');
  432. console.log(data); // for browser console
  433. $scope.user = data; // for UI
  434. })
  435. .error(function(data, status, headers,config){
  436. console.log('data error');
  437. })
  438. .then(function(result){
  439. user = result.data;
  440. });
  441. $http.get(urlapi + 'travels/user/'+$stateParams.username)
  442. .success(function(data, status, headers,config){
  443. console.log('data success');
  444. console.log(data); // for browser console
  445. $scope.travels = data; // for UI
  446. })
  447. .error(function(data, status, headers,config){
  448. console.log('data error');
  449. })
  450. .then(function(result){
  451. travels = result.data;
  452. });
  453. });