forked from quintanar401/kdb-wc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdb-wc.min.js
2 lines (2 loc) · 41.8 KB
/
kdb-wc.min.js
1
2
(function(){"use strict";var _KDBChart,_KDBEditor,_KDBQuery,_KDBSrv,_KDBTable,copyCfg,extractInfo,getConfig,jsonpRegistry,keval,mergeCfgs,extend=function(child,parent){for(var key in parent){if(hasProp.call(parent,key))child[key]=parent[key]}function ctor(){this.constructor=child}ctor.prototype=parent.prototype;child.prototype=new ctor;child.__super__=parent.prototype;return child},hasProp={}.hasOwnProperty,indexOf=[].indexOf||function(item){for(var i=0,l=this.length;i<l;i++){if(i in this&&this[i]===item)return i}return-1};(function(){var CE,error1;try{return new CustomEvent("test")}catch(error1){CE=function(event,params){var evt;params=params||{bubbles:false,cancelable:false,detail:void 0};evt=document.createEvent("CustomEvent");evt.initCustomEvent(event,params.bubbles,params.cancelable,params.detail);return evt};CE.prototype=window.CustomEvent.prototype;return window.CustomEvent=CE}})();extractInfo=function(v){var ref,txt;if(typeof v==="string"){return v}txt="";if(v.nodeName==="SELECT"){if(v.selectedIndex>=0){txt=v.options[v.selectedIndex].text}}else if(v.nodeName==="INPUT"){if(v.type==="checkbox"){txt=v.checked?"1b":"0b"}else if(v.type==="radio"){txt=((ref=v.form.querySelector("input[type='radio'][name='"+v.name+"']:checked"))!=null?ref.value:void 0)||""}else{txt=(v!=null?v.value:void 0)||""}}else if(v.nodeName==="TEXTAREA"){txt=v.value}else if(v.nodeName==="KDB-EDITOR"){txt=v.kEditor.getValue()}else{txt=v.textContent}return txt};mergeCfgs=function(c1,c2){var n,v,v2;for(n in c1){v=c1[n];if((v2=c2[n])===void 0){continue}if(typeof v2==="object"&&typeof v==="object"&&!v2.length&&!v.length){c1[n]=mergeCfgs(v,v2)}else{c1[n]=v2}}for(n in c2){v=c2[n];if(c1[n]){continue}c1[n]=v}return c1};copyCfg=function(c){var cc,n,v;cc={};for(n in c){v=c[n];if(typeof v==="object"&&!v.length){cc[n]=copyCfg(v)}else{cc[n]=v}}return cc};getConfig=function(c){var cfg,err,error1;if(/^[\w\.]+$/.test(c)){return copyCfg(c.split(".").reduce(function(x,y){return x[y]},window))}try{return cfg=JSON.parse(c)}catch(error1){err=error1;console.log("kdb-wc: config parse exception");return console.log(err)}};keval=function(s){var err,error1;try{return eval(s)}catch(error1){err=error1;return null}};jsonpRegistry={};_KDBSrv=function(superClass){extend(_KDBSrv,superClass);function _KDBSrv(){return _KDBSrv.__super__.constructor.apply(this,arguments)}_KDBSrv.prototype.createdCallback=function(){var ref,ref1,ref10,ref11,ref2,ref3,ref4,ref5,ref6,ref7,ref8,ref9;this.srvType=((ref=this.attributes["k-srv-type"])!=null?ref.textContent:void 0)||"http";this.target=((ref1=this.attributes["k-target"])!=null?ref1.textContent:void 0)||null;this.wsSrv=(ref2=this.srvType)==="http"||ref2==="https"?location.host:((ref3=this.attributes["k-srv-uri"])!=null?ref3.textContent:void 0)||location.host;this.srvUser=((ref4=this.attributes["k-srv-user"])!=null?ref4.textContent:void 0)||null;this.srvPass=((ref5=this.attributes["k-srv-pass"])!=null?ref5.textContent:void 0)||null;this.qPrefix=((ref6=this.attributes["k-prefix"])!=null?ref6.textContent:void 0)||"";this.debug=((ref7=this.attributes["debug"])!=null?ref7.textContent:void 0)||null;this.rType=((ref8=this.attributes["k-return-type"])!=null?ref8.textContent:void 0)||"json";this.fixJson=((ref9=this.attributes["fix-json"])!=null?ref9.textContent:void 0)||null;this.kFeed=(((ref10=this.attributes["k-feed"])!=null?ref10.textContent:void 0)||"false")==="true";this.kDeser=(((ref11=this.attributes["k-deserialize"])!=null?ref11.textContent:void 0)||"false")==="true";this.hidden=true;this.ws=this.wsReq=null;this.wsQueue=[];if(this.srvType==="jsonp"){this.rType="json"}this.srvProto=/^http.?\:/.test(this.wsSrv)?"":this.srvType==="https"?"https://":"http://";if(this.debug){console.log("kdb-srv inited: srvType:"+this.srvType+", target:"+this.target+", prefix:"+this.qPrefix+", rType:"+this.rType+", proto:"+this.srvProto+", srv: "+this.wsSrv)}if(this.target){return this.target=document.querySelector("[k-id='"+this.target+"']")||this.target}};_KDBSrv.prototype.runQuery=function(q,cb){var ref,ref1;if(!cb){cb=function(r,e){return null}}if((ref=this.srvType)==="http"||ref==="xhttp"||ref==="jsonp"||ref==="https"){return this.sendHTTP(q,cb)}if((ref1=this.srvType)==="ws"||ref1==="wss"){return this.sendWS(q,cb)}return console.error("kdb-srv: unknown srv type: "+this.srvType)};_KDBSrv.prototype.sendWS=function(qq,clb){this.wsQueue.push({q:qq,cb:clb});if(!this.ws){this.ws=new WebSocket(this.srvType+"://"+this.wsSrv+"/");this.ws.binaryType="arraybuffer";this.ws.onopen=function(_this){return function(){if(_this.debug){console.log("kdb-srv-ws: opened")}return _this.processWSQueue()}}(this);this.ws.onclose=function(_this){return function(){if(_this.debug){console.log("kdb-srv-ws: closed")}_this.ws=null;return _this.sendWSRes(null,"closed")}}(this);this.ws.onerror=function(_this){return function(e){if(_this.debug){console.log("kdb-srv-ws: error "+e.data)}return _this.sendWSRes(null,e.data)}}(this);this.ws.onmessage=function(_this){return function(e){var error,error1,res;if(_this.debug){console.log("kdb-srv-ws: msg")}try{res=_this.rType==="json"&&typeof e.data==="string"?JSON.parse(e.data):typeof e.data==="object"?deserialize(e.data):e.data}catch(error1){error=error1;console.error("kdb-srv-ws: exception in ws parse "+error);return _this.sendWSRes(null,"result parse error: "+error.toString())}return _this.sendWSRes(res,null)}}(this);return}if(this.ws.readyState===1){return this.processWSQueue()}};_KDBSrv.prototype.sendWSRes=function(r,e){var err,error1,req;if(!(req=this.wsReq)){return}if(!this.kFeed){this.wsReq=null}try{req.cb(r,e)}catch(error1){err=error1;console.error("kdb-srv-ws: exception in callback");console.log(err)}return this.processWSQueue()};_KDBSrv.prototype.processWSQueue=function(){var error,error1,req,trg;if(this.wsReq||this.wsQueue.length===0){return}this.wsReq=this.wsQueue.shift();req=this.wsReq.q;if(typeof req==="string"&&this.qPrefix){req=this.qPrefix+req}if(this.target&&(trg=extractInfo(this.target))){req="%target="+trg+"%"+req}if(this.rType==="q"){try{if(typeof req==="string"&&req[0]==="`"){req=" "+req}req=serialize(req)}catch(error1){error=error1;console.error("kdb-srv-ws: exception in ws send "+error);return this.sendWSRes(null,"send")}}if(this.ws&&this.ws.readyState===1){return this.ws.send(req)}this.sendWS(this.wsReq.q,this.wsReq.cb);return this.wsReq=null};_KDBSrv.prototype.sendHTTP=function(q,cb){var query,rid,trg,xhr;if(this.fixJson){this.fixJson=null;if(!this.qPrefix){this.qPrefix=this.srvType==="jsonp"?"jsp?":"jsn?enlist "}query=".h.tx[`jsn]:(.j.j');";if(this.srvType==="jsonp"){query='.h.tx[`jsp]:{enlist "KDB.processJSONP(\'",string[x 0],"\',",(.j.j x 1),")"};'}if(this.srvType==="xhttp"){query+='if[105=type .h.hp;.h.hp:(f:{ssr[x y;"\nConnection: close";{"\nAccess-Control-Allow-Origin: *\r",x}]})[.h.hp];.h.he:f .h.he;.h.hy:{[a;b;c;d]a[b c;d]}[f;.h.hy]];'}return this.runQuery("{"+query+";1}[]",function(_this){return function(r,e){return _this.runQuery(q,cb)}}(this))}if(!this.qPrefix&&this.rType==="json"){this.qPrefix=this.srvType==="jsonp"?"jsp?":"json?enlist "}if(this.srvType==="jsonp"){q=this.qPrefix+("(`"+(rid="id"+Date.now())+";"+encodeURIComponent(q)+")")}else{q=this.qPrefix+encodeURIComponent(q)}if(this.target&&(trg=extractInfo(this.target))){q=q+"&target="+trg}q=this.srvProto+this.wsSrv+"/"+q;if(this.debug){console.log("kdb-srv sending request:"+q)}if(this.srvType==="jsonp"){return this.sendJSONP(rid,q,cb)}xhr=new XMLHttpRequest;xhr.onerror=function(_this){return function(){if(_this.debug){console.log("kdb-srv error: "+xhr.statusText+" - "+xhr.responseText)}return cb(null,xhr.statusText+": "+xhr.responseText)}}(this);xhr.ontimeout=function(_this){return function(){if(_this.debug){console.log("kdb-srv timeout")}return cb(null,"timeout")}}(this);xhr.onload=function(_this){return function(){var err,error,error1,error2,res;if(xhr.status!==200){return xhr.onerror()}if(_this.debug){console.log("kdb-srv data: "+xhr.responseText.slice(0,50))}try{try{res=_this.rType==="json"?JSON.parse(xhr.responseText):_this.rType==="xml"?xhr.responseXML:xhr.responseText;if(_this.kDeser&&res instanceof Array&&res[0]==="deserialize"){res=deserialize(res[1])}}catch(error1){error=error1;console.error("kdb-srv: exception in JSON.parse");return cb(null,"JSON.parse error: "+error.toString())}return cb(res,null)}catch(error2){err=error2;console.error("kdb-srv: HTTP callback exception");return console.log(err)}}}(this);xhr.open("GET",q,true,this.srvUser,this.srvPass);return xhr.send()};_KDBSrv.prototype.sendJSONP=function(rid,q,cb){var resOk,script;resOk=false;jsonpRegistry[rid]=function(_this){return function(res){var error,error1;if(_this.debug){console.log("kdb-srv(jsonp) data: "+res.toString().slice(0,50))}delete jsonpRegistry[rid];resOk=true;try{return cb(res,null)}catch(error1){error=error1;console.error("kdb-srv: HTTP callback exception");return console.log(error)}}}(this);script=document.createElement("script");script.onload=script.onerror=function(_this){return function(){var error,error1;if(resOk){return}delete jsonpRegistry[rid];if(_this.debug){console.log("kdb-srv(jsonp): error")}try{return cb(null,"url: "+q)}catch(error1){error=error1;console.error("kdb-srv: HTTP callback exception");return console.log(error)}}}(this);script.src=q;return document.body.appendChild(script)};return _KDBSrv}(HTMLElement);_KDBQuery=function(superClass){extend(_KDBQuery,superClass);function _KDBQuery(){return _KDBQuery.__super__.constructor.apply(this,arguments)}_KDBQuery.prototype.createdCallback=function(){this.hidden=true;return this.setupQuery()};_KDBQuery.prototype.setupQuery=function(){var el,j,len,prvExec,ref,ref1,ref10,ref11,ref12,ref13,ref14,ref15,ref2,ref3,ref4,ref5,ref6,ref7,ref8,ref9,v;prvExec=this.exec;if(this.ktimer){clearTimeout(this.ktimer)}this.ktimer=null;this.iterationNumber=0;this.kID=((ref=this.attributes["k-id"])!=null?ref.textContent:void 0)||"undefined";this.query=((ref1=this.attributes["k-query"])!=null?ref1.textContent:void 0)||this.textContent;this.srv=((ref2=this.attributes["k-srv"])!=null?ref2.textContent:void 0)||"";this.exec=((ref3=this.attributes["k-execute-on"])!=null?ref3.textContent.split(" ").filter(function(e){return e.length>0}):void 0)||["load"];this.debug=((ref4=this.attributes["debug"])!=null?ref4.textContent:void 0)||null;this.escapeQ=((ref5=this.attributes["k-escape-q"])!=null?ref5.textContent:void 0)||"";this.kDispUpd=(((ref6=this.attributes["k-dispatch-update"])!=null?ref6.textContent:void 0)||"false")==="true";this.updObjs=((ref7=this.attributes["k-update-elements"])!=null?ref7.textContent.split(" ").filter(function(e){return e.length>0}):void 0)||[];this.updErr=((ref8=this.attributes["k-on-error"])!=null?ref8.textContent.split(" ").filter(function(e){return e.length>0}):void 0)||[];this.kInterval=((ref9=this.attributes["k-interval"])!=null?ref9.textContent:void 0)||"0";this.kInterval=Number.parseInt?Number.parseInt(this.kInterval):Number(this.kInterval);this.kDelay=((ref10=this.attributes["k-delay"])!=null?ref10.textContent:void 0)||"0";this.kDelay=Number.parseInt?Number.parseInt(this.kDelay):Number(this.kDelay);this.kQStatus=((ref11=this.attributes["k-status-var"])!=null?ref11.textContent:void 0)||null;this.kQNum=0;if(this.kFilter=(ref12=this.attributes["k-filter"])!=null?ref12.textContent:void 0){this.kFilter=keval(this.kFilter)}this.result=null;if(indexOf.call(this.exec,"load")>=0&&(!prvExec||!(indexOf.call(prvExec,"load")>=0))){if((ref13=document.readyState)==="complete"||ref13==="interactive"){setTimeout(function(_this){return function(){return _this.runQuery({src:"self",txt:"load"})}}(this),100)}else{document.addEventListener("DOMContentLoaded",function(_this){return function(ev){return _this.runQuery({src:"self",txt:"load"})}}(this))}}ref14=this.exec;for(j=0,len=ref14.length;j<len;j++){el=ref14[j];if(!(el==="load"||el==="manual"||el==="timer")){if(v=document.querySelector("[k-id='"+el+"']")){this.addUpdater(v,el)}}}this.kRefs=(ref15=this.query.match(/\$(\w|\.)(\w|\.|\]|\[|\-)*\$/g))!=null?ref15.map(function(e){return e.slice(1,e.length-1)}):void 0;this.kMap=null;if(indexOf.call(this.exec,"timer")>=0){setTimeout(function(_this){return function(){return _this.rerunQuery({src:"self",txt:"timer"})}}(this),this.kDelay?this.kDelay:this.kInterval)}if(this.debug){return console.log("kdb-query inited: srv:"+this.srv+", query:"+this.query+", executeOn:"+this.exec+", updateObs:"+this.updObjs+", refs:"+this.kRefs+", delay:"+this.kDelay+", interval:"+this.kInterval)}};_KDBQuery.prototype.rerunQuery=function(args){if(args==null){args={}}args["pres"]=this.result;this.result=null;return this.runQuery(args)};_KDBQuery.prototype.runQuery=function(args){var ref;if(args==null){args={}}args["i"]=this.iterationNumber;if(this.result!==null){return}if(typeof this.srv==="string"){this.srv=this.srv===""?(ref=document.getElementsByTagName("kdb-srv"))!=null?ref[0]:void 0:document.querySelector("[k-id='"+this.srv+"']")}if(this.debug){console.log("kdb-query: executing query")}this.kQNum+=1;this.updateStatus();this.srv.runQuery(this.resolveRefs(this.query,args),function(_this){return function(r,e){var j,len,o,ref1;_this.kQNum-=1;if(_this.debug){console.log("kdb-query: got response with status "+e)}if(e){_this.updateStatus();ref1=_this.updErr;for(j=0,len=ref1.length;j<len;j++){o=ref1[j];_this.updObjWithRes(o,document.querySelector("[k-id='"+o+"']"),e)}}else{if(_this.kFilter){r=typeof _this.kFilter==="object"?_this.kFilter.filter(r):_this.kFilter(r)}_this.result=r;_this.updateStatus();_this.updateObjects();_this.sendEv()}if(_this.kInterval&&indexOf.call(_this.exec,"timer")>=0){return setTimeout(function(){return _this.rerunQuery({src:"self",txt:"timer"})},_this.kInterval)}}}(this));return this.iterationNumber+=1};_KDBQuery.prototype.sendEv=function(){if(this.result!==null){return this.dispatchEvent(this.getEv())}};_KDBQuery.prototype.getEv=function(){return new CustomEvent("newResult",{detail:this.kDispUpd?this.result[""]:this.result,bubbles:true,cancelable:true})};_KDBQuery.prototype.onresult=function(f){this.addEventListener("newResult",f);if(this.result!==null){return f(this.getEv())}};_KDBQuery.prototype.setQueryParams=function(o,c2){var attrs,c1,j,len,n,ref,ref1;if(!(attrs=(ref=o.attributes["k-attr"])!=null?ref.textContent:void 0)){return c2}c1={};ref1=attrs.split(" ").filter(function(e){return e.length>0});for(j=0,len=ref1.length;j<len;j++){n=ref1[j];c1[n]=o.attributes[n].textContent||""}return mergeCfgs(c1,c2)};_KDBQuery.prototype.addUpdater=function(v,kid){var ref;if(v.nodeName==="BUTTON"){return v.addEventListener("click",function(_this){return function(ev){return _this.rerunQuery(_this.setQueryParams(v,{src:"button",id:kid}))}}(this))}else if(v.nodeName==="KDB-EDITOR"){return v.onexec(function(_this){return function(ev){return _this.rerunQuery(_this.setQueryParams(v,{src:"editor",id:kid,txt:ev.detail}))}}(this))}else if(v.nodeName==="KDB-QUERY"){return v.onresult(function(_this){return function(ev){return _this.rerunQuery(_this.setQueryParams(v,{src:"query",id:kid,txt:ev.detail}))}}(this))}else if((ref=v.nodeName)==="SELECT"||ref==="TEXTAREA"||ref==="INPUT"){return v.addEventListener("change",function(_this){return function(ev){return _this.rerunQuery(_this.setQueryParams(v,{src:v.nodeName,id:kid,txt:extractInfo(v)}))}}(this))}else{return v.addEventListener("click",function(_this){return function(ev){var ref1;return _this.rerunQuery(_this.setQueryParams(v,{src:v.nodeName,id:kid,txt:typeof ev.kdetail!=="undefined"&&ev.kdetail!==null?ev.kdetail:(ref1=ev.target)!=null?ref1.textContent:void 0}))}}(this))}};_KDBQuery.prototype.kdbUpd=function(r,kid){return this.rerunQuery({src:"query",id:kid,txt:r})};_KDBQuery.prototype.updateStatus=function(){var a,error1;if(this.kQStatus){a=new Function("x",this.kQStatus+" = x");try{a(this.kQNum)}catch(error1){null}}};_KDBQuery.prototype.updateObjects=function(){var j,len,n,o,ref,results;ref=this.updObjs;for(j=0,len=ref.length;j<len;j++){o=ref[j];this.updateObj(o,true,document.querySelector("[k-id='"+o+"']"))}if(this.kDispUpd){if(typeof this.result!=="object"){console.error("kdb-query: dictionary is expected with dispatch setting");return console.log(this.result)}results=[];for(n in this.result){if(n){results.push(this.updateObj(n,false,document.querySelector("[k-id='"+n+"']")))}}return results}};_KDBQuery.prototype.updateObj=function(n,isUpd,o){var r;r=this.kDispUpd?this.result[isUpd?"":n]:this.result;if(r===void 0){return}return this.updObjWithRes(n,o,r)};_KDBQuery.prototype.updObjWithRes=function(n,o,r){var a,e,err,error1,error2,i,idx,j,len,opt,prv,ref,ref1,ref2,ref3,ref4,s,ty;if(!o){a=new Function("x",n+" = x");try{a(r)}catch(error1){null}return}if(o.kdbUpd){try{return o.kdbUpd(r,this.kID)}catch(error2){err=error2;console.log("kdb-query:exception in kdbUpd");return console.log(err)}}else if((ref=o.nodeName)==="SELECT"||ref==="DATALIST"){prv=o.nodeName==="SELECT"&&o.selectedIndex>=0?o.options[o.selectedIndex].text:null;idx=-1;o.innerHTML="";for(i=j=0,len=r.length;j<len;i=++j){e=r[i];opt=document.createElement("option");opt.value=e.toString();opt.text=e.toString();if(prv===opt.text){idx=i}o.appendChild(opt)}if(idx>=0&&((ref1=o.attributes["k-preserve"])!=null?ref1.textContent:void 0)){return o.selectedIndex=idx}}else if((ref2=o.nodeName)==="KDB-CHART"||ref2==="KDB-TABLE"||ref2==="KDB-EDITOR"){return setTimeout(function(_this){return function(){return o.kdbUpd(r,_this.kID)}}(this),0)}else{a=((ref3=o.attributes["k-append"])!=null?ref3.textContent:void 0)||"overwrite";ty=((ref4=o.attributes["k-content-type"])!=null?ref4.textContent:void 0)||"text";s=o.textContent?"\n":"";if(ty==="text"){if(a==="top"){return o.textContent=r.toString()+s+o.textContent}else if(a==="bottom"){return o.textContent+=s+r.toString()}else{return o.textContent=r.toString()}}else{if(a==="overwrite"){return o.innerHTML=r.toString()}if(a==="top"){return o.insertAdjacentHTML("afterBegin",r.toString()+s)}else{return o.insertAdjacentHTML("beforeEnd",s+r.toString())}}}};_KDBQuery.prototype.resolveRefs=function(q,args){var e,j,len,n,ref,ref1,txt,v,val;if(this.debug){console.log(args)}if(!this.kRefs){return q}if(!this.kMap){this.kMap={};ref=this.kRefs;for(j=0,len=ref.length;j<len;j++){e=ref[j];this.kMap[e]=null}for(e in this.kMap){this.kMap[e]=document.querySelector("[k-id='"+e+"']")}}ref1=this.kMap;for(n in ref1){v=ref1[n];if(!v){val=args[n];if(val===null||val===void 0){val=keval(n)}txt=val===null||val===void 0?n:val.toString()}else{txt=extractInfo(v)}q=q.replace(new RegExp("\\$"+n.replace(/(\[|\])/g,".")+"\\$","g"),this.escape(txt))}if(this.debug){console.log("kdb-query: after resolve - "+q)}return q};_KDBQuery.prototype.escape=function(s){if(this.escapeQ){return s.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\240/g," ").replace(/\n/g,"\\n")}else{return s}};return _KDBQuery}(HTMLElement);_KDBTable=function(superClass){extend(_KDBTable,superClass);function _KDBTable(){return _KDBTable.__super__.constructor.apply(this,arguments)}_KDBTable.prototype.createdCallback=function(){var ref,ref1,ref2,ref3,ref4,ref5,ref6,ref7,ref8;this.srv=((ref=this.attributes["k-srv"])!=null?ref.textContent:void 0)||"";this.query=((ref1=this.attributes["k-query"])!=null?ref1.textContent:void 0)||this.textContent;this.kLib=((ref2=this.attributes["k-lib"])!=null?ref2.textContent:void 0)||"table";this.debug=((ref3=this.attributes["debug"])!=null?ref3.textContent:void 0)||null;this.escHtml=(((ref4=this.attributes["k-escape-html"])!=null?ref4.textContent:void 0)||"true")==="true";this.kConfig=(ref5=this.attributes["k-config"])!=null?ref5.textContent:void 0;this.kClass=((ref6=this.attributes["k-class"])!=null?ref6.textContent:void 0)||"kdb-table";this.kStyle=((ref7=this.attributes["k-style"])!=null?ref7.textContent:void 0)||"";this.kSearch=(((ref8=this.attributes["k-search"])!=null?ref8.textContent:void 0)||"false")==="true";this.inited=false;this.initContainer();if(this.debug){return console.log("kdb-table: srv: "+this.srv+", query: "+this.query+", lib:"+this.kLib)}};_KDBTable.prototype.initContainer=function(){var cont,ref;if((ref=this.kLib)==="jsgrid"||ref==="datatables"){if(this.kCont){this.innerHTML=""}this.kCont=document.createElement(this.kLib==="jsgrid"?"div":"table");cont=document.createElement("div");cont.className=this.kClass;cont.style.cssText=this.kStyle;if(this.kLib==="datatables"){this.kCont.style.cssText="width: 100%;"}cont.appendChild(this.kCont);return this.appendChild(cont)}};_KDBTable.prototype.attachedCallback=function(){var cfg,q,ref,ref1,srv;if(!this.inited){if(this.debug){console.log("kdb-table: initing")}this.inited=true;if(this.query===""){return}if(/\w+/.test(this.query)){if(srv=document.querySelector("[k-id='"+this.query+"']")){this.query=srv}}if(typeof this.query==="string"){if(this.debug){console.log("kdb-table: creating a query")}q=new KDB.KDBQuery;q.setAttribute("k-query",this.query);if(this.srv){q.setAttribute("k-srv",this.srv)}if(this.debug){q.setAttribute("debug",this.debug)}q.setupQuery();this.query=q}if(!((ref=this.query)!=null?ref.runQuery:void 0)){return}this.query.onresult(function(_this){return function(ev){return _this.onResult(ev)}}(this));if(this.debug){console.log("kdb-table: init complete")}if(((ref1=this.kLib)==="jsgrid"||ref1==="datatables")&&this.kConfig){cfg=getConfig(this.kConfig);if(!((cfg!=null?cfg.pageLoading:void 0)||(cfg!=null?cfg.serverSide:void 0))){return}if(this.debug){console.log("kdb-table: pageLoading/serverSide is set, forcing the first page")}return this.query.rerunQuery({start:0,size:1,sortBy:"",sortOrder:"",data:null})}}};_KDBTable.prototype.onResult=function(ev){if(this.debug){console.log("kdb-table: got event")}return this.updateTbl(ev.detail)};_KDBTable.prototype.kdbUpd=function(r,kID){this.query=document.querySelector("[k-id='"+kID+"']");return this.updateTbl(r)};_KDBTable.prototype.updateTbl=function(r){var c,d,e,j,len,ref,ref1,tbl;if(this.debug){console.log("kdb-table: data")}if(this.debug){console.log(r)}if(this.kLib==="jsgrid"&&((ref=this.kCfg)!=null?ref.pageLoading:void 0)){return this.updateJSGrid(r)}if(this.kLib==="datatables"&&((ref1=this.kCfg)!=null?ref1.serverSide:void 0)){return this.updateDT(r)}if((r.length||0)===0){return}if(this.kLib==="jsgrid"){return this.updateJSGrid(r)}if(this.kLib==="datatables"){return this.updateDT(r)}tbl="<table class='"+this.kClass+"' style='"+this.kStyle+"'><tr>";for(c in r[0]){tbl+="<th>"+this.escapeHtml(c)+"</th>"}tbl+="</tr>";for(j=0,len=r.length;j<len;j++){e=r[j];tbl+="<tr>";for(c in e){d=e[c];tbl+="<td>"+this.escapeHtml(d)+"</td>"}tbl+="</tr>"}tbl+="</table>";return this.innerHTML=tbl};_KDBTable.prototype.updateJSGrid=function(r){var cfg,f,n,ref,ref1,v;if((ref=this.kCfg)!=null?ref.pageLoading:void 0){return this.kPromise.resolve(r)}this.kData=r;f=[];ref1=r[0];for(n in ref1){v=ref1[n];if(typeof v==="string"){f.push({name:n,type:"text"})}else if(typeof v==="number"){f.push({name:n,type:"number"})}else if(typeof v==="boolean"){f.push({name:n,type:"checkbox"})}else if(v instanceof Date){f.push({name:n,type:"text",subtype:"date",itemTemplate:function(v){return v.toISOString()}})}else{f.push({name:n,type:"text"})}}cfg={width:"100%",height:"100%",filtering:this.kSearch,sorting:true,paging:r.length>100,pageButtonCount:5,pageSize:50,fields:f,controller:{loadData:function(_this){return function(a){return _this.loadData(a)}}(this)}};if(this.kConfig){cfg=mergeCfgs(cfg,getConfig(this.kConfig))}if(cfg.pageLoading){cfg.paging=true;cfg.autoload=true}else{cfg.data=r}this.kCfg=cfg;if(this.debug){console.log("kdb-table: cfg")}if(this.debug){console.log(cfg)}return $(this.kCont).jsGrid(cfg)};_KDBTable.prototype.updateDT=function(r){var c,cfg,n,ref,ref1,v;if(((ref=this.kCfg)!=null?ref.serverSide:void 0)&&this.kCB&&r.draw!=null){if(r.draw===this.kDraw){this.kCB(r);this.kCB=null}return}if(this.kCfg){this.initContainer()}c=[];ref1=r[0];for(n in ref1){v=ref1[n];c.push({data:n,title:n})}cfg={columns:c,searching:this.kSearch,scrollX:true,processing:true};if(this.kConfig){cfg=mergeCfgs(cfg,getConfig(this.kConfig))}this.kCfg=cfg;if(cfg.paging==null){cfg.paging=r.length>100||(cfg.serverSide||false)}if(cfg.serverSide){cfg.ajax=function(_this){return function(d,cb,set){_this.kCB=cb;_this.kDraw=d.draw;return _this.query.rerunQuery({data:JSON.stringify(d)})}}(this)}else{cfg.data=r}if(this.debug){console.log("kdb-table: cfg");console.log(cfg)}return $(this.kCont).DataTable(cfg)};_KDBTable.prototype.loadData=function(f){if(f.pageIndex){this.query.rerunQuery({start:(f.pageIndex-1)*f.pageSize,size:f.pageSize,sortBy:f.sortField||"",sortOrder:f.sortOrder||"asc"});this.kPromise=$.Deferred();return this.kPromise}return this.kData.filter(function(_this){return function(e){var j,len,r,ref,v;r=true;ref=_this.kCfg.fields;for(j=0,len=ref.length;j<len;j++){v=ref[j];if(v.type==="text"&&v.subtype==="date"){r=r&&(!f[v.name]||-1<e[v.name].toISOString().indexOf(f[v.name]))}else if(v.type==="text"){r=r&&(!f[v.name]||-1<e[v.name].indexOf(f[v.name]))}else if(v.type==="number"){r=r&&(!f[v.name]||1>Math.abs(e[v.name]-f[v.name]))}else{r=r&&(f[v.name]===void 0||e[v.name]===f[v.name])}}return r}}(this))};_KDBTable.prototype.escapeHtml=function(s){s=s===null?"":s.toString();if(this.escHtml){return s.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}else{return s}};return _KDBTable}(HTMLElement);_KDBChart=function(superClass){extend(_KDBChart,superClass);function _KDBChart(){return _KDBChart.__super__.constructor.apply(this,arguments)}_KDBChart.prototype.createdCallback=function(){var kClass,kStyle,ref,ref1,ref10,ref2,ref3,ref4,ref5,ref6,ref7,ref8,ref9;this.srv=((ref=this.attributes["k-srv"])!=null?ref.textContent:void 0)||"";this.query=((ref1=this.attributes["k-query"])!=null?ref1.textContent:void 0)||this.textContent;this.debug=((ref2=this.attributes["debug"])!=null?ref2.textContent:void 0)||null;this.kFlow=(((ref3=this.attributes["k-flow"])!=null?ref3.textContent:void 0)||"false")==="true";this.kConfig=(ref4=this.attributes["k-config"])!=null?ref4.textContent:void 0;kClass=((ref5=this.attributes["k-class"])!=null?ref5.textContent:void 0)||"";kStyle=((ref6=this.attributes["k-style"])!=null?ref6.textContent:void 0)||"";this.kChType=((ref7=this.attributes["k-chart-type"])!=null?ref7.textContent:void 0)||"line";this.kTime=(ref8=this.attributes["k-time-col"])!=null?ref8.textContent:void 0;this.kData=(ref9=this.attributes["k-data-cols"])!=null?ref9.textContent.split(" ").filter(function(el){return el.length>0}):void 0;this.inited=false;this.chart=null;this.chSrc="";this.kDygraph=/^dygraph/.test(this.kChType);if(this.kDygraph){this.kChType=((ref10=this.kChType.match(/^dygraph-(.*)$/))!=null?ref10[1]:void 0)||"line"}this.kCont=document.createElement("div");this.kCont.className=kClass;this.kCont.style.cssText=kStyle;this.innerHTML="";this.appendChild(this.kCont);if(this.debug){return console.log("kdb-chart: query:"+this.query+", type:"+this.kChType+", cfg:"+this.kConfig)}};_KDBChart.prototype.attachedCallback=function(){var q,ref,srv;if(!this.inited){if(this.debug){console.log("kdb-chart: initing")}this.inited=true;if(this.query===""){return}if(/\w+/.test(this.query)){if(srv=document.querySelector("[k-id='"+this.query+"']")){this.query=srv}}if(typeof this.query==="string"){if(this.debug){console.log("kdb-chart: creating a query")}q=new KDB.KDBQuery;q.setAttribute("k-query",this.query);if(this.srv){q.setAttribute("k-srv",this.srv)}if(this.debug){q.setAttribute("debug",this.debug)}q.setupQuery();this.query=q}if(!((ref=this.query)!=null?ref.runQuery:void 0)){return}this.query.onresult(function(_this){return function(ev){return _this.onResult(ev)}}(this));if(this.debug){return console.log("kdb-chart: init complete")}}};_KDBChart.prototype.attributeChangedCallback=function(a,o,n){if(a==="k-init"){this.createdCallback();return this.attachedCallback()}};_KDBChart.prototype.onResult=function(ev){if(this.debug){console.log("kdb-chart: got event")}if(this.debug){console.log(ev.detail)}return this.updateChart(ev.detail)};_KDBChart.prototype.kdbUpd=function(r){if(this.debug){console.log("kdb-chart: got update")}if(this.debug){console.log(r)}return this.updateChart(r)};_KDBChart.prototype.updateDyChart=function(r){var cfg,data,dt,tm;if(this.chart&&this.kFlow){if(this.debug){console.log("Flow update")}if(this.chSrc==="dy"){data=r}if(this.chSrc==="user"){data=this.convertDyAllTbl(r)}if(this.chSrc==="auto"){data=this.convertDyTbl(r,this.dtCfg.time,this.dtCfg.data)[1]}if(this.debug){console.log(data)}this.dyData=this.dyData.concat(data).slice(data.length);return this.chart.updateOptions({file:this.dyData})}if(this.kChType==="use-config"){if(!(this.kConfig&&typeof r==="object")){return}if(r.length===0){return}if(this.debug){console.log("kdb-chart: will use provided cfg")}cfg=getConfig(this.kConfig);data=this.convertDyAllTbl(r);this.chSrc="user"}else{if(typeof r==="object"&&r.length===2&&(r[0]instanceof Array||typeof r[0]==="string")){if(this.debug){console.log("kdb-chart: raw config")}data=r[0];cfg=r[1];this.chSrc="dy"}else{if(this.debug){console.log("Will detect the user format")}if(!(tm=this.detectTime(r[0]))){return}if(this.debug){console.log("Time is "+tm)}dt=this.detectData(r[0]);if(this.debug){console.log("Data is "+dt)}this.dtCfg={data:dt,time:tm};if(dt.length===0){return}r=this.convertDyTbl(r,tm,dt);data=r[1];cfg={labels:r[0]};this.chSrc="auto"}}if(this.kChType==="merge-config"){if(this.debug){console.log("kdb-chart: will merge cfgs")}cfg=mergeCfgs(cfg,getConfig(this.kConfig))}if(typeof data==="string"){cfg=mergeCfgs(cfg,{xValueParser:function(_this){return function(d){return _this.convDyTime(d)}}(this),axes:{x:{valueFormatter:Dygraph.dateString_,ticker:Dygraph.dateTicker}}})}if(this.debug){console.log("kdb-chart: cfg is")}if(this.debug){console.log(cfg)}if(this.debug){console.log(data)}if(this.kFlow){this.dyData=data}return this.updateDyChartWithData(data,cfg)};_KDBChart.prototype.updateDyChartWithData=function(d,c){return this.chart=new Dygraph(this.kCont,d,c)};_KDBChart.prototype.updateChart=function(r){var cfg,d,dt,fmt,n,ref,t,tbl,tm,v,xfmt;if(this.kDygraph){return this.updateDyChart(r)}if(this.chart&&this.kFlow){if(this.chSrc==="c3"){return this.updateFlowWithData(r)}tbl=r;cfg={};if(r["data"]){if(r.to){cfg.to=r.to}if(r.length){cfg.length=r.length}if(r.duration){cfg.duration=r.duration}tbl=r.data}if(this.chSrc==="user"){cfg.rows=this.convertAllTbl(tbl)}if(this.chSrc==="auto"){cfg.rows=this.convertTbl(tbl,this.dtCfg.time,this.dtCfg.data)}if(this.chSrc==="dict"){cfg.columns=function(){var results;results=[];for(n in tbl){v=tbl[n];results.push([n].concat(v))}return results}()}return this.updateFlowWithData(cfg)}if(this.kChType==="use-config"){if(!(this.kConfig&&typeof r==="object")){return}if(r.length===0){return}if(this.debug){console.log("kdb-chart: will use provided cfg")}cfg=getConfig(this.kConfig);cfg.data.rows=this.convertAllTbl(r);this.chSrc="user"}else if(typeof r==="object"&&r.data){if(this.debug){console.log("C3 format detected")}if(this.debug){console.log(r)}this.chSrc="c3";cfg=r}else if(typeof r==="object"&&r.length>0){if(this.debug){console.log("Will detect the user format")}if(!(tm=this.detectTime(r[0]))){return}fmt=this.detectTimeFmt(r[0][tm]);xfmt=this.detectTimeXFmt(r,tm,fmt);if(this.debug){console.log("Time is "+tm+", fmt is "+fmt+", xfmt is "+xfmt)}dt=this.detectData(r[0]);if(this.debug){console.log("Data is "+dt)}this.dtCfg={data:dt,time:tm};if(dt.length===0){return}cfg={data:{x:tm,rows:this.convertTbl(r,tm,dt),type:this.kChType,xFormat:fmt},point:{show:false},axis:{x:{type:"timeseries",tick:{fit:true,format:xfmt}}}};this.chSrc="auto"}else if(typeof r==="object"){t=((ref=this.attributes["k-chart-type"])!=null?ref.textContent:void 0)||"pie";d=function(){var results;results=[];for(n in r){v=r[n];results.push([n].concat(v))}return results}();cfg={data:{columns:d,type:t}};this.chSrc="dict"}if(this.kChType==="merge-config"){if(this.debug){console.log("kdb-chart: will merge cfgs")}cfg=mergeCfgs(cfg,getConfig(this.kConfig))}if(this.debug){console.log("kdb-chart: cfg is");
}if(this.debug){console.log(cfg)}return this.updateChartWithData(cfg)};_KDBChart.prototype.updateChartWithData=function(d){d["bindto"]=this.kCont;return this.chart=c3.generate(d)};_KDBChart.prototype.updateFlowWithData=function(d){return this.chart.flow(d)};_KDBChart.prototype.convertTbl=function(t,tm,dt){var cols,j,len,n,rec,rows;cols=[];for(n in t[0]){if(n===tm||indexOf.call(dt,n)>=0){cols.push(n)}}rows=[cols];for(j=0,len=t.length;j<len;j++){rec=t[j];rows.push(function(){var k,len1,results;results=[];for(k=0,len1=cols.length;k<len1;k++){n=cols[k];results.push(n===tm?this.convTime(rec[n]):rec[n])}return results}.call(this))}return rows};_KDBChart.prototype.convertDyTbl=function(t,tm,dt){var cols,j,len,n,rec,rows;cols=[tm];for(n in t[0]){if(indexOf.call(dt,n)>=0){cols.push(n)}}rows=[];for(j=0,len=t.length;j<len;j++){rec=t[j];rows.push(function(){var k,len1,results;results=[];for(k=0,len1=cols.length;k<len1;k++){n=cols[k];results.push(n===tm?this.convDyTime(rec[n]):rec[n])}return results}.call(this))}return[cols,rows]};_KDBChart.prototype.convertAllTbl=function(t){var cols,f,fmts,j,len,n,rec,ref,rows,v;if(!t.length){t=[t]}cols=[];fmts=[];ref=t[0];for(n in ref){v=ref[n];cols.push(n);if(f=this.detectTimeFmt(v)){fmts[n]=d3.time.format(f)}}rows=[cols];for(j=0,len=t.length;j<len;j++){rec=t[j];rows.push(function(){var k,len1,results;results=[];for(k=0,len1=cols.length;k<len1;k++){n=cols[k];results.push(fmts[n]?fmts[n].parse(this.convTime(rec[n])):rec[n])}return results}.call(this))}return rows};_KDBChart.prototype.convertDyAllTbl=function(t){var cols,i,j,len,n,rec,rows;if(!t.length){t=[t]}rows=[];cols=function(){var results;results=[];for(n in t[0]){results.push(n)}return results}();for(j=0,len=t.length;j<len;j++){rec=t[j];rows.push(function(){var k,len1,results;results=[];for(i=k=0,len1=cols.length;k<len1;i=++k){n=cols[i];results.push(i===0?this.convDyTime(rec[n]):rec[n])}return results}.call(this))}return rows};_KDBChart.prototype.detectData=function(r){var n,v;if(this.kData){return this.kData}for(n in r){v=r[n];if(typeof v==="number"||v instanceof Number){return[n]}}return[]};_KDBChart.prototype.detectTime=function(r){var n,t,v;if(this.kTime&&r[this.kTime]){return this.kTime}t=null;for(n in r){v=r[n];if(v instanceof Date){return n}if(typeof v==="string"&&this.detectTimeFmt(v)){return n}if(!t&&v instanceof Number){t=n}}return t};_KDBChart.prototype.detectTimeFmt=function(v){if(v instanceof Date){return function(d){return d}}if(/^\d\d:\d\d:\d\d\.\d\d\d/.test(v)){return"%H:%M:%S.%L"}if(/^\d\d\d\d[-\.]\d\d[-\.]\d\d[DT]\d\d:\d\d:\d\d\.\d\d\d/.test(v)){return"%Y-%m-%dT%H:%M:%S.%L"}if(/^\d\d\d\d-\d\d-\d\d/.test(v)){return"%Y-%m-%d"}if(/^\d\d\d\d\.\d\d\.\d\d/.test(v)){return"%Y.%m.%d"}if(/^\d+D\d\d:\d\d:\d\d\.\d\d\d/.test(v)){return"%jT%H:%M:%S.%L"}if(/^\d\d:\d\d:\d\d/.test(v)){return"%H:%M:%S"}if(/^\d\d:\d\d/.test(v)){return"%H:%M"}};_KDBChart.prototype.detectTimeXFmt=function(r,tm,f){var fmt,i;if(typeof f==="string"&&f.length<12){return f}if(typeof f==="string"){fmt=d3.time.format(f);f=function(d){return fmt.parse(d)}}i=Math.abs(f(this.convTime(r[r.length-1][tm]))-f(this.convTime(r[0][tm])));if(i<864e5){return"%H:%M:%S.%L"}return"%Y.%m.%dT%H:%M"};_KDBChart.prototype.convTime=function(d){if(!(typeof d==="string"&&d.length>=20)){return d}if(d[d.length-4]!=="."){d=d.slice(0,-6)}if(d[4]==="."){d=d.replace(".","-").replace(".","-")}return d.replace("D","T")};_KDBChart.prototype.convDyTime=function(d){if(typeof d!=="string"){return d}if(!(0<=d.indexOf(":")||d[4]==="."&&d[7]==="."||d[4]==="-")){return Number(d)}if(d[4]==="."){d=d.replace(".","-").replace(".","-")}if(0<=d.indexOf("D")){d=d.replace("D","T")}if(!(d[4]==="-"||d[2]===":")){d=d.match(/^\d+T(.*)$/)[1]}if(d[2]===":"){d="2000-01-01T"+d}return new Date(d)};return _KDBChart}(HTMLElement);_KDBEditor=function(superClass){extend(_KDBEditor,superClass);function _KDBEditor(){return _KDBEditor.__super__.constructor.apply(this,arguments)}_KDBEditor.prototype.createdCallback=function(){var kClass,kStyle,ref,ref1,ref2,ref3,ref4;this.query=(ref=this.attributes["k-query"])!=null?ref.textContent:void 0;this.debug=((ref1=this.attributes["debug"])!=null?ref1.textContent:void 0)||null;this.kConfig=(ref2=this.attributes["k-config"])!=null?ref2.textContent:void 0;kClass="k-ace-editor "+(((ref3=this.attributes["k-class"])!=null?ref3.textContent:void 0)||"");kStyle=((ref4=this.attributes["k-style"])!=null?ref4.textContent:void 0)||"";this.kEditor=null;this.kCont=document.createElement("pre");this.kCont.className=kClass;this.kCont.style.cssText=kStyle;this.kCont.textContent=this.textContent;this.kMarkers=null;this.innerHTML="";this.appendChild(this.kCont);if(this.debug){return console.log("kdb-editor: query: "+this.query)}};_KDBEditor.prototype.attachedCallback=function(){var ref,srv;this.kEditor=ace.edit(this.kCont);if(srv=document.querySelector("[k-id='"+this.query+"']")){this.query=srv}this.setCfg();if((ref=this.query)!=null?ref.runQuery:void 0){return this.query.onresult(function(_this){return function(ev){return _this.onResult(ev)}}(this))}};_KDBEditor.prototype.onResult=function(ev){if(this.debug){console.log("kdb-editor: got event")}if(this.debug){console.log(ev.detail)}return this.kdbUpd(ev.detail)};_KDBEditor.prototype.setCfg=function(){var cfg;cfg={theme:"ace/theme/textmate",mode:"ace/mode/q",readOnly:true,scrollPastEnd:false,fadeFoldWidgets:false,behavioursEnabled:true,useSoftTabs:true,animatedScroll:true,verticalScrollBar:false,horizontalScrollBar:false,highlightSelectedWord:true,showGutter:true,displayIndentGuides:false,showInvisibles:false,highlightActiveLine:true,selectionStyle:"line",wrap:"off",foldStyle:"markbegin",fontSize:12,keybindings:"ace",showPrintMargin:true,useElasticTabstops:false,useIncrementalSearch:false,execLine:{win:"Ctrl-Return",mac:"Command-Return"},execSelection:{win:"Ctrl-e",mac:"Command-e"}};if(this.kConfig){cfg=mergeCfgs(cfg,getConfig(this.kConfig))}if(this.debug){console.log("kdb-editor: config")}if(this.debug){console.log(cfg)}this.kCfg=cfg;this.kEditor.setTheme(cfg.theme);this.kEditor.getSession().setMode(cfg.mode);this.kEditor.setReadOnly(cfg.readOnly);this.kEditor.setOption("scrollPastEnd",cfg.scrollPastEnd);this.kEditor.setFadeFoldWidgets(cfg.fadeFoldWidgets);this.kEditor.setBehavioursEnabled(cfg.behavioursEnabled);this.kEditor.session.setUseSoftTabs(cfg.useSoftTabs);this.kEditor.setAnimatedScroll(cfg.animatedScroll);this.kEditor.setOption("vScrollBarAlwaysVisible",cfg.verticalScrollBar);this.kEditor.setOption("hScrollBarAlwaysVisible",cfg.horizontalScrollBar);this.kEditor.setHighlightSelectedWord(cfg.highlightSelectedWord);this.kEditor.renderer.setShowGutter(cfg.showGutter);this.kEditor.setDisplayIndentGuides(cfg.displayIndentGuides);this.kEditor.setShowInvisibles(cfg.showInvisibles);this.kEditor.setHighlightActiveLine(cfg.highlightActiveLine);this.kEditor.setOption("selectionStyle",cfg.selectionStyle);this.kEditor.setOption("wrap",cfg.wrap);this.kEditor.session.setFoldStyle(cfg.foldStyle);this.kEditor.setFontSize(cfg.fontSize);if(cfg.keybindings!=="ace"){this.kEditor.setKeyboardHandler(cfg.keybindings)}this.kEditor.renderer.setShowPrintMargin(cfg.showPrintMargin);if(cfg.useElasticTabstops){this.kEditor.setOption("useElasticTabstops",cfg.useElasticTabstops)}if(cfg.useIncrementalSearch){this.kEditor.setOption("useIncrementalSearch",cfg.useIncrementalSearch)}return this.setCommands()};_KDBEditor.prototype.kdbUpd=function(r){var Range,a,cfg,j,k,len,len1,m,ref,ref1,ref2;if(this.debug){console.log("kdb-editor update");console.log(r)}cfg=null;if(typeof r==="object"&&!Array.isArray(r)){cfg=r;r=cfg.text}if(typeof r==="string"||Array.isArray(r)){a=((ref=this.attributes["k-append"])!=null?ref.textContent:void 0)||"overwrite";if(a==="overwrite"){this.kEditor.setValue(r,0);if(this.kMarkers){ref1=this.kMarkers;for(j=0,len=ref1.length;j<len;j++){m=ref1[j];this.kEditor.getSession().removeMarker(m)}this.kMarkers=null}this.kEditor.getSession().clearAnnotations();this.kEditor.getSession().clearBreakpoints()}else if(a==="top"){this.kEditor.navigateFileStart();this.kEditor.insert(r)}else{this.kEditor.navigateFileEnd();this.kEditor.navigateLineEnd();this.kEditor.insert(r)}this.kEditor.navigateTo(0,0)}if(cfg){if(cfg.row!=null){this.kEditor.scrollToLine(cfg.row||0,true,true,null);this.kEditor.navigateTo(cfg.row||0,cfg.column||0)}if(cfg.markers!=null){if(this.kMarkers){ref2=this.kMarkers;for(k=0,len1=ref2.length;k<len1;k++){m=ref2[k];this.kEditor.getSession().removeMarker(m)}}Range=ace.require("./range").Range;this.kMarkers=function(){var len2,p,ref3,results;ref3=cfg.markers;results=[];for(p=0,len2=ref3.length;p<len2;p++){m=ref3[p];results.push(this.kEditor.getSession().addMarker(new Range(m.xy[0],m.xy[1],m.xy[2],m.xy[3]),m["class"],m.type||"text",false))}return results}.call(this)}if(cfg.annotations!=null){this.kEditor.getSession().setAnnotations(cfg.annotations)}if(cfg.breakpoints!=null){return this.kEditor.getSession().setBreakpoints(cfg.breakpoints)}}};_KDBEditor.prototype.setCommands=function(){var ref;return(ref=this.kEditor)!=null?ref.commands.addCommands([{name:"execLine",bindKey:this.kCfg.execLine,readOnly:true,exec:function(_this){return function(e){return _this.execLine(e)}}(this)},{name:"execSelection",bindKey:this.kCfg.execSelection,readOnly:true,exec:function(_this){return function(e){return _this.execSelection(e)}}(this)}]):void 0};_KDBEditor.prototype.execLine=function(e){var l;if(!(l=this.kEditor.getSession().getLine(this.kEditor.getCursorPosition().row))){return}if(this.debug){console.log("exec line: "+l)}return this.sendEv(l)};_KDBEditor.prototype.execSelection=function(e){var s;if(!(s=this.kEditor.getSelectedText())){return}if(this.debug){console.log("exec select: "+s)}return this.sendEv(s)};_KDBEditor.prototype.sendEv=function(s){return this.dispatchEvent(this.getEv(s))};_KDBEditor.prototype.getEv=function(s){return new CustomEvent("execText",{detail:s,bubbles:true,cancelable:true})};_KDBEditor.prototype.onexec=function(f){return this.addEventListener("execText",f)};return _KDBEditor}(HTMLElement);if(window.KDB==null){window.KDB={}}KDB.processJSONP=function(id,res){if(this.debug){console.log("kdb-srv(JSONP): "+id+res)}return typeof jsonpRegistry[id]==="function"?jsonpRegistry[id](res):void 0};KDB.rerunQuery=function(kID,args){var ref;return(ref=document.querySelector("[k-id='"+kID+"']"))!=null?ref.rerunQuery(args):void 0};KDB.KDBChart=document.registerElement("kdb-chart",{prototype:_KDBChart.prototype});KDB.KDBSrv=document.registerElement("kdb-srv",{prototype:_KDBSrv.prototype});KDB.KDBQuery=document.registerElement("kdb-query",{prototype:_KDBQuery.prototype});KDB.KDBTable=document.registerElement("kdb-table",{prototype:_KDBTable.prototype});KDB.KDBEditor=document.registerElement("kdb-editor",{prototype:_KDBEditor.prototype})}).call(this);