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.

210 lines
6.8 KiB

  1. 'use strict';
  2. angular.module('app.travel', ['ngRoute', 'ui-leaflet'])
  3. .config(['$routeProvider', function($routeProvider) {
  4. $routeProvider.when('/travel/:travelid', {
  5. templateUrl: 'views/travel/travel.html',
  6. controller: 'TravelCtrl'
  7. });
  8. }])
  9. .controller('TravelCtrl', function($scope, $http, $routeParams,
  10. leafletData, leafletBoundsHelpers, toastr) {
  11. $scope.storageuser = JSON.parse(localStorage.getItem("cr_webapp_userdata"));
  12. $scope.travel = {};
  13. //map
  14. $scope.center = {
  15. /*lat: 0,
  16. lng: 0,
  17. zoom: 1*/
  18. };
  19. $scope.bounds = {};
  20. $scope.markers = [];
  21. $scope.tiles = {
  22. url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  23. options: {
  24. attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  25. }
  26. };
  27. $http.get(urlapi + 'travels/id/' + $routeParams.travelid)
  28. .then(function(data, status, headers, config) {
  29. console.log('data success');
  30. console.log(data);
  31. $scope.travel = data.data;
  32. //map
  33. $scope.markers = [];
  34. $scope.markers.push({
  35. lat: Number($scope.travel.from.lat),
  36. lng: Number($scope.travel.from.long),
  37. message: $scope.travel.from.name
  38. });
  39. $scope.markers.push({
  40. lat: Number($scope.travel.to.lat),
  41. lng: Number($scope.travel.to.long),
  42. message: $scope.travel.to.name
  43. });
  44. $scope.center = {
  45. lat: (Number($scope.travel.from.lat) + Number($scope.travel.to.lat)) / 2,
  46. lng: (Number($scope.travel.from.long) + Number($scope.travel.to.long)) / 2,
  47. zoom: 4
  48. };
  49. }, function(data, status, headers, config) {
  50. console.log('data error');
  51. });
  52. //delete travel
  53. $scope.deleteTravel = function() {
  54. console.log("delete travel: " + $routeParams.travelid);
  55. $http({
  56. url: urlapi + '/admin/travels/id/' + $routeParams.travelid,
  57. method: "DELETE"
  58. })
  59. .then(function(data) {
  60. console.log(data);
  61. $scope.travels = data.data;
  62. toastr.info('Travel deleted');
  63. window.location = "#!/main/";
  64. },
  65. function(data) { // optional
  66. // failed
  67. toastr.error('Error on delete travel');
  68. });
  69. };
  70. $scope.joinTravel = function() {
  71. $http({
  72. url: urlapi + 'travels/join/' + $routeParams.travelid,
  73. method: "POST",
  74. data: {}
  75. })
  76. .then(function(data) {
  77. console.log("data: ");
  78. console.log(data);
  79. if (data.data.success == false) {
  80. toastr.error('Error on join');
  81. } else {
  82. $scope.travel = data.data;
  83. toastr.success('Joined travel');
  84. }
  85. },
  86. function(response) { // optional
  87. // failed
  88. });
  89. };
  90. $scope.unjoinTravel = function() {
  91. $http({
  92. url: urlapi + 'travels/unjoin/' + $routeParams.travelid,
  93. method: "POST",
  94. data: {}
  95. })
  96. .then(function(data) {
  97. console.log("data: ");
  98. console.log(data);
  99. if (data.data.success == false) {
  100. toastr.error('Error on unjoin');
  101. } else {
  102. $scope.travel = data.data;
  103. toastr.success('Unjoined travel');
  104. }
  105. },
  106. function(response) { // optional
  107. // failed
  108. });
  109. };
  110. $scope.declineJoin = function(joinPetition) {
  111. $http({
  112. url: urlapi + 'travels/declineJoin/' + $routeParams.travelid,
  113. method: "POST",
  114. data: {
  115. userid: joinPetition._id
  116. }
  117. })
  118. .then(function(data) {
  119. console.log("data: ");
  120. console.log(data);
  121. if (data.data.success == false) {
  122. toastr.error('Error on declining');
  123. } else {
  124. $scope.travel = data.data;
  125. console.log("success");
  126. toastr.success('Join declined');
  127. }
  128. },
  129. function(response) { // optional
  130. // failed
  131. });
  132. };
  133. $scope.acceptJoin = function(joinPetition) {
  134. $http({
  135. url: urlapi + 'travels/acceptJoin/' + $routeParams.travelid,
  136. method: "POST",
  137. data: {
  138. userid: joinPetition._id
  139. }
  140. })
  141. .then(function(data) {
  142. console.log("data: ");
  143. console.log(data);
  144. if (data.data.success == false) {
  145. toastr.error('Error on accepting');
  146. } else {
  147. $scope.travel = data.data;
  148. console.log("success");
  149. toastr.success('Join accepted');
  150. }
  151. },
  152. function(response) { // optional
  153. // failed
  154. });
  155. };
  156. $scope.leaveTravel = function() {
  157. $http({
  158. url: urlapi + 'travels/leave/' + $routeParams.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. toastr.error('Error on leave');
  167. } else {
  168. $scope.travel = data.data;
  169. toastr.success('Travel leaved');
  170. }
  171. },
  172. function(response) { // optional
  173. // failed
  174. });
  175. };
  176. $scope.userHasJoined = function(myArray, searchTerm) {
  177. //console.log(myArray+", "+searchTerm);
  178. if (myArray) {
  179. for (var i = 0, len = myArray.length; i < len; i++) {
  180. //console.log(myArray[i] + " - " + searchTerm);
  181. if (myArray[i]._id === searchTerm) {
  182. //console.log("i: " + i);
  183. return i;
  184. }
  185. }
  186. }
  187. //console.log("i: -1");
  188. return -1;
  189. };
  190. });