pywebio.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Generate Doc online Demo link
  2. let DEMO_URL;
  3. if (localStorage.getItem('pywebio_doc_demo_url'))
  4. DEMO_URL = localStorage.getItem('pywebio_doc_demo_url');
  5. else
  6. DEMO_URL = 'http://pywebio-demos.pywebio.online/doc_demo';
  7. var parseHTML = function (str) {
  8. let tmp = document.implementation.createHTMLDocument();
  9. tmp.body.innerHTML = str;
  10. return tmp.body.children;
  11. };
  12. function ready(fn) {
  13. if (document.readyState != 'loading') {
  14. fn();
  15. } else {
  16. document.addEventListener('DOMContentLoaded', fn);
  17. }
  18. }
  19. let demo_url = new URL(DEMO_URL);
  20. ready(function () {
  21. let codes = document.querySelectorAll('.demo-cb');
  22. for (let c of codes) {
  23. let id = c.getAttribute('id');
  24. let ele = c.querySelector('.highlight');
  25. demo_url.searchParams.set("app", id);
  26. let node = parseHTML(`<a class="viewcode-back" href="${demo_url.href}" target="_blank">[Demo]</a>`)[0];
  27. ele.insertBefore(node, ele.firstChild);
  28. }
  29. });
  30. // Translated Version Prompt
  31. const user_lang = (navigator.language || navigator.userLanguage || 'en').toLowerCase().split('-')[0];
  32. const doc_lang = window.location.pathname.split('/')[1].split('-')[0];
  33. if (user_lang !== doc_lang && ['zh', 'en'].indexOf(user_lang) !== -1) {
  34. let u = new URL(window.location.href);
  35. let t = u.pathname.split('/');
  36. t[1] = user_lang === 'en' ? 'en' : 'zh-cn';
  37. u.pathname = t.join('/');
  38. let turl = u.href;
  39. let target_name, summary;
  40. if (user_lang === 'en') {
  41. target_name = 'English version';
  42. summary = 'The English version of this document is available. Switch to ';
  43. } else {
  44. target_name = '中文版';
  45. summary = '本文档有中文版本可用。切换至';
  46. }
  47. jQuery.ajax({
  48. url: turl,
  49. success: function () {
  50. jQuery(function () {
  51. jQuery('.rst-content>.document').before(`
  52. <div style="position: relative;
  53. padding: 1rem 1rem;
  54. margin-bottom: 0.8rem;
  55. margin-top: 0.8rem;
  56. border: 1px solid transparent;
  57. border-radius: .25rem;
  58. color: #0f5132;
  59. background-color: #d1e7dd;
  60. border-color: #badbcc;" role="alert">
  61. ${summary}<a href="${turl}" style="color: #0c4128;font-weight: 700;">${target_name}</a>
  62. </div>`);
  63. jQuery('body').append(`
  64. <div style="position: fixed;
  65. font-size: 14px;
  66. width: auto;
  67. right: 1rem;
  68. bottom: 0rem;
  69. padding: 0.4rem 1rem;
  70. margin-bottom: 3rem;
  71. border: 1px solid transparent;
  72. border-radius: .25rem;
  73. color: #0f5132;
  74. background-color: #d1e7dd;
  75. border-color: #badbcc;" role="alert">
  76. <span style="vertical-align:middle">
  77. <svg style="width: 1rem;height: 1rem;fill: currentColor;" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12.87 15.07l-2.54-2.51.03-.03A17.52 17.52 0 0014.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7l1.62-4.33L19.12 17h-3.24z"></path></svg>
  78. </span>
  79. <a href="${turl}" style="color: #0c4128;font-weight: 700;">${target_name}</a>
  80. </div>`);
  81. })
  82. },
  83. dataType: 'html'
  84. });
  85. }