zui.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * The zui component class file of zin lib.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin;
  12. require_once dirname(__DIR__) . DS . 'core' . DS . 'wg.class.php';
  13. require_once dirname(__DIR__) . DS . 'core' . DS . 'zin.func.php';
  14. require_once __DIR__ . DS . 'toggle.class.php';
  15. class zui extends wg
  16. {
  17. /**
  18. * @var mixed[]
  19. */
  20. protected static $defineProps = array(
  21. '_name:string',
  22. '_to?:string',
  23. '_tag:string="div"',
  24. '_map?:array',
  25. '_props?: array',
  26. '_options?: array',
  27. '_style?: array',
  28. '_id?: string',
  29. '_class?: string',
  30. '_call?: string',
  31. '_children?: array',
  32. );
  33. /**
  34. * @return mixed
  35. */
  36. protected function build()
  37. {
  38. list($name, $target, $tagName, $targetProps, $style, $id, $class, $map, $call, $userOptions, $targetChildren) = $this->prop(array('_name', '_to', '_tag', '_props', '_style', '_id', '_class', '_map', '_call', '_options', '_children'));
  39. $options = $this->getRestProps();
  40. $children = $this->children();
  41. if(is_array($map))
  42. {
  43. foreach($options as $key => $value)
  44. {
  45. if(!isset($map[$key])) continue;
  46. $options[$map[$key]] = $value;
  47. unset($options[$key]);
  48. }
  49. }
  50. if(is_array($userOptions)) $options = array_merge($options, $userOptions);
  51. if($target)
  52. {
  53. if(empty($call)) $call = '~zui.create';
  54. return array
  55. (
  56. h::jsCall($call, $name, $target, $options),
  57. $children
  58. );
  59. }
  60. return h($tagName, setClass($class), setID($id), $style ? setStyle($style) : null, static::create($name, $options), set($targetProps), $targetChildren, $children);
  61. }
  62. public static function __callStatic($name, $args)
  63. {
  64. return new zui(set('_name', $name), $args);
  65. }
  66. /**
  67. * @param string $name
  68. * @param mixed[]|null $options
  69. */
  70. public static function create($name, $options = null)
  71. {
  72. $zinEvents = [];
  73. $jsOptions = [];
  74. if(is_array($options))
  75. {
  76. foreach($options as $key => $value)
  77. {
  78. if(str_starts_with($key, '@')) $zinEvents[$key] = $value;
  79. else $jsOptions[$key] = $value;
  80. }
  81. }
  82. return array
  83. (
  84. set('zui-create', ''),
  85. set("zui-create-$name", empty($jsOptions) ? '' : js::value($jsOptions)),
  86. empty($zinEvents) ? null : set($zinEvents)
  87. );
  88. }
  89. public static function toggle($name, $options = array())
  90. {
  91. return toggle($name, $options);
  92. }
  93. public static function setClass(/* $name, ...$args */)
  94. {
  95. $args = func_get_args();
  96. $name = array_shift($args);
  97. $class = array($name => true);
  98. foreach($args as $arg)
  99. {
  100. if(is_bool($arg)) $class[$name] = $arg;
  101. else $class["$name-$arg"] = true;
  102. }
  103. if(isset($class[$name]) && $class[$name] === false) return null;
  104. return setClass($class);
  105. }
  106. public static function skin($name, $flag = true, $falseValue = null, $cssProp = null)
  107. {
  108. if($flag === null) return null;
  109. if(is_array($flag))
  110. {
  111. return array_map(function($value) use($name, $cssProp) {return zui::skin($name, $value, null, $cssProp);}, $flag);
  112. }
  113. if($flag === false)
  114. {
  115. if(empty($falseValue)) return null;
  116. $flag = 'none';
  117. }
  118. elseif($cssProp !== null && is_string($flag) && (str_ends_with($flag, 'px') || str_starts_with($flag, '#') || str_contains($flag, '.') || str_contains($flag, '(')))
  119. {
  120. if(str_starts_with($flag, '(')) $flag = substr($flag, 1, -1);
  121. return setStyle($cssProp, $flag);
  122. }
  123. return setClass($flag === true ? $name : "$name-$flag");
  124. }
  125. public static function rounded($value = true)
  126. {
  127. return zui::skin('rounded', $value, 'none', 'border-radius');
  128. }
  129. public static function shadow($value = true)
  130. {
  131. return zui::skin('shadow', $value, 'none');
  132. }
  133. public static function primary($value = true)
  134. {
  135. return zui::skin('primary', $value);
  136. }
  137. public static function secondary($value = true)
  138. {
  139. return zui::skin('secondary', $value);
  140. }
  141. public static function success($value = true)
  142. {
  143. return zui::skin('success', $value);
  144. }
  145. public static function warning($value = true)
  146. {
  147. return zui::skin('warning', $value);
  148. }
  149. public static function danger($value = true)
  150. {
  151. return zui::skin('danger', $value);
  152. }
  153. public static function important($value = true)
  154. {
  155. return zui::skin('important', $value);
  156. }
  157. public static function special($value = true)
  158. {
  159. return zui::skin('special', $value);
  160. }
  161. public static function bg($value = null)
  162. {
  163. return zui::skin('bg', $value, 'transparent', 'background');
  164. }
  165. public static function text($value = null)
  166. {
  167. return zui::skin('text', $value, 'fore', 'color');
  168. }
  169. public static function muted($value = true)
  170. {
  171. return $value ? setClass('muted') : null;
  172. }
  173. public static function opacity($value)
  174. {
  175. return zui::skin('opacity', $value, '0', 'opacity');
  176. }
  177. public static function disabled($value = true)
  178. {
  179. return $value ? setClass('disabled') : null;
  180. }
  181. public static function gap($value)
  182. {
  183. return zui::skin('gap', $value, '0', 'gap');
  184. }
  185. public static function width($value)
  186. {
  187. return zui::skin('w', $value, '0', 'width');
  188. }
  189. public static function height($value)
  190. {
  191. return zui::skin('h', $value, '0', 'height');
  192. }
  193. public static function ring(/* ...$args */)
  194. {
  195. return zui::skin('ring', func_get_args(), '0');
  196. }
  197. public static function border(/* ...$args */)
  198. {
  199. return zui::skin('border', func_get_args(), 'none', 'border');
  200. }
  201. }