boot.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. function renderTopBar(title = ' ', subtitle = '', buttonHTML = '', showSearch = false, additionalHTML = '') {
  2. return `<style>@media (max-width:520px) {body{background:white} @media (prefers-color-scheme: dark) {body{background:black}}}</style><div class="topbar flx">
  3. <button onclick="window.history.back();" class="acss aobh no_print" style="padding:0.5em;font-size:16px;white-space:pre;transition-duration:0s" id="btn_back" tabindex="0" title="back"><b>く<span class="no_mobile"> back</span></b></button>
  4. <div id="topbar_loading"></div>
  5. <div class="dwhdt flx dwhdto"><div class="dwhdt">
  6. <b id="topbar_title_wrap">
  7. <b id="topbar_title_inwrap_main">
  8. <h4 id="topbar_title">` + title + '</h4><p4 id="topbar_subtitle">' + subtitle + `</p4>
  9. </b>
  10. <b id="topbar_title_inwrap_secondary">
  11. <div id="topbar_title_inwarp_sec_buf"></div><h4 id="topbar_inwrap_title"></h4><p4 id="topbar_inwrap_subtitle"></p4>
  12. </b>
  13. </b>
  14. </div></div>
  15. <div class="flx dwhda">` + buttonHTML + `</div>` + ((showSearch) ? `
  16. <div class="pgsbtn flx no_print">
  17. <label for="search_box" id="topbtn_search" class="dwhdab" onclick="if (event.shiftKey) { window.open('/dongwaa/_dig/', '_blank') }" title="Search box\nHold shift to open in new tab">
  18. <img alt="Search" src="` + resourceNETpath + `image/search.png" draggable="false">
  19. <style>#topbtn_search{background-color:rgba(160,160,255,.1)} #topbtn_search:hover{background-color:rgba(160,160,255,.25)} #topbtn_search:hover:active > img{transform:rotate(6deg)}</style>
  20. </label>
  21. <form onsubmit="boot('/dongwaa/_dig/'.concat(document.getElementById('search_box').value));this.blur();return false"><input id="search_box" name="dw" type="search" placeholder="Search for something..." title="Search for something..."></form>
  22. </div>` : ``) + additionalHTML + `</div>`
  23. }
  24. function renderBottomBar(path) {
  25. return `<div class="bottom_wrp flx">` + renderBottomBarButtons(bottombarbuttons, path) + `</div><div id="divheadbuffer"></div>`
  26. }
  27. function renderBottomBarButtons(Buttons, path) {
  28. var results = `<form class="tabview flx" tabindex="1002"><div class="tabview-wrp" tabindex="1001">`;
  29. var btn = [];
  30. var hit = false;
  31. Buttons.forEach(btns => {
  32. btn = btns.split(',');
  33. try {
  34. if (((path != '/dongwaa/') ? (btn[1] != '/dongwaa/') : (btn[1] === '/dongwaa/')) && path.startsWith(btn[1])) {
  35. results += `
  36. <button type="button" tabindex="1000" class="tabview-sel tabview-sel-s">
  37. <img src="` + btn[2] + `" draggable="false" alt=""><p3>` + btn[0] + `</p3><div class="tabview-sel-tri"></div>
  38. </button>
  39. `
  40. hit = true
  41. } else {
  42. results += `
  43. <button onclick="boot('` + btn[1] + `')" class="tabview-sel tabview-sel-h">
  44. <img src="` + btn[2] + `" draggable="false" alt=""><p3>` + btn[0] + `</p3>
  45. </button>
  46. `
  47. }
  48. } catch (error) { };
  49. });
  50. if (!hit) {
  51. results += `
  52. <button type="button" tabindex="1000" class="tabview-sel tabview-sel-s">
  53. <img src="` + resourceNETpath + `image/nullicon.png" draggable="false" alt=""><p3>` + path + `</p3><div class="tabview-sel-tri"></div>
  54. </button>
  55. `
  56. }
  57. results += `</div><button type="button" tabindex="1000" class="tabview-close"><img alt="Close" draggable="false" src="` + resourceNETpath + `image/circle-cross.png"> <p3 style="opacity:0.8"><b>Close</b></p3></button></form><style>@media only screen and (max-width:520px) {.bottom_wrp > a > .ckimg{display:none!important}}</style>`
  58. return results
  59. }
  60. var popArray = []
  61. var LoadingStatusQueue = 0;
  62. var errorLoadingCSS = ['<style>#topbar_loading::before{border-color:#FF4238!important;background-color:#FF4238;animation:none}</style>', '<style>#topbar_loading::before{border-color:#FF4238!important;background-color:#FF4238;color:white;animation:none;content:"✘"}</style>']
  63. function LoadingStatus(type, presistant = false, title = '', subtitle = '') {
  64. if (type != 'hide' && LoadingStatusQueue > 0) {
  65. //console.log('[LoadingStatus] ' + type + ' queued')
  66. setTimeout(() => {LoadingStatus(type, presistant, title, subtitle)}, 500, type, presistant, title, subtitle);
  67. return false;
  68. }
  69. var l = document.getElementById("topbar_loading");
  70. var presistTime = 2850;
  71. switch (type) {
  72. case 'success':
  73. LoadingStatus('show')
  74. presistTime = 2850; LoadingStatusQueue += 1; setTimeout(() => {LoadingStatusQueue -= 1}, presistTime + 750)
  75. l.innerHTML = '<style>#topbar_loading::before{border-color:#33ff99!important;background-color:#33ff99;color:green;animation:none;content:"✓"}</style>'
  76. break
  77. case 'warning':
  78. LoadingStatus('show')
  79. presistTime = 6350; LoadingStatusQueue += 1; setTimeout(() => {LoadingStatusQueue -= 1}, presistTime + 750)
  80. l.innerHTML = '<style>#topbar_loading::before{border-color:#ffaa33!important;animation:none}</style>'
  81. setTimeout(() => {l.style.opacity = 0.3}, 600)
  82. setTimeout(() => {l.style.opacity = 1}, 1300)
  83. setTimeout(() => {l.style.opacity = 0.3}, 2000)
  84. setTimeout(() => {l.style.opacity = 1}, 2700)
  85. break
  86. case 'error':
  87. LoadingStatus('show')
  88. presistTime = 6850; LoadingStatusQueue += 1; setTimeout(() => {LoadingStatusQueue -= 1}, presistTime + 750)
  89. l.innerHTML = errorLoadingCSS[1]
  90. setTimeout(() => {l.innerHTML = errorLoadingCSS[0]}, 850)
  91. setTimeout(() => {l.innerHTML = errorLoadingCSS[1]}, 1350)
  92. setTimeout(() => {l.innerHTML = errorLoadingCSS[0]}, 1850)
  93. setTimeout(() => {l.innerHTML = errorLoadingCSS[1]}, 2350)
  94. setTimeout(() => {l.innerHTML = errorLoadingCSS[0]}, 2850)
  95. setTimeout(() => {l.innerHTML = errorLoadingCSS[1]}, 3350)
  96. break
  97. case 'show':
  98. LoadingStatusQueue += 1; setTimeout(() => {LoadingStatusQueue -= 1}, 50)
  99. l.innerHTML = "";
  100. l.style.display = 'unset';
  101. setTimeout(() => {
  102. l.style.opacity = 1;
  103. l.style.transform = 'translateX(0)';
  104. l.style.width = '2.25em';
  105. }, 25)
  106. presistant = true;
  107. break
  108. case 'hide':
  109. LoadingStatusQueue += 1; setTimeout(() => {LoadingStatusQueue -= 1}, 700)
  110. l.style.opacity = 0;
  111. l.style.transform = 'translateX(-1.5em)';
  112. setTimeout(() => {
  113. l.style.width = '0';
  114. }, 0) //50
  115. setTimeout(() => {
  116. l.style.display = 'none';
  117. l.innerHTML = "";
  118. }, 650) //700
  119. presistant = true;
  120. break
  121. }
  122. if (title) {
  123. document.getElementById("topbar_title_inwarp_sec_buf").style.maxHeight = '0'
  124. document.getElementById("topbar_title_inwrap_secondary").style.maxHeight = '0'
  125. document.getElementById("topbar_title_inwrap_main").style.opacity = 0
  126. document.getElementById("topbar_title_inwrap_secondary").style.opacity = 0
  127. document.getElementById("topbar_inwrap_title").innerText = title
  128. document.getElementById("topbar_inwrap_subtitle").innerText = subtitle
  129. setTimeout(() => {
  130. document.getElementById("topbar_title_inwrap_main").style.display = 'none'
  131. document.getElementById("topbar_title_inwrap_secondary").style.opacity = 1
  132. document.getElementById("topbar_title_inwrap_secondary").style.maxHeight = 'unset'
  133. document.getElementById("topbar_title_inwarp_sec_buf").style.maxHeight = '3.5em'
  134. }, 200)
  135. }
  136. if (!presistant) {
  137. setTimeout(() => {
  138. LoadingStatus('hide')
  139. }, presistTime - 300)
  140. setTimeout(() => {
  141. document.getElementById("topbar_title_inwrap_secondary").style.opacity = 0
  142. document.getElementById("topbar_title_inwarp_sec_buf").style.maxHeight = '0'
  143. }, presistTime - 200)
  144. setTimeout(() => {
  145. document.getElementById("topbar_title_inwrap_main").style.display = 'block'
  146. document.getElementById("topbar_inwrap_title").innerText = ''
  147. document.getElementById("topbar_inwrap_subtitle").innerText = ''
  148. setTimeout(() => {document.getElementById("topbar_title_inwrap_main").style.opacity = 1}, 200)
  149. }, presistTime)
  150. }
  151. }
  152. //make back button works
  153. window.onpopstate = function (event) {
  154. popArray.pop()
  155. boot(decodeURIComponent((event.state) ? event.state.plate : window.location.pathname), true)
  156. }
  157. //getting acc info in cookies
  158. function getCookie(name) {
  159. let matches = document.cookie.match(new RegExp(
  160. "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  161. ));
  162. return matches ? decodeURIComponent(matches[1]) : undefined;
  163. }
  164. const authemail = getCookie('authemail');
  165. var signinlevel = (authemail != undefined) ? 1 : 0 ;
  166. //add css into html
  167. function installCSS(targetCSS) {
  168. var stylesheet = document.createElement('link')
  169. stylesheet.href = resourceNETpath.concat(targetCSS)
  170. stylesheet.rel = 'stylesheet'
  171. document.getElementsByTagName('head')[0].appendChild(stylesheet)
  172. }
  173. installCSS('webel.css');
  174. //<a href=""> -> <a onclick="boot()">
  175. function hrefInterrupt(event) {
  176. if (!(event.target.getAttribute('href').startsWith('http://') || event.target.getAttribute('href').startsWith('https://'))) {
  177. event.preventDefault();
  178. boot(event.target.getAttribute('href'));
  179. }
  180. }
  181. //list of post-script cleanup
  182. function postCleanup() {
  183. document.querySelectorAll('a').forEach(link => link.addEventListener('mousedown', hrefInterrupt));
  184. exe('cleanup')
  185. }
  186. //load new page
  187. var prev_boot_call = 'none';
  188. var isBootRunning = false;
  189. function boot(path, noHistory) {
  190. //check if boot is already running, prevent accidental double-clicking and overwriting
  191. if (isBootRunning) {
  192. console.log('[boot] boot aready running! current process: '.concat(prev_boot_call).concat(', ').concat(path).concat(' will be skipped loading.'));
  193. return false; //stop running boot
  194. }
  195. isBootRunning = true;
  196. //check should add current URL into history, then change URL shown in browser
  197. (noHistory) ? history.replaceState(null, window.title, path) : history.pushState({ plate: path }, window.title, path);
  198. window.scrollTo({ top: 0, behavior: 'smooth' });
  199. //window.removeEventListener('scroll', dwLib_scrolling); //remove infinite scroll script from lib
  200. //document.querySelectorAll('.lib_div').forEach(e => e.remove()); //remove lib div from body
  201. if (path == '/' || path == '' || path == undefined) { //home page
  202. path = '/';
  203. } else if (path.startsWith('/!')) { //reserved for api call, invalid for HTML, no init and no script
  204. path = undefined;
  205. } else { //any other page
  206. //path = path;
  207. }
  208. if (prev_boot_call != path) { //if already init page then don't init it again
  209. prev_boot_call = path;
  210. document.getElementById('core').innerHTML = init(path);
  211. postCleanup();
  212. }
  213. exe(path); //execute scripts
  214. isBootRunning = false;
  215. return true;
  216. }
  217. boot(window.location.pathname, true)