Bladeren bron

+ UI::h('p5:CustomTag', ...)

Piotr Labudda 5 jaren geleden
bovenliggende
commit
60e012e089
4 gewijzigde bestanden met toevoegingen van 69 en 14 verwijderingen
  1. 30 14
      SE/se-lib/UI.php
  2. 34 0
      SE/se-lib/UI/SidePanelButton.php
  3. 0 0
      SE/se-lib/UI/SidePanelButton.php.script.js
  4. 5 0
      SE/se-lib/UITagInterface.php

+ 30 - 14
SE/se-lib/UI.php

@@ -1,6 +1,7 @@
 <?php
 
 Lib::loadClass('Theme');
+require_once dirname(__FILE__) . '/' . 'UITagInterface.php';
 
 class UI {
 
@@ -305,22 +306,26 @@ class UI {
 	}
 
 	public static function inlineJS($jsFile, $jsonVars = []) {
+		echo self::hScript($jsFile, $jsonVars);
+	}
+	public static function hScript($jsFile, $jsonVars = []) {
 		if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
-		UI::startTag('script', [], "\n");
-		echo "(function (global) {" . "\n";
-		echo "  var module = {}; module.exports = {};\n";
+		$ret = '<script>' . "\n";
+		$ret .= "(function (global) {" . "\n";
+		$ret .= "  var module = {}; module.exports = {};\n";
 		foreach ($jsonVars as $name => $var) {
-			echo "  var {$name} = " . json_encode($var) . ";\n";
-		}
-		echo file_get_contents($jsFile);
-		echo "\n;\n";
-		echo "  if (module && module.exports && Object.keys(module.exports).length) {" . "\n";
-		echo "    Object.keys(module.exports).forEach(function (key) {" . "\n";
-		echo "      global[key] = module.exports[key];" . "\n";
-		echo "    })" . "\n";
-		echo "  }" . "\n";
-		echo "})(window)" . "\n";
-		UI::endTag('script', "\n");
+			$ret .= "  var {$name} = " . json_encode($var) . ";\n";
+		}
+		$ret .= file_get_contents($jsFile);
+		$ret .= "\n;\n";
+		$ret .= "  if (module && module.exports && Object.keys(module.exports).length) {" . "\n";
+		$ret .= "    Object.keys(module.exports).forEach(function (key) {" . "\n";
+		$ret .= "      global[key] = module.exports[key];" . "\n";
+		$ret .= "    })" . "\n";
+		$ret .= "  }" . "\n";
+		$ret .= "})(window)" . "\n";
+		$ret .= '</script>' . "\n";
+		return $ret;
 	}
 	public static function inlineRawJS($jsFile) {
 		if (!file_exists($jsFile)) throw new Exception("js file '" . basename($jsFile) . "' not exists!");
@@ -466,6 +471,7 @@ class UI {
 		");
 	}
 	public static function h($tagName, $params = [], $childrens = []) {
+		if (null === $tagName && empty($params)) return self::hChildrens($childrens);
 		$emptyTags = [];
 		$emptyTags[] = 'hr';
 		$emptyTags[] = 'br';
@@ -483,8 +489,18 @@ class UI {
 		$emptyTags[] = 'track';
 		$emptyTags[] = 'wbr';
 		if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
+		if ('p5:' === substr($tagName, 0, 3)) return self::hCustomTag($tagName, $params, $childrens);
 		return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
 	}
+	public static function hCustomTag($tagName, $params = [], $childrens = []) {
+		if ('p5:' === substr($tagName, 0, 3)) {
+			$tagClass = "UI_" . substr($tagName, 3);
+			if (!class_exists($tagClass)) Lib::tryLoadClass($tagClass);
+			if (!class_exists($tagClass)) throw new Exception("Not implemented custom tag '{$tagName}'");
+			return $tagClass::h($tagName, $params, $childrens);
+		}
+		throw new Exception("Not implemented custom tag prefix '{$tagName}'");
+	}
 	public static function hAttributes($params = []) {
 		$attr = [];
 		if (null === $params) return '';

+ 34 - 0
SE/se-lib/UI/SidePanelButton.php

@@ -0,0 +1,34 @@
+<?php
+
+// UI::hAttributes($params);
+// UI::hChildrens($childrens);
+
+class UI_SidePanelButton implements UITagInterface {
+
+	/**
+	 * @param string $tagName = 'p5:SidePanelButton'
+	 * @param array $props
+	 * @param array $childrens
+	 * 
+	 * @return string html code
+	 */
+	static function h($tagName, $props = [], $childrens = []) {
+		$jsFuncName = "p5_openSideBar"; // TODO: generate name based on $props['name']
+		if (empty($props['label'])) throw new Exception("Missing 'label'");
+
+		return UI::h('span', [], [
+			UI::h('button', [
+				'onClick' => "return {$jsFuncName}(event, this);",
+				'data-name' => $props['name'],
+				// 'data-url' => $props['url'], // TODO: if dynamic content
+			], $props['label']),
+			UI::h('div', [ 'style' => "display:none" ], [
+
+			]),
+			UI::hScript($jsFile = __FILE__ . '.script.js', $jsonVars = [
+				// 'FUNCTION_NAME' => $jsFuncName,
+			]),
+		]);
+	}
+
+}

+ 0 - 0
SE/se-lib/UI/SidePanelButton.php.script.js


+ 5 - 0
SE/se-lib/UITagInterface.php

@@ -0,0 +1,5 @@
+<?php
+
+interface UITagInterface {
+	static function h($tagName, $params = [], $childrens = []);
+}