CodeEntryView.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. export default def(class CodeEntryView extends Component {
  21. static ID = 'ui.component.CodeEntryView';
  22. static PROPERTIES = {
  23. value: {},
  24. error: {},
  25. is_checking_code: {},
  26. }
  27. static RENDER_MODE = Component.NO_SHADOW;
  28. static CSS = /*css*/`
  29. .wrapper {
  30. -webkit-font-smoothing: antialiased;
  31. -moz-osx-font-smoothing: grayscale;
  32. color: #3e5362;
  33. }
  34. fieldset[name=number-code] {
  35. display: flex;
  36. justify-content: space-between;
  37. gap: 5px;
  38. }
  39. .digit-input {
  40. box-sizing: border-box;
  41. flex-grow: 1;
  42. height: 50px;
  43. font-size: 25px;
  44. text-align: center;
  45. border-radius: 0.5rem;
  46. -moz-appearance: textfield;
  47. border: 2px solid #9b9b9b;
  48. color: #485660;
  49. }
  50. .digit-input::-webkit-outer-spin-button,
  51. .digit-input::-webkit-inner-spin-button {
  52. -webkit-appearance: none;
  53. margin: 0;
  54. }
  55. .confirm-code-hyphen {
  56. display: inline-block;
  57. flex-grow: 2;
  58. text-align: center;
  59. font-size: 40px;
  60. font-weight: 300;
  61. }
  62. `
  63. create_template ({ template }) {
  64. // TODO: static member for strings
  65. const submit_btn_txt = i18n('confirm_code_generic_submit');
  66. $(template).html(/*html*/`
  67. <div class="wrapper">
  68. <form>
  69. <div class="error"></div>
  70. <fieldset name="number-code" style="border: none; padding:0;" data-number-code-form>
  71. <input class="digit-input" type="number" min='0' max='9' name='number-code-0' data-number-code-input='0' required />
  72. <input class="digit-input" type="number" min='0' max='9' name='number-code-1' data-number-code-input='1' required />
  73. <input class="digit-input" type="number" min='0' max='9' name='number-code-2' data-number-code-input='2' required />
  74. <span class="confirm-code-hyphen">-</span>
  75. <input class="digit-input" type="number" min='0' max='9' name='number-code-3' data-number-code-input='3' required />
  76. <input class="digit-input" type="number" min='0' max='9' name='number-code-4' data-number-code-input='4' required />
  77. <input class="digit-input" type="number" min='0' max='9' name='number-code-5' data-number-code-input='5' required />
  78. </fieldset>
  79. <button type="submit" class="button button-block button-primary code-confirm-btn" style="margin-top:10px;" disabled>${
  80. submit_btn_txt
  81. }</button>
  82. </form>
  83. </div>
  84. `);
  85. }
  86. on_focus () {
  87. $(this.dom_).find('.digit-input').first().focus();
  88. }
  89. on_ready ({ listen }) {
  90. listen('error', (error) => {
  91. if ( ! error ) return $(this.dom_).find('.error').hide();
  92. $(this.dom_).find('.error').text(error).show();
  93. });
  94. listen('value', value => {
  95. // clear the inputs
  96. if ( value === undefined ) {
  97. $(this.dom_).find('.digit-input').val('');
  98. return;
  99. }
  100. })
  101. listen('is_checking_code', (is_checking_code, { old_value }) => {
  102. if ( old_value === is_checking_code ) return;
  103. if ( old_value === undefined ) return;
  104. const $button = $(this.dom_).find('.code-confirm-btn');
  105. if ( is_checking_code ) {
  106. // set animation
  107. $button.prop('disabled', true);
  108. $button.html(`<svg style="width:20px; margin-top: 5px;" xmlns="http://www.w3.org/2000/svg" height="24" width="24" viewBox="0 0 24 24"><title>circle anim</title><g fill="#fff" class="nc-icon-wrapper"><g class="nc-loop-circle-24-icon-f"><path d="M12 24a12 12 0 1 1 12-12 12.013 12.013 0 0 1-12 12zm0-22a10 10 0 1 0 10 10A10.011 10.011 0 0 0 12 2z" fill="#eee" opacity=".4"></path><path d="M24 12h-2A10.011 10.011 0 0 0 12 2V0a12.013 12.013 0 0 1 12 12z" data-color="color-2"></path></g><style>.nc-loop-circle-24-icon-f{--animation-duration:0.5s;transform-origin:12px 12px;animation:nc-loop-circle-anim var(--animation-duration) infinite linear}@keyframes nc-loop-circle-anim{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}</style></g></svg>`);
  109. return;
  110. }
  111. const submit_btn_txt = i18n('confirm_code_generic_try_again');
  112. $button.html(submit_btn_txt);
  113. $button.prop('disabled', false);
  114. });
  115. const that = this;
  116. $(this.dom_).find('.code-confirm-btn').on('click submit', function(e){
  117. e.preventDefault();
  118. e.stopPropagation();
  119. const $button = $(this);
  120. $button.prop('disabled', true);
  121. $button.closest('.error').hide();
  122. that.set('is_checking_code', true);
  123. // force update to trigger the listener
  124. that.set('value', that.get('value'));
  125. })
  126. // Elements
  127. const numberCodeForm = this.dom_.querySelector('[data-number-code-form]');
  128. const numberCodeInputs = [...numberCodeForm.querySelectorAll('[data-number-code-input]')];
  129. // Event listeners
  130. numberCodeForm.addEventListener('input', ({ target }) => {
  131. const inputLength = target.value.length || 0;
  132. let currentIndex = Number(target.dataset.numberCodeInput);
  133. if(inputLength === 2){
  134. const inputValues = target.value.split('');
  135. target.value = inputValues[0];
  136. }
  137. else if (inputLength > 1) {
  138. const inputValues = target.value.split('');
  139. inputValues.forEach((value, valueIndex) => {
  140. const nextValueIndex = currentIndex + valueIndex;
  141. if (nextValueIndex >= numberCodeInputs.length) { return; }
  142. numberCodeInputs[nextValueIndex].value = value;
  143. });
  144. currentIndex += inputValues.length - 2;
  145. }
  146. const nextIndex = currentIndex + 1;
  147. if (nextIndex < numberCodeInputs.length) {
  148. numberCodeInputs[nextIndex].focus();
  149. }
  150. // Concatenate all inputs into one string to create the final code
  151. let current_code = '';
  152. for(let i=0; i< numberCodeInputs.length; i++){
  153. current_code += numberCodeInputs[i].value;
  154. }
  155. const submit_btn_txt = i18n('confirm_code_generic_submit');
  156. $(this.dom_).find('.code-confirm-btn').html(submit_btn_txt);
  157. // Automatically submit if 6 digits entered
  158. if(current_code.length === 6){
  159. $(this.dom_).find('.code-confirm-btn').prop('disabled', false);
  160. this.set('value', current_code);
  161. this.set('is_checking_code', true);
  162. } else {
  163. $(this.dom_).find('.code-confirm-btn').prop('disabled', true);
  164. }
  165. });
  166. numberCodeForm.addEventListener('keydown', (e) => {
  167. const { code, target } = e;
  168. const currentIndex = Number(target.dataset.numberCodeInput);
  169. const previousIndex = currentIndex - 1;
  170. const nextIndex = currentIndex + 1;
  171. const hasPreviousIndex = previousIndex >= 0;
  172. const hasNextIndex = nextIndex <= numberCodeInputs.length - 1
  173. switch (code) {
  174. case 'ArrowLeft':
  175. case 'ArrowUp':
  176. if (hasPreviousIndex) {
  177. numberCodeInputs[previousIndex].focus();
  178. }
  179. e.preventDefault();
  180. break;
  181. case 'ArrowRight':
  182. case 'ArrowDown':
  183. if (hasNextIndex) {
  184. numberCodeInputs[nextIndex].focus();
  185. }
  186. e.preventDefault();
  187. break;
  188. case 'Backspace':
  189. if (!e.target.value.length && hasPreviousIndex) {
  190. numberCodeInputs[previousIndex].value = null;
  191. numberCodeInputs[previousIndex].focus();
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. });
  198. }
  199. })