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.

287 lines
8.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
  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({
  48. template: 'Error connecting server',
  49. noBackdrop: true,
  50. duration: 2000
  51. });
  52. });
  53. };
  54. $scope.doRefresh();
  55. $scope.deleteTravel = function() {
  56. var confirmPopup = $ionicPopup.confirm({
  57. title: 'Deleting publication',
  58. template: 'Are you sure you want to delete <b>' + $scope.travel.title + '</b>?'
  59. });
  60. confirmPopup.then(function(res) {
  61. if (res) {
  62. console.log('You are sure');
  63. console.log("delete travel: " + $stateParams.travelid);
  64. $http({
  65. url: urlapi + '/travels/id/modify/' + $stateParams.travelid,
  66. method: "DELETE"
  67. })
  68. .then(function(response) {
  69. console.log(response);
  70. $scope.travels = response.data;
  71. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  72. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  73. },
  74. function(response) { // optional
  75. // failed
  76. });
  77. } else {
  78. console.log('You are not sure');
  79. }
  80. });
  81. };
  82. $scope.joinTravel = function() {
  83. $http({
  84. url: urlapi + 'travels/join/' + $stateParams.travelid,
  85. method: "POST",
  86. data: {}
  87. })
  88. .then(function(data) {
  89. console.log("data: ");
  90. console.log(data);
  91. if (data.data.success == false) {
  92. $ionicLoading.show({
  93. template: 'Error on unjoin',
  94. noBackdrop: true,
  95. duration: 2000
  96. });
  97. } else {
  98. $scope.travel = data.data;
  99. }
  100. },
  101. function(response) { // optional
  102. // failed
  103. });
  104. };
  105. $scope.unjoinTravel = function() {
  106. $http({
  107. url: urlapi + 'travels/unjoin/' + $stateParams.travelid,
  108. method: "POST",
  109. data: {}
  110. })
  111. .then(function(data) {
  112. console.log("data: ");
  113. console.log(data);
  114. if (data.data.success == false) {
  115. $ionicLoading.show({
  116. template: 'Error on unjoin',
  117. noBackdrop: true,
  118. duration: 2000
  119. });
  120. } else {
  121. $scope.travel = data.data;
  122. }
  123. },
  124. function(response) { // optional
  125. // failed
  126. });
  127. };
  128. $scope.declineJoin = function(joinPetition) {
  129. $http({
  130. url: urlapi + 'travels/declineJoin/' + $stateParams.travelid,
  131. method: "POST",
  132. data: {
  133. userid: joinPetition._id
  134. }
  135. })
  136. .then(function(data) {
  137. console.log("data: ");
  138. console.log(data);
  139. if (data.data.success == false) {
  140. $ionicLoading.show({
  141. template: 'Error on declining',
  142. noBackdrop: true,
  143. duration: 2000
  144. });
  145. } else {
  146. $scope.travel = data.data;
  147. console.log("success");
  148. }
  149. },
  150. function(response) { // optional
  151. // failed
  152. });
  153. };
  154. $scope.acceptJoin = function(joinPetition) {
  155. $http({
  156. url: urlapi + 'travels/acceptJoin/' + $stateParams.travelid,
  157. method: "POST",
  158. data: {
  159. userid: joinPetition._id
  160. }
  161. })
  162. .then(function(data) {
  163. console.log("data: ");
  164. console.log(data);
  165. if (data.data.success == false) {
  166. $ionicLoading.show({
  167. template: 'Error on accepting',
  168. noBackdrop: true,
  169. duration: 2000
  170. });
  171. } else {
  172. $scope.travel = data.data;
  173. console.log("success");
  174. }
  175. },
  176. function(response) { // optional
  177. // failed
  178. });
  179. };
  180. $scope.leaveTravel = function() {
  181. var confirmPopup = $ionicPopup.confirm({
  182. title: 'Leaving travel',
  183. template: 'Are you sure you want to leave <b>' + $scope.travel.title + '</b>?'
  184. });
  185. confirmPopup.then(function(res) {
  186. if (res) {
  187. $http({
  188. url: urlapi + 'travels/leave/' + $stateParams.travelid,
  189. method: "POST",
  190. data: {}
  191. })
  192. .then(function(data) {
  193. console.log("data: ");
  194. console.log(data);
  195. if (data.data.success == false) {
  196. $ionicLoading.show({
  197. template: 'Error on unjoin',
  198. noBackdrop: true,
  199. duration: 2000
  200. });
  201. } else {
  202. $scope.travel = data.data;
  203. }
  204. },
  205. function(response) { // optional
  206. // failed
  207. });
  208. } else {
  209. console.log('You are not sure');
  210. }
  211. });
  212. };
  213. /* adding comment */
  214. $scope.doingNewComment = false;
  215. $scope.newComment = {};
  216. $scope.showNewComment = function() {
  217. $scope.doingNewComment = true;
  218. };
  219. $scope.closeNewComment = function() {
  220. $scope.doingNewComment = false;
  221. };
  222. $scope.doNewComment = function() {
  223. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  224. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  225. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  226. console.log($scope.newComment);
  227. $http({
  228. url: urlapi + 'travels/' + $stateParams.travelId + '/comment',
  229. method: "POST",
  230. data: $scope.newComment
  231. })
  232. .then(function(response) {
  233. // success
  234. console.log("newComment added to server: " + response);
  235. console.log(response);
  236. $scope.travels = response.data;
  237. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  238. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  239. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  240. if (response.data.success == false) {
  241. $ionicLoading.show({
  242. template: 'failed to generate new asking package',
  243. noBackdrop: true,
  244. duration: 2000
  245. });
  246. }
  247. },
  248. function(response) { // optional
  249. // failed
  250. });
  251. $scope.closeNewComment();
  252. };
  253. $scope.userHasJoined = function(myArray, searchTerm) {
  254. //console.log(myArray+", "+searchTerm);
  255. if (myArray) {
  256. for (var i = 0, len = myArray.length; i < len; i++) {
  257. //console.log(myArray[i] + " - " + searchTerm);
  258. if (myArray[i]._id === searchTerm) {
  259. //console.log("i: " + i);
  260. return i;
  261. }
  262. }
  263. }
  264. //console.log("i: -1");
  265. return -1;
  266. };
  267. });