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                window.location="#app/users/userTravels/" + $scope.storageuser._id;
 83              },
 84              function(response) { // optional
 85                // failed
 86              });
 87        } else {
 88          console.log('You are not sure');
 89        }
 90      });
 91
 92
 93    };
 94    $scope.joinTravel = function() {
 95      $http({
 96          url: urlapi + 'travels/join/' + $stateParams.travelid,
 97          method: "POST",
 98          data: {}
 99        })
100        .then(function(data) {
101            console.log("data: ");
102            console.log(data);
103            if (data.data.success == false) {
104              $ionicLoading.show({
105                template: 'Error on unjoin',
106                noBackdrop: true,
107                duration: 2000
108              });
109            } else {
110              $scope.travel = data.data;
111            }
112          },
113          function(response) { // optional
114            // failed
115          });
116    };
117    $scope.unjoinTravel = function() {
118      $http({
119          url: urlapi + 'travels/unjoin/' + $stateParams.travelid,
120          method: "POST",
121          data: {}
122        })
123        .then(function(data) {
124            console.log("data: ");
125            console.log(data);
126            if (data.data.success == false) {
127              $ionicLoading.show({
128                template: 'Error on unjoin',
129                noBackdrop: true,
130                duration: 2000
131              });
132            } else {
133              $scope.travel = data.data;
134            }
135          },
136          function(response) { // optional
137            // failed
138          });
139    };
140
141    $scope.declineJoin = function(joinPetition) {
142      $http({
143          url: urlapi + 'travels/declineJoin/' + $stateParams.travelid,
144          method: "POST",
145          data: {
146            userid: joinPetition._id
147          }
148        })
149        .then(function(data) {
150            console.log("data: ");
151            console.log(data);
152            if (data.data.success == false) {
153              $ionicLoading.show({
154                template: 'Error on declining',
155                noBackdrop: true,
156                duration: 2000
157              });
158            } else {
159              $scope.travel = data.data;
160              console.log("success");
161            }
162          },
163          function(response) { // optional
164            // failed
165          });
166    };
167
168    $scope.acceptJoin = function(joinPetition) {
169      $http({
170          url: urlapi + 'travels/acceptJoin/' + $stateParams.travelid,
171          method: "POST",
172          data: {
173            userid: joinPetition._id
174          }
175        })
176        .then(function(data) {
177            console.log("data: ");
178            console.log(data);
179            if (data.data.success == false) {
180              $ionicLoading.show({
181                template: 'Error on accepting',
182                noBackdrop: true,
183                duration: 2000
184              });
185            } else {
186              $scope.travel = data.data;
187              console.log("success");
188            }
189          },
190          function(response) { // optional
191            // failed
192          });
193    };
194
195    $scope.leaveTravel = function() {
196      var confirmPopup = $ionicPopup.confirm({
197        title: 'Leaving travel',
198        template: 'Are you sure you want to leave <b>' + $scope.travel.title + '</b>?'
199      });
200      confirmPopup.then(function(res) {
201        if (res) {
202          $http({
203              url: urlapi + 'travels/leave/' + $stateParams.travelid,
204              method: "POST",
205              data: {}
206            })
207            .then(function(data) {
208                console.log("data: ");
209                console.log(data);
210                if (data.data.success == false) {
211                  $ionicLoading.show({
212                    template: 'Error on unjoin',
213                    noBackdrop: true,
214                    duration: 2000
215                  });
216                } else {
217                  $scope.travel = data.data;
218                }
219              },
220              function(response) { // optional
221                // failed
222              });
223        } else {
224          console.log('You are not sure');
225        }
226      });
227    };
228
229    /* adding comment */
230    $scope.doingNewComment = false;
231    $scope.newComment = {};
232
233    $scope.showNewComment = function() {
234      $scope.doingNewComment = true;
235    };
236    $scope.closeNewComment = function() {
237      $scope.doingNewComment = false;
238    };
239    $scope.doNewComment = function() {
240      /*$scope.newComment.commentUserId=localStorage.getItem("c_userid");
241      $scope.newComment.commentUsername=localStorage.getItem("c_username");
242      $scope.newComment.commentAvatar=localStorage.getItem("c_avatar");*/
243      console.log($scope.newComment);
244      $http({
245          url: urlapi + 'travels/' + $stateParams.travelId + '/comment',
246          method: "POST",
247          data: $scope.newComment
248        })
249        .then(function(response) {
250            // success
251            console.log("newComment added to server: " + response);
252            console.log(response);
253            $scope.travels = response.data;
254            localStorage.setItem('c_travels', JSON.stringify($scope.travels));
255            localStorage.setItem('c_travelsLastDate', JSON.stringify(new Date()));
256            $scope.travel = $filter('filter')($scope.travels, $stateParams.travelId, true)[0];
257
258            if (response.data.success == false) {
259
260              $ionicLoading.show({
261                template: 'failed to generate new asking package',
262                noBackdrop: true,
263                duration: 2000
264              });
265            }
266          },
267          function(response) { // optional
268            // failed
269          });
270      $scope.closeNewComment();
271    };
272
273
274    $scope.userHasJoined = function(myArray, searchTerm) {
275      //console.log(myArray+", "+searchTerm);
276      if (myArray) {
277        for (var i = 0, len = myArray.length; i < len; i++) {
278          //console.log(myArray[i] + " - " + searchTerm);
279          if (myArray[i]._id === searchTerm) {
280            //console.log("i: " + i);
281            return i;
282          }
283        }
284      }
285      //console.log("i: -1");
286      return -1;
287    };
288    $scope.openTelegram = function(telegramuser) {
289      window.open('http://telegram.me/' + telegramuser, '_system', 'location=yes');
290      return false;
291    };
292    $scope.copyTravelLink = function(link) {
293      $cordovaClipboard.copy("Text To Copy").then(function() {
294          // success
295      }, function() {
296          // error
297      });
298    };
299  });