{"size": 3770, "ext": "js", "max_stars_repo_path": "app/views/forms/view/view-articles.js", "max_stars_repo_name": "scbd/absch.cbd.int", "max_stars_repo_stars_event_min_datetime": "2020-03-20T23:54:58.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-23T19:41:59.000Z", "max_issues_repo_path": "app/views/forms/view/view-articles.js", "max_issues_repo_name": "scbd/absch.cbd.int", "max_issues_repo_issues_event_min_datetime": "2015-08-20T19:17:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-21T04:54:30.000Z", "max_forks_repo_path": "app/views/forms/view/view-articles.js", "max_forks_repo_name": "scbd/absch.cbd.int", "max_forks_repo_forks_event_min_datetime": "2016-12-12T20:33:22.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-15T08:53:41.000Z"}
{"max_stars_count": 2, "max_issues_count": 60, "max_forks_count": 6, "avg_line_length": 31.9491525424, "max_line_length": 132, "alphanum_fraction": 0.4904509284}
import app from 'app';
import _ from 'lodash';
import 'views/forms/view/view-articles';
import 'components/scbd-angularjs-services/main';
import 'services/main';
import 'ng-breadcrumbs';
import 'components/scbd-angularjs-controls/main';
import 'ck-editor-css';

  export { default as template } from './view-articles.html';

  export default ["$scope","$route", "realm", "$location", "locale", '$q', 'breadcrumbs','articlesService','ngMeta', '$routeParams',
    function($scope,$route, realm,  $location, locale, $q, breadcrumbs, articlesService, ngMeta, $routeParams) {
      
      $scope.currentPage=1;
      $scope.itemCount=0;
      $scope.itemsPerPage=9;

      $scope.status   = "loading";
      $scope.error    = null;

      var id = $route.current.params.id;
   
      if (id) 
        loadArticle(id);
      else
        
        loadArticles(1);

      //---------------------------------------------------------------------
      function loadArticle(id){
        
        articlesService.getArticle(id, true).then(function(data){
          
          $scope.article = data;
          
          if($scope.article){
              breadcrumbs.options = {'article_title':  $scope.article.title[locale] };
            
            ngMeta.resetMeta();   
            ngMeta.setTitle( $scope.article.title[locale]);
            ngMeta.setTag('description',  $scope.article.summary[locale]);
         }
        })
      }

      //---------------------------------------------------------------------
      function loadArticles(page){
        var ag = [];
        var agLimit = [];
        var agCount = [];
        var itemCountQuery;

console.log($routeParams);
        const tags = [encodeURIComponent($routeParams.type)]
        tags.push(encodeURIComponent(realm.value.replace(/\-.*/, '')))


        ag.push({"$match":{"$and":[{"adminTags": { $in : tags}}]}});
        ag.push({"$project" : {"title":1, "content":1, "coverImage":1, "meta":1, "summary":1}});

        agLimit = JSON.parse(JSON.stringify(ag))
        agLimit.push({"$sort" : {"meta.modifiedOn":-1}});
        agLimit.push({"$skip" : (page||1)*$scope.itemsPerPage});
        agLimit.push({"$limit": $scope.itemsPerPage });

        var qsLimit = {
            "ag" : JSON.stringify(agLimit)
        };
               
        if($scope.itemCount == 0){

            agCount = JSON.parse(JSON.stringify(ag))
            agCount.push({"$count" : "mycount"});
            var qsCount = {
                "ag" : JSON.stringify(agCount) 
            };
            
            itemCountQuery = articlesService.getArticles(qsCount).then(function(data){
                if(data)
                  return (data[0]||{}).mycount||0;
                return 0;
              })
        } 
        else{
          itemCountQuery = $scope.itemCount;
        }     
       
        $q.all([articlesService.getArticles(qsLimit), itemCountQuery]).then(function(results){
            $scope.articles      = results[0];
            $scope.itemCount     = results[1];
            $scope.pageCount     = Math.ceil( $scope.itemCount / $scope.itemsPerPage);
            $scope.currentPage   = page;
        })
      }

      //---------------------------------------------------------------------
      $scope.getSizedImage = function(url, size){
        // return url;
          return url && url
          .replace(/attachments.cbd.int\//, '$&'+size+'/')
          .replace(/\.s3-website-us-east-1\.amazonaws\.com\//, '$&'+size+'/')
      }
      //---------------------------------------------------------------------
      $scope.loadArticle = function(id){
        $location.path('/articles/' + id );
      }

      $scope.onPageChange = function(page){
        loadArticles(page);
      } 
   

    }
  ];


