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.

169 lines
6.0 KiB

7 years ago
  1. angular.module('app.travel', ['pascalprecht.translate'])
  2. .controller('TravelCtrl', function($scope, $stateParams, $http, $ionicModal, $ionicPopup, $filter) {
  3. if(localStorage.getItem('c_token')){// adding token to the headers
  4. $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('c_token');
  5. }
  6. $scope.storageusername=localStorage.getItem("c_username");
  7. console.log($stateParams.travelId);
  8. $scope.travels= JSON.parse(localStorage.getItem('c_travels'));
  9. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  10. /*$http.get(urlapi + 'travels/comment/'+$stateParams.travelId)
  11. .success(function(data, status, headers,config){
  12. console.log(data); // for browser console
  13. $scope.comments = data; // for UI
  14. })
  15. .error(function(data, status, headers,config){
  16. console.log('data error');
  17. })
  18. .then(function(result){
  19. comments = result.data;
  20. });*/
  21. $scope.deleteTravel = function(){
  22. var confirmPopup = $ionicPopup.confirm({
  23. title: 'Deleting publication',
  24. template: 'Are you sure you want to delete <b>'+ $scope.travel.title+'</b>?'
  25. });
  26. confirmPopup.then(function(res) {
  27. if(res) {
  28. console.log('You are sure');
  29. console.log("delete travel: " + $stateParams.travelId);
  30. $http({
  31. url: urlapi + 'travels/' + $stateParams.travelId,
  32. method: "DELETE"
  33. })
  34. .then(function(response) {
  35. console.log(response);
  36. $scope.travels=response.data;
  37. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  38. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  39. },
  40. function(response) { // optional
  41. // failed
  42. });
  43. } else {
  44. console.log('You are not sure');
  45. }
  46. });
  47. };
  48. $scope.joinTravel = function(){
  49. $scope.newjoin={
  50. //travelId: $stateParams.travelId,
  51. /*joinedUserId: localStorage.getItem("c_userid"),
  52. joinedUsername: localStorage.getItem("c_username"),
  53. joinedAvatar: localStorage.getItem("c_avatar")*/
  54. };
  55. $http({
  56. url: urlapi + 'travels/'+ $stateParams.travelId+'/join',
  57. method: "POST",
  58. data: $scope.newjoin
  59. })
  60. .then(function(response) {
  61. // success
  62. console.log("response: ");
  63. console.log(response);
  64. $scope.travels=response.data;
  65. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  66. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  67. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  68. },
  69. function(response) { // optional
  70. // failed
  71. });
  72. };
  73. $scope.unjoinTravel = function(){
  74. console.log("unjoin");
  75. $scope.unjoin={
  76. travelId: $stateParams.travelId,
  77. /*joinedUserId: localStorage.getItem("c_userid"),
  78. joinedUsername: localStorage.getItem("c_username"),
  79. joinedAvatar: localStorage.getItem("c_avatar")*/
  80. };
  81. $http({
  82. url: urlapi + 'travels/'+ $stateParams.travelId+'/unjoin',
  83. method: "POST",
  84. data: $scope.unjoin
  85. })
  86. .then(function(response) {
  87. // success
  88. console.log("response: ");
  89. console.log(response);
  90. $scope.travels=response.data;
  91. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  92. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  93. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  94. },
  95. function(response) { // optional
  96. // failed
  97. });
  98. };
  99. /* adding comment */
  100. $scope.doingNewComment=false;
  101. $scope.newComment={};
  102. $scope.showNewComment = function() {
  103. $scope.doingNewComment=true;
  104. };
  105. $scope.closeNewComment = function() {
  106. $scope.doingNewComment=false;
  107. };
  108. $scope.doNewComment = function() {
  109. /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
  110. $scope.newComment.commentUsername=localStorage.getItem("c_username");
  111. $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
  112. console.log($scope.newComment);
  113. $http({
  114. url: urlapi + 'travels/'+ $stateParams.travelId+'/comment',
  115. method: "POST",
  116. data: $scope.newComment
  117. })
  118. .then(function(response) {
  119. // success
  120. console.log("newComment added to server: " + response);
  121. console.log(response);
  122. $scope.travels=response.data;
  123. localStorage.setItem('c_travels', JSON.stringify($scope.travels));
  124. localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
  125. $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
  126. if(response.data.success==false){
  127. $ionicLoading.show({ template: 'failed to generate new asking package', noBackdrop: true, duration: 2000 });
  128. }
  129. },
  130. function(response) { // optional
  131. // failed
  132. });
  133. $scope.closeNewComment();
  134. };
  135. console.log("a");
  136. console.log($scope.storageusername);
  137. console.log($scope.travel.owner);
  138. $scope.arrayObjectIndexOf = function(myArray, searchTerm, property) {
  139. //console.log(myArray+", "+searchTerm+", "+property);
  140. if(myArray)
  141. {
  142. for(var i = 0, len = myArray.length; i < len; i++) {
  143. if (myArray[i][property] === searchTerm){
  144. //console.log("i: " + i);
  145. return i;
  146. }
  147. }
  148. }
  149. //console.log("i: -1");
  150. return -1;
  151. };
  152. });