utils.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. function BxArtificerUtils(oOptions)
  2. {
  3. this._sActionUri = oOptions.sActionUri;
  4. this._sActionUrl = oOptions.sActionUrl;
  5. this._sObject = oOptions.sObject;
  6. this._aHtmlIds = undefined == oOptions.aHtmlIds ? {} : oOptions.aHtmlIds;
  7. this._sColorScheme = undefined == oOptions.sColorScheme ? 'auto' : oOptions.sColorScheme;
  8. if(htmx != undefined) {
  9. // 4xx and 5xx responses stay out of the page, same as htmx 2.
  10. htmx.config.noSwap = [204, 304, '4xx', '5xx'];
  11. htmx.on('htmx:before:settle', function(evt) {
  12. // outerHTML settle fires on the first inserted node. A leading
  13. // newline makes that a text node, which has no querySelectorAll.
  14. var aNodes = evt.target && evt.target.nodeType === 1 ? [evt.target] : ((evt.detail && evt.detail.newContent) || []);
  15. for(var i = 0; i < aNodes.length; i++) {
  16. var oNode = aNodes[i];
  17. if(!oNode || oNode.nodeType !== 1 || oNode.id == 'bx-content-preload')
  18. continue;
  19. $(oNode).bxProcessHtml();
  20. }
  21. });
  22. }
  23. this.init();
  24. }
  25. BxArtificerUtils.prototype.init = function()
  26. {
  27. var $this = this;
  28. switch(this._sColorScheme) {
  29. case 'auto':
  30. window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
  31. $this.setColorSchemeIcon();
  32. $this.setColorSchemeHtml();
  33. $this.onColorSchemeChange();
  34. });
  35. break;
  36. case 'light_only':
  37. this.setColorScheme(1);
  38. break;
  39. case 'dark_only':
  40. this.setColorScheme(2);
  41. break;
  42. }
  43. this.setColorSchemeHtml();
  44. $(document).ready(function() {
  45. $this.setColorSchemeIcon();
  46. $this.onColorSchemeChange();
  47. });
  48. };
  49. BxArtificerUtils.prototype.setColorScheme = function(iCode)
  50. {
  51. switch(iCode) {
  52. case 0:
  53. localStorage.removeItem('theme');
  54. break;
  55. case 1:
  56. localStorage.theme = 'sun'
  57. break;
  58. case 2:
  59. localStorage.theme = 'dark'
  60. break;
  61. }
  62. this.setColorSchemeIcon();
  63. this.setColorSchemeHtml();
  64. this.onColorSchemeChange(iCode);
  65. };
  66. BxArtificerUtils.prototype.isColorSchemeDark = function()
  67. {
  68. return localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches);
  69. };
  70. BxArtificerUtils.prototype.setColorSchemeHtml = function()
  71. {
  72. if(this.isColorSchemeDark())
  73. $('html').addClass('dark')
  74. else
  75. $('html').removeClass('dark')
  76. };
  77. BxArtificerUtils.prototype.setColorSchemeIcon = function()
  78. {
  79. $('.bx-sb-theme-current').html('<i class="sys-icon ' + (this.isColorSchemeDark() ? 'moon' : 'sun') + '"></i>').bxProcessHtml();
  80. };
  81. BxArtificerUtils.prototype.onColorSchemeChange = function(iCode)
  82. {
  83. if(typeof glOnColorSchemeChange === 'undefined' || !(glOnColorSchemeChange instanceof Array))
  84. return;
  85. if(typeof iCode === 'undefined' || !iCode)
  86. iCode = this.isColorSchemeDark() ? 2 : 1;
  87. for(var i = 0; i < glOnColorSchemeChange.length; i++)
  88. if(typeof glOnColorSchemeChange[i] === "function")
  89. glOnColorSchemeChange[i](iCode);
  90. };
  91. BxArtificerUtils.prototype.getColorSchemeMenu = function() {
  92. $('#bx-sb-theme-switcher-menu').dolPopup({
  93. pointer: {
  94. el: $('.bx-sb-theme-switcher')
  95. },
  96. moveToDocRoot: true,
  97. cssClass: 'bx-popup-menu'
  98. });
  99. };
  100. BxArtificerUtils.prototype.getAddContentMenu = function(sMenu, e, sPosition) {
  101. $('.bx-popup-applied:visible').dolPopupHide();
  102. var sSidebar = 'account';
  103. if(bx_sidebar_active(sSidebar))
  104. bx_sidebar_toggle(sSidebar);
  105. bx_menu_popup_inline(sMenu, e, {});
  106. };
  107. BxArtificerUtils.prototype.getNotificationsMenu = function(sMenu, e, sPosition, oOptions) {
  108. $('.bx-popup-applied:visible').dolPopupHide();
  109. var sSidebar = 'account';
  110. if(bx_sidebar_active(sSidebar))
  111. bx_sidebar_toggle(sSidebar);
  112. oOptions = $.extend({}, {pointer: {align:'right'}, cssClass: ''}, oOptions);
  113. bx_menu_popup(sMenu, e, oOptions);
  114. };
  115. BxArtificerUtils.prototype.mmenuClickAl = function(oElement) {
  116. var sClass = 'bx-menu-tab-active';
  117. var oItem = $(oElement).parent().addClass(sClass);
  118. oItem.siblings().removeClass(sClass);
  119. oItem.siblings('.bx-menu-item-more-auto').find('li').removeClass(sClass);
  120. oItem.parents('li:first').siblings().removeClass(sClass);
  121. if(oItem.parents('.bx-popup-applied.bx-popup-menu'))
  122. $('.bx-popup-applied:visible').dolPopupHide();
  123. };
  124. BxArtificerUtils.prototype.submenuClickBl = function(oElement) {
  125. var oItems = $(oElement).parents('ul:first').find('li');
  126. var iPosNew = oItems.index($(oElement).parent());
  127. var iPosOld = oItems.index($(oElement).parent().siblings('.bx-menu-tab-active'));
  128. var sClassAdd = iPosOld > iPosNew ? 'ltr' : 'rtl';
  129. $('#bx-content-wrapper').addClass(sClassAdd);
  130. };
  131. BxArtificerUtils.prototype.submenuClickAl = function(oElement) {
  132. var sClass = 'bx-menu-tab-active';
  133. var oItem = $(oElement).parent().addClass(sClass);
  134. oItem.siblings().removeClass(sClass);
  135. oItem.siblings('.bx-menu-item-more-auto').find('li').removeClass(sClass);
  136. oItem.parents('li:first').siblings().removeClass(sClass);
  137. if(oItem.parents('.bx-popup-applied.bx-popup-menu'))
  138. $('.bx-popup-applied:visible').dolPopupHide();
  139. var sSidebar = 'site';
  140. if(bx_sidebar_active(sSidebar))
  141. bx_sidebar_toggle(sSidebar);
  142. };