@davoodmehraban89 / Factor-easy / commits / ffd101b
Fork 0

سامانه صدور فاکتور و حسابداری

feat(ux): safe context-scoped F2 fallback for additional modular form save commands

davoodmehraban89 committed Oct 12, 2026 at 00:13 UTC ffd101b61aedbfcf0d3f38ee8d817abc8b605ee1
1 file changed +28 -2
js/ux-hardening-r2.js
+28 -2
@@ -135,10 +135,32 @@
135 // Use the form's original validation/save function so its exact rejection reason remains visible.
136 let f2Running=false;
137 function f2Error(message){alert(message);return false}
138 + // Discover only explicitly safe save commands in modular forms. Never infer a
139 + // delete, approval, posting, membership or finalization action from button text.
140 + function f2SafeSaveButton(scope){
141 + const focused=document.activeElement;
142 + const section=focused&&scope.contains(focused)?focused.closest('form,.card,.accordion-item,[data-finora-task],.modal-card'):null;
143 + const root=section&&scope.contains(section)?section:scope;
144 + const candidates=[...root.querySelectorAll('button')].filter(button=>{
145 + if(button.disabled||!button.getClientRects().length)return false;
146 + const call=(button.getAttribute('onclick')||'').trim();
147 + const action=call.match(/^([A-Za-z][A-Za-z0-9_]*)\s*\(\s*\)\s*;?$/)?.[1]||'';
148 + return /^(?:afSave|entSave|p5Save|p6Save|commitSave)[A-Za-z0-9_]*$/.test(action)
149 + && !/حذف|ابطال|برگشت|قطعی|لغو|اعطای دسترسی|حذف کاربر/.test(button.textContent||'');
150 + });
151 + return candidates.length===1?candidates[0]:null;
152 + }
153 async function f2ConfirmCurrentForm(){
154 if(f2Running)return false;
155 if(!currentUser)return f2Error('ابتدا وارد حساب کاربری شوید.');
141 - if(document.querySelector('.modal-backdrop.active'))return f2Error('این پنجره تأیید اختصاصی دارد. برای جلوگیری از عملیات ناخواسته، دکمه ثبت همان پنجره را انتخاب کنید.');
156 + const openModal=[...document.querySelectorAll('.modal-backdrop.active')].at(-1);
157 + if(openModal){
158 + const safe=f2SafeSaveButton(openModal);
159 + if(!safe)return f2Error('این پنجره عملیات ذخیره قابل‌تشخیص و ایمن ندارد. از دکمه اختصاصی همان پنجره استفاده کنید.');
160 + const invalid=[...openModal.querySelectorAll('input,select,textarea')].find(el=>el.getClientRects().length&&!el.disabled&&!el.checkValidity());
161 + if(invalid){invalid.focus();return f2Error('این فیلد پنجره معتبر نیست: '+(invalid.labels?.[0]?.textContent?.trim()||invalid.placeholder||invalid.id||'فیلد'))}
162 + safe.click();return true;
163 + }
164 const view=document.querySelector('.view-pane.active')?.id;
165 if(!view)return f2Error('هیچ فرم فعالی برای تأیید پیدا نشد.');
166 const ids={
@@ -155,7 +177,11 @@
177 const companyPanel=$('company-save-btn')?.closest('.accordion-item');
178 const actionId=view==='view-settings'&&companyPanel?.contains(document.activeElement)?'commitSaveCompany':ids[view];
179 const handler=window[actionId];
158 - if(typeof handler!=='function')return f2Error('در این صفحه، فرم قابل ذخیره‌ای برای کلید F2 تعریف نشده است.');
180 + if(typeof handler!=='function'){
181 + const safe=f2SafeSaveButton(document.getElementById(view));
182 + if(!safe)return f2Error('برای این صفحه، فرم قابل ذخیره مشخص نیست یا چند عملیات وجود دارد. روی فیلد فرم موردنظر کلیک کنید و دوباره F2 بزنید.');
183 + safe.click();return true;
184 + }
185 // Do not turn approval of the manual voucher or a sales invoice into an implicit posting.
186 if(view==='view-journal'&&document.querySelector('[data-finora-task="documents"]')?.getClientRects().length>0)
187 return f2Error('قطعی‌سازی سند مالی فقط از مدیریت اسناد و با تأیید صریح انجام می‌شود؛ F2 برای ثبت پیش‌نویس است.');