QRCode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const Component = use('util.Component');
  20. import UIComponentWindow from "../UIComponentWindow.js";
  21. export default def(class QRCodeView extends Component {
  22. static ID = 'ui.component.QRCodeView';
  23. static PROPERTIES = {
  24. value: {
  25. description: 'The text to encode in the QR code',
  26. },
  27. size: {
  28. value: 150,
  29. },
  30. enlarge_option: {
  31. value: true,
  32. }
  33. }
  34. static CSS = /*css*/`
  35. .qr-code {
  36. width: 100%;
  37. display: flex;
  38. justify-content: center;
  39. flex-direction: column;
  40. align-items: center;
  41. }
  42. .qr-code img {
  43. margin-bottom: 20px;
  44. }
  45. .has-enlarge-option {
  46. cursor: -moz-zoom-in;
  47. cursor: -webkit-zoom-in;
  48. cursor: zoom-in
  49. }
  50. `
  51. create_template ({ template }) {
  52. $(template).html(`
  53. <div class="qr-code opt-qr-code">
  54. </div>
  55. `);
  56. }
  57. on_ready ({ listen }) {
  58. listen('value', value => {
  59. // $(this.dom_).find('.qr-code').empty();
  60. new QRCode($(this.dom_).find('.qr-code').get(0), {
  61. text: value,
  62. // TODO: dynamic size
  63. width: this.get('size'),
  64. height: this.get('size'),
  65. currectLevel: QRCode.CorrectLevel.H,
  66. });
  67. if ( this.get('enlarge_option') ) {
  68. $(this.dom_).find('.qr-code img').addClass('has-enlarge-option');
  69. $(this.dom_).find('.qr-code img').on('click', async () => {
  70. UIComponentWindow({
  71. component: new QRCodeView({
  72. value: value,
  73. size: 400,
  74. enlarge_option: false,
  75. }),
  76. title: i18n('enlarged_qr_code'),
  77. backdrop: true,
  78. dominant: true,
  79. width: 550,
  80. height: 'auto',
  81. body_css: {
  82. width: 'initial',
  83. height: '100%',
  84. 'background-color': 'rgb(245 247 249)',
  85. 'backdrop-filter': 'blur(3px)',
  86. padding: '20px',
  87. },
  88. })
  89. });
  90. }
  91. });
  92. }
  93. });