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.

169 lines
5.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. angular.module('app.travel', ['pascalprecht.translate', 'ui-leaflet'])
  2. .controller('TravelCtrl', function($scope, $stateParams, $http,
  3. $ionicModal, $ionicLoading, $ionicPopup, $filter) {
  4. $scope.center= {
  5. lat: 0,
  6. lng: 0,
  7. zoom: 1
  8. };
  9. $scope.markers=[];
  10. $scope.travel={};
  11. $scope.doRefresh = function() {
  12. /* travels refresh: */
  13. $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
  14. .then(function(data){
  15. console.log('data success travels');
  16. console.log(data); // for browser console
  17. $scope.travel = data.data; // for UI
  18. $scope.markers=[];
  19. $scope.markers.push({
  20. lat: Number($scope.travel.from.lat),
  21. lng: Number($scope.travel.from.long),
  22. message: $scope.travel.from.name
  23. });
  24. $scope.markers.push({
  25. lat: Number($scope.travel.to.lat),
  26. lng: Number($scope.travel.to.long),
  27. message: $scope.travel.to.name
  28. });
  29. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  30. }, function(data){
  31. console.log('data error');
  32. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  33. $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
  34. });
  35. };
  36. $scope.doRefresh();
  37. $scope.deleteTravel = function(){
  38. var confirmPopup = $ionicPopup.confirm({
  39. title: 'Deleting publication',
  40. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  41. });
  42. confirmPopup.then(function(res) {
  43. if(res) {
  44. console.log('You are sure');
  45. console.log("delete travel: " + $stateParams.travelId);
  46. $http({
  47. url: urlapi + 'travels/' + $stateParams.travelId,
  48. method: "DELETE"
  49. })
  50. .then(function(response) {
  51. console.log(response);
  52. $scope.travels=response.data;
  53. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  54. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  55. },
  56. function(response) { // optional
  57. // failed
  58. });
  59. } else {
  60. console.log('You are not sure');
  61. }
  62. });
  63. };
  64. $scope.joinTravel = function(){
  65. $http({
  66. url: urlapi + 'travels/join/'+ $stateParams.travelid,
  67. method: "POST",
  68. data: {}
  69. })
  70. .then(function(data) {
  71. console.log("data: ");
  72. console.log(data);
  73. if(data.success==false){
  74. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  75. }else{
  76. $scope.travel=data.data;
  77. }
  78. },
  79. function(response) { // optional
  80. // failed
  81. });
  82. };
  83. $scope.unjoinTravel = function(){
  84. $http({
  85. url: urlapi + 'travels/unjoin/'+ $stateParams.travelid,
  86. method: "POST",
  87. data: {}
  88. })
  89. .then(function(data) {
  90. console.log("data: ");
  91. console.log(data);
  92. if(data.success==false){
  93. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  94. }else{
  95. $scope.travel=data.data;
  96. }
  97. },
  98. function(response) { // optional
  99. // failed
  100. });
  101. };
  102. /* adding comment */
  103. $scope.doingNewComment=false;
  104. $scope.newComment={};
  105. $scope.showNewComment = function() {
  106. $scope.doingNewComment=true;
  107. };
  108. $scope.closeNewComment = function() {
  109. $scope.doingNewComment=false;
  110. };
  111. $scope.doNewComment = function() {
  112. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  113. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  114. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  115. console.log($scope.newComment);
  116. $http({
  117. url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
  118. method: "POST",
  119. data: $scope.newComment
  120. })
  121. .then(function(response) {
  122. // success
  123. console.log("newComment added to server: " + response);
  124. console.log(response);
  125. $scope.travels=response.data;
  126. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  127. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  128. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  129. if(response.data.success==false){
  130. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  131. }
  132. },
  133. function(response) { // optional
  134. // failed
  135. });
  136. $scope.closeNewComment();
  137. };
  138. $scope.userHasJoined = function(myArray, searchTerm) {
  139. //console.log(myArray+", "+searchTerm+", "+property);
  140. if(myArray)
  141. {
  142. for(var i = 0, len = myArray.length; i < len; i++) {
  143. //console.log(myArray[i] + " - " + searchTerm);
  144. if (myArray[i] === searchTerm){
  145. //console.log("i: " + i);
  146. return i;
  147. }
  148. }
  149. }
  150. //console.log("i: -1");
  151. return -1;
  152. };
  153. });