-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
12 lines (12 loc) · 11.1 KB
/
Copy pathbundle.js
File metadata and controls
12 lines (12 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
// deno-lint-ignore-file
var E=Object.defineProperty;var h=(i,t)=>E(i,"name",{value:t,configurable:!0});var S=!1,p=class{static{h(this,"KvClient")}nextMsgID=0;querySet=[];transactions=new Map;currentPage=1;focusedRow=null;kvCache;CTX;ServiceURL;RegistrationURL;constructor(t,e){this.CTX=e,this.ServiceURL=e.LOCAL_DB?e.LocalDbURL:e.RemoteDbURL,this.RegistrationURL=this.ServiceURL+e.RpcURL,this.kvCache=t,this.transactions=new Map}init(t){let e=new EventSource(this.RegistrationURL);console.log("CONNECTING"),e.addEventListener("open",()=>{this.callProcedure(this.ServiceURL,"GET",{key:["PIN"]}).then(s=>{t.PIN=s.value,this.fetchQuerySet()})}),e.addEventListener("error",s=>{switch(e.readyState){case EventSource.OPEN:console.log("CONNECTED");break;case EventSource.CONNECTING:console.log("CONNECTING");break;case EventSource.CLOSED:console.log("DISCONNECTED");break}}),e.addEventListener("message",s=>{let n=JSON.parse(s.data),{txID:o,error:a,result:r}=n;if(o===-1&&this.handleMutation(r),!this.transactions.has(o))return;let c=this.transactions.get(o);this.transactions.delete(o),c&&c(a,r)})}handleMutation(t){console.info("Mutation event:",t)}async setKvPin(t){let e=this.kvCache.encryptText(t);await this.callProcedure(this.ServiceURL,"SET",{key:["PIN"],value:e}).then(s=>{S&&console.log(`Set PIN ${t} to: `,e)})}addNewRecord(){let t=Object.assign({},this.kvCache.schema.sampleRecord);for(let s in t)typeof t[s]=="object"&&(t[s]=t[s][0]);let e=this.kvCache.schema.keyColumnName;this.kvCache.set(t[e],t)}async fetchQuerySet(){let t=this.kvCache;await this.callProcedure(this.ServiceURL,"GET",{key:[this.kvCache.schema.dbKey]}).then(e=>{e.value?t.restoreCache(t.encryptText(e.value)):(this.addNewRecord(),d.fire("buildDataTableEV",this.kvCache))})}get(t){for(let e=0;e<this.querySet.length;e++){let s=this.querySet[e];if(s.id===t)return s}}set(t){try{this.callProcedure(this.ServiceURL,"SET",{key:[this.kvCache.schema.dbKey],value:t}).then(e=>(this.querySet=e.querySet,this.querySet))}catch(e){return{Error:e}}}delete(t){try{this.callProcedure(this.ServiceURL,"DELETE",{key:t,value:""}).then(e=>{console.info("Delete result: ",e)})}catch(e){return{Error:e}}}callProcedure(t,e,s){let n=this.nextMsgID++;return new Promise((o,a)=>{this.transactions.set(n,(r,c)=>{if(r)return a(new Error(r));o(c)}),fetch(t,{method:"POST",mode:"cors",body:JSON.stringify({txID:n,procedure:e,params:s})})})}};var m=class{static{h(this,"KvCache")}dbKey="";schema;nextMsgID=0;querySet=[];callbacks;columns=[];kvClient;dbMap;raw=[];constructor(t,e,s){this.dbKey=`${t.dbKey}`,this.schema=t,this.callbacks=new Map,this.dbMap=new Map,this.columns=this.buildColumnSchema(this.schema.sampleRecord),this.kvClient=new p(this,e),this.kvClient.init(s)}encryptText(t){let e="",s="ndhg";for(let n=0;n<t.length;n++)e+=String.fromCharCode(t.charCodeAt(n)^s.charCodeAt(n%s.length));return e}restoreCache(t){let e=JSON.parse(t);this.dbMap=new Map(e),this.persist(),this.hydrate()=="ok"&&d.fire("buildDataTableEV",this)}buildColumnSchema(t){let e=[];for(let[s,n]of Object.entries(t)){let o=!1;(typeof n=="number"&&n===-1||typeof n=="string"&&n==="READONLY")&&(o=!0),e.push({name:s,type:typeof n,defaultValue:n,readOnly:o})}return e}persist(t=!0){t&&(this.dbMap=new Map([...this.dbMap.entries()].sort()));let e=JSON.stringify(Array.from(this.dbMap.entries())),s=this.encryptText(e);this.kvClient.set(s)}hydrate(){return this.raw=[...this.dbMap.values()],this.querySet=[...this.raw],d.fire("buildDataTableEV",this),this.raw.length>2?"ok":"Not found"}resetData(){this.querySet=[...this.raw]}clean(t=null){let e=new Map;[...this.dbMap.keys()].forEach(n=>{n!==t&&e.set(n,this.dbMap.get(n))}),this.dbMap=e,this.persist(!0)}set(t,e){try{return this.dbMap.set(t,e),this.persist(!0),this.hydrate(),t}catch(s){return console.error("error setting "),"Error "+s}}get(t){try{return this.dbMap.get(t)}catch(e){return"Error "+e}}delete(t){try{let e=this.dbMap.delete(t);return e===!0&&this.persist(!0),this.hydrate(),e}catch(e){return"Error "+e}}};function k(){let i=new Map;return{on(e,s){i.has(e)?i.get(e).push(s):i.set(e,[s])},fire(e,s){let n=i.get(e);if(n)for(let o of n)o(s)}}}h(k,"buildSignalBus");var d=k();var f=h((i,t,e)=>i.addEventListener(t,e),"on"),y=class extends HTMLElement{static{h(this,"PinComponent")}static register(){customElements.define("pin-component",this)}shadow;constructor(){super(),this.shadow=this.attachInternals()?.shadowRoot}init(){return new Promise((t,e)=>{let s="]]YT",n=this.shadow.getElementById("popupDialog"),o=this.shadow.getElementById("pinDialog"),a=this.shadow.getElementById("pin"),r=this.shadow.getElementById("popup_text");f(n,"click",l=>{l.preventDefault(),n.close()});let c=0,u=!1;f(n,"close",l=>{l.preventDefault(),u||o.showModal()}),f(n,"keyup",l=>{l.preventDefault(),n.close(),u||o.showModal()}),o?.addEventListener("keydown",l=>{l.key==="Escape"&&l.preventDefault()}),f(a,"keyup",l=>{l.preventDefault();let C=this.encryptText(a.value);(l.key==="Enter"||C===s)&&(c+=1,C===s?(a.value="",u=!0,o.close(),t("ok")):(o.close(),a.value="",u=!1,r&&(r.textContent=c===3?`Incorrect pin entered ${c} times!
Please close this Page!`:`Incorrect pin entered ${c} times!`),c===3?(document.body.innerHTML=`
<h1>Three failed PIN attempts!</h1>
<h1>Please close this page!</h1>`,e("Three failed PIN attempts! You are not authorized!")):n.showModal()))}),o.showModal(),a.focus({focusVisible:!0}),document.addEventListener("keydown",function(l){l.ctrlKey&&l.key==="x"&&(l.preventDefault(),u=!0,o.close())})})}encryptText(t){let e="",s="ndhg";for(let n=0;n<t.length;n++)e+=String.fromCharCode(t.charCodeAt(n)^s.charCodeAt(n%s.length));return e}};y.register();var g=class extends HTMLElement{static{h(this,"FooterComponent")}static register(){customElements.define("footer-component",this)}addBtn;deleteBtn;shadow;constructor(){super(),this.shadow=this.attachInternals()?.shadowRoot}init(t){this.addBtn=this.shadow.getElementById("addbtn"),this.addBtn.onclick=s=>{t.addNewRow()},this.deleteBtn=this.shadow.getElementById("deletebtn"),this.deleteBtn.onclick=s=>{t.deleteCurrentRow()};let e=this.shadow.getElementById("fileload");document.addEventListener("keydown",function(s){if(s.ctrlKey&&s.key==="b"){s.preventDefault();let n=t.getJsonDataSet(),o=document.createElement("a"),a=new Blob([n],{type:"application/json"});o.href=URL.createObjectURL(a),o.download="backup.json",o.click(),URL.revokeObjectURL(o.href)}s.ctrlKey&&s.key==="r"&&(s.preventDefault(),e.click(),e.addEventListener("change",function(){let n=new FileReader;n.onload=function(){t.restoreCache(n.result)},n.readAsText(e.files[0])}))})}};g.register();var b=class extends HTMLElement{static{h(this,"TableComponent")}static register(){customElements.define("table-component",this)}APP_CTX;focusedCell;focusedRow;schema;cache;footer;table;tablehead;tableBody;shadow;constructor(){super(),this.shadow=this.attachInternals()?.shadowRoot}init(t,e,s){return d.on("buildDataTableEV",()=>{this.buildDataTable()}),this.APP_CTX=e,this.schema=t,this.cache=s,this.table=this.shadow.getElementById("table"),this.tableBody=this.shadow.getElementById("table-body"),this.tableBody.addEventListener("click",this),this.tablehead=this.shadow.getElementById("table-head"),this.footer=document.getElementById("footer-component"),this.footer.init(this),this.buildTableHead(),this}handleEvent(t){this[`handle${t.type}`](t)}handleclick(t){let e=t.target;console.info("row click",e),console.info("row click",e.dataset),this.focusedRow&&this.focusedCell&&e!==this.focusedCell&&(this.focusedCell.removeAttribute("contenteditable"),this.focusedCell.className="",this.focusedCell.oninput=null),this.focusedRow?.classList.remove("selected_row"),this.focusedRow=e.parentElement,this.focusedRow.classList.add("selected_row"),this.APP_CTX.FocusedKey=this.focusedRow.dataset.cache_key,this.resetFooter(!1),this.focusedCell=e,this.focusedCell.setAttribute("contenteditable",""),this.focusedCell.className="editable ";let s=this.focusedRow.dataset.cache_key,n=this.focusedCell.dataset.column_id,o=parseInt(this.focusedCell.dataset.column_index)||0,a=this.cache.get(s);this.focusedCell.onblur=()=>{let r=this.focusedCell.textContent;if(this.focusedCell.tagName==="SELECT"){n=this.focusedCell.parentElement.dataset.column_id||"",o=parseInt(this.focusedCell.parentElement.dataset.column_index)||0;let c=this.focusedCell;r=c.options[c.selectedIndex].text}a[n]!==r&&(a[n]=r,o===0?s!==r&&(this.cache.delete(s),s=r,this.cache.set(s,a)):this.cache.set(s,a))}}scrollToBottom(){this.tableBody.rows[this.tableBody.rows.length-1].scrollIntoView({behavior:"smooth"})}buildTableHead(){let t='<tr class="headerRow">',e="";for(let s=0;s<this.cache.columns.length;s++)e+=` <th id="header${s+1}" data-index=${s} value=1>${this.cache.columns[s].name}</th>`;this.tablehead.innerHTML+=t+e+"</tr>";for(let s=0;s<this.cache.columns.length;s++){let n=this.shadow.getElementById(`header${s+1}`);n.onclick=o=>{this.resetFocusedRow(),this.buildDataTable()}}}buildDataTable(){this.tableBody.innerHTML="",this.cache.querySet&&this.buildRows(),this.resetFocusedRow(),this.focusedCell?.focus()}buildRows(){let t=this.cache.querySet;if(t)for(let e=0;e<t.length;e++){let s=t[e],n=`<tr data-cache_key="${s[this.cache.columns[0].name]}">`;for(let o=0;o<this.cache.columns.length;o++){let a=this.cache.columns[o].name;switch(this.cache.columns[o].type){case"boolean":{let r=s[a]==="true"?"checked":"";n+=`<td data-column_index=${o}
data-column_id="${a}"><input type="checkbox" ${r}></td>`;break}case"number":n+=`<td data-column_index=${o}
data-column_id="${a}">${parseFloat(s[a])}</td>`;break;case"object":n+=`<td data-column_index=${o}
data-column_id="${a}">${this.buildSelect(this.cache.columns[o].defaultValue,s[a])}</td>`;break;default:n+=`<td data-column_index=${o}
data-column_id="${a}">${s[a]}</td>`;break}}n+="</tr>",this.tableBody.innerHTML+=n}}buildSelect(t,e){let s=`<select>
`;return t.forEach(n=>{e===n?s+=`<option value="${n}" selected>${n}</option>
`:s+=`<option value="${n}">${n}</option>
`}),s+="</select>",s}addNewRow(){let t=Object.assign({},this.schema.sampleRecord);for(let s in t)typeof t[s]=="object"&&(t[s]=t[s][0]);let e=this.schema.keyColumnName;this.cache.set(t[e],t),this.buildDataTable(),this.scrollToBottom()}deleteCurrentRow(){this.cache.delete(this.APP_CTX.FocusedKey),this.buildDataTable()}getJsonDataSet(){return JSON.stringify(Array.from(this.cache.dbMap.entries()))}restoreCache(t){this.cache.restoreCache(t)}resetFocusedRow(){this.resetFooter(!0),this.focusedRow=null}resetFooter(t){t?(this.footer.deleteBtn.setAttribute("hidden",""),this.footer.addBtn.removeAttribute("hidden")):(this.footer.addBtn.setAttribute("hidden",""),this.footer.deleteBtn.removeAttribute("hidden"))}};b.register();var T={dbKey:"PWA",keyColumnName:"host",sampleRecord:{host:"Z",login:"",pw:"",remarks:""}},w={DEV:!1,PIN:"",FocusedKey:""},v={LOCAL_DB:!1,LocalDbURL:"http://localhost:9099/",RemoteDbURL:"https://dt-kv-rpc.deno.dev/",RpcURL:"SSERPC/kvRegistration"},R=document.getElementById("pin-component");R.init().then(i=>{if(i==="ok"){let t=new m(T,v,w);document.getElementById("table-component").init(T,w,t)}}).catch(i=>{alert(i)});export{g as FooterComponent,y as PinComponent,b as TableComponent,f as on};