CodeEntryView.js 8.4 KB

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