arnaucube
arnaucube/commonroutesAppPublic Self-hosted repository
commonroutesApp/www/js/travel.js
www/js/travel.jsRaw
  1angular.module('app.travel', ['pascalprecht.translate', 'ui-leaflet'])
  2
  3  .controller('TravelCtrl', function($scope, $stateParams, $http,
  4    $ionicModal, $ionicLoading, $ionicPopup, $filter,
  5    leafletData, leafletBoundsHelpers) {
  6    $scope.storageuser = JSON.parse(localStorage.getItem("cim_app_userdata"));
  7
  8    $scope.center = {
  9      /*lat: 0,
 10      lng: 0,
 11      zoom: 1*/
 12    };
 13    $scope.bounds = {};
 14    $scope.markers = [];
 15    $scope.tiles = {
 16      url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
 17      options: {
 18        attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
 19      }
 20    };
 21
 22    $scope.travel = {};
 23    $scope.doRefresh = function() {
 24      /* travels refresh: */
 25      $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
 26        .then(function(data) {
 27          console.log('data success travels');
 28          console.log(data); // for browser console
 29          $scope.travel = data.data; // for UI
 30          $scope.markers = [];
 31          $scope.markers.push({
 32            lat: Number($scope.travel.from.lat),
 33            lng: Number($scope.travel.from.long),
 34            message: $scope.travel.from.name
 35          });
 36          $scope.markers.push({
 37            lat: Number($scope.travel.to.lat),
 38            lng: Number($scope.travel.to.long),
 39            message: $scope.travel.to.name
 40          });
 41          $scope.center = {
 42            lat: (Number($scope.travel.from.lat) + Number($scope.travel.to.lat)) / 2,
 43            lng: (Number($scope.travel.from.long) + Number($scope.travel.to.long)) / 2,
 44            zoom: 4
 45          };
 46
 47          $scope.$broadcast('scroll.refreshComplete'); //refresher stop
 48
 49        }, function(data) {
 50          console.log('data error');
 51          $scope.$broadcast('scroll.refreshComplete'); //refresher stop
 52          $ionicLoading.show({
 53            template: 'Error connecting server',
 54            noBackdrop: true,
 55            duration: 2000
 56          });
 57
 58        });
 59    };
 60    $scope.doRefresh();
 61
 62    $scope.deleteTravel = function() {
 63
 64      var confirmPopup = $ionicPopup.confirm({
 65        title: 'Deleting publication',
 66        template: 'Are you sure you want to delete <b>' + $scope.travel.title + '</b>?'
 67      });
 68      confirmPopup.then(function(res) {
 69        if (res) {
 70          console.log('You are sure');
 71          console.log("delete travel: " + $stateParams.travelid);
 72          $http({
 73              url: urlapi + '/travels/id/modify/' + $stateParams.travelid,
 74              method: "DELETE"
 75            })
 76            .then(function(response) {
 77                console.log(response);
 78                $scope.travels = response.data;
 79                localStorage.setItem('c_travels', JSON.stringify($scope.travels));
 80                localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
 81
 82              },
 83              function(response) { // optional
 84                // failed
 85              });
 86        } else {
 87          console.log('You are not sure');
 88        }
 89      });
 90
 91
 92    };
 93    $scope.joinTravel = function() {
 94      $http({
 95          url: urlapi + 'travels/join/' + $stateParams.travelid,
 96          method: "POST",
 97          data: {}
 98        })
 99        .then(function(data) {
100            console.log("data: ");
101            console.log(data);
102            if (data.data.success == false) {
103              $ionicLoading.show({
104                template: 'Error on unjoin',
105                noBackdrop: true,
106                duration: 2000
107              });
108            } else {
109              $scope.travel = data.data;
110            }
111          },
112          function(response) { // optional
113            // failed
114          });
115    };
116    $scope.unjoinTravel = function() {
117      $http({
118          url: urlapi + 'travels/unjoin/' + $stateParams.travelid,
119          method: "POST",
120          data: {}
121        })
122        .then(function(data) {
123            console.log("data: ");
124            console.log(data);
125            if (data.data.success == false) {
126              $ionicLoading.show({
127                template: 'Error on unjoin',
128                noBackdrop: true,
129                duration: 2000
130              });
131            } else {
132              $scope.travel = data.data;
133            }
134          },
135          function(response) { // optional
136            // failed
137          });
138    };
139
140    $scope.declineJoin = function(joinPetition) {
141      $http({
142          url: urlapi + 'travels/declineJoin/' + $stateParams.travelid,
143          method: "POST",
144          data: {
145            userid: joinPetition._id
146          }
147        })
148        .then(function(data) {
149            console.log("data: ");
150            console.log(data);
151            if (data.data.success == false) {
152              $ionicLoading.show({
153                template: 'Error on declining',
154                noBackdrop: true,
155                duration: 2000
156              });
157            } else {
158              $scope.travel = data.data;
159              console.log("success");
160            }
161          },
162          function(response) { // optional
163            // failed
164          });
165    };
166
167    $scope.acceptJoin = function(joinPetition) {
168      $http({
169          url: urlapi + 'travels/acceptJoin/' + $stateParams.travelid,
170          method: "POST",
171          data: {
172            userid: joinPetition._id
173          }
174        })
175        .then(function(data) {
176            console.log("data: ");
177            console.log(data);
178            if (data.data.success == false) {
179              $ionicLoading.show({
180                template: 'Error on accepting',
181                noBackdrop: true,
182                duration: 2000
183              });
184            } else {
185              $scope.travel = data.data;
186              console.log("success");
187            }
188          },
189          function(response) { // optional
190            // failed
191          });
192    };
193
194    $scope.leaveTravel = function() {
195      var confirmPopup = $ionicPopup.confirm({
196        title: 'Leaving travel',
197        template: 'Are you sure you want to leave <b>' + $scope.travel.title + '</b>?'
198      });
199      confirmPopup.then(function(res) {
200        if (res) {
201          $http({
202              url: urlapi + 'travels/leave/' + $stateParams.travelid,
203              method: "POST",
204              data: {}
205            })
206            .then(function(data) {
207                console.log("data: ");
208                console.log(data);
209                if (data.data.success == false) {
210                  $ionicLoading.show({
211                    template: 'Error on unjoin',
212                    noBackdrop: true,
213                    duration: 2000
214                  });
215                } else {
216                  $scope.travel = data.data;
217                }
218              },
219              function(response) { // optional
220                // failed
221              });
222        } else {
223          console.log('You are not sure');
224        }
225      });
226    };
227
228    /* adding comment */
229    $scope.doingNewComment = false;
230    $scope.newComment = {};
231
232    $scope.showNewComment = function() {
233      $scope.doingNewComment = true;
234    };
235    $scope.closeNewComment = function() {
236      $scope.doingNewComment = false;
237    };
238    $scope.doNewComment = function() {
239      /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
240      $scope.newComment.commentUsername=localStorage.getItem("c_username");
241      $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
242      console.log($scope.newComment);
243      $http({
244          url: urlapi + 'travels/' + $stateParams.travelId + '/comment',
245          method: "POST",
246          data: $scope.newComment
247        })
248        .then(function(response) {
249            // success
250            console.log("newComment added to server: " + response);
251            console.log(response);
252            $scope.travels = response.data;
253            localStorage.setItem('c_travels', JSON.stringify($scope.travels));
254            localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
255            $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
256
257            if (response.data.success == false) {
258
259              $ionicLoading.show({
260                template: 'failed to generate new asking package',
261                noBackdrop: true,
262                duration: 2000
263              });
264            }
265          },
266          function(response) { // optional
267            // failed
268          });
269      $scope.closeNewComment();
270    };
271
272
273    $scope.userHasJoined = function(myArray, searchTerm) {
274      //console.log(myArray+", "+searchTerm);
275      if (myArray) {
276        for (var i = 0, len = myArray.length; i < len; i++) {
277          //console.log(myArray[i] + " - " + searchTerm);
278          if (myArray[i]._id === searchTerm) {
279            //console.log("i: " + i);
280            return i;
281          }
282        }
283      }
284      //console.log("i: -1");
285      return -1;
286    };
287  });