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