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

controllers.js

Blame
  • user avatar
    Eike Cochu authored
    added query builder to create more complex queries to database service
    added reverse query to fetch articles for topics, topics for words
    renamed service to dbServicetype in rest resources
    added pair class for query equality
    b724e923
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    controllers.js 4.68 KiB
    (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 = [];
          }
        });
    
      }]);
    
      /*
       * ARTICLES
       */
    
      app.controller('ArticlesIndexController', ['$scope', '$stateParams', 'ArticleFactory',
        function($scope, $stateParams, ArticleFactory, testService) {
    
        $scope.page = Math.max($stateParams.page || 1, 1);
        $scope.limit = pageSize;
    
        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.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;
        });
    
      }]);
    
      /*
       * TOPICS
       */
    
      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;
        });
    
      }]);
    
      /*
       * WORDS
       */
    
      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;
        });
    
      }]);
    
      /*
       * DIRECTIVES
       */
    
       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();
    
       }]);
    
    })();