inlinedform.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. return this.isOpen.get() && input && input.value.replaceAll(/\s +$/gm, '');
  45. },
  46. events() {
  47. return [
  48. {
  49. 'click .js-close-inlined-form': this.close,
  50. 'click .js-open-inlined-form': this.open,
  51. // Pressing Ctrl+Enter should submit the form
  52. 'keydown form textarea'(evt) {
  53. if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
  54. this.find('button[type=submit]').click();
  55. }
  56. },
  57. // Close the inlined form when after its submission
  58. submit() {
  59. if (this.currentData().autoclose !== false) {
  60. Tracker.afterFlush(() => {
  61. this.close();
  62. });
  63. }
  64. },
  65. },
  66. ];
  67. },
  68. }).register('inlinedForm');
  69. // Press escape to close the currently opened inlinedForm
  70. EscapeActions.register(
  71. 'inlinedForm',
  72. () => {
  73. currentlyOpenedForm.get().close();
  74. },
  75. () => {
  76. return currentlyOpenedForm.get() !== null;
  77. },
  78. {
  79. enabledOnClick: false,
  80. },
  81. );
  82. // submit on click outside
  83. //document.addEventListener('click', function(evt) {
  84. // const openedForm = currentlyOpenedForm.get();
  85. // const isClickOutside = $(evt.target).closest('.js-inlined-form').length === 0;
  86. // if (openedForm && isClickOutside) {
  87. // $('.js-inlined-form button[type=submit]').click();
  88. // openedForm.close();
  89. // }
  90. //}, true);