'use strict'; // display 'Wait' var editor1; var editor2; var plot; function debug(txt) { var dbg = document.getElementById("debug"); dbg.appendChild(document.createTextNode(txt)); dbg.appendChild(document.createElement("br")); } function setupAccordion(accordion, container, inactive) { function onAccordion(event, ui) { if (ui.oldHeader.length==0) $(container).slideDown(400); else $(container).slideUp(400); } var config = { collapsible: true }; if (inactive) { config.active = false; $(container).hide(); } var acc = $(accordion); acc.accordion(config); acc.on("accordionbeforeactivate", onAccordion); } function onResize(event, ui) { if (!ui.size) return; $(event.target.id).css({marginRight:'2px'}); var editor = event.target.id=="textcontainer1" ? editor1 : editor2; editor.setSize("100%", ui.size.height); editor.refresh(); } function onResizeCenter(event, ui) { var w = document.getElementById("cameracontainer").clientWidth/4; var offy = 0; var offx = 5; var cont = document.getElementById("center").childNodes[0]; var nn; if (cont) { nn = parseInt(cont.id[cont.id.length-1]); document.getElementById("camera"+nn).height=parseInt(w*2); document.getElementById("camera"+nn).width=parseInt(w*2); } if (nn!=1) { document.getElementById("camera1").height=parseInt(w)-offy; document.getElementById("camera1").width=parseInt(w)-offx; } if (nn!=2) { document.getElementById("camera2").height=parseInt(w)-offy; document.getElementById("camera2").width=parseInt(w)-offx; } if (nn!=3) { document.getElementById("camera3").height=parseInt(w)-offy; document.getElementById("camera3").width=parseInt(w)-offx; } if (nn!=4) { document.getElementById("camera4").height=parseInt(w)-offy; document.getElementById("camera4").width=parseInt(w)-offx; } document.getElementById("center").width=parseInt(w*2); document.getElementById("cont1").width=parseInt(w); document.getElementById("cont2").width=parseInt(w); document.getElementById("cont3").width=parseInt(w); document.getElementById("cont4").width=parseInt(w); document.getElementById("cont1").height=parseInt(w); document.getElementById("cont2").height=parseInt(w); document.getElementById("cont3").height=parseInt(w); document.getElementById("cont4").height=parseInt(w); drawFullCam("camera1"); drawFullCam("camera2"); drawFullCam("camera3"); drawFullCam("camera4"); } function createEditor(textarea) { var editor; var config = { //value: "function myScript(){return 100;}\n", mode: { name: "text/typescript", globalVars: true }, lineNumbers: true, styleActiveLine: true, matchBrackets: true, indentUnit: 4, foldGutter: true, gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"], extraKeys: { //"Ctrl-D": "duplicateLine", //"Alt--": "goToBracket", //"Ctrl-H": "findPrev", "Ctrl-Down": "autocomplete", "Tab": "indentAuto", "Ctrl-Y": "deleteLine", "Ctrl-.": function(cm) { cm.foldCode(cm.getCursor()); }, "F11": function(cm) { editor.setOption("fullScreen", !editor.getOption("fullScreen")); }, "Ctrl-R": function(cm) { editor.execCommand("replace"); }, "Esc": function(cm) { if (editor.getOption("fullScreen")) editor.setOption("fullScreen", false); }, "Enter": function(cm) { editor.execCommand("indentAuto"); editor.execCommand("newlineAndIndent"); }, } }; editor = CodeMirror.fromTextArea(document.getElementById(textarea), config); editor.setOption("theme", "blackboard"); return editor; } function colorizeHTML(textarea) { var config = { //value: "function myScript(){return 100;}\n", mode: { name: "text/typescript", globalVars: true }, readOnly: true, }; CodeMirror.fromTextArea(document.getElementById(textarea), config); } function disableControls(disabled) { $('#event').prop('disabled', disabled); $('#pixel').prop('disabled', disabled); $('#cbpx').prop('disabled', disabled); $('#cbpx-c').prop('disabled', disabled); $('#cbpx-b').prop('disabled', disabled); $('#cbpx-p').prop('disabled', disabled); $('#cbpx-x').prop('disabled', disabled); $('#file').prop('disabled', disabled); } function onReady() { //$('input,select').keypress(function(event) { return event.keyCode != 13; }); //colorizeHTML("code0"); //colorizeHTML("code1"); $("#accordion").accordion({collapsible:true,active:false,heightStyle:'content'}); $("#accordion").find('h3').filter(':contains(Runtime)').hide(); if (location.href.search('debug')==-1) $("#accordion").find('h3').filter(':contains(Debug)').hide(); $("#textcontainer1").resizable({handles:"s",autoHide:true,}); $("#textcontainer1").on("resize", onResize); $("#textcontainer2").resizable({handles:"s",autoHide:true,}); $("#textcontainer2").on("resize", onResize); $("#cameracontainer").on("resize", onResizeCenter); onResizeCenter(); $("#cont1").click(onClickNew); $("#cont2").click(onClickNew); $("#cont3").click(onClickNew); $("#cont4").click(onClickNew); $("#camera1").click(onClick); $("#camera2").click(onClick); $("#camera3").click(onClick); $("#camera4").click(onClick); editor1 = createEditor("editor1"); editor2 = createEditor("editor2"); setupAccordion('#accordion5', '#editorcontainer1', true); setupAccordion('#accordion1', '#editorcontainer2'); $('#accordion5').on("accordionactivate", function() { $('#editorcontainer1fake').hide(); editor1.refresh(); }); $('#accordion1').on("accordionactivate", function() { $('#editorcontainer2fake').hide(); editor2.refresh(); }); setupAccordion('#accordion2', '#cameracontainer'); setupAccordion('#accordion3', '#waveformcontainer'); setupAccordion('#accordion4', '#helpcontainer', true); $("#selectfile1").on('change', onFile); $("#selectfile2").on('change', onFile); $(document).ajaxStart(function() { disableControls(true) }).ajaxStop(function() { disableControls(false); }); $.ajax({ type: "POST", cache: false, url: "index.php", success: onFilelistReceived, error: function(xhr) { if (xhr.status==0) alert("ERROR[0] - Request failed!"); else alert("ERROR[0] - "+xhr.statusText+" ["+xhr.status+"]"); } }); } function onFileSelect(event, ui) { onSubmit(ui.item.value); } function onFilelistReceived(result) { var dbg = document.getElementById("debug"); var pre = document.createElement("pre"); pre.appendChild(document.createTextNode(rc)); dbg.appendChild(pre); var rc; try { rc = JSON.parse(result); } catch (e) { alert("ERROR[0] - Decoding answer:\n"+e); return; } $("#file").autocomplete({source: rc, select: onFileSelect} ); document.getElementById("file").value = "2014/04/17-181"; onSubmit("2014/04/17-181"); } function setZoom(xfrom, xto, yfrom, yto) { var xaxis = plot.getXAxes()[0]; var yaxis = plot.getYAxes()[0]; if (xfrom!==undefined) xaxis.options.min = xfrom; if (xto!==undefined) xaxis.options.max = xto; if (yfrom!==undefined) yaxis.options.min = yfrom; if (yto!==undefined) yaxis.options.max = yto; plot.setupGrid(); plot.draw(); plot.clearSelection(); } function processCameraData(id, data) { var canv = document.getElementById(id); canv.dataAbs = [ ]; for (var i=0; i<1440; i++) if (data[i]!==undefined && data[i]!==null) canv.dataAbs[i] = data[i]; canv.min = Math.min.apply(Math, canv.dataAbs.filter(function(e){return !isNaN(e)})); canv.max = Math.max.apply(Math, canv.dataAbs.filter(function(e){return !isNaN(e)})); canv.dataRel = [ ]; for (var i=0; i<1440; i++) if (data[i]!==undefined && data[i]!==null) canv.dataRel[i] = (data[i]-canv.min)/canv.max; var n = id[id.length-1]; if (document.getElementById("cameraminon"+n).checked) document.getElementById("cameramin"+n).value = canv.min; if (document.getElementById("cameramaxon"+n).checked) document.getElementById("cameramax"+n).value = canv.max; onCameraMinMax(n); } function onDataReceived(rc) { var dbg = document.getElementById("debug"); var pre = document.createElement("pre"); pre.appendChild(document.createTextNode(rc)); dbg.appendChild(pre); try { rc = JSON.parse(rc); } catch (e) { alert("ERROR[1] - Decoding answer:\n"+e); return; } document.getElementById("event").max = rc.event.numEvents; var el = document.getElementById("numevents"); if (el.firstChild) el.removeChild(el.firstChild); el.appendChild(document.createTextNode(rc.event.numEvents)); if (rc.timeJs) { var err = document.getElementById("error"); while (err.lastChild) err.removeChild(err.lastChild); $("#accordion").accordion("option", "active", false); $("#accordion").find('h3').filter(':contains(Runtime)').hide(); var con = document.getElementById("console"); while (con.lastChild) con.removeChild(con.lastChild); } if (rc.err) { err.appendChild(document.createTextNode("Javascript runtime exception: "+rc.err.file+":"+rc.err.lineNumber)); err.appendChild(document.createTextNode("\n")); err.appendChild(document.createTextNode(rc.err.sourceLine)); err.appendChild(document.createTextNode("\n")); err.appendChild(document.createTextNode(rc.err.trace)); var editor = rc.err.file=="main" ? editor2 : editor1; editor.setCursor(rc.err.lineNumber-1, 1); $("#accordion").find('h3').filter(':contains(Runtime)').show(); $("#accordion").accordion("option", "active", 0); } var canv = document.getElementById("camera1"); if (rc.debug!==undefined) { con.appendChild(document.createTextNode(rc.debug)); debug("return "+JSON.stringify(rc.ret)); debug(""); debug("Execution times:"); debug("TimeJS="+(rc.timeJs*1000).toFixed(2)+"ms"); } if (rc.ret!==undefined && Array.isArray(rc.ret)) { if (!Array.isArray(rc.ret[0])) processCameraData("camera1", rc.ret); else processCameraData("camera1", rc.ret[0]); if (rc.ret.length>1) processCameraData("camera2", rc.ret[1]); if (rc.ret.length>2) processCameraData("camera3", rc.ret[2]); if (rc.ret.length>3) processCameraData("camera4", rc.ret[3]); } if (canv.dataAbs && rc.event) document.getElementById("value").value = canv.dataAbs[rc.event.pixel]; debug("TimePhp="+(rc.timePhp*1000).toFixed(1)+"ms"); if (Array.isArray(rc.waveform)) { var waveform = document.getElementById("waveform"); var data = [[],[],[],[]]; var min = []; var max = []; if (Array.isArray(rc.waveform) && rc.waveform.length==rc.event.numRoi) { min.push(Math.min.apply(Math, rc.waveform)); max.push(Math.max.apply(Math, rc.waveform)); for (var i=0; i 395.75) continue; if (isInside(pos.x, pos.y, mouse)) return cnt; cnt++; } } } if (isInside(7, -22, mouse)) return 1438; if (isInside(7, 22, mouse)) return 1439; return -1; } var inprogress = 0; function moveAnimate(n, target) { if (inprogress==n || inprogress<0) return; inprogress = target ? -n : n; var element = $("#camera"+n); //Allow passing in either a JQuery object or selector var newParent = $(target ? target : "#cont"+n); //Allow passing in either a JQuery object or selector var oldOffset = element.offset(); var newOffset = newParent.offset(); var w = newParent.width(); var h = newParent.height(); var temp = element.appendTo('body'); temp.css('position', 'absolute') .css('left', oldOffset.left) .css('top', oldOffset.top) .css('zIndex', 400); temp.animate( {'top': newOffset.top, 'left':newOffset.left, 'width':w, 'height': h}, 'slow', function() { temp = temp.appendTo(newParent); temp.css('position', 'default'); temp.css('width', 'default'); temp.css('height', 'default'); temp.css('left', '0'); temp.css('top', '0'); var canv = document.getElementById("camera"+n); canv.width = w; canv.height = h; drawFullCam("camera"+n); inprogress = 0; }); } function onClickNew(event) { var id = event.target.id; var n = parseInt(id[id.length-1]); if (id.substr(0, 3)=="cam") { var cont = document.getElementById("center").childNodes[0]; if (cont) { var nn = parseInt(cont.id[cont.id.length-1]); moveAnimate(nn); } moveAnimate(n, "#center"); } else moveAnimate(n); } function onClick(event) { var cont = document.getElementById("center").childNodes[0]; if (!cont) return; if (cont.id!=event.target.id) return; var mouse = getClickPosition(); var index = getIndex(event.target.id, mouse); if (index<0) return; document.getElementById("pixel").value = index; onPixel(); } function getClickPosition(event) { var event = window.event; if (event.offsetX !== undefined && event.offsetY !== undefined) return { x: event.offsetX, y: event.offsetY }; return { x: event.layerX, y: event.layerY }; } function onCameraMinMax(n) { var canv = document.getElementById("camera"+n); canv.zmin = document.getElementById("cameramin"+n).value; canv.zmax = document.getElementById("cameramax"+n).value; drawFullCam("camera"+n); } function onCameraMinMaxOn(n) { var canv = document.getElementById("camera"+n); var redraw; if (document.getElementById("cameraminon"+n).checked) { document.getElementById("cameramin"+n).setAttribute("disabled", "true"); document.getElementById("cameramin"+n).value = canv.min; redraw = true; } else document.getElementById("cameramin"+n).removeAttribute("disabled"); if (document.getElementById("cameramaxon"+n).checked) { document.getElementById("cameramax"+n).setAttribute("disabled", "true"); document.getElementById("cameramax"+n).value = canv.max; redraw = true; } else document.getElementById("cameramax"+n).removeAttribute("disabled"); if (redraw) onCameraMinMax(n); } function onWaveformMinMax() { var wf = document.getElementById("waveform"); var xmin, xmax, ymin, ymax; var redraw; if (!document.getElementById("waveformminon").checked) ymin = document.getElementById("waveformmin").value; if (!document.getElementById("waveformmaxon").checked) ymax = document.getElementById("waveformmax").value; setZoom(xmin, xmax, ymin, ymax); } function onWaveformMinMaxOn() { var wf = document.getElementById("waveform"); var xmin, xmax, ymin, ymax; var redraw; if (document.getElementById("waveformminon").checked) { document.getElementById("waveformmin").setAttribute("disabled", "true"); document.getElementById("waveformmin").value = wf.ymin; ymin = wf.ymin-5; } else document.getElementById("waveformmin").removeAttribute("disabled"); if (document.getElementById("waveformmaxon").checked) { document.getElementById("waveformmax").setAttribute("disabled", "true"); document.getElementById("waveformmax").value = wf.ymax; ymax = wf.ymax+5; } else document.getElementById("waveformmax").removeAttribute("disabled"); setZoom(xmin, xmax, ymin, ymax); } //document.addEventListener("click", getClickPosition, false); $(document).ready(onReady); // ================================== Pixel mapping ================================================= var codedMap = "966676:6:A;68656364626Y?\\?;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F;C;@;@_>`>8B7B6BZBXBWB_CaC`CFDEDDDlDgDoEmE>FAFDFGFJFMF90<0?0003060714121a0^0]0G1C1B1N2L2E2C2B2@2L3I3`6d6E7F7G7H7O8Q8192969T9U9X96;3;0;?;<;9;Q;N;K;H;E;B;W1) return "700"; // 666 hue *= 14; var sr = hueToHex(20-hue); var sg = hueToHex(14-hue); var sb = hueToHex(26-hue); return sr+sg+sb; } function drawHex(ctx, x, y, col, min, max) { if (col===undefined || col===null) return; var lvl = (col-min)/(max-min); ctx.fillStyle = "#"+HLStoRGB(lvl); ctx.save(); ctx.translate(x, y); ctx.scale(1/2, 1/3); ctx.beginPath(); ctx.moveTo( 1, 1); ctx.lineTo( 0, 2); ctx.lineTo(-1, 1); ctx.lineTo(-1, -1); ctx.lineTo( 0, -2); ctx.lineTo( 1, -1); ctx.fill(); ctx.restore(); } function Position(s, ring, i) { switch (s) { case 1: this.x = ring - i*0.5; this.y = + i; break; case 2: this.x = ring*0.5 - i; this.y = ring ; break; case 3: this.x = -ring*0.5 - i*0.5; this.y = ring - i; break; case 4: this.x = -ring + i*0.5; this.y = - i; break; case 5: this.x = -ring*0.5 + i; this.y = -ring ; break; case 0: this.x = ring*0.5 + i*0.5; this.y = -ring + i; break; } this.d = (function () { return this.x*this.x + this.y*this.y*3/4; }); } function drawFullCam(id) { var canv = document.getElementById(id); if (!canv) return; var ctx = canv.getContext("2d"); ctx.clearRect(0, 0, canv.width, canv.height); // ======================= Draw Graphics ====================== var data = canv.dataRel; if (!data) return; var min = (canv.zmin-canv.min)/canv.max; var max = (canv.zmax-canv.min)/canv.max; var scale = 83; var w = Math.min(canv.width/scale, canv.height/scale); ctx.save(); ctx.translate(canv.width/2, canv.height/2); ctx.scale(w*2, w*2); // ctx.rotate(Math.PI/3); ctx.scale(1, Math.sqrt(3)/2); ctx.translate(-0.5, 0); drawHex(ctx, 0, 0, data[map[0]], min, max); var cnt = 1; for (var ring=1; ring<24; ring++) { for (var s=0; s<6; s++) { for (var i=1; i<=ring; i++) { var pos = new Position(s, ring, i); if (pos.d() - pos.x > 395.75) continue; drawHex(ctx, pos.x, pos.y, data[map[cnt]], min, max); cnt++; } } } drawHex(ctx, 7, -22, data[map[1438]], min, max); drawHex(ctx, 7, 22, data[map[1439]], min, max); ctx.restore(); // ======================= Draw Legend ====================== var lmin = parseFloat(canv.min).toFixed(1); var lmax = parseFloat(canv.max).toFixed(1); var v0 = parseFloat(canv.zmin); var v1 = parseFloat(canv.zmax); var diff = v1-v0; var cw = canv.width; //var ch = canv.height; ctx.font = "8pt Arial"; ctx.textAlign = "right"; ctx.textBaseline = "top"; for (var i=0; i<11; i++) { ctx.strokeStyle = "#"+HLStoRGB(i/10); ctx.strokeText((v0+diff*i/10).toPrecision(3), cw-5, 125-i*12); } var mw = Math.max(ctx.measureText(lmin).width, ctx.measureText(lmax).width); ctx.textBaseline = "top"; ctx.strokeStyle = "#000"; //ctx.strokeText(min, 5+mw, 5+24); ctx.strokeText(lmin, 5+mw, 5+12); ctx.strokeText(lmax, 5+mw, 5); }