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.

303 lines
9.3 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. window.location="#app/users/userTravels/" + $scope.storageuser._id;
  74. },
  75. function(response) { // optional
  76. // failed
  77. });
  78. } else {
  79. console.log('You are not sure');
  80. }
  81. });
  82. };
  83. $scope.joinTravel = function() {
  84. $http({
  85. url: urlapi + 'travels/join/' + $stateParams.travelid,
  86. method: "POST",
  87. data: {}
  88. })
  89. .then(function(data) {
  90. console.log("data: ");
  91. console.log(data);
  92. if (data.data.success == false) {
  93. $ionicLoading.show({
  94. template: 'Error on unjoin',
  95. noBackdrop: true,
  96. duration: 2000
  97. });
  98. } else {
  99. $scope.travel = data.data;
  100. }
  101. },
  102. function(response) { // optional
  103. // failed
  104. });
  105. };
  106. $scope.unjoinTravel = function() {
  107. $http({
  108. url: urlapi + 'travels/unjoin/' + $stateParams.travelid,
  109. method: "POST",
  110. data: {}
  111. })
  112. .then(function(data) {
  113. console.log("data: ");
  114. console.log(data);
  115. if (data.data.success == false) {
  116. $ionicLoading.show({
  117. template: 'Error on unjoin',
  118. noBackdrop: true,
  119. duration: 2000
  120. });
  121. } else {
  122. $scope.travel = data.data;
  123. }
  124. },
  125. function(response) { // optional
  126. // failed
  127. });
  128. };
  129. $scope.declineJoin = function(joinPetition) {
  130. $http({
  131. url: urlapi + 'travels/declineJoin/' + $stateParams.travelid,
  132. method: "POST",
  133. data: {
  134. userid: joinPetition._id
  135. }
  136. })
  137. .then(function(data) {
  138. console.log("data: ");
  139. console.log(data);
  140. if (data.data.success == false) {
  141. $ionicLoading.show({
  142. template: 'Error on declining',
  143. noBackdrop: true,
  144. duration: 2000
  145. });
  146. } else {
  147. $scope.travel = data.data;
  148. console.log("success");
  149. }
  150. },
  151. function(response) { // optional
  152. // failed
  153. });
  154. };
  155. $scope.acceptJoin = function(joinPetition) {
  156. $http({
  157. url: urlapi + 'travels/acceptJoin/' + $stateParams.travelid,
  158. method: "POST",
  159. data: {
  160. userid: joinPetition._id
  161. }
  162. })
  163. .then(function(data) {
  164. console.log("data: ");
  165. console.log(data);
  166. if (data.data.success == false) {
  167. $ionicLoading.show({
  168. template: 'Error on accepting',
  169. noBackdrop: true,
  170. duration: 2000
  171. });
  172. } else {
  173. $scope.travel = data.data;
  174. console.log("success");
  175. }
  176. },
  177. function(response) { // optional
  178. // failed
  179. });
  180. };
  181. $scope.leaveTravel = function() {
  182. var confirmPopup = $ionicPopup.confirm({
  183. title: 'Leaving travel',
  184. template: 'Are you sure you want to leave <b>' + $scope.travel.title + '</b>?'
  185. });
  186. confirmPopup.then(function(res) {
  187. if (res) {
  188. $http({
  189. url: urlapi + 'travels/leave/' + $stateParams.travelid,
  190. method: "POST",
  191. data: {}
  192. })
  193. .then(function(data) {
  194. console.log("data: ");
  195. console.log(data);
  196. if (data.data.success == false) {
  197. $ionicLoading.show({
  198. template: 'Error on unjoin',
  199. noBackdrop: true,
  200. duration: 2000
  201. });
  202. } else {
  203. $scope.travel = data.data;
  204. }
  205. },
  206. function(response) { // optional
  207. // failed
  208. });
  209. } else {
  210. console.log('You are not sure');
  211. }
  212. });
  213. };
  214. /* adding comment */
  215. $scope.doingNewComment = false;
  216. $scope.newComment = {};
  217. $scope.showNewComment = function() {
  218. $scope.doingNewComment = true;
  219. };
  220. $scope.closeNewComment = function() {
  221. $scope.doingNewComment = false;
  222. };
  223. $scope.doNewComment = function() {
  224. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  225. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  226. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  227. console.log($scope.newComment);
  228. $http({
  229. url: urlapi + 'travels/' + $stateParams.travelId + '/comment',
  230. method: "POST",
  231. data: $scope.newComment
  232. })
  233. .then(function(response) {
  234. // success
  235. console.log("newComment added to server: " + response);
  236. console.log(response);
  237. $scope.travels = response.data;
  238. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  239. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  240. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  241. if (response.data.success == false) {
  242. $ionicLoading.show({
  243. template: 'failed to generate new asking package',
  244. noBackdrop: true,
  245. duration: 2000
  246. });
  247. }
  248. },
  249. function(response) { // optional
  250. // failed
  251. });
  252. $scope.closeNewComment();
  253. };
  254. $scope.userHasJoined = function(myArray, searchTerm) {
  255. //console.log(myArray+", "+searchTerm);
  256. if (myArray) {
  257. for (var i = 0, len = myArray.length; i < len; i++) {
  258. //console.log(myArray[i] + " - " + searchTerm);
  259. if (myArray[i]._id === searchTerm) {
  260. //console.log("i: " + i);
  261. return i;
  262. }
  263. }
  264. }
  265. //console.log("i: -1");
  266. return -1;
  267. };
  268. $scope.openTravelLink = function(travelid) {
  269. window.open('http://routes.fair.coop/app/#!/travel/' + travelid, '_system', 'location=yes');
  270. return false;
  271. };
  272. $scope.openTelegram = function(telegramuser) {
  273. window.open('http://telegram.me/' + telegramuser, '_system', 'location=yes');
  274. return false;
  275. };
  276. $scope.copyTravelLink = function(link) {
  277. $cordovaClipboard.copy("Text To Copy").then(function() {
  278. // success
  279. }, function() {
  280. // error
  281. });
  282. };
  283. });