|
| 1 | +const reportFormFieldsSelectorsMap = { |
| 2 | + "project": "select[name=project]", |
| 3 | + "taskActivity": "select[name=task_activity]", |
| 4 | + "workHours": "input[name=work_hours]", |
| 5 | + "description": "textarea[name=description]", |
| 6 | +}; |
| 7 | + |
1 | 8 | $(function() { |
2 | 9 | $(".bs-modal").each(function () { |
3 | 10 | $(this).modalForm({ |
@@ -26,8 +33,44 @@ $(function() { |
26 | 33 | document.activeElement.blur(); |
27 | 34 | }); |
28 | 35 |
|
29 | | - $("#modal").on('shown.bs.modal', function() { |
| 36 | + $("#modal").on('show.bs.modal', function () { |
| 37 | + let reportForm = $(this).find("form") |
| 38 | + if (reportForm.attr("name") === "create-report-form") { |
| 39 | + getFieldsValuesFromLocalStorage(reportForm); |
| 40 | + } |
| 41 | + |
| 42 | + }).on('shown.bs.modal', function() { |
30 | 43 | $("body").removeClass("modal-open"); |
31 | 44 | $(".custom-modal").removeClass("modal"); |
| 45 | + |
| 46 | + }).on('hide.bs.modal', function() { |
| 47 | + let reportForm = $(this).find("form") |
| 48 | + if (reportForm.attr("name") === "create-report-form") { |
| 49 | + storeReportFormValuesToLocalStorage(reportForm); |
| 50 | + } |
32 | 51 | }); |
| 52 | + |
| 53 | + resetLocalStorage(); |
33 | 54 | }); |
| 55 | + |
| 56 | +function storeReportFormValuesToLocalStorage(form) { |
| 57 | + var createReportFields = {} |
| 58 | + for (let[formField, formFieldSelector] of Object.entries(reportFormFieldsSelectorsMap)) { |
| 59 | + createReportFields[formField] = form.find(formFieldSelector).val() |
| 60 | + } |
| 61 | + localStorage.setItem("createReportFields", JSON.stringify(createReportFields)); |
| 62 | +} |
| 63 | + |
| 64 | +function getFieldsValuesFromLocalStorage(form) { |
| 65 | + if (localStorage.getItem("createReportFields") !== null) { |
| 66 | + var storedCreateReportFields = JSON.parse(localStorage.getItem("createReportFields")); |
| 67 | + |
| 68 | + for (let[formField, formFieldSelector] of Object.entries(reportFormFieldsSelectorsMap)) { |
| 69 | + form.find(formFieldSelector).val(storedCreateReportFields[formField]); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function resetLocalStorage() { |
| 75 | + localStorage.removeItem("createReportFields"); |
| 76 | +} |
0 commit comments