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.

195 lines
6.0 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
  1. angular.module('app.user', ['pascalprecht.translate'])
  2. .controller('UserCtrl', function($scope, $stateParams, $http,
  3. $ionicLoading, $filter, $ionicModal) {
  4. $scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
  5. $scope.user = {};
  6. $scope.doRefresh = function() {
  7. $http.get(urlapi + 'users/id/' + $stateParams.userid)
  8. .then(function(data, status, headers, config) {
  9. console.log('data success');
  10. console.log(data); // for browser console
  11. $scope.user = data.data; // for UI
  12. if ($scope.storageuser._id == $scope.user._id) {
  13. localStorage.setItem("cim_app_userdata", JSON.stringify(data.data));
  14. }
  15. $scope.$broadcast('scroll.refreshComplete'); //refresher stop
  16. }, function(data, status, headers, config) {
  17. console.log('data error');
  18. $scope.$broadcast('scroll.refreshComplete'); //refresher stop
  19. });
  20. };
  21. $scope.doRefresh();
  22. $scope.likeUser = function() {
  23. $http({
  24. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  25. url: urlapi + 'users/id/like/' + $scope.user._id,
  26. method: "POST",
  27. data: {}
  28. })
  29. .then(function(data) {
  30. // success
  31. if (data.data.success == false) {
  32. console.log("failed");
  33. $ionicLoading.show({
  34. template: 'Error on like',
  35. noBackdrop: true,
  36. duration: 2000
  37. });
  38. } else {
  39. $scope.user = data.data; // for UI
  40. }
  41. },
  42. function(response) { // optional
  43. // failed
  44. });
  45. };
  46. $scope.unlikeUser = function() {
  47. $http({
  48. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  49. url: urlapi + 'users/id/unlike/' + $scope.user._id,
  50. method: "POST",
  51. data: {}
  52. })
  53. .then(function(data) {
  54. // success
  55. if (data.data.success == false) {
  56. console.log("failed");
  57. $ionicLoading.show({
  58. template: 'Error on unlike',
  59. noBackdrop: true,
  60. duration: 2000
  61. });
  62. } else {
  63. $scope.user = data.data; // for UI
  64. }
  65. },
  66. function(response) { // optional
  67. // failed
  68. });
  69. };
  70. $scope.favUser = function() {
  71. $scope.newfav = {
  72. //travelId: $stateParams.travelId,
  73. /*userId: localStorage.getItem("c_userid"),
  74. username: localStorage.getItem("c_username"),
  75. avatar: localStorage.getItem("c_avatar")*/
  76. };
  77. $scope.user.favs.push($scope.newfav); //al unfav no cal fer aquest simulacre pq ja no existeix a l'array i no el resta dos cops en cas de que cliquin dos cops
  78. $http({
  79. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  80. url: urlapi + 'users/' + $scope.user._id + '/fav',
  81. method: "POST",
  82. data: $scope.newfav
  83. })
  84. .then(function(response) {
  85. // success
  86. console.log("response: ");
  87. console.log(response);
  88. $scope.users = response.data;
  89. localStorage.setItem('c_users', JSON.stringify($scope.users));
  90. $scope.user = $filter('filter')($scope.users, {
  91. username: $stateParams.username
  92. }, true)[0];
  93. },
  94. function(response) { // optional
  95. // failed
  96. });
  97. };
  98. $scope.unfavUser = function() {
  99. console.log("unfav");
  100. $scope.unfav = {
  101. /*userId: localStorage.getItem("c_userid"),
  102. username: localStorage.getItem("c_username"),
  103. avatar: localStorage.getItem("c_avatar")*/
  104. };
  105. $http({
  106. //url: urlapi + 'users/'+ $stateParams.username+'/fav',
  107. url: urlapi + 'users/' + $scope.user._id + '/unfav',
  108. method: "POST",
  109. data: $scope.unfav
  110. })
  111. .then(function(response) {
  112. // success
  113. console.log("response: ");
  114. console.log(response);
  115. $scope.users = response.data;
  116. localStorage.setItem('c_users', JSON.stringify($scope.users));
  117. $scope.user = $filter('filter')($scope.users, {
  118. username: $stateParams.username
  119. }, true)[0];
  120. },
  121. function(response) { // optional
  122. // failed
  123. });
  124. };
  125. $ionicModal.fromTemplateUrl('templates/favsList.html', {
  126. scope: $scope
  127. }).then(function(modal) {
  128. $scope.modalFavsList = modal;
  129. });
  130. $scope.closeModalFavsList = function() {
  131. $scope.modalFavsList.hide();
  132. };
  133. $scope.showFavsList = function() {
  134. $scope.modalFavsList.show();
  135. };
  136. $scope.closeModalAndGoUser = function() {
  137. $scope.modalFavsList.hide();
  138. };
  139. $scope.arrayObjectIndexOf = function(myArray, searchTerm) {
  140. if (myArray) {
  141. for (var i = 0, len = myArray.length; i < len; i++) {
  142. if (myArray[i] === searchTerm) {
  143. return i;
  144. }
  145. }
  146. }
  147. return -1;
  148. };
  149. //show image
  150. $ionicModal.fromTemplateUrl('templates/imgView.html', {
  151. scope: $scope,
  152. animation: 'slide-in-up'
  153. }).then(function(modal) {
  154. $scope.modal = modal;
  155. });
  156. $scope.openModal = function() {
  157. $scope.modal.show();
  158. };
  159. $scope.closeModal = function() {
  160. $scope.modal.hide();
  161. };
  162. // Cleanup the modal when we're done with it!
  163. $scope.$on('$destroy', function() {
  164. $scope.modal.remove();
  165. });
  166. // Execute action on hide modal
  167. $scope.$on('modal.hidden', function() {
  168. // Execute action
  169. });
  170. // Execute action on remove modal
  171. $scope.$on('modal.removed', function() {
  172. // Execute action
  173. });
  174. $scope.showImg = function() {
  175. console.log("show image");
  176. $scope.openModal();
  177. };
  178. $scope.openTelegram = function(telegramuser) {
  179. window.open('http://telegram.me/' + telegramuser, '_system', 'location=yes');
  180. return false;
  181. };
  182. });