qrcode_modal.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. {{define "modals/qrcodeModal"}}
  2. <a-modal id="qrcode-modal" v-model="qrModal.visible" :title="qrModal.title"
  3. :dialog-style="DeviceUtils.isMobile() ? { 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="copy(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="copy(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="copy(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 qrModal = {
  38. title: '',
  39. dbInbound: new DBInbound(),
  40. client: null,
  41. qrcodes: [],
  42. visible: false,
  43. subId: '',
  44. show: function(title = '', dbInbound, client) {
  45. this.title = title;
  46. this.dbInbound = dbInbound;
  47. this.inbound = dbInbound.toInbound();
  48. this.client = client;
  49. this.subId = '';
  50. this.qrcodes = [];
  51. if (this.inbound.protocol == Protocols.WIREGUARD) {
  52. this.inbound.genInboundLinks(dbInbound.remark).split('\r\n').forEach((l, index) => {
  53. this.qrcodes.push({
  54. remark: "Peer " + (index + 1),
  55. link: l
  56. });
  57. });
  58. } else {
  59. this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, client).forEach(l => {
  60. this.qrcodes.push({
  61. remark: l.remark,
  62. link: l.link
  63. });
  64. });
  65. }
  66. this.visible = true;
  67. },
  68. close: function() {
  69. this.visible = false;
  70. },
  71. };
  72. const qrModalApp = new Vue({
  73. delimiters: ['[[', ']]'],
  74. el: '#qrcode-modal',
  75. data: {
  76. qrModal: qrModal,
  77. },
  78. methods: {
  79. copy(content) {
  80. ClipboardManager
  81. .copyText(content)
  82. .then(() => {
  83. app.$message.success('{{ i18n "copied" }}')
  84. })
  85. },
  86. setQrCode(elementId, content) {
  87. new QRious({
  88. element: document.querySelector('#' + elementId),
  89. size: 400,
  90. value: content,
  91. background: 'white',
  92. backgroundAlpha: 0,
  93. foreground: 'black',
  94. padding: 2,
  95. level: 'L'
  96. });
  97. },
  98. genSubLink(subID) {
  99. return app.subSettings.subURI + subID;
  100. },
  101. genSubJsonLink(subID) {
  102. return app.subSettings.subJsonURI + subID;
  103. },
  104. revertOverflow() {
  105. const elements = document.querySelectorAll(".qr-tag");
  106. elements.forEach((element) => {
  107. element.classList.remove("tr-marquee");
  108. element.children[0].style.animation = '';
  109. while (element.children.length > 1) {
  110. element.removeChild(element.lastChild);
  111. }
  112. });
  113. }
  114. },
  115. updated() {
  116. if (this.qrModal.visible) {
  117. fixOverflow();
  118. } else {
  119. this.revertOverflow();
  120. }
  121. if (qrModal.client && qrModal.client.subId) {
  122. qrModal.subId = qrModal.client.subId;
  123. this.setQrCode("qrCode-sub", this.genSubLink(qrModal.subId));
  124. this.setQrCode("qrCode-subJson", this.genSubJsonLink(qrModal.subId));
  125. }
  126. qrModal.qrcodes.forEach((element, index) => {
  127. this.setQrCode("qrCode-" + index, element.link);
  128. });
  129. }
  130. });
  131. function fixOverflow() {
  132. const elements = document.querySelectorAll(".qr-tag");
  133. elements.forEach((element) => {
  134. function isElementOverflowing(element) {
  135. const overflowX = element.offsetWidth < element.scrollWidth,
  136. overflowY = element.offsetHeight < element.scrollHeight;
  137. return overflowX || overflowY;
  138. }
  139. function wrapContentsInMarquee(element) {
  140. element.classList.add("tr-marquee");
  141. element.children[0].style.animation = `move-ltr ${
  142. (element.children[0].clientWidth / element.clientWidth) * 5
  143. }s ease-in-out infinite`;
  144. const marqueeText = element.children[0];
  145. if (element.children.length < 2) {
  146. for (let i = 0; i < 1; i++) {
  147. const marqueeText = element.children[0].cloneNode(true);
  148. element.children[0].after(marqueeText);
  149. }
  150. }
  151. }
  152. if (isElementOverflowing(element)) {
  153. wrapContentsInMarquee(element);
  154. }
  155. });
  156. }
  157. </script>
  158. {{end}}