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.

166 lines
5.8 KiB

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