@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 8abf347
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 17:00 UTC

fix: rehydrate invoice and purchase product options and unclipped searchable list

davoodmehraban89 committed Oct 11, 2026 at 14:38 UTC 8abf3472a9861fc2b362b74e7de005006e68cfb3
1 file changed +25 -6
js/ux-hardening-r2.js
+25 -6
@@ -15,24 +15,43 @@
15 }
16 function searchNorm(v){return String(v||'').toLowerCase().replace(/[يى]/g,'ی').replace(/ك/g,'ک').replace(/[۰-۹]/g,d=>'۰۱۲۳۴۵۶۷۸۹'.indexOf(d)).replace(/[أإآ]/g,'ا').replace(/\s+/g,' ').trim()}
17 function comboOptions(select,q){
18 - const query=searchNorm(q),tokens=query.split(' ').filter(Boolean);return [...select.options].filter(o=>o.value).map(o=>({o,hay:searchNorm([o.dataset.search,searchMeta(o.value,o.textContent)].filter(Boolean).join(' '))})).filter(x=>!tokens.length||tokens.every(t=>x.hay.includes(t))).sort((a,b)=>{if(!query)return 0;const as=a.hay.startsWith(query)?0:a.hay.includes(' '+query)?1:2,bs=b.hay.startsWith(query)?0:b.hay.includes(' '+query)?1:2;return as-bs}).map(x=>x.o);
18 + const query=searchNorm(q),tokens=query.split(' ').filter(Boolean),productSelect=select.matches('.row-product-select,.pur-prod');return [...select.options].filter(o=>o.value).map(o=>({o,hay:searchNorm([o.dataset.search,productSelect?o.textContent:searchMeta(o.value,o.textContent)].filter(Boolean).join(' '))})).filter(x=>!tokens.length||tokens.every(t=>x.hay.includes(t))).sort((a,b)=>{if(!query)return 0;const as=a.hay.startsWith(query)?0:a.hay.includes(' '+query)?1:2,bs=b.hay.startsWith(query)?0:b.hay.includes(' '+query)?1:2;return as-bs}).map(x=>x.o);
19 }
20 function comboLabel(o){return String(o?.textContent||'').trim()}
21 + function syncProductSelectOptions(select){
22 + if(!select||!select.matches('.row-product-select,.pur-prod')||typeof getMyProducts!=='function')return;
23 + const products=getMyProducts();if(!Array.isArray(products))return;
24 + const purchase=select.classList.contains('pur-prod'),existing=[...select.options].filter(o=>o.value),selectedId=select.value;
25 + const rows=products.filter(p=>p&&p.id!=null).map(p=>({id:String(p.id),name:String(p.name||''),spec:String(p.spec||''),unit:String(p.unit||'عدد'),price:String((purchase?p.buy_price:p.sale_price)||0),search:[p.code,p.name,p.spec,p.barcode,p.internal_id,p.official_id,p.category,p.subcategory,p.unit].filter(Boolean).join(' ').toLowerCase()}));
26 + const same=existing.length===rows.length&&rows.every((p,i)=>existing[i].value===p.id&&existing[i].textContent===p.name+(p.spec?' ('+p.spec+')':'')&&existing[i].dataset.price===p.price&&existing[i].dataset.unit===p.unit&&existing[i].dataset.search===p.search);
27 + if(same)return;
28 + const placeholder=select.querySelector('option[value=""]')?.cloneNode(true)||new Option('-- انتخاب کالا / خدمات --','');
29 + const original=existing.find(o=>o.value===selectedId),fragment=document.createDocumentFragment();fragment.appendChild(placeholder);
30 + for(const p of rows){const option=new Option(p.name+(p.spec?' ('+p.spec+')':''),p.id);option.dataset.price=p.price;option.dataset.unit=p.unit;option.dataset.search=p.search;fragment.appendChild(option)}
31 + // Preserve selected historical records when a master item was archived or removed.
32 + if(selectedId&&!rows.some(p=>p.id===selectedId)&&original)fragment.appendChild(original.cloneNode(true));
33 + select.replaceChildren(fragment);
34 + if(selectedId&&[...select.options].some(o=>o.value===selectedId))select.value=selectedId;
35 + }
36 window.finoraRefreshSearchableSelect=function(select){
22 - if(!select||select.multiple||select.dataset.finoraSearchReady==='1'||select.classList.contains('je-account'))return;
37 + if(!select||select.multiple||select.classList.contains('je-account'))return;
38 + syncProductSelectOptions(select);
39 + if(select.dataset.finoraSearchReady==='1')return;
40 const ids=['invoice-contact-id','pur-supplier','invoice-project-id','pur-costcenter','chq-contact-select','trx-contact-select','trx-project-select','company-parent-id','invoice-company-id','filter-invoice-company','af-project-parent','af-project-branch'];
41 const targeted=ids.includes(select.id)||select.classList.contains('row-product-select')||select.classList.contains('pur-prod');if(!targeted&&(select.options.length<8||select.dataset.finoraNoSearch==='1'))return;
42 select.dataset.finoraSearchReady='1';const wrap=document.createElement('div');wrap.className='finora-combobox';wrap.style.cssText='position:relative;width:100%';select.parentNode?.insertBefore(wrap,select);wrap.appendChild(select);select.style.display='none';
43 const input=document.createElement('input');input.type='text';input.className='form-control finora-combobox-input';input.autocomplete='off';input.placeholder=select.classList.contains('row-product-select')||select.classList.contains('pur-prod')?'انتخاب یا جستجوی کالا (نام، کد، مشخصه...)':'انتخاب یا جستجو...';wrap.insertBefore(input,select);
44 const list=document.createElement('div');list.className='finora-combobox-list';list.setAttribute('role','listbox');list.style.cssText='display:none;position:absolute;z-index:1200;top:calc(100% + 3px);right:0;left:0;max-height:240px;overflow:auto;background:var(--card,#fff);border:1px solid var(--border,#cbd5e1);border-radius:8px;box-shadow:0 8px 24px rgba(15,23,42,.14)';wrap.appendChild(list);
28 - const close=()=>{list.style.display='none'};
29 - const render=()=>{const rows=comboOptions(select,input.value);list.replaceChildren();if(!rows.length){const empty=document.createElement('div');empty.className='finora-combobox-empty';empty.textContent='موردی پیدا نشد';empty.style.cssText='padding:10px;color:var(--text-muted)';list.appendChild(empty)}else for(const o of rows){const b=document.createElement('button');b.type='button';b.className='finora-combobox-option';b.dataset.value=o.value;b.textContent=comboLabel(o);b.style.cssText='display:block;width:100%;text-align:right;border:0;border-bottom:1px solid var(--border,#e2e8f0);background:transparent;padding:9px 10px;cursor:pointer;color:inherit';list.appendChild(b)}list.style.display='block'};
45 + const isProduct=select.matches('.row-product-select,.pur-prod');
46 + const placeList=()=>{if(!isProduct)return;const rect=wrap.getBoundingClientRect(),spaceBelow=window.innerHeight-rect.bottom,above=spaceBelow<115&&rect.top>spaceBelow;const maxH=Math.max(75,Math.min(240,(above?rect.top:spaceBelow)-8));list.style.position='fixed';list.style.right='auto';list.style.left=Math.max(0,rect.left)+'px';list.style.top=(above?Math.max(0,rect.top-maxH-3):rect.bottom+3)+'px';list.style.width=rect.width+'px';list.style.maxHeight=maxH+'px';list.style.zIndex='10000'};
47 + const close=()=>{list.style.display='none';if(isProduct&&list.parentNode!==wrap)wrap.appendChild(list)};
48 + const render=()=>{if(isProduct){syncProductSelectOptions(select);if(list.parentNode!==document.body)document.body.appendChild(list);placeList()}const rows=comboOptions(select,input.value);list.replaceChildren();if(!rows.length){const empty=document.createElement('div');empty.className='finora-combobox-empty';empty.textContent='موردی پیدا نشد';empty.style.cssText='padding:10px;color:var(--text-muted)';list.appendChild(empty)}else for(const o of rows){const b=document.createElement('button');b.type='button';b.className='finora-combobox-option';b.dataset.value=o.value;b.textContent=comboLabel(o);b.style.cssText='display:block;width:100%;text-align:right;border:0;border-bottom:1px solid var(--border,#e2e8f0);background:transparent;padding:9px 10px;cursor:pointer;color:inherit';list.appendChild(b)}list.style.display='block'};
49 const sync=()=>{const o=select.options[select.selectedIndex];if(document.activeElement===input&&input.dataset.searchDirty==='1')return;input.value=o&&o.value?comboLabel(o):''};
50 input.addEventListener('focus',()=>{input.dataset.searchDirty='1';input.value='';render()});input.addEventListener('click',()=>{if(list.style.display==='none'){input.dataset.searchDirty='1';input.value='';render()}});input.addEventListener('input',()=>{input.dataset.searchDirty='1';render()});input.addEventListener('keydown',e=>{if(e.key==='Escape'){close();input.dataset.searchDirty='0';sync();input.blur();return}if(e.key==='Enter'){const b=list.querySelector('.finora-combobox-option');if(b){e.preventDefault();select.value=b.dataset.value;input.dataset.searchDirty='0';select.dispatchEvent(new Event('change',{bubbles:true}));sync();close()}}});
51 list.addEventListener('mousedown',e=>{const b=e.target.closest('.finora-combobox-option');if(!b)return;e.preventDefault();select.value=b.dataset.value;input.dataset.searchDirty='0';select.dispatchEvent(new Event('change',{bubbles:true}));sync();close()});
33 - select.addEventListener('change',()=>{input.dataset.searchDirty='0';sync()});document.addEventListener('mousedown',e=>{if(!wrap.contains(e.target)){close();input.dataset.searchDirty='0';sync()}});new MutationObserver(()=>{if(input.dataset.searchDirty!=='1')sync()}).observe(select,{childList:true,subtree:true});sync();
52 + select.addEventListener('change',()=>{input.dataset.searchDirty='0';sync()});document.addEventListener('mousedown',e=>{if(!wrap.contains(e.target)){close();input.dataset.searchDirty='0';sync()}});new MutationObserver(()=>{if(input.dataset.searchDirty!=='1')sync();if(list.style.display==='block')render()}).observe(select,{childList:true,subtree:true});if(isProduct){document.addEventListener('scroll',()=>{if(list.style.display==='block')placeList()},true);window.addEventListener('resize',()=>{if(list.style.display==='block')placeList()})}sync();
53 }
35 - function refreshProductOptionMetadata(){document.querySelectorAll('.row-product-select,.pur-prod').forEach(sel=>[...sel.options].forEach(o=>{if(!o.value)return;const p=typeof getMyProducts==='function'?getMyProducts().find(x=>x.id===o.value):null;if(!p)return;o.dataset.price=String(sel.classList.contains('pur-prod')?(p.buy_price||0):(p.sale_price||0));o.dataset.unit=p.unit||'عدد';o.dataset.search=[p.code,p.name,p.spec,p.barcode,p.internal_id,p.official_id,p.category,p.subcategory].filter(Boolean).join(' ').toLowerCase()}))}
54 + function refreshProductOptionMetadata(){document.querySelectorAll('.row-product-select,.pur-prod').forEach(syncProductSelectOptions)}
55 function enhanceAll(){refreshProductOptionMetadata();document.querySelectorAll('select').forEach(finoraRefreshSearchableSelect)}
56 function closeTopModal(){
57 const modals=[...document.querySelectorAll('.modal-backdrop.active')];if(!modals.length)return false;const m=modals[modals.length-1],cancel=[...m.querySelectorAll('button')].find(b=>/انصراف|بستن|لغو|✕/.test(b.textContent||''));if(cancel){cancel.click();return true}m.classList.remove('active');return true;