// jshint esversion: 6 const bpmData = {}; const corr = {}; const bpmscalesteps = 10; let bpmFactorHor = document.location.search.indexOf('bpmFactorHor=')>-1? document.location.search.split('bpmFactorHor=')[1].split('&')[0]*1000: 1000; let bpmFactorVer = document.location.search.indexOf('bpmFactorVer=')>-1? document.location.search.split('bpmFactorVer=')[1].split('&')[0]*1000: 1000; let bpmmaxVer = bpmFactorVer*10; let bpmmaxHor = bpmFactorVer*10; /*rulerInit(); main.addEventListener('mousemove', function(e) {getRulerPosition(main, e, lattice.sr? 'sr': 'fel1');}); if (ruler) { main.style.cursor = 'crosshair'; } else $('#rulerg').hide();*/ function bpmelapseHor(step, up) { // console.log('elapseHor()',step,scaleHor.$fill.style.width, (bpmscalesteps-step)/bpmscalesteps*90); if (up) { if (step<bpmscalesteps) {const pc = Math.round((bpmscalesteps-step)/bpmscalesteps*90); bpmscaleHor.$fill.style.width=pc+"%";setTimeout(bpmelapseHor, 20, step+1, up); return;} const buf = scaleHor._max; scaleHor.max(buf*10); scaleHor.min(buf/10); scaleHor.step(buf/100); } else { if (step<bpmscalesteps) {const pc = Math.round(step/bpmscalesteps*10); bpmscaleHor.$fill.style.width=pc+"%";setTimeout(bpmelapseHor, 20, step+1, up); return;} const buf = scaleHor._max/10; scaleHor.max(buf); scaleHor.min(buf/100); scaleHor.step(buf/1000); } scaleHor.$fill.style.width="10%"; scaleHor.$fill.style.borderColor = '#2cc9ff'; } function bpmelapseVer(step, up) { if (up) { if (step<bpmscalesteps) {const pc = Math.round((bpmscalesteps-step)/bpmscalesteps*90); bpmscaleVer.$fill.style.width=pc+"%";setTimeout(bpmelapseVer, 20, step+1, up); return;} const buf = scaleVer._max; scaleVer.max(buf*10); scaleVer.min(buf/10); scaleVer.step(buf/100); } else { if (step<bpmscalesteps) {const pc = Math.round(step/bpmscalesteps*10); bpmscaleVer.$fill.style.width=pc+"%";setTimeout(bpmelapseVer, 20, step+1, up); return;} const buf = scaleVer._max/10; scaleVer.max(buf); scaleVer.min(buf/100); scaleVer.step(buf/1000); } scaleVer.$fill.style.width="10%"; scaleVer.$fill.style.borderColor = '#2cc9ff'; } function bpmfinishHor() { // console.log('finishHor()', bpmscaleHor.$fill.style.width); if (bpmscaleHor.$fill.style.width=="100%" || bpmscaleHor.$fill.style.width=="0%") { setTimeout(bpmelapseHor, 200, 1, bpmscaleHor.$fill.style.width=="100%"); } } function bpmfinishVer() { if (bpmscaleVer.$fill.style.width=="100%" || bpmscaleVer.$fill.style.width=="0%") { setTimeout(bpmelapseVer, 200, 1, bpmscaleVer.$fill.style.width=="100%"); } } function bpmfactorHor(v) { // console.log('factorHor(v)', v, scaleHor.$fill.style.width); bpmscaleHor.$fill.style.borderColor = (bpmscaleHor.$fill.style.width=="100%" || bpmscaleHor.$fill.style.width=="0%")? 'yellow': '#2cc9ff'; bpmFactorHor = v*1000; rulerScale('bpm', 1000); } function bpmfactorVer(v) { console.log('factorVer(v)', v, bpmscaleVer.$fill.style.width); bpmscaleVer.$fill.style.borderColor = (bpmscaleVer.$fill.style.width=="100%" || bpmscaleVer.$fill.style.width=="0%")? 'yellow': '#2cc9ff'; bpmFactorVer = v*1000; rulerScale('bpm', 1000); } function bpmguiscale(gui, id) { const i = gui.children.length - 1; gui.children[i].domElement.style.display = 'none'; gui.children[i].domElement.setAttribute('id', id); } function bpmMenu(lattice, facilities, params) { if (document.location.search.indexOf('old')>-1) {bpmMenuOld(lattice, facilities, params); return;} params.bpm = false; $('.bpmhor').css('display', 'none'); $('.bpmver').css('display', 'none'); /*menuParams['ruler'] = ruler; gui.add(menuParams, 'ruler').onChange(function() {rulerSwitch();}); guiscale(gui, 'ruler');*/ params.bpm = document.location.search.indexOf('bpm')>-1 && document.location.search.indexOf('=bpm')==-1; gui.add(params, 'bpm').name('bpm H <img id="h" src="red.svg" style="margin-bottom: -4px;"> V <img id="v" src="green.svg" style="margin-bottom: -4px;"> ').onChange(function() {bpmSwitch(bpmData, params);}); menuParams['bpmscale H'] = bpmFactorHor/1000; bpmscaleHor = gui.add(menuParams, 'bpmscale H', bpmmaxVer/10000, bpmmaxVer/1000, Math.round(bpmmaxVer/10000)).name('scale H').onChange(function() {bpmfactorHor(menuParams['bpmscale H'], gui);}).onFinishChange(function() {bpmfinishHor(gui);}); bpmguiscale(gui, 'bpmscaleh'); $('#bpmscaleh').children().eq(0).css('color','red'); menuParams['bpmscale V'] = bpmFactorHor/1000; bpmscaleVer = gui.add(menuParams, 'bpmscale V', bpmmaxHor/1000, bpmmaxHor/100, Math.round(bpmmaxHor/10000)).name('scale V').onChange(function() {bpmfactorVer(menuParams['bpmscale V'], gui);}).onFinishChange(function() {bpmfinishVer(gui);}); bpmguiscale(gui, 'bpmscalev'); $('#bpmscalev').children().eq(0).css('color','green'); for (let f in facilities) { const b = facilities[f]; if (b=='' || typeof bpmData[b] == 'undefined' || typeof lattice[b].bpm == 'undefined' || typeof lattice[b].bpm.orbitconf == 'undefined') continue; console.log('bpmMenu() - ',b); // bpmData[b].datasrc = lattice[b].bpm.orbithor+';'+lattice[b].bpm.orbitver; bpmData[b].datasrc = lattice[b].bpm.orbithor; bpmData[b].map = []; fetch(conf.rchan+lattice[b].bpm.orbitconf) .then((response) => {return response.json();}) .then((bpmData2) => { let bpmCounter = 0; let photoCounter = 0; for (let i in bpmData2) { const name = bpmData2[i]; if (name.indexOf(bpmData[b].skip)>-1) continue; for (let bl in bpmData[b].obj) { // console.log('b', bl, bpmData[b].obj, bpmData[b].obj[bl]); if (name==bpmData[b].obj[bl]) {bpmData[b].map[bl] = bpmCounter; bpmCounter++;} } } console.log('bpmMenu(), name', b, bpmData[b], lattice[b]); }); } if (document.location.search.indexOf('bpm')>-1) {params.bpm=true; bpmSwitch(bpmData, params);} } const threshold = document.location.search.indexOf('threshold=')>-1? document.location.search.split('threshold=')[1].split('&')[0]: 1000; const f = document.location.search.indexOf('factor=')>-1? document.location.search.split('factor=')[1].split('&')[0]: 3000; function bpmInit(facilities) { console.log('bpmData', bpmData); for (let fi in facilities) { const facility = facilities[fi]; if (facility.length>0 && typeof bpmData[facility]!='undefined' && bpmData[facility].obj && bpmData[facility].obj.length>0) { console.log('bpmInit', facility, bpmData[facility]); const dhor = []; const dver = []; for (i=0; i<bpmData[facility].pos.length; i++) { if (i>threshold) break; const dir = bpmData[facility].dir[i]; const beta = Math.PI*dir/180; if (threshold<1000) console.log('i', i, 'pos',bpmData[facility].pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta)); dhor.push((i==0?'M':'L')+ Math.round(bpmData[facility].pos[i][0] - f*Math.sin(beta)) + ' ' + Math.round(bpmData[facility].pos[i][1] + f*Math.cos(beta)) ); dver.push((i==0?'M':'L')+ Math.round(bpmData[facility].pos[i][0] - f*1.05*Math.sin(beta)) + ' ' + Math.round(bpmData[facility].pos[i][1] + f*1.05*Math.cos(beta)) ); } appendSvg("path", {id:facility+"_bpmhor", class: "bpmhor trajectory "+facility, visibility:"hidden", name:"bpmhor", fill: "none", "stroke-width":"100", "stroke":"red", "stroke-opacity":"0.6", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}); appendSvg("path", {id:facility+"_bpmver", class: "bpmver trajectory "+facility, visibility:"hidden", name:"bpmver", fill: "none", "stroke-width":"100", "stroke":"green", "stroke-opacity":"0.6", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}); console.log("path", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'}); } } } function bpmSwitch(bpmData, params) { console.log('bpmSwitch()',bpmData, params, params.bpm); if (bpmData.reader !== false) { clearInterval(bpmData.reader); } bpmData.reader = false; if (params.bpm=='') { $('#bpmscaleh').hide(); $('#bpmscalev').hide(); $('#ruler').hide(); for (let facility in bpmData) { if (typeof $('#'+facility+"_bpmhor").attr('visibility') != 'undefined') { $('#'+facility+"_bpmhor").attr('visibility', "hidden"); $('#'+facility+"_bpmver").attr('visibility', "hidden"); } } // $('#application').hide(); $('#applicationFrame').removeAttr("src"); } else { $('#bpmscaleh').show(); $('#bpmscalev').show(); $('#ruler').show(); const bpms = []; for (let i in bpmData) if (i!="reader") bpms.push(i); console.log('bpms', bpms, bpmData); for (let facility in bpmData) { if (typeof $('#'+facility+"_bpmhor").attr('visibility') != 'undefined') { $('#'+facility+"_bpmhor").attr('visibility', "visible"); $('#'+facility+"_bpmver").attr('visibility', "visible"); } // console.log('#'+facility+"_bpmhor", $('#'+facility+"_bpmhor").attr('visibility'), typeof bpmData[facility]); if (typeof bpmData[facility] == 'undefined' || typeof bpmData[facility].map == 'undefined') continue; bpmData.oldIndex = facility; bpmRead(bpmData, params); } bpmData.reader = setInterval(bpmRead, 200, bpmData, params); // $('#application').show(); $('.bpmhor').css('display', 'block'); $('.bpmver').css('display', 'block'); $('#applicationFrame').attr("src", "/spa/index.html?s=bpm&src="+params.bpm+(document.location.search.indexOf('demo')>-1? '&demo': '')); $("#applicationFrame").height(window.innerHeight); } rulerScale('bpm', 1000); } function bpmRender(facility, val) { const dhor = []; const dver = []; const bpmFactor = conf.bpmFactor[facility]*1000; for (i=0; i<val.Hor.length; i++) { // if (i>threshold) break; const pos = bpmData[facility].pos[i]; if (pos==null) continue; const beta = Math.PI*bpmData[facility].dir[i]/180; // console.log('i', i, 'pos',pos, 'dir', bpmData[facility].dir[i], 'facility', facility, beta, Math.cos(beta), Math.sin(beta)); dhor.push((i==0?'M':'L')+ Math.round(pos[0] - val.Hor[i]*bpmFactorHor*Math.sin(beta)) + ' ' + Math.round(pos[1] + val.Hor[i]*bpmFactorHor*Math.cos(beta)) ); dver.push((i==0?'M':'L')+ Math.round(pos[0] - val.Ver[i]*bpmFactorVer*Math.sin(beta)) + ' ' + Math.round(pos[1] + val.Ver[i]*bpmFactorVer*Math.cos(beta)) ); } $('#'+facility+"_bpmhor").attr('d', dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')); $('#'+facility+"_bpmver").attr('d', dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')); // console.log("bpmRender(),", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'}); // if (params.bpm) {bpmLabel(bpmData, params, camera, THREE);} } function bpmRead(bpmData, params) { fetch(conf.bpmUrl) .then((response) => {return response.json();}) .then((eventData) => { // console.log('bpmRead()', conf.bpmUrl, eventData); for (let i in eventData) { // console.log('bpmRead()', i, eventData[i], conf.bpmUrl); bpmRender(i, eventData[i]); } }); }