bmc_hub/static/js/case-command-suggestions.js

46 lines
5.1 KiB
JavaScript
Raw Normal View History

/* Completion is read-only; insertion replaces only the current argument. */
(function(root,factory){const api=factory();if(typeof module==='object'&&module.exports)module.exports=api;if(root)root.CaseCommandSuggestions=api;})(typeof window!=='undefined'?window:globalThis,function(){
'use strict';
const names=['tid','tildel','forventet','undersag','mig','todo','afventer','løsning','deadline','udsat','fil','hjælp','timer','relation','prioritet','gruppe','mail','note'];
const textCommands=new Set(['undersag','todo','note','mail','løsning']);
const descriptions={tid:'Registrer tid',tildel:'Vælg medarbejder',forventet:'Forventet tid',undersag:'Ny undersag',mig:'Tildel mig',todo:'Ny opgave',afventer:'Afventer kunde',løsning:'Ny løsning',deadline:'Deadline',udsat:'Udsæt sagen',fil:'Vedhæft filer',hjælp:'Vis hjælp',timer:'Start eller pause',relation:'Kobl sag',prioritet:'Prioritet',gruppe:'Vælg gruppe',mail:'Mailkladde',note:'Intern note'};
const quote=value=>'"'+String(value).replace(/\\/g,'\\\\').replace(/"/g,'\\"')+'"';
const initials=user=>String(user.full_name||'').trim().split(/\s+/).filter(Boolean).map(part=>part[0]).join('').toLocaleLowerCase('da');
function employeeToken(user,users){
const short=initials(user);
const matches=token=>users.filter(candidate=>[String(candidate.user_id),String(candidate.username||'').toLocaleLowerCase('da'),String(candidate.full_name||'').trim().toLocaleLowerCase('da'),initials(candidate)].includes(token.toLocaleLowerCase('da'))).length;
if(short && matches(short)===1)return short;
if(user.username && matches(user.username)===1)return user.username;
return quote(user.full_name||user.username||'');
}
function suggest(line,cursor,values={}){
const before=line.slice(0,cursor),match=/^(\s*)(\/\/|\/)([^\s]*)/.exec(before);if(!match)return [];
const start=match[1].length,prefix=match[2],name=match[3].toLowerCase(),commandEnd=start+prefix.length+(/^(?:\s*)(?:\/\/|\/)([^\s]*)/.exec(line)?.[1] || name).length;
if(cursor<=commandEnd)return names.filter(n=>n.startsWith(name)).map(n=>({label:`${prefix}${n}`,hint:descriptions[n],value:`${prefix}${n} ${textCommands.has(n)?'""':''}`,caret:textCommands.has(n)?prefix.length+n.length+2:undefined,from:start,to:commandEnd}));
const argsStart=commandEnd+1;
const matches=[...line.slice(argsStart).matchAll(/"(?:\\.|[^"\\])*(?:"|$)|\S+/g)].map(m=>({text:m[0],start:argsStart+m.index,end:argsStart+m.index+m[0].length}));
let index=matches.findIndex(t=>cursor>=t.start&&cursor<=t.end);if(index<0)index=matches.filter(t=>t.end<cursor).length;
const token=matches[index],from=token?.start??cursor,to=token?.end??cursor;
const query=line.slice(from,cursor).replace(/^[@"]|"$/g,'').toLocaleLowerCase('da');
let options=[];
const durations=['15m','30m','45m','1h','1,5h','2h'];
if((name==='tid'||name==='forventet')&&index===0)options=durations.map(v=>[v,v,'Minutter eller timer']);
if(name==='tid'&&index===1)options=['research','support','development','troubleshooting','on_site','meeting','other'].map(v=>[v,v,'Arbejdets kategori']);
if((name==='tildel'&&index===0)||(name==='todo'&&index===1&&(query.startsWith('@')||token?.text.startsWith('@')||!query)))options=(values.users||[]).map(u=>{const token=employeeToken(u,values.users||[]);return [u.full_name||u.username,name==='todo'?(token.startsWith('"')?quote('@'+(u.full_name||u.username)):'@'+token):token,`${initials(u)} · ${u.username||'Medarbejder'}`];});
if(name==='gruppe'&&index===0)options=(values.groups||[]).map(g=>[g.name,quote(g.name),'Gruppe']);
if(name==='prioritet'&&index===0)options=['lav','normal','høj','kritisk'].map(v=>[v,v,'Prioritet']);
if(name==='afventer'&&index===0)options=[['kunde','kunde','Afvent kunden og opret todo']];
if(name==='timer'&&index===0)options=[['start','start','Start ny timer'],['pause','pause','Pause din aktive timer']];
if(name==='relation'&&index===0)options=(values.cases||[]).map(c=>[`#${c.id} · ${c.titel}`,`#${c.id}`,'Eksisterende sag']);
if(['deadline','udsat'].includes(name)){
const q=before.slice(argsStart).toLowerCase();return ['i dag','i morgen','mandag','tirsdag','onsdag','torsdag','fredag','lørdag','søndag'].filter(v=>v.startsWith(q)).map(v=>({label:v,hint:'Dato vises i forhåndsvisningen',value:v,from:argsStart,to:line.length}));
}
if(name==='todo'&&((index===1&&!token?.text.startsWith('@'))||index===2))options.push(...['i morgen','fredag','mandag'].map(v=>[v,v,'Opgavens frist']));
const textFields={undersag:[0,'Titel'],todo:[0,'Opgave'],note:[0,'Intern note'],mail:[0,'Mailtekst'],løsning:[0,'Løsning'],tildel:[1,'Begrundelse'],gruppe:[1,'Begrundelse'],prioritet:[1,'Begrundelse'],afventer:[1,'Begrundelse'],tid:[2,'Beskrivelse']};
const field=textFields[name] || (name==='timer' && matches[0]?.text==='start' ? [1,'Beskrivelse'] : null);
if(field && index===field[0] && !query)options.push([`Skriv ${field[1].toLowerCase()}`,quote(''),'Tekst i dobbelte anførselstegn']);
return options.filter(([label,value,hint])=>`${label} ${value} ${hint}`.toLocaleLowerCase('da').includes(query)).slice(0,30).map(([label,value,hint])=>({label,hint,value:value+' ',caret:value==='""'?1:undefined,from,to}));
}
return {suggest};
});