Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.3 KiB

  1. {>header}
  2. <script src="https://code.jquery.com/jquery-3.3.1.min.js"
  3. integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  4. crossorigin="anonymous">
  5. </script>
  6. <script>
  7. var postsToLoad= [];
  8. /**
  9. * Runs a get request using ajax
  10. */
  11. function runAjax(url, successCallBack, errorCallBack)
  12. {
  13. console.log(url);
  14. $.ajax({
  15. type:'GET',
  16. url: url,
  17. crossDomain: true,
  18. dataType: "json",
  19. success: successCallBack,
  20. error:errorCallBack,
  21. timeout: 3000
  22. });
  23. }
  24. /** Lazy loads youtube videos on the page
  25. */
  26. function lazyLoad()
  27. {
  28. var youtube = document.querySelectorAll( ".youtube" );
  29. for (var i = 0; i < youtube.length; i++) {
  30. var source = "https://img.youtube.com/vi/"+ youtube[i].dataset.embed +"/sddefault.jpg";
  31. var image = new Image();
  32. image.src = source;
  33. image.addEventListener( "load", function() {
  34. youtube[ i ].appendChild( image );
  35. }( i ) );
  36. youtube[i].addEventListener( "click", function() {
  37. var iframe = document.createElement( "iframe" );
  38. iframe.setAttribute( "frameborder", "0" );
  39. iframe.setAttribute( "allowfullscreen", "" );
  40. iframe.setAttribute( "src", "https://www.youtube.com/embed/"+ this.dataset.embed +"?rel=0&showinfo=0&autoplay=1" );
  41. this.innerHTML = "";
  42. this.appendChild( iframe );
  43. } );
  44. };
  45. }
  46. function buildPostHTML(post)
  47. {
  48. var pic = '';
  49. if(post.hasPicture)
  50. {
  51. pic = '<img src="/blogContent/headerImages/' + post.picture_url + '" style="width:100%;">';
  52. }
  53. return '<div class="blogPost">' + pic + '<div class="p-4"><b><h3>' + post.name + '</h3></b><h5>' + post.published + '</span></h5><div>' + post.blogBody + '<p class="text-center"><a class="btn btn-secondary btn-lg text-white" href="https://jrtechs.net/' + post.categoryURL + '/' + post.url + '"><b>Read More &raquo;</b></a></p></div></div></div><br><br>';
  54. }
  55. function addPosts(id)
  56. {
  57. return new Promise(function(resolve, reject)
  58. {
  59. runAjax("/api/preview/" + id,
  60. (html)=>
  61. {
  62. var content = buildPostHTML(html)
  63. $("#newPosts").append(content);
  64. //lazy loads youtube videos
  65. lazyLoad();
  66. //highlights code
  67. document.querySelectorAll('pre code').forEach((block) => {
  68. hljs.highlightBlock(block);
  69. });
  70. //renders latex math
  71. MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
  72. resolve();
  73. },
  74. (err)=>
  75. {
  76. console.log(err);
  77. reject();
  78. });
  79. })
  80. }
  81. async function loadMore()
  82. {
  83. for(var i = 0; i < 3; i++)
  84. {
  85. if(postsToLoad.length == 0)
  86. {
  87. $('#morePostsBtn').html('');
  88. break;
  89. }
  90. await addPosts(postsToLoad.shift());
  91. }
  92. console.log("Wham");
  93. }
  94. $(document).ready(function()
  95. {
  96. runAjax("/api/getPostsIds/{categoryID}",
  97. (idsList)=>
  98. {
  99. postsToLoad=idsList;
  100. },
  101. (err)=>
  102. {
  103. console.log(err);
  104. reject();
  105. });
  106. });
  107. </script>
  108. <br><br><br><br><br>
  109. <div class="container">
  110. <div class="row">
  111. <div class="col-md-8 col-12">
  112. {for post in posts}
  113. <div class="blogPost">
  114. {if post.hasPicture}
  115. <img src="/blogContent/headerImages/{post.picture_url}" style="width:100%;">
  116. {/if}
  117. <div class="p-4">
  118. <h3><b>{post.name}</b></h3>
  119. <h5>
  120. <span class="w3-opacity">{post.published}</span>
  121. </h5>
  122. {post.blogBody}
  123. {if preview}
  124. <p class="text-center">
  125. <a class="btn btn-secondary btn-lg text-white"
  126. href="https://jrtechs.net/{post.categoryURL}/{post.url}"><b>Read More &raquo;</b></a>
  127. </p>
  128. {/if}
  129. </div>
  130. </div>
  131. <br>
  132. <br>
  133. {else}
  134. <div class="row p-lg-0">
  135. <h1 class="align-content-center">Page Not Found</h1>
  136. <div class="align-content-center">
  137. <img src="/includes/img/404.jpg" alt="Page not found" width="70%" />
  138. </div>
  139. </div>
  140. <br><br>
  141. {/for}
  142. <div id="newPosts"></div>
  143. {if preview}
  144. <div class="justify-content-center w-100 blogPost" id="morePostsBtn">
  145. <a class="btn btn-secondary btn-lg btn-block text-white w-100 text-center" id="readMore"
  146. onclick="loadMore()"><b>More Posts &raquo;</b></a>
  147. </div>
  148. {/if}
  149. <br>
  150. </div>
  151. <div class="col-md-4 col-12">
  152. {>sideBar}
  153. </div>
  154. </div>
  155. </div>
  156. {footer}