app.js 4.5 KB

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