Skip to content
Snippets Groups Projects
Select Git revision
  • 829874b38be8c2d7d190833ea3d4601f5be681cd
  • master default
  • ember-ui
3 results

controllers.js

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    controllers.js 6.64 KiB
    /******************************************************************************
     * Vipra Application
     * Controllers
     ******************************************************************************/
    (function() {
    
      var app = angular.module('vipra.controllers', [
        'ui.router',
        'vipra.factories'
      ]);
    
      var latestItemsCount = 3,
          searchItemsCount = 10,
          pageSize = 100,
          paginationPadding = 4;
    
      app.controller('IndexController', ['$scope', '$location', 'ArticleFactory', 'TopicFactory', 'WordFactory', 'SearchFactory',
        function($scope, $location, ArticleFactory, TopicFactory, WordFactory, SearchFactory) {
    
        $scope.search = $location.search().query;
    
        ArticleFactory.query({limit:latestItemsCount, sort:'-created'}, function(response) {
          $scope.latestArticles = response.data;
        });
    
        TopicFactory.query({limit:latestItemsCount, sort:'-created'}, function(response) {
          $scope.latestTopics = response.data;
        });
    
        WordFactory.query({limit:latestItemsCount, sort:'-created'}, function(response) {
          $scope.latestWords = response.data;
        });
    
        $scope.$watch('search', function() {
          if($scope.search) {
            $location.search('query', $scope.search);
            $scope.searching = true;
            SearchFactory.query({limit:searchItemsCount, query:$scope.search}, function(response) {
              $scope.searching = false;
              $scope.searchResults = response.data;
              $scope.queryTime = response.$queryTime;
            });
          } else {
            $location.search('query', null);
            $scope.searchResults = [];
          }
        });
    
      }]);
    
      app.controller('NetworkController', ['$scope', '$state', '$stateParams', 'ArticleFactory', 'TopicFactory',
        function($scope, $state, $stateParams, ArticleFactory, TopicFactory) {
    
        $scope.type = $stateParams.type;
    
        var factory;
        if($stateParams.type === 'articles')
          factory = ArticleFactory;
        else if($stateParams.type === 'topics')
          factory = TopicFactory;
        else {
          console.log('unknown network type');
          return;
        }
    
        factory.get({id: $stateParams.id}, function(response) {
          $scope.model = response.data;
        });
    
      }]);
    
      /*
       * Article Controllers
       */
    
      app.controller('ArticlesIndexController', ['$scope', '$stateParams', 'ArticleFactory',
        function($scope, $stateParams, ArticleFactory, testService) {
    
        $scope.page = Math.max($stateParams.page || 1, 1);
        $scope.limit = pageSize;
    
        var loadedOnce = false;
        $scope.$watch('page', function(newVal, oldVal) {
          if(newVal != oldVal || !loadedOnce) {
            loadedOnce = true;
            ArticleFactory.query({
              skip: ($scope.page-1)*pageSize,
              limit: pageSize
            }, function(response) {
              $scope.articles = response.data;
              $scope.articlesMeta = response.meta;
              $scope.queryTime = response.$queryTime;
            });
          }
        });
    
      }]);
    
      app.controller('ArticlesShowController', ['$scope', '$stateParams', 'ArticleFactory',
        function($scope, $stateParams, ArticleFactory, testService) {
    
        ArticleFactory.get({id: $stateParams.id}, function(response) {
          $scope.article = response.data;
          $scope.article.text = createInitial($scope.article.text);
          $scope.article.date = formatDate($scope.article.date);
          $scope.article.created = formatDateTime($scope.article.created);
          $scope.article.modified = formatDateTime($scope.article.modified);
          $scope.articleMeta = response.meta;
          $scope.queryTime = response.$queryTime;
    
          // calculate percentage share
          var topicShareSeries = [],
              topics = $scope.article.topics;
          for(var i = 0; i < topics.length; i++) {
            var share = toPercent(topics[i].count / $scope.article.stats.wordCount);
            topics[i].share = share;
            topicShareSeries.push({name: topics[i].topic.name.ellipsize(20), y: share});
          }
    
          // highcharts data
          var topicShare = {
            chart: { type: 'pie' },
            credits: { enabled: false },
            plotOptions: {
              pie: { allowPointSelect: true }
            },
            title: { text: '' },
            tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' },
            series: [{
              name: 'Topic Share',
              colorByPoint: true,
              data: topicShareSeries
            }]
          };
    
          $scope.topicShare = topicShare;
        });
    
      }]);
    
      /*
       * Topic Controllers
       */
    
      app.controller('TopicsIndexController', ['$scope', 'TopicFactory',
        function($scope, TopicFactory) {
    
        TopicFactory.query(function(response) {
          $scope.topics = response.data;
          $scope.topicsMeta = response.meta;
          $scope.queryTime = response.$queryTime;
        });
    
      }]);
    
      app.controller('TopicsShowController', ['$scope', '$stateParams', 'TopicFactory',
        function($scope, $stateParams, TopicFactory) {
    
        TopicFactory.get({id: $stateParams.id}, function(response) {
          $scope.topic = response.data;
          $scope.topic.created = formatDateTime($scope.topic.created);
          $scope.topic.modified = formatDateTime($scope.topic.modified);
          $scope.topicMeta = response.meta;
          $scope.queryTime = response.$queryTime;
        });
    
      }]);
    
      /*
       * Word Controllers
       */
    
      app.controller('WordsIndexController', ['$scope', 'WordFactory',
        function($scope, WordFactory) {
    
        WordFactory.query(function(response) {
          $scope.words = response.data;
          $scope.wordsMeta = response.meta;
          $scope.queryTime = response.$queryTime;
        });
    
      }]);
    
      app.controller('WordsShowController', ['$scope', '$stateParams', 'WordFactory',
        function($scope, $stateParams, WordFactory) {
    
        WordFactory.get({id: $stateParams.id}, function(response) {
          $scope.word = response.data;
          $scope.word.created = formatDateTime($scope.word.created);
          $scope.wordMeta = response.meta;
          $scope.queryTime = response.$queryTime;
        });
    
      }]);
    
      /*
       * Directive Controllers
       */
    
       app.controller('PaginationController', ['$scope',
        function($scope) {
    
          $scope.calculatePages = function() {
            var pages = [],
                max   = Math.ceil($scope.total/$scope.limit*1.0),
                start = Math.max($scope.page - paginationPadding, 1),
                end   = Math.min(Math.max($scope.page + paginationPadding, start + paginationPadding * 2), max);
            for(var i = start; i <= end; i++) {
              pages.push(i);
            }
            $scope.pages = pages;
            $scope.maxPage = max;
          };
    
          $scope.$watchGroup(['total', 'page', 'limit'], function(newVal, oldVal) {
            if(!angular.equals(newVal, oldVal)) {
              $scope.calculatePages();
            }
          });
    
          $scope.calculatePages();
    
          $scope.changePage = function(page) {
            $scope.page = page;
          };
       }]);
    
    })();