qrcode_modal.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {{define "qrcodeModal"}}
  2. <a-modal id="qrcode-modal" v-model="qrModal.visible" :title="qrModal.title"
  3. :dialog-style="isMobileQr ? { top: '18px' } : {}"
  4. :closable="true"
  5. :class="themeSwitcher.currentTheme"
  6. :footer="null" width="fit-content">
  7. <tr-qr-modal class="qr-modal">
  8. <template v-if="app.subSettings.enable && qrModal.subId">
  9. <tr-qr-box class="qr-box">
  10. <a-tag color="purple" class="qr-tag"><span>{{ i18n "pages.settings.subSettings"}}</span></a-tag>
  11. <tr-qr-bg class="qr-bg-sub">
  12. <tr-qr-bg-inner class="qr-bg-sub-inner">
  13. <canvas @click="copyToClipboard('qrCode-sub',genSubLink(qrModal.client.subId))" id="qrCode-sub" class="qr-cv"></canvas>
  14. </tr-qr-bg-inner>
  15. </tr-qr-bg>
  16. </tr-qr-box>
  17. <tr-qr-box class="qr-box">
  18. <a-tag color="purple" class="qr-tag"><span>{{ i18n "pages.settings.subSettings"}} Json</span></a-tag>
  19. <tr-qr-bg class="qr-bg-sub">
  20. <tr-qr-bg-inner class="qr-bg-sub-inner">
  21. <canvas @click="copyToClipboard('qrCode-subJson',genSubJsonLink(qrModal.client.subId))" id="qrCode-subJson" class="qr-cv"></canvas>
  22. </tr-qr-bg-inner>
  23. </tr-qr-bg>
  24. </tr-qr-box>
  25. </template>
  26. <template v-for="(row, index) in qrModal.qrcodes">
  27. <tr-qr-box class="qr-box">
  28. <a-tag color="green" class="qr-tag"><span>[[ row.remark ]]</span></a-tag>
  29. <tr-qr-bg class="qr-bg">
  30. <canvas @click="copyToClipboard('qrCode-'+index, row.link)" :id="'qrCode-'+index" class="qr-cv"></canvas>
  31. </tr-qr-bg>
  32. </tr-qr-box>
  33. </template>
  34. </tr-qr-modal>
  35. </a-modal>
  36. <script>
  37. const isMobileQr = window.innerWidth <= 768;
  38. const qrModal = {
  39. title: '',
  40. dbInbound: new DBInbound(),
  41. client: null,
  42. qrcodes: [],
  43. clipboard: null,
  44. visible: false,
  45. subId: '',
  46. show: function(title = '', dbInbound, client) {
  47. this.title = title;
  48. this.dbInbound = dbInbound;
  49. this.inbound = dbInbound.toInbound();
  50. this.client = client;
  51. this.subId = '';
  52. this.qrcodes = [];
  53. if (this.inbound.protocol == Protocols.WIREGUARD) {
  54. this.inbound.genInboundLinks(dbInbound.remark).split('\r\n').forEach((l, index) => {
  55. this.qrcodes.push({
  56. remark: "Peer " + (index + 1),
  57. link: l
  58. });
  59. });
  60. } else {
  61. this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, client).forEach(l => {
  62. this.qrcodes.push({
  63. remark: l.remark,
  64. link: l.link
  65. });
  66. });
  67. }
  68. this.visible = true;
  69. },
  70. close: function() {
  71. this.visible = false;
  72. },
  73. };
  74. const qrModalApp = new Vue({
  75. delimiters: ['[[', ']]'],
  76. el: '#qrcode-modal',
  77. data: {
  78. qrModal: qrModal,
  79. },
  80. methods: {
  81. copyToClipboard(elementId, content) {
  82. this.qrModal.clipboard = new ClipboardJS('#' + elementId, {
  83. text: () => content,
  84. });
  85. this.qrModal.clipboard.on('success', () => {
  86. app.$message.success('{{ i18n "copied" }}')
  87. this.qrModal.clipboard.destroy();
  88. });
  89. },
  90. setQrCode(elementId, content) {
  91. new QRious({
  92. element: document.querySelector('#' + elementId),
  93. size: 400,
  94. value: content,
  95. background: 'white',
  96. backgroundAlpha: 0,
  97. foreground: 'black',
  98. padding: 2,
  99. level: 'L'
  100. });
  101. },
  102. genSubLink(subID) {
  103. return app.subSettings.subURI + subID;
  104. },
  105. genSubJsonLink(subID) {
  106. return app.subSettings.subJsonURI + subID;
  107. },
  108. revertOverflow() {
  109. const elements = document.querySelectorAll(".qr-tag");
  110. elements.forEach((element) => {
  111. element.classList.remove("tr-marquee");
  112. element.children[0].style.animation = '';
  113. while (element.children.length > 1) {
  114. element.removeChild(element.lastChild);
  115. }
  116. });
  117. }
  118. },
  119. updated() {
  120. if (this.qrModal.visible) {
  121. fixOverflow();
  122. } else {
  123. this.revertOverflow();
  124. }
  125. if (qrModal.client && qrModal.client.subId) {
  126. qrModal.subId = qrModal.client.subId;
  127. this.setQrCode("qrCode-sub", this.genSubLink(qrModal.subId));
  128. this.setQrCode("qrCode-subJson", this.genSubJsonLink(qrModal.subId));
  129. }
  130. qrModal.qrcodes.forEach((element, index) => {
  131. this.setQrCode("qrCode-" + index, element.link);
  132. });
  133. }
  134. });
  135. function fixOverflow() {
  136. const elements = document.querySelectorAll(".qr-tag");
  137. elements.forEach((element) => {
  138. function isElementOverflowing(element) {
  139. const overflowX = element.offsetWidth < element.scrollWidth,
  140. overflowY = element.offsetHeight < element.scrollHeight;
  141. return overflowX || overflowY;
  142. }
  143. function wrapContentsInMarquee(element) {
  144. element.classList.add("tr-marquee");
  145. element.children[0].style.animation = `move-ltr ${
  146. (element.children[0].clientWidth / element.clientWidth) * 5
  147. }s ease-in-out infinite`;
  148. const marqueeText = element.children[0];
  149. if (element.children.length < 2) {
  150. for (let i = 0; i < 1; i++) {
  151. const marqueeText = element.children[0].cloneNode(true);
  152. element.children[0].after(marqueeText);
  153. }
  154. }
  155. }
  156. if (isElementOverflowing(element)) {
  157. wrapContentsInMarquee(element);
  158. }
  159. });
  160. }
  161. </script>
  162. {{end}}