Advertisement
Masadow

Algolia + angular controller

May 27th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. angular.module('seekube.component.listingForum.controller', [])
  4.  
  5.     .controller('listingForumIndexCtrl', ['$scope', 'AlgoliaClient', function ($scope, AlgoliaClient) {
  6.         $scope.forums = [];
  7.        
  8.         //translations
  9.         $scope.monthTranslation = [];
  10.         for (var i=1; i <= 12; ++i) {
  11.             var trans = Translator.trans('month' + i, {}, 'seekube');
  12.             $scope.monthTranslation[i] = trans.charAt(0).toUpperCase() + trans.slice(1);
  13.         }
  14.        
  15.         console.log($scope.monthTranslation);
  16.        
  17.         $scope.filterModel = {
  18.             string: "",
  19.             pastForum: false,
  20.             months: [],
  21.             years: []
  22.         };
  23.        
  24.         $scope.facets = {};
  25.        
  26.         $scope.queue = 1;
  27.         var forumIdx = algoliasearchHelper(
  28.             AlgoliaClient, 'dev_forum', {
  29.                 disjunctiveFacets: ['month', 'year'],
  30.                 hitsPerPage: 6
  31.             }
  32.         );
  33.  
  34.         $scope.isDisjunctiveRefined = forumIdx.isDisjunctiveRefined;
  35.        
  36.         forumIdx.addNumericRefinement('endAt', '>', moment().unix());
  37.        
  38.         var search = function() {
  39.             $scope.queue++;
  40.             $scope.forums = [];
  41.             forumIdx.search();
  42.            
  43.             forumIdx.isRefined('month', '2');
  44.         }
  45.        
  46.         //Search callback
  47.         forumIdx.on('result', function (result) {
  48.             $scope.$apply(function () {
  49.                 //Update the result set
  50.                 console.log(result);
  51.                 $scope.forums = result.hits;
  52.                 $scope.facets = {
  53.                     month: result.disjunctiveFacets[0].data,
  54.                     year: result.disjunctiveFacets[1].data
  55.                 };
  56.  
  57.                 //Remove the loader if the queue is empty
  58.                 --$scope.queue;
  59.             });
  60.         });
  61.        
  62.         forumIdx.on('error', function (err) {
  63.             $scope.$apply(function () {
  64.                 console.log(error);
  65.                 --$scope.queue;
  66.             });
  67.         })
  68.        
  69.         $scope.toggleRefine = function($event, facet, value) {
  70.             $event.preventDefault();
  71.             forumIdx.toggleRefine(facet, value);
  72.             search();
  73.         };
  74.        
  75.         //First result set
  76.         forumIdx.search();
  77.  
  78.         //Add watchers
  79.         $scope.$watch('filterModel.string', function (newValue, oldValue) {
  80.             forumIdx.setQuery(newValue);
  81.             search();
  82.         });
  83.        
  84.         $scope.$watch('filterModel.pastForum', function(newValue, oldValue) {
  85.             forumIdx.removeNumericRefinement('endAt', oldValue ? '<=' : '>', moment().unix());
  86.             forumIdx.addNumericRefinement('endAt', newValue ? '<=' : '>', moment().unix());
  87.             search();
  88.         });
  89.        
  90.     }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement