app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. var lastPage=0;
  55. var itemsPerPage=15;
  56. $scope.keywords=[];
  57. $scope.selected_kw=[];
  58. $scope.filtered = function(q) {
  59. q=q.toLowerCase();
  60. return $scope.keywords.filter(function(e) { return e.toLowerCase().indexOf(q) > -1});
  61. };
  62. $scope.addKeyword = function(k) {
  63. c=$scope.selected_kw.filter(function(el) { return el.text==k; }).length;
  64. if (c>0)
  65. return;
  66. $scope.selected_kw.push({text:k});
  67. $scope.filter();
  68. };
  69. $scope.getKeywords = function() {
  70. $http.get("/pymnts/keywords").then(
  71. function(data) {
  72. //$scope.keywords=data.data.keywords.map(function(e) { return {text:e} });
  73. $scope.keywords=data.data.keywords;
  74. },
  75. function(data){
  76. }
  77. );
  78. };
  79. $scope.getKeywords();
  80. $scope.getArticle = function(el) {
  81. $http.get("/pymnts/article/"+el._id).then(
  82. function(data) {
  83. $scope.newsArticle=$sce.trustAsHtml(data.data.articles.html);
  84. },
  85. function(data){
  86. }
  87. );
  88. };
  89. $scope.reset = function() {
  90. $scope.mindate=undefined;
  91. $scope.maxdate=undefined;
  92. $scope.site=undefined;
  93. $scope.selected_kw=[];
  94. $scope.minweight="1";
  95. $scope.title=undefined;
  96. $scope.category="-1";
  97. $scope.filter();
  98. };
  99. $scope.filter = function(page) {
  100. var kw = $scope.selected_kw.map(function(e){return e.text});
  101. $http.post("/pymnts/articles/",{"mindate":$scope.mindate,
  102. "maxdate":$scope.maxdate,
  103. "site":$scope.site,
  104. "keyword":kw,
  105. "minweight":$scope.minweight,
  106. "title":$scope.title,
  107. "category":$scope.category,
  108. "page":page}).then(
  109. function(data) {
  110. if(page==undefined)
  111. $scope.curPage=1;
  112. $scope.news=data.data.articles.map(
  113. function(el) {
  114. el.kw=el.kw.filter(function(item, pos) {
  115. return el.kw.indexOf(item) == pos;
  116. });
  117. return el;
  118. });
  119. //$scope.news=data.data.articles;
  120. $scope.totPages=Math.ceil(data.data.count/itemsPerPage);
  121. },
  122. function(data) {
  123. }
  124. );
  125. };
  126. $scope.filter();
  127. $scope.changePage = function(newPage) {
  128. if (newPage > $scope.totPages)
  129. newPage=$scope.totPages;
  130. if (newPage < 1)
  131. newPage=1;
  132. if (newPage==lastPage)
  133. return;
  134. lastPage=newPage;
  135. $scope.curPage=newPage; //clamp
  136. $scope.filter(newPage);
  137. };
  138. $scope.delete_article = function(id) {
  139. $http.get("/pymnts/article/delete/"+id).then(
  140. function(data) {
  141. $scope.news=$scope.news.filter(function(el) { return !(el._id == id) } );
  142. },
  143. function(data){
  144. }
  145. );
  146. };
  147. });