app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. app=angular.module('app', ["ngTagsInput","ngRoute"]);
  2. app.config(function($routeProvider, $locationProvider) {
  3. // $locationProvider.html5Mode(true);
  4. // $compileProvider.debugInfoEnabled(false);
  5. $routeProvider
  6. .when("/", {
  7. templateUrl: 'main.html',
  8. controller: 'main'
  9. })
  10. .when("/sources", {
  11. templateUrl: 'sources.html',
  12. controller: 'sources'
  13. })
  14. .otherwise({
  15. redirectTo: "/"
  16. })
  17. });
  18. app.controller('sources', function($scope,$http) {
  19. $scope.sources=[];
  20. $scope.parsed_articles=[];
  21. $scope.sourceCategory=1;
  22. var categories = ["PR","News"];
  23. $http.get("/pymnts/sources").then(
  24. function(data){
  25. $scope.sources=data.data.sources.map(
  26. function(e){
  27. e.category=categories[e.category];
  28. return e;
  29. });
  30. },
  31. function(data){
  32. }
  33. );
  34. $scope.parseSource = function(source,name,sel,rss,category, add) {
  35. url="/pymnts/source/test/";
  36. if(add==1)
  37. url="/pymnts/source/add/";
  38. $http.post(url, { "source": source, "selector":sel, "rss":rss, "name":name, "category": category }).then(
  39. function(data) {
  40. if(!add)
  41. $scope.parsed_articles=data.data.links;
  42. },
  43. function(data){
  44. }
  45. );
  46. };
  47. });
  48. app.controller('main', function($scope,$http,$sce) {
  49. $scope.category="-1";
  50. $scope.news=[];
  51. $scope.newsArticle="";
  52. $scope.curPage=1;
  53. $scope.totPages=1;
  54. $scope.pages=[]
  55. var lastPage=0;
  56. var itemsPerPage=15;
  57. $scope.keywords=[];
  58. $scope.selected_kw=[];
  59. $scope.filtered = function(q) {
  60. q=q.toLowerCase();
  61. return $scope.keywords.filter(function(e) { return e.toLowerCase().indexOf(q) > -1});
  62. };
  63. $scope.addKeyword = function(k) {
  64. c=$scope.selected_kw.filter(function(el) { return el.text==k; }).length;
  65. if (c>0)
  66. return;
  67. $scope.selected_kw.push({text:k});
  68. $scope.filter();
  69. };
  70. $scope.getKeywords = function() {
  71. $http.get("/pymnts/keywords").then(
  72. function(data) {
  73. //$scope.keywords=data.data.keywords.map(function(e) { return {text:e} });
  74. $scope.keywords=data.data.keywords;
  75. },
  76. function(data){
  77. }
  78. );
  79. };
  80. $scope.getKeywords();
  81. $scope.getArticle = function(el) {
  82. $http.get("/pymnts/article/"+el._id).then(
  83. function(data) {
  84. $scope.newsArticle=$sce.trustAsHtml(data.data.articles.html);
  85. },
  86. function(data){
  87. }
  88. );
  89. };
  90. $scope.reset = function() {
  91. $scope.mindate=undefined;
  92. $scope.maxdate=undefined;
  93. $scope.site=undefined;
  94. $scope.selected_kw=[];
  95. $scope.minweight="1";
  96. $scope.title=undefined;
  97. $scope.category="-1";
  98. $scope.filter();
  99. };
  100. $scope.filter = function(page) {
  101. var kw = $scope.selected_kw.map(function(e){return e.text});
  102. $http.post("/pymnts/articles/",{"mindate":$scope.mindate,
  103. "maxdate":$scope.maxdate,
  104. "site":$scope.site,
  105. "keyword":kw,
  106. "minweight":$scope.minweight,
  107. "title":$scope.title,
  108. "category":$scope.category,
  109. "page":page}).then(
  110. function(data) {
  111. if(page==undefined)
  112. $scope.curPage=1;
  113. $scope.news=data.data.articles.map(
  114. function(el) {
  115. el.kw=el.kw.filter(function(item, pos) {
  116. return el.kw.indexOf(item) == pos;
  117. });
  118. return el;
  119. });
  120. //$scope.news=data.data.articles;
  121. $scope.totPages=Math.ceil(data.data.count/itemsPerPage);
  122. var arr = []
  123. var maxPage=Math.min($scope.curPage+2,$scope.totPages);
  124. var minPage=Math.max($scope.curPage-2,1);
  125. for(var i=minPage; i<=maxPages;i++)
  126. arr.push(i);
  127. $scope.pages=arr;
  128. },
  129. function(data) {
  130. }
  131. );
  132. };
  133. $scope.filter();
  134. $scope.changePage = function(newPage) {
  135. if (newPage > $scope.totPages)
  136. newPage=$scope.totPages;
  137. if (newPage < 1)
  138. newPage=1;
  139. if (newPage==lastPage)
  140. return;
  141. lastPage=newPage;
  142. $scope.curPage=newPage; //clamp
  143. $scope.filter(newPage);
  144. };
  145. $scope.delete_article = function(id) {
  146. $http.get("/pymnts/article/delete/"+id).then(
  147. function(data) {
  148. $scope.news=$scope.news.filter(function(el) { return !(el._id == id) } );
  149. },
  150. function(data){
  151. }
  152. );
  153. };
  154. });