v1.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace zin;
  3. class chatBtn extends wg
  4. {
  5. public static function getPageCSS()
  6. {
  7. return <<<CSS
  8. #chat-container {position: fixed; left: 100px; right: 0; height: calc(100% - 36px); display: none;}
  9. .hide-menu #chat-container {left: 64px;}
  10. #xuan-chat-view {position: absolute; width: 100%; height: 100%; z-index: 10; display: none;}
  11. #xuan-chat-view #xx-embed-container {position: absolute; bottom: 0; left: 0; right: 0; top: 0;}
  12. .unconfigured {position: absolute; width: 330px; padding: 20px; right: 0; top: 0; bottom: 0; background: #fff; outline: 1px solid #eee;}
  13. .unconfigured > div {margin-bottom: 10px;}
  14. #xuan-chat-view .unconfigured {top: 50px;}
  15. #reload-ai-chat.disabled {cursor: wait; color: #999!important;}
  16. #chat-btn .notice-badge {position: absolute; bottom: 2px; right: 1px; line-height: 14px; height: 14px; min-width: 14px; text-align: center; display: inline-block; font-size: 12px; border-radius: 7px; opacity: 0; transform: scale(0); transition: .2s; transition-property: transform, opacity; padding: 0 2px;}
  17. #chat-btn .notice-badge.show {opacity: 1; transform: scale(1);}
  18. CSS;
  19. }
  20. public static function getPageJS()
  21. {
  22. global $app;
  23. $xuanUnconfiguredTip = sprintf($app->lang->index->chat->unconfiguredFormat, $app->lang->index->chat->chat, (common::hasPriv('setting', 'xuanxuan') ? sprintf($app->lang->index->chat->goConfigureFormat, helper::createLink('setting', 'xuanxuan'), $app->lang->index->chat->chat) : $app->lang->index->chat->contactAdminForHelp));
  24. $chatContainer = <<<HTML
  25. <div id="chat-container">
  26. <div id="xuan-chat-view">
  27. <div class="unconfigured text-gray">{$xuanUnconfiguredTip}</div>
  28. </div>
  29. </div>
  30. HTML;
  31. return <<<JAVASCRIPT
  32. window.toggleChatContainer = () =>
  33. {
  34. if(window.xuan) window.xuan[window.xuan.shown ? 'expand' : 'show']();
  35. \$('#chat-btn').toggleClass('active');
  36. \$('#chat-container').toggle();
  37. \$('#xuan-chat-view').toggle();
  38. };
  39. /* Setup xuan web chat. */
  40. window.setupXuan = () =>
  41. {
  42. if(!window.xuan)
  43. {
  44. window.initXuan();
  45. return;
  46. }
  47. /* Move xuan web client into #xuan-chat-view. */
  48. if(!document.querySelector('#xuan-chat-view #xx-embed-container'))
  49. {
  50. document.querySelector('#xuan-chat-view').prepend(document.querySelector('#xx-embed-container'));
  51. /* Set style into xuan frame. */
  52. let tries = 0;
  53. const xuanFrameWaitLoop = setInterval(function()
  54. {
  55. if(document.querySelector('#xx-embed-container iframe'))
  56. {
  57. clearInterval(xuanFrameWaitLoop);
  58. tries = 0;
  59. const chatsViewWaitLoop = setInterval(function()
  60. {
  61. if(document.querySelector('#xx-embed-container iframe').contentDocument.querySelector('.app-chats-menu'))
  62. {
  63. clearInterval(chatsViewWaitLoop);
  64. document.querySelector('#xx-embed-container iframe').contentDocument.querySelector('.app-chats-menu').style.cssText = 'width: 330px !important;';
  65. document.querySelector('#xx-embed-container iframe').contentDocument.querySelector('.app-chats-cache').style.cssText = 'right: 330px !important;';
  66. }
  67. else
  68. {
  69. if(++tries > 20) clearInterval(chatsViewWaitLoop);
  70. }
  71. }, 200);
  72. }
  73. else
  74. {
  75. if(++tries > 20) clearInterval(xuanFrameWaitLoop);
  76. }
  77. }, 200);
  78. }
  79. };
  80. (() =>
  81. {
  82. /* Insert chat views. */
  83. \$('body').append(`{$chatContainer}`);
  84. /* Handle clicking outside. */
  85. \$(document).on('click', e =>
  86. {
  87. if(!\$('#chat-container').length || \$('#chat-container').prop('style') && \$('#chat-container').prop('style').display !== 'block') return;
  88. if(\$(e.target).closest('#chat-switch,#xuan-chat-view>.unconfigured,#ai-chat-view,#chat-btn').length) return;
  89. window.toggleChatContainer();
  90. });
  91. })();
  92. JAVASCRIPT;
  93. }
  94. protected function build()
  95. {
  96. return div
  97. (
  98. set::id('chat-btn-container'),
  99. btn
  100. (
  101. set::id('chat-btn'),
  102. setClass('ghost px-1'),
  103. set('square', false),
  104. set::icon(img(set('src', 'static/svg/chat.svg'))),
  105. set('onclick', 'window.toggleChatContainer()'),
  106. span(setClass(array('badge', 'bg-danger', 'text-white', 'notice-badge')))
  107. )
  108. );
  109. }
  110. }