comments.swig 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {%- if page.comments %}
  2. {%- if theme.injects.comment.length == 1 %}
  3. {%- set inject_item = theme.injects.comment[0] %}
  4. {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }}
  5. {%- elif theme.injects.comment.length > 1 %}
  6. {%- if theme.comments.style == 'buttons' %}
  7. <div class="comment-button-group">
  8. {%- for inject_item in theme.injects.comment %}
  9. <a class="btn comment-button {{ inject_item.locals.class }}">{{ inject_item.locals.button }}</a>
  10. {%- endfor %}
  11. </div>
  12. {%- for inject_item in theme.injects.comment %}
  13. <div class="comment-position {{ inject_item.locals.class }}">
  14. {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }}
  15. </div>
  16. {%- endfor %}
  17. <script{{ pjax }}>
  18. (function() {
  19. let commentButton = document.querySelectorAll('.comment-button');
  20. commentButton.forEach(element => {
  21. let commentClass = element.classList[2];
  22. element.addEventListener('click', () => {
  23. commentButton.forEach(rmActive => rmActive.classList.remove('active'));
  24. element.classList.add('active');
  25. document.querySelectorAll('.comment-position').forEach(rmActive => rmActive.classList.remove('active'));
  26. document.querySelector(`.comment-position.${commentClass}`).classList.add('active');
  27. if (CONFIG.comments.storage) {
  28. localStorage.setItem('comments_active', commentClass);
  29. }
  30. });
  31. });
  32. let { activeClass } = CONFIG.comments;
  33. if (CONFIG.comments.storage) {
  34. activeClass = localStorage.getItem('comments_active') || activeClass;
  35. }
  36. if (activeClass) {
  37. let activeButton = document.querySelector(`.comment-button.${activeClass}`);
  38. if (activeButton) {
  39. activeButton.click();
  40. }
  41. }
  42. })();
  43. </script>
  44. {%- elif theme.comments.style == 'tabs' %}
  45. <div class="tabs tabs-comment">
  46. <ul class="nav-tabs">
  47. {%- for inject_item in theme.injects.comment %}
  48. <li class="tab"><a href="#comment-{{ inject_item.locals.class }}">{{ inject_item.locals.button }}</a></li>
  49. {%- endfor %}
  50. </ul>
  51. <div class="tab-content">
  52. {%- for inject_item in theme.injects.comment %}
  53. <div class="tab-pane {{ inject_item.locals.class }}" id="comment-{{ inject_item.locals.class }}">
  54. {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }}
  55. </div>
  56. {%- endfor %}
  57. </div>
  58. </div>
  59. {%- endif %}
  60. {%- endif %}
  61. {%- endif %}
  62. <script>
  63. window.addEventListener('tabs:register', () => {
  64. let { activeClass } = CONFIG.comments;
  65. if (CONFIG.comments.storage) {
  66. activeClass = localStorage.getItem('comments_active') || activeClass;
  67. }
  68. if (activeClass) {
  69. let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
  70. if (activeTab) {
  71. activeTab.click();
  72. }
  73. }
  74. });
  75. if (CONFIG.comments.storage) {
  76. window.addEventListener('tabs:click', event => {
  77. if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
  78. let commentClass = event.target.classList[1];
  79. localStorage.setItem('comments_active', commentClass);
  80. });
  81. }
  82. </script>