inlinedform.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // A inlined form is used to provide a quick edition of single field for a given
  2. // document. Clicking on a edit button should display the form to edit the field
  3. // value. The form can then be submited, or just closed.
  4. //
  5. // When the form is closed we save non-submitted values in memory to avoid any
  6. // data loss.
  7. //
  8. // Usage:
  9. //
  10. // +inlineForm
  11. // // the content when the form is open
  12. // else
  13. // // the content when the form is close (optional)
  14. // We can only have one inlined form element opened at a time
  15. const currentlyOpenedForm = new ReactiveVar(null);
  16. InlinedForm = BlazeComponent.extendComponent({
  17. template() {
  18. return 'inlinedForm';
  19. },
  20. onCreated() {
  21. this.isOpen = new ReactiveVar(false);
  22. },
  23. onDestroyed() {
  24. currentlyOpenedForm.set(null);
  25. },
  26. open(evt) {
  27. if (evt) {
  28. evt.preventDefault();
  29. // Close currently opened form, if any
  30. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  31. } else {
  32. // Close currently opened form, if any
  33. EscapeActions.executeUpTo('inlinedForm');
  34. }
  35. this.isOpen.set(true);
  36. currentlyOpenedForm.set(this);
  37. },
  38. close() {
  39. this.isOpen.set(false);
  40. currentlyOpenedForm.set(null);
  41. },
  42. getValue() {
  43. const input = this.find('textarea,input[type=text]');
  44. // \s without \n + unicode (https://developer.mozilla.org/de/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space)
  45. return this.isOpen.get() && input && input.value.replaceAll(/[ \f\r\t\v]+$/gm, '');
  46. },
  47. events() {
  48. return [
  49. {
  50. 'click .js-close-inlined-form': this.close,
  51. 'click .js-open-inlined-form': this.open,
  52. // Pressing Ctrl+Enter should submit the form
  53. 'keydown form textarea'(evt) {
  54. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  55. this.find('button[type=submit]').click();
  56. }
  57. },
  58. // Close the inlined form when after its submission
  59. submit() {
  60. if (this.currentData().autoclose !== false) {
  61. Tracker.afterFlush(() => {
  62. this.close();
  63. });
  64. }
  65. },
  66. },
  67. ];
  68. },
  69. }).register('inlinedForm');
  70. // Press escape to close the currently opened inlinedForm
  71. EscapeActions.register(
  72. 'inlinedForm',
  73. () => {
  74. currentlyOpenedForm.get().close();
  75. },
  76. () => {
  77. return currentlyOpenedForm.get() !== null;
  78. },
  79. {
  80. enabledOnClick: false,
  81. },
  82. );
  83. // submit on click outside
  84. //document.addEventListener('click', function(evt) {
  85. // const openedForm = currentlyOpenedForm.get();
  86. // const isClickOutside = $(evt.target).closest('.js-inlined-form').length === 0;
  87. // if (openedForm && isClickOutside) {
  88. // $('.js-inlined-form button[type=submit]').click();
  89. // openedForm.close();
  90. // }
  91. //}, true);