js/ui.parts.js

  1. /**=====LICENSE STATEMENT START=====
  2. Translator++
  3. CAT (Computer-Assisted Translation) tools and framework to create quality
  4. translations and localizations efficiently.
  5. Copyright (C) 2018 Dreamsavior<dreamsavior@gmail.com>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. =====LICENSE STATEMENT END=====*/
  17. const UIParts = {};
  18. const CustomFields = require('www/js/customFields.js');
  19. const renderStyle = function($formElm, css={}) {
  20. for (let key in css) {
  21. $formElm.css(key, css[key]);
  22. }
  23. }
  24. /**
  25. * Creates a form dialog with custom fields.
  26. * @param {jQuery|Object} $elm - jQuery object or customField object representing the element to which the form will be appended.
  27. * @param {Object} [options={}] - Options for customization.
  28. * @param {boolean} [options.ignoreValidation=false] - Flag to submit the form regardless of validation errors.
  29. * @returns {Promise<Object>} - A promise that resolves to the form field values when the form is submitted successfully, or rejects if an error occurs.
  30. */
  31. UIParts.form = async function($elm, options={}) {
  32. options ||= {};
  33. options.css ||= {};
  34. options.overlayCss ||= {};
  35. // submit regardless validation error
  36. options.ignoreValidation ||= false;
  37. return new Promise((resolve, reject) => {
  38. var result;
  39. const $form = $(`<form class="uiCustomFields"></form>`);
  40. const customFields = new CustomFields($form, $elm);
  41. $form.data("customFields", customFields)
  42. $form.on("submit", function(e) {
  43. e.preventDefault();
  44. console.log("form submitted!");
  45. //$form.dialog("close");
  46. let validate = {};
  47. if (!options.ignoreValidation) {
  48. validate = customFields.validate();
  49. }
  50. if (!validate?.errors) {
  51. //result = customFields.getValues();
  52. result = customFields.getFormValues();
  53. $form.dialog("close");
  54. }
  55. });
  56. const dialogOptions = {...{
  57. title : options.title || t("Form"),
  58. autoOpen: false,
  59. modal :options.modal || true,
  60. width :options.width ||800,
  61. height :options.height ||600,
  62. minWidth:options.minWidth ||640,
  63. minHeight:options.minHeight ||320,
  64. show: {
  65. effect: "fade",
  66. duration: 200
  67. },
  68. hide: {
  69. effect: "fade",
  70. duration: 200
  71. },
  72. close:function(event, ui) {
  73. resolve(result);
  74. common.wait(500)
  75. .then(()=> {
  76. console.log("removing elm");
  77. $form.remove();
  78. });
  79. },
  80. open: function(event, ui) {
  81. console.log("form opened", ui, this);
  82. if (typeof options.onOpen == "function") options.onOpen.call($form, event, ui);
  83. },
  84. buttons:[
  85. {
  86. text: options.buttonCancelText || t("Cancel"),
  87. icon: "ui-icon-close",
  88. click: function() {
  89. $(this).dialog( "close" );
  90. }
  91. },
  92. {
  93. text: options.buttonOkText || t("Ok"),
  94. click: async function() {
  95. $form.submit();
  96. }
  97. }
  98. ]
  99. }, ...options}
  100. $form.dialog(dialogOptions);
  101. renderStyle($form.closest(".ui-dialog"), options.css);
  102. renderStyle($("body > .ui-widget-overlay.ui-front"), options.overlayCss);
  103. $form.dialog("open");
  104. })
  105. }
  106. module.exports = UIParts;