app=angular.module('app', ["ngTagsInput","ngRoute"]); app.config(function($routeProvider, $locationProvider) { // $locationProvider.html5Mode(true); // $compileProvider.debugInfoEnabled(false); $routeProvider .when("/", { templateUrl: 'main.html', controller: 'main' }) .when("/keywords", { templateUrl: 'keywords.html', controller: 'keywords' }) .when("/sources", { templateUrl: 'sources.html', controller: 'sources' }) .otherwise({ redirectTo: "/" }) }); app.controller('sources', function($scope,$http,$location) { $scope.sources=[]; $scope.parsed_articles=[]; $scope.sourceCategory=1; $scope.dev=$location.search().dev; var categories = ["PR","News"]; $http.get("/pymnts/sources").then( function(data){ $scope.sources=data.data.sources.map( function(e){ e.category=categories[e.category]; return e; }); }, function(data){ } ); $scope.parseSource = function(source,name,sel,rss,category, add) { url="/pymnts/source/test/"; if(add==1) url="/pymnts/source/add/"; $http.post(url, { "source": source, "selector":sel, "rss":rss, "name":name, "category": category }).then( function(data) { if(!add) $scope.parsed_articles=data.data.links; }, function(data){ } ); }; }); app.controller('main', function($scope,$http,$sce) { $scope.category="-1"; $scope.news=[]; $scope.newsArticle=""; $scope.selectedArticle = {}; $scope.curPage=1; $scope.totPages=1; $scope.pages=[] var lastPage=0; var itemsPerPage=15; $scope.keywords=[]; $scope.selected_kw=[]; $scope.filtered = function(q) { q=q.toLowerCase(); return $scope.keywords.filter(function(e) { return e.toLowerCase().indexOf(q) > -1}); }; $scope.addKeyword = function(k) { c=$scope.selected_kw.filter(function(el) { return el.text==k; }).length; if (c>0) return; $scope.selected_kw.push({text:k}); $scope.filter(); }; $scope.getKeywords = function() { $http.get("/pymnts/keywords").then( function(data) { //$scope.keywords=data.data.keywords.map(function(e) { return {text:e} }); $scope.keywords=data.data.keywords; }, function(data){ } ); }; $scope.getKeywords(); $scope.getArticle = function(el) { $scope.selectedArticle = el; $http.get("/pymnts/article/"+el._id).then( function(data) { $scope.newsArticle=$sce.trustAsHtml(data.data.articles.html); }, function(data){ } ); }; $scope.reset = function() { $scope.mindate=undefined; $scope.maxdate=undefined; $scope.site=undefined; $scope.selected_kw=[]; $scope.minweight="1"; $scope.title=undefined; $scope.category="-1"; $scope.filter(); }; $scope.filter = function(page) { var kw = $scope.selected_kw.map(function(e){return e.text}); $http.post("/pymnts/articles/",{"mindate":$scope.mindate, "maxdate":$scope.maxdate, "site":$scope.site, "keyword":kw, "minweight":$scope.minweight, "title":$scope.title, "category":$scope.category, "page":page}).then( function(data) { if(page==undefined) $scope.curPage=1; $scope.news=data.data.articles.map( function(el) { el.kw=el.kw.filter(function(item, pos) { return el.kw.indexOf(item) == pos; }); return el; }); //$scope.news=data.data.articles; $scope.totPages=Math.ceil(data.data.count/itemsPerPage); var arr = [] var maxPage=Math.min($scope.curPage+2,$scope.totPages); var minPage=Math.max($scope.curPage-2,1); for(var i=minPage; i<=maxPage;i++) arr.push(i); $scope.pages=arr; }, function(data) { } ); }; $scope.filter(); $scope.changePage = function(newPage) { if (newPage > $scope.totPages) newPage=$scope.totPages; if (newPage < 1) newPage=1; if (newPage==lastPage) return; lastPage=newPage; $scope.curPage=newPage; //clamp $scope.filter(newPage); }; $scope.pages = function() { var arr = []; for(var i=$scope.curPage -2; i<=$scope.curPage+2;i++) if (i>=1 && i<=$scope.totPages) arr.push(i); return arr; }; $scope.isCurrent = function(page){ return page === $scope.curPage; } $scope.delete_article = function(id) { $http.get("/pymnts/article/delete/"+id).then( function(data) { $scope.news=$scope.news.filter(function(el) { return !(el._id == id) } ); }, function(data){ } ); }; }); app.controller('keywords', function($scope,$http,$location) { $scope.keywords=[]; $http.get("/pymnts/keywordsw").then( function(data){ $scope.keywords = data.data.keywords; }, function(data){ } ); });