No Description

theme-chooser.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. function initThemeChooser(settings) {
  2. var isInitialized = false;
  3. var currentThemeSystem; // don't set this directly. use setThemeSystem
  4. var currentStylesheetEl;
  5. var loadingEl = document.getElementById('loading');
  6. var systemSelectEl = document.querySelector('#theme-system-selector select');
  7. var themeSelectWrapEls = Array.prototype.slice.call( // convert to real array
  8. document.querySelectorAll('.selector[data-theme-system]')
  9. );
  10. systemSelectEl.addEventListener('change', function() {
  11. setThemeSystem(this.value);
  12. });
  13. setThemeSystem(systemSelectEl.value);
  14. themeSelectWrapEls.forEach(function(themeSelectWrapEl) {
  15. var themeSelectEl = themeSelectWrapEl.querySelector('select');
  16. themeSelectWrapEl.addEventListener('change', function() {
  17. setTheme(
  18. currentThemeSystem,
  19. themeSelectEl.options[themeSelectEl.selectedIndex].value
  20. );
  21. });
  22. });
  23. function setThemeSystem(themeSystem) {
  24. var selectedTheme;
  25. currentThemeSystem = themeSystem;
  26. themeSelectWrapEls.forEach(function(themeSelectWrapEl) {
  27. var themeSelectEl = themeSelectWrapEl.querySelector('select');
  28. if (themeSelectWrapEl.getAttribute('data-theme-system') === themeSystem) {
  29. selectedTheme = themeSelectEl.options[themeSelectEl.selectedIndex].value;
  30. themeSelectWrapEl.style.display = 'inline-block';
  31. } else {
  32. themeSelectWrapEl.style.display = 'none';
  33. }
  34. });
  35. setTheme(themeSystem, selectedTheme);
  36. }
  37. function setTheme(themeSystem, themeName) {
  38. var stylesheetUrl = generateStylesheetUrl(themeSystem, themeName);
  39. var stylesheetEl;
  40. function done() {
  41. if (!isInitialized) {
  42. isInitialized = true;
  43. settings.init(themeSystem);
  44. }
  45. else {
  46. settings.change(themeSystem);
  47. }
  48. showCredits(themeSystem, themeName);
  49. }
  50. if (stylesheetUrl) {
  51. stylesheetEl = document.createElement('link');
  52. stylesheetEl.setAttribute('rel', 'stylesheet');
  53. stylesheetEl.setAttribute('href', stylesheetUrl);
  54. document.querySelector('head').appendChild(stylesheetEl);
  55. loadingEl.style.display = 'inline';
  56. whenStylesheetLoaded(stylesheetEl, function() {
  57. if (currentStylesheetEl) {
  58. currentStylesheetEl.parentNode.removeChild(currentStylesheetEl);
  59. }
  60. currentStylesheetEl = stylesheetEl;
  61. loadingEl.style.display = 'none';
  62. done();
  63. });
  64. } else {
  65. if (currentStylesheetEl) {
  66. currentStylesheetEl.parentNode.removeChild(currentStylesheetEl);
  67. currentStylesheetEl = null
  68. }
  69. done();
  70. }
  71. }
  72. function generateStylesheetUrl(themeSystem, themeName) {
  73. if (themeSystem === 'bootstrap') {
  74. if (themeName) {
  75. return 'https://bootswatch.com/4/' + themeName + '/bootstrap.min.css';
  76. }
  77. else { // the default bootstrap theme
  78. return 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css';
  79. }
  80. }
  81. }
  82. function showCredits(themeSystem, themeName) {
  83. var creditId;
  84. if (themeSystem.match('bootstrap')) {
  85. if (themeName) {
  86. creditId = 'bootstrap-custom';
  87. }
  88. else {
  89. creditId = 'bootstrap-standard';
  90. }
  91. }
  92. Array.prototype.slice.call( // convert to real array
  93. document.querySelectorAll('.credits')
  94. ).forEach(function(creditEl) {
  95. if (creditEl.getAttribute('data-credit-id') === creditId) {
  96. creditEl.style.display = 'block';
  97. } else {
  98. creditEl.style.display = 'none';
  99. }
  100. })
  101. }
  102. function whenStylesheetLoaded(linkNode, callback) {
  103. var isReady = false;
  104. function ready() {
  105. if (!isReady) { // avoid double-call
  106. isReady = true;
  107. callback();
  108. }
  109. }
  110. linkNode.onload = ready; // does not work cross-browser
  111. setTimeout(ready, 2000); // max wait. also handles browsers that don't support onload
  112. }
  113. }