Subversion Repositories SmartDukaan

Rev

Rev 13557 | Blame | Compare with Previous | Last modification | View Log | RSS feed

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope, $stateParams, Categories, CategoryProducts, localStorageService, apihost,$location) {  
  $scope.me = $location.search().user_id;
  if($scope.me) {
         localStorageService.set('me',$scope.me);
  }else{
         $scope.me = localStorageService.get('me');
  }
  $scope.apihost = apihost;
  $scope.curpage = 1;
  $scope.maxpages = 10;
  $scope.products = [];
  $scope.categories = Categories.all({'me':$scope.me});
  $scope.getCategoryName = function(id,cats){
    for(var i in cats){
      if(cats[i].Category.id == id){
        return cats[i].Category.name;
      }
    }  
  }
  /*
  $scope.loadmore = function () {    
    console.log('Fetch category products page '+$scope.curpage);
    CategoryProducts.all({'me':$scope.me, page: $scope.curpage++},  
      function( result ) {          
        // console.log(result);
        // $scope.category = result.categories;
        $scope.maxpages = result.maxresults;
        $scope.$broadcast('scroll.infiniteScrollComplete');
        for(var i in result.products){
          $scope.products[i] = result.products;
          // var catproducts = result.products[i];
          // console.log(catproducts.products);
          // $scope.products[i] = catproducts.products;
        }
        // $scope.chunkedData = $scope.chunk($scope.products, 2);
      },
      function( error ) {
        console.log( "Something went wrong!" );
      }
    );
  },
  */  
  $scope.moreDataCanBeLoaded = function() {
    if($scope.curpage <= $scope.maxpages)
      return true;
    return false;
  },
  $scope.$on('$stateChangeSuccess', function() {
    // $scope.loadmore();
  });
  // var catproducts = CategoryProducts.all({'me':$scope.me, page: $scope.curpage++});    
  // $scope.products = catproducts.products;
  CategoryProducts.all({'me':$scope.me, page: $scope.curpage++},  
      function( result ) {          
        console.log(result);
        $scope.products = result.products;
        $scope.maxpages = result.maxresults;
        $scope.$broadcast('scroll.infiniteScrollComplete');
        /*for(var i in result.products){
          console.log(result.products[i].products);
          $scope.products[i] = result.products[i];
          console.log($scope.products);
        }*/
      },
      function( error ) {
        console.log( "Something went wrong!" );
      }
    );
  $scope.hasProducts = function(categoryId,products) {
    for(index in products) {
      // console.log(products[index].Product.category_id);
      // console.log(objects[index].Product.category_id)
      if(products[index].Product.category_id == categoryId) // filter by name only
        return true;
    }
  }
})

.controller('ChatsCtrl', function($scope, $rootScope, Products, localStorageService, apihost, $location, Myactions) {
  $scope.me = $location.search().user_id;
  if($scope.me) {
        localStorageService.set('me',$scope.me);
  }else{
        $scope.me = localStorageService.get('me');
  }
  $scope.apihost = apihost;
  $scope.curpage = 1;
  $scope.maxpages = 10;
  $scope.products = [];
  $rootScope.liked = [];
  $rootScope.disliked = [];

  Myactions.all({user_id:$scope.me},
    function( result ) {          
      var userActions = result;   
      if(userActions){
        for(var i in userActions){
          for(var j in userActions[i]) {
            if(userActions[i][j].UserAction){
              if(userActions[i][j].UserAction.action == 'like'){
                $rootScope.liked[userActions[i][j].UserAction.store_product_id] = 1;
              }else{
                $rootScope.disliked[userActions[i][j].UserAction.store_product_id] = 1;
              }
            }            
          }          
        }
      }      
    },
    function( error ) {
        console.log( "Something went wrong!" );
    }
  ),

  $scope.loadmore = function () {    
    console.log('Fetch deal products page '+$scope.curpage);
    Products.all({'me':$scope.me, page: $scope.curpage++},  
      function( result ) {          
        $scope.category = result.category;
        $scope.maxpages = result.maxresults;
        $scope.$broadcast('scroll.infiniteScrollComplete');                               
        for(var i in result.products){
          $scope.products.push(result.products[i]);          
        }
        $scope.chunkedData = $scope.chunk($scope.products, 2);
      },
      function( error ) {
        console.log( "Something went wrong!" );
      }
    );
  },  
  $scope.moreDataCanBeLoaded = function() {
    if($scope.curpage <= $scope.maxpages)
      return true;
    return false;
  },
  $scope.$on('$stateChangeSuccess', function() {
    $scope.loadmore();
  });   
})

.controller('CategoryCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

.controller('FriendsCtrl', function($scope, Friends) {
  $scope.friends = Friends.all();
})

.controller('FriendDetailCtrl', function($scope, $stateParams, Friends) {
  $scope.friend = Friends.get($stateParams.friendId);
})

.controller('AccountCtrl', function($scope) {
  $scope.settings = {
    enableFriends: true
  };
})
.controller('LoginCtrl', function($scope,$location,localStorageService) {
  $scope.me = $location.search().user_id;
  if($scope.me){
    console.log($scope.me);
    localStorageService.set('me',$scope.me); 
    $location.path("/tab/chats");    
  }  
});