Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. 'use strict';
  2. function titleClickHandler(event) {
  3. event.preventDefault();
  4. const clickedElement = this;
  5. console.log('Link was clicked!');
  6. /* [DONE] remove class 'active' from all article links */
  7. const activeLinks = document.querySelectorAll('.titles a.active');
  8. for (let activeLink of activeLinks){
  9. activeLink.classList.remove('active');
  10. }
  11. /* add class 'active' to the clicked link */
  12. clickedElement.classList.add('active');
  13. console.log('clickedElement:', clickedElement);
  14. const activeArticles = document.querySelectorAll('.posts .post.active');
  15. for(let activeArticle of activeArticles){
  16. activeArticle.classList.remove('active');
  17. }
  18. const articleSelector = clickedElement.getAttribute('href');
  19. const targetArticle = document.querySelector(articleSelector);
  20. targetArticle.classList.add('active');
  21. }
  22. const links = document.querySelectorAll('.titles a');
  23. for(let link of links) {
  24. link.addEventListener('click', titleClickHandler);
  25. }
  26. const optArticleSelector = '.post';
  27. const optTitleSelector = '.post-title';
  28. const optTitleListSelector = '.titles';
  29. const optArticleTagsSelector = '.post-tags .list';
  30. const optTagsListSelector = '.tags.list';
  31. let allTags = {};
  32. const optCloudClassCount = 5;
  33. const optCloudClassPrefix = 'tag-size';
  34.  
  35. function generateTitleLinks(){
  36. const titleList = document.querySelector('.list.titles');
  37. titleList.innerHTML = '';
  38. const articles = document.querySelectorAll('.posts .post');
  39. let html = '';
  40. for(let article of articles){
  41. const articleId = article.getAttribute('id');
  42. const articleTitleElement = article.querySelector('.post-title');
  43. const articleTitle = articleTitleElement.innerHTML;
  44. const linkHTML = '<li><a href="#' + articleId + '"><span>' + articleTitle + '</span></a></li>';
  45. html = html + linkHTML;
  46. }
  47. titleList.innerHTML = html;
  48. const links = document.querySelectorAll('.titles a');
  49. for(let link of links) {
  50. link.addEventListener('click', titleClickHandler);
  51. }
  52. }
  53.  
  54. generateTitleLinks();
  55.  
  56. function calculateTagsParams(tags) {
  57. const params = {
  58. max: 0,
  59. min: 999999,
  60. }
  61. return params;
  62. }
  63.  
  64. function calculateTagClass(){
  65. }
  66.  
  67. function calculateTagsParams(tags) {
  68. const params = {
  69. max: 0,
  70. min: 999999
  71. };
  72. for(let tag in tags){
  73. if(tags[tag] > params.max) {
  74. params.max = tags[tag];
  75. }
  76. if(tags[tag] < params.min) {
  77. params.min = tags[tag];
  78. }
  79. }
  80. return params;
  81. }
  82.  
  83. function generateTags(){
  84. const articles = document.querySelectorAll(optArticleSelector)
  85. for(let article of articles) {
  86. const tagsWrapper = article.querySelector(optArticleTagsSelector);
  87. let html = '';
  88. const tags = article.getAttribute('data-tags');
  89. const tagsArray = tags.split(' ');
  90. for(let tag of tagsArray) {
  91. const linkHTML = '<a href="tag-' + tag + '">' + tag + ' ' + '</a>';
  92. html = html + linkHTML;
  93. if(!allTags.hasOwnProperty(tag)){
  94. allTags[tag] = 1;
  95. } else {
  96. allTags[tag]++;
  97. }
  98. }
  99. tagsWrapper.innerHTML = html;
  100. }
  101.  
  102. const tagsParams = calculateTagsParams(allTags);
  103.  
  104. console.log(tagsParams);
  105.  
  106. const tagList = document.querySelector('.tags');
  107. let allTagsHTML = '';
  108. for(let tag in allTags){
  109. const link = '<li class="' + tag +'"><a href="#">'+ tag + '</a>'+'(' + allTags[tag] + ')'+'</li>'
  110. allTagsHTML = link + allTagsHTML
  111. }
  112. tagList.innerHTML = allTagsHTML;
  113.  
  114. }
  115. generateTags();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement