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