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.

205 lines
6.7 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
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. leafletData, leafletBoundsHelpers) {
  5. $scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
  6. $scope.center= {
  7. /*lat: 0,
  8. lng: 0,
  9. zoom: 1*/
  10. };
  11. $scope.bounds={};
  12. $scope.markers=[];
  13. $scope.tiles= {
  14. url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  15. options: {
  16. attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  17. }
  18. };
  19. $scope.travel={};
  20. $scope.doRefresh = function() {
  21. /* travels refresh: */
  22. $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
  23. .then(function(data){
  24. console.log('data success travels');
  25. console.log(data); // for browser console
  26. $scope.travel = data.data; // for UI
  27. $scope.markers=[];
  28. $scope.markers.push({
  29. lat: Number($scope.travel.from.lat),
  30. lng: Number($scope.travel.from.long),
  31. message: $scope.travel.from.name
  32. });
  33. $scope.markers.push({
  34. lat: Number($scope.travel.to.lat),
  35. lng: Number($scope.travel.to.long),
  36. message: $scope.travel.to.name
  37. });
  38. $scope.center= {
  39. lat: (Number($scope.travel.from.lat)+Number($scope.travel.to.lat))/2,
  40. lng: (Number($scope.travel.from.long)+Number($scope.travel.to.long))/2,
  41. zoom: 4
  42. };
  43. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  44. }, function(data){
  45. console.log('data error');
  46. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  47. $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
  48. });
  49. };
  50. $scope.doRefresh();
  51. $scope.deleteTravel = function(){
  52. var confirmPopup = $ionicPopup.confirm({
  53. title: 'Deleting publication',
  54. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  55. });
  56. confirmPopup.then(function(res) {
  57. if(res) {
  58. console.log('You are sure');
  59. console.log("delete travel: " + $stateParams.travelId);
  60. $http({
  61. url: urlapi + 'travels/' + $stateParams.travelId,
  62. method: "DELETE"
  63. })
  64. .then(function(response) {
  65. console.log(response);
  66. $scope.travels=response.data;
  67. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  68. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  69. },
  70. function(response) { // optional
  71. // failed
  72. });
  73. } else {
  74. console.log('You are not sure');
  75. }
  76. });
  77. };
  78. $scope.joinTravel = function(){
  79. $http({
  80. url: urlapi + 'travels/join/'+ $stateParams.travelid,
  81. method: "POST",
  82. data: {}
  83. })
  84. .then(function(data) {
  85. console.log("data: ");
  86. console.log(data);
  87. if(data.success==false){
  88. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  89. }else{
  90. $scope.travel=data.data;
  91. }
  92. },
  93. function(response) { // optional
  94. // failed
  95. });
  96. };
  97. $scope.unjoinTravel = function(){
  98. $http({
  99. url: urlapi + 'travels/unjoin/'+ $stateParams.travelid,
  100. method: "POST",
  101. data: {}
  102. })
  103. .then(function(data) {
  104. console.log("data: ");
  105. console.log(data);
  106. if(data.success==false){
  107. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  108. }else{
  109. $scope.travel=data.data;
  110. }
  111. },
  112. function(response) { // optional
  113. // failed
  114. });
  115. };
  116. $scope.acceptJoin = function(joinPetition){
  117. $http({
  118. url: urlapi + 'travels/acceptJoin/'+ $stateParams.travelid,
  119. method: "POST",
  120. data: {userid: joinPetition._id}
  121. })
  122. .then(function(data) {
  123. console.log("data: ");
  124. console.log(data);
  125. if(data.success==false){
  126. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  127. }else{
  128. $scope.travel=data.data;
  129. console.log("success");
  130. }
  131. },
  132. function(response) { // optional
  133. // failed
  134. });
  135. };
  136. /* adding comment */
  137. $scope.doingNewComment=false;
  138. $scope.newComment={};
  139. $scope.showNewComment = function() {
  140. $scope.doingNewComment=true;
  141. };
  142. $scope.closeNewComment = function() {
  143. $scope.doingNewComment=false;
  144. };
  145. $scope.doNewComment = function() {
  146. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  147. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  148. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  149. console.log($scope.newComment);
  150. $http({
  151. url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
  152. method: "POST",
  153. data: $scope.newComment
  154. })
  155. .then(function(response) {
  156. // success
  157. console.log("newComment added to server: " + response);
  158. console.log(response);
  159. $scope.travels=response.data;
  160. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  161. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  162. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  163. if(response.data.success==false){
  164. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  165. }
  166. },
  167. function(response) { // optional
  168. // failed
  169. });
  170. $scope.closeNewComment();
  171. };
  172. $scope.userHasJoined = function(myArray, searchTerm) {
  173. //console.log(myArray+", "+searchTerm);
  174. if(myArray)
  175. {
  176. for(var i = 0, len = myArray.length; i < len; i++) {
  177. //console.log(myArray[i] + " - " + searchTerm);
  178. if (myArray[i]._id === searchTerm){
  179. //console.log("i: " + i);
  180. return i;
  181. }
  182. }
  183. }
  184. //console.log("i: -1");
  185. return -1;
  186. };
  187. });