Skip to content
Snippets Groups Projects
component.php 3.71 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
<?php
header("Content-type: text/javascript");
function bundle_include($c) {
	global $import, $bundle;
	$f = file("./components/{$c}.js");
	foreach($f as $i=> $l) {
		if (strpos($l, 'import')!==false) {$import[$l] = true; unset($f[$i]);}
		if (strpos($l, 'jshint')!==false) {unset($f[$i]);}
	}
	$bundle .= implode('', $f)."\n";
}
function bundle_include2d($c) {
	global $import, $bundleArray;
	$height = $width = '';
	$f = file("./components/{$c}.svg");
	foreach($f as $i=> $l) {if (strpos($l, 'svg')!==false) {$g = explode('"', $l); break;}}
	// print_r($g);
	foreach ($g as $i=>$v) {
		if (strpos($v, '=')!==false) {$id = strtr($v, array(' '=>'','='=>'','<svg'=>'')); $$id = $g[$i+1];}
	}
	$vb = explode(' ', $viewBox);
	if (empty($width)) { $width = strtr($vb[2], array('px'=>''));}
	if (empty($height)) { $height = strtr($vb[3], array('px'=>''));}
	$width *= 2;
	$height *= 2;
	$bundle = "<symbol id=\"$c\" viewBox=\"0 0 {$width}px {$height}px\" width=\"$width\" height=\"$height\"><g class=\"{$c}scale scale\">\n";
	foreach($f as $i=> $l) {
		if (strpos($l, 'svg')!==false) {unset($f[$i]);}
	}
	$bundle .= implode('', $f)."</g></symbol>\n";
	$bundleArray[$c] = $bundle; 
}

$machine = empty($_REQUEST['machine'])? 'elettra': $_REQUEST['machine'];
$latticeFile = empty($_REQUEST['lattice'])? "./{$machine}_lattice.json": $_REQUEST['lattice'];
$latticeStr = file_get_contents($latticeFile);
$lattice = json_decode($latticeStr, true);
if (isset($_REQUEST['debug'])) {echo "latticeFile: $latticeFile\nlattice: \n"; print_r($lattice);}
$comp = $import = array();
$bundle = "\n";
$bundleArray = [];
foreach ($lattice as $l) {
	foreach ($l['sections'] as $s) {
		$comp[$s['bending']['type']] = true; 
		foreach ($s['components'] as $m) $comp[$m['type']] = true;
	}
}
if (isset($_REQUEST['2d'])) {
	$bundle = '<svg id="panther" viewBox="-50000 -50000 50000 50000" version="1.1" xmlns="http://www.w3.org/2000/svg" style="width: 100%; overflow: hidden; display: inline; min-width: inherit; max-width: inherit; min-height: inherit;">'."\n";
	foreach ($comp as $c=>$v) {
		if (file_exists("./components/{$c}.svg")) bundle_include2d($c); else unset($comp[$c]);
	}
	ksort($bundleArray);
	$bundle .= implode('', $bundleArray);
	if (isset($_REQUEST['import'])) {
		$f = file_get_contents('./panther.js');
		$js = explode('// import components', $f);
		$c = implode(',', array_keys($comp));
		die(strtr($js[0], array('let lattice'=>'//let lattice'))."\nimport { $c } from './component.php';\nconst amesh = { $c };\n\nconst lattice = $latticeStr;\n	init();\nfor (var key in lattice) {if (document.location.search.indexOf(key+'=hide')==-1) initLattice(lattice[key], key);}\n//".$js[2]);
	}
	if (isset($_REQUEST['save'])) {file_put_contents("./components/bundle_{$machine}2d.svg", implode('', array_keys($import)).$bundle);}
	die(implode('', array_keys($import)).$bundle);
}

foreach ($comp as $c=>$v) {
	$fast = (isset($_REQUEST['fast']) && file_exists("./components/{$c}fast.js"))? 'fast': '';
	if (file_exists("./components/{$c}{$fast}.js")) bundle_include($c.$fast); else unset($comp[$c]);
}
if (isset($_REQUEST['import'])) {
	$f = file_get_contents('./panther.js');
	$js = explode('// import components', $f);
	$c = implode(',', array_keys($comp));
	die(strtr($js[0], array('let lattice'=>'//let lattice'))."\nimport { $c } from './component.php';\nconst amesh = { $c };\n\nconst lattice = $latticeStr;\n	init();\nfor (var key in lattice) {if (document.location.search.indexOf(key+'=hide')==-1) initLattice(lattice[key], key);}\n//".$js[2]);
}
$fast = isset($_REQUEST['fast'])? 'fast': '';
if (isset($_REQUEST['save'])) {file_put_contents("./components/bundle_{$machine}{$fast}.js","/*jshint esversion: 6 */\n".implode('', array_keys($import)).$bundle);}
echo implode('', array_keys($import)).$bundle;

?>