Parcourir la source

updated UI helper functions

Piotr Labudda il y a 9 ans
Parent
commit
93ce69eb2f
2 fichiers modifiés avec 92 ajouts et 6 suppressions
  1. 4 4
      SE/se-lib/Route/Storage.php
  2. 88 2
      SE/se-lib/UI.php

+ 4 - 4
SE/se-lib/Route/Storage.php

@@ -278,7 +278,6 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 		if (empty($params['tableTwo'])) throw new Exception("Missing tableTwo in Connect widget");
 		$tableOne = $params['tableOne'];
 		$tableTwo = $params['tableTwo'];
-		// TODO: add __js_on_click to $tableOne and $tableTwo
 		// TODO: add p5BtnAjax to table filters button
 		// TODO: add p5BtnAjax to conn button
 		$jsEventNamespace = 'ConnectTableWidget' . time();
@@ -288,12 +287,12 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 		$stateSelectedTotalId = "{$jsEventNamespace}-state-selected";
 		$stateClearSelectedBtnId = "{$jsEventNamespace}-state-clear-selected-btn";
 		foreach ($tableOne['rows'] as $idx => $r) {
-			$tableOne['rows'][$idx]['__js_on_click'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableOne', { primary_key: '{$r['__primary_key']}' });";
+			$tableOne['rows'][$idx]['@onClick'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableOne', { primary_key: '{$r['__primary_key']}' });";
 		}
 		foreach ($tableTwo['rows'] as $idx => $r) {
-			$tableTwo['rows'][$idx]['__js_on_click'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableTwo', { primary_key: '{$r['__primary_key']}' });";
+			$tableTwo['rows'][$idx]['@onClick'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableTwo', { primary_key: '{$r['__primary_key']}' });";
 		}
-		$tableOne['hidden_cols'] = $tableTwo['hidden_cols'] = array('__primary_key', '__js_on_click', '__html_id');
+		$tableOne['hidden_cols'] = $tableTwo['hidden_cols'] = array('__primary_key', '@onClick', '__html_id');
 		$tableTwo['__html_id'] = "{$jsEventNamespace}-table-two";
 ?>
 <div id="<?php echo $htmlIdWrap; ?>">
@@ -1408,6 +1407,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:addBaseProces:ajaxLoaded', function(e,
 					function ($item) {
 						return [
 							'ns' => $item['namespace'],
+							'nazwa' => $item['nazwa'],
 							'edit' => '<a href="index.php?_route=ViewTableAjax&namespace=' . $item['namespace'] . '">edit</a>',
 							'wfs Describe' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureType&TYPENAME=' . $item['typeName'] . '">DescribeFeatureType</a>',
 							'wfs getFeature' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME=' . $item['typeName'] . '&MAXFEATURES=10">GetFeature</a> (max:10)'

+ 88 - 2
SE/se-lib/UI.php

@@ -121,7 +121,12 @@ class UI {
 				$firstRow = $row;
 				break;
 			}
-			$cols = array_keys((array)$firstRow);
+			$cols = array_filter(
+				array_keys((array)$firstRow),
+				function ($col) {
+					return ('@' != substr($col, 0, 1));
+				}
+			);
 		}
 		// if (empty($cols)) return;
 		$hiddenCols = V::get('hidden_cols', array(), $params);
@@ -152,7 +157,9 @@ class UI {
 			foreach ($rows as $row) {
 				$i++;
 				$trAttrs = array();
-				if (!empty($row['__js_on_click'])) $trAttrs['onClick'] = $row['__js_on_click'];
+				if (!empty($row['@onClick'])) $trAttrs['onClick'] = $row['@onClick'];
+				if (!empty($row['@class'])) $trAttrs['class'] = $row['@class'];
+				if (!empty($row['@style'])) $trAttrs['style'] = $row['@style'];
 				self::startTag('tr', $trAttrs); echo "\n";
 				if ($showLp) { self::tag('th', [ 'style' => "padding:2px; color:#ccc" ], $i); echo "\n"; }
 				foreach ($cols as $colName) {
@@ -239,4 +246,83 @@ class UI {
 		UI::endTag('script', "\n");
 	}
 
+	public static function includeView($viewPath, $data = array()) {
+		if (!file_exists($viewPath)) throw new Exception("view file '" . basename($viewPath) . "' not exists!");
+		if (false === strpos($viewPath, APP_PATH_ROOT)) throw new Exception("Access Denied to include view '" . basename($viewPath) . "'!");
+		if (is_array($data) && !empty($data)) {
+			extract($data);
+		}
+		include $viewPath;
+	}
+
+	public static function postButton($label, $params = []) {
+		UI::startTag('form', [
+			'action' => V::get('action', '', $params),
+			'method' => V::get('method', 'post', $params),
+			'style' => "display:inline"
+		]);
+			foreach (V::get('data', [], $params, 'array') as $name => $value) {
+				UI::emptyTag('input', ['type'=>'hidden', 'name'=>$name, 'value'=>$value]);
+			}
+			UI::tag('button', ['type'=>'submit', 'class' => 'btn ' . V::get('class', 'btn-default btn-xs', $params)], $label);
+		UI::endTag('form');
+	}
+
+	public static function hButtonPost($label, $params = []) {
+		$fields = [];
+		if (!empty($params['data'])) foreach ($params['data'] as $k => $v) $fields[] = self::h('input', ['type'=>'hidden', 'name'=>$k, 'value'=>$v]);
+		$fields[] = self::h('button', ['type'=>'submit', 'class' => 'btn ' . V::get('class', 'btn-default btn-xs', $params)], $label);
+		return self::h('form', [
+				'action' => V::get('action', '', $params),
+				'method' => V::get('method', 'post', $params),
+				'style' => "display:inline"
+			],
+			$fields
+		);
+	}
+
+	public static function h($tagName, $params = [], $childrens = []) {
+		$emptyTags = [];
+		$emptyTags[] = 'hr';
+		$emptyTags[] = 'br';
+		$emptyTags[] = 'input';
+		$emptyTags[] = 'link';
+		$emptyTags[] = 'area';
+		$emptyTags[] = 'base';
+		$emptyTags[] = 'col';
+		$emptyTags[] = 'embed';
+		$emptyTags[] = 'img';
+		$emptyTags[] = 'keygen';
+		$emptyTags[] = 'meta';
+		$emptyTags[] = 'param';
+		$emptyTags[] = 'source';
+		$emptyTags[] = 'track';
+		$emptyTags[] = 'wbr';
+		if (in_array($tagName, $emptyTags)) return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '/>';
+		return '<' . $tagName . (empty($params) ? '' : ' ' . self::hAttributes($params)) . '>' . self::hChildrens($childrens) . '</' . $tagName . '>';
+	}
+	public static function hAttributes($params = []) {
+		$attr = [];
+		foreach ($params as $k => $v) {
+			if (is_array($v)) {
+				$attr[] = "{$k}=\"" . implode(" ", $v) . "\"";
+			} else {
+				$attr[] = "{$k}=\"{$v}\"";
+			}
+		}
+		return implode(" ", $attr);
+	}
+	public static function hChildrens($childrens = []) {
+		if (empty($childrens)) return '';
+		if (is_string($childrens)) return $childrens;
+		if (!is_array($childrens)) throw new Exception("Unsupported children type");
+		return array_reduce(
+			$childrens,
+			function ($curry, $child) {
+				return "{$curry}{$child}";
+			},
+			""
+		);
+	}
+
 }