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.

246 lines
8.0 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
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.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.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.declineJoin = function(joinPetition){
  117. $http({
  118. url: urlapi + 'travels/declineJoin/'+ $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.data.success==false){
  126. $ionicLoading.show({template: 'Error on declining', 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. $scope.acceptJoin = function(joinPetition){
  137. $http({
  138. url: urlapi + 'travels/acceptJoin/'+ $stateParams.travelid,
  139. method: "POST",
  140. data: {userid: joinPetition._id}
  141. })
  142. .then(function(data) {
  143. console.log("data: ");
  144. console.log(data);
  145. if(data.data.success==false){
  146. $ionicLoading.show({template: 'Error on accepting', noBackdrop: true, duration: 2000});
  147. }else{
  148. $scope.travel=data.data;
  149. console.log("success");
  150. }
  151. },
  152. function(response) { // optional
  153. // failed
  154. });
  155. };
  156. $scope.leaveTravel = function(){
  157. $http({
  158. url: urlapi + 'travels/leave/'+ $stateParams.travelid,
  159. method: "POST",
  160. data: {}
  161. })
  162. .then(function(data) {
  163. console.log("data: ");
  164. console.log(data);
  165. if(data.data.success==false){
  166. $ionicLoading.show({template: 'Error on unjoin', noBackdrop: true, duration: 2000});
  167. }else{
  168. $scope.travel=data.data;
  169. }
  170. },
  171. function(response) { // optional
  172. // failed
  173. });
  174. };
  175. /* adding comment */
  176. $scope.doingNewComment=false;
  177. $scope.newComment={};
  178. $scope.showNewComment = function() {
  179. $scope.doingNewComment=true;
  180. };
  181. $scope.closeNewComment = function() {
  182. $scope.doingNewComment=false;
  183. };
  184. $scope.doNewComment = function() {
  185. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  186. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  187. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  188. console.log($scope.newComment);
  189. $http({
  190. url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
  191. method: "POST",
  192. data: $scope.newComment
  193. })
  194. .then(function(response) {
  195. // success
  196. console.log("newComment added to server: " + response);
  197. console.log(response);
  198. $scope.travels=response.data;
  199. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  200. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  201. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  202. if(response.data.success==false){
  203. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  204. }
  205. },
  206. function(response) { // optional
  207. // failed
  208. });
  209. $scope.closeNewComment();
  210. };
  211. $scope.userHasJoined = function(myArray, searchTerm) {
  212. //console.log(myArray+", "+searchTerm);
  213. if(myArray)
  214. {
  215. for(var i = 0, len = myArray.length; i < len; i++) {
  216. //console.log(myArray[i] + " - " + searchTerm);
  217. if (myArray[i]._id === searchTerm){
  218. //console.log("i: " + i);
  219. return i;
  220. }
  221. }
  222. }
  223. //console.log("i: -1");
  224. return -1;
  225. };
  226. });