js.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. /**
  3. * The js class file of zin lib.
  4. *
  5. * @copyright Copyright 2024 青岛易软天创网络科技有限公司(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 . 'utils' . DS . 'json.func.php';
  13. require_once __DIR__ . DS . 'node.class.php';
  14. require_once __DIR__ . DS . 'directive.class.php';
  15. require_once __DIR__ . DS . 'zin.func.php';
  16. use zin\jsContext;
  17. use zin\jsCallback;
  18. use zin\jQuery;
  19. use zin\node;
  20. /**
  21. * Class for generating js code.
  22. * 用于生成 JS 代码。
  23. *
  24. * @access public
  25. */
  26. class js implements \JsonSerializable, iDirective
  27. {
  28. /**
  29. * @var bool
  30. */
  31. public $notRenderInGlobal = true;
  32. /**
  33. * The parent node.
  34. * 父节点。
  35. *
  36. * @access public
  37. * @var object
  38. */
  39. public $parent = null;
  40. /**
  41. * The js code lines.
  42. * JS 代码行。
  43. *
  44. * @access protected
  45. * @var array
  46. */
  47. protected $jsLines = array();
  48. /**
  49. * The construct function.
  50. * 构造函数。
  51. *
  52. * @param null|string|js|array ...$codes Codes.
  53. */
  54. public function __construct(...$codes)
  55. {
  56. $this->appendLines(...$codes);
  57. }
  58. /**
  59. * The call magic function, used to call other methods on the object by condition through the xxxIf method.
  60. * 魔术方法,用于通过 xxxIf 的方式根据条件调用对象上的其他方法。
  61. *
  62. * @access public
  63. * @param string $name Method name.
  64. * @param array $arguments Arguments.
  65. * @return self
  66. */
  67. public function __call($name, $arguments)
  68. {
  69. if(str_ends_with($name, 'If'))
  70. {
  71. $methodName = substr($name, 0, -2);
  72. if(method_exists($this, $methodName))
  73. {
  74. $condition = array_shift($arguments);
  75. $this->beginIf($condition);
  76. $this->$methodName(...$arguments);
  77. return $this->endIf();
  78. }
  79. }
  80. trigger_error("Call to undefined method " . __CLASS__ . "::{$name}()", E_USER_ERROR);
  81. }
  82. /**
  83. * The magic function, used to convert the object to a string.
  84. *
  85. * @access public
  86. * @return string
  87. */
  88. public function __toString()
  89. {
  90. return $this->toJS();
  91. }
  92. public function __debugInfo()
  93. {
  94. return array(
  95. 'jsLines' => $this->jsLines
  96. );
  97. }
  98. /**
  99. * Call a function.
  100. * 调用一个函数。
  101. *
  102. * @access public
  103. * @param string $func Function name.
  104. * @param mixed ...$args Arguments.
  105. * @return self
  106. */
  107. public function call($func, ...$args)
  108. {
  109. $argCodes = array();
  110. foreach($args as $arg)
  111. {
  112. $argCodes[] = ($arg instanceof js) ? $arg->toJS() : static::value($arg);
  113. }
  114. return $this->appendLine($func . '(' . implode(',', $argCodes) . ')');
  115. }
  116. /**
  117. * Append JS codes.
  118. * 追加要执行的 JS 代码。
  119. *
  120. * @access public
  121. * @param null|string|\zin\js|mixed[] ...$codes Codes.
  122. * @return self
  123. */
  124. public function do(...$codes)
  125. {
  126. return $this->appendLines(...$codes);
  127. }
  128. /**
  129. * Declare JS variables.
  130. * 声明 JS 变量。
  131. *
  132. * @access public
  133. * @param string $name Variable name.
  134. * @param mixed $value Variable value.
  135. * @return self
  136. */
  137. public function let($name, $value)
  138. {
  139. return $this->appendLine('let', $name, '=', static::value($value));
  140. }
  141. /**
  142. * Declare JS constants.
  143. * 声明 JS 常量。
  144. *
  145. * @access public
  146. * @param string $name Constant name.
  147. * @param mixed $value Constant value.
  148. * @return self
  149. */
  150. public function const($name, $value)
  151. {
  152. return $this->appendLine('const', $name, '=', static::value($value));
  153. }
  154. /**
  155. * Declare JS variables globally.
  156. * 声明 JS 全局变量。
  157. *
  158. * @access public
  159. * @param string $name Variable name.
  160. * @param mixed $value Variable value.
  161. * @return self
  162. */
  163. public function globalVar($name, $value)
  164. {
  165. if(!str_starts_with($name, 'window.')) $name = 'window.' . $name;
  166. return $this->appendLine($name, '=', static::value($value));
  167. }
  168. /**
  169. * @param string|mixed[] $nameOrVars
  170. * @param mixed $value
  171. */
  172. public function var($nameOrVars, $value = null)
  173. {
  174. if(is_array($nameOrVars))
  175. {
  176. foreach($nameOrVars as $name => $val) $this->var($name, $val);
  177. return $this;
  178. }
  179. $name = $nameOrVars;
  180. if(str_starts_with($name, '+')) return $this->let(substr($name, 1), $value);
  181. if(str_starts_with($name, 'window.')) return $this->globalVar($name, $value);
  182. return $this->const($name, $value);
  183. }
  184. /**
  185. * Declare JS scope with keyword "with".
  186. * 声明 JS 作用域,使用 with 关键字。
  187. *
  188. * @access public
  189. * @param string $name Variable name.
  190. * @param null|string|\zin\js|mixed[] $codes Scoped codes.
  191. * @return self
  192. */
  193. public function with($name, ...$codes)
  194. {
  195. $this->appendLine('with(', $name, '){');
  196. $this->appendLines(...$codes);
  197. return $this->appendLine('}');
  198. }
  199. /**
  200. * Begin declaring "if" statements.
  201. * 开始声明 "if" 语句。
  202. *
  203. * @access public
  204. * @param string $conditions Conditions.
  205. * @return self
  206. */
  207. public function beginIf(...$conditions)
  208. {
  209. $conditions = implode(' && ', $conditions);
  210. return $this->appendLine("if($conditions){");
  211. }
  212. /**
  213. * Declare "else if" statements.
  214. * 声明 "else if" 语句。
  215. *
  216. * @access public
  217. * @param string $conditions Conditions.
  218. * @return self
  219. */
  220. public function elseIf(...$conditions)
  221. {
  222. $conditions = implode(' && ', $conditions);
  223. return $this->appendLine("}else if($conditions){");
  224. }
  225. /**
  226. * Declare "else" statements.
  227. * 声明 "else" 语句。
  228. *
  229. * @access public
  230. * @return self
  231. */
  232. public function else()
  233. {
  234. return $this->appendLine('}else{');
  235. }
  236. /**
  237. * Declare "if" statements end.
  238. * 声明 "if" 结束的括号。
  239. *
  240. * @access public
  241. * @return self
  242. */
  243. public function endIf()
  244. {
  245. return $this->appendLine('}');
  246. }
  247. /**
  248. * Declare independent scopes with IIFE.
  249. * 使用立即执行函数声明独立的作用域。
  250. *
  251. * @access public
  252. * @return self
  253. */
  254. public function scopeBegin()
  255. {
  256. return $this->appendLine(';(function(){');
  257. }
  258. /**
  259. * Declare independent scopes end.
  260. * 声明独立的作用域的结束部分。
  261. *
  262. * @access public
  263. * @return self
  264. */
  265. public function scopeEnd()
  266. {
  267. return $this->appendLine('}());');
  268. }
  269. /**
  270. * Append a line of JS code.
  271. * 追加一行 JS 代码。
  272. *
  273. * @access public
  274. * @param string ...$codes Codes.
  275. * @return self
  276. */
  277. public function appendLine(...$codes)
  278. {
  279. $line = trim(implode(' ', $codes));
  280. if(empty($line)) return $this;
  281. if(!str_ends_with(';', $line)) $line .= ';';
  282. $this->jsLines[] = $line;
  283. return $this;
  284. }
  285. /**
  286. * Append lines of JS code.
  287. * 追加多行 JS 代码。
  288. *
  289. * @access public
  290. * @param null|string|js|array ...$lines Lines.
  291. * @return self
  292. */
  293. public function appendLines(...$lines)
  294. {
  295. foreach($lines as $line)
  296. {
  297. if(is_null($line)) continue;
  298. if(is_array($line))
  299. {
  300. $this->appendLines(...$line);
  301. continue;
  302. }
  303. if($line instanceof js)
  304. {
  305. $line = $line->toJS();
  306. }
  307. $this->appendLine($line);
  308. }
  309. return $this;
  310. }
  311. /**
  312. * Append JS codes.
  313. * 追加要执行的 JS 代码。
  314. *
  315. * @access public
  316. * @param string ...$codes Codes.
  317. * @return self
  318. */
  319. public function appendCode(...$codes)
  320. {
  321. foreach($codes as $code)
  322. {
  323. if(empty($code)) continue;
  324. $this->jsLines[] = $code;
  325. }
  326. return $this;
  327. }
  328. /**
  329. * Convert js code to string.
  330. * 将 JS 导出为代码字符串。
  331. *
  332. * @access public
  333. * @param string $joiner Joiner.
  334. * @return string
  335. */
  336. public function toJS($joiner = "\n")
  337. {
  338. return implode($joiner, $this->jsLines);
  339. }
  340. /**
  341. * Convert js code to string with IIFE scope.
  342. * 将 JS 导出为代码字符串,并使用立即执行函数作用域。
  343. *
  344. * @access public
  345. * @param string $joiner Joiner.
  346. * @return string
  347. */
  348. public function toScopeJS($joiner = "\n")
  349. {
  350. return $this->scope($this->toJS($joiner));
  351. }
  352. /**
  353. * Apply JS code to zin node.
  354. * 将 JS 代码应用到指定的 zin 部件中。
  355. *
  356. * @access public
  357. * @param node $node zin node object.
  358. * @param string $blockName zin node block name.
  359. */
  360. public function apply($node, $blockName)
  361. {
  362. $node->addToBlock($blockName, h::js($this->toJS()));
  363. }
  364. /**
  365. * Serialized to JSON.
  366. * 序列化为 JSON。
  367. *
  368. * @access public
  369. * @return mixed
  370. */
  371. #[\ReturnTypeWillChange]
  372. public function jsonSerialize()
  373. {
  374. $js = trim($this->toJS());
  375. if(str_ends_with(';', $js)) $js = substr($js, 0, -1);
  376. return js::raw($js);
  377. }
  378. /**
  379. * @param string ...$codes
  380. */
  381. public static function raw(...$codes)
  382. {
  383. $js = implode('<RAWJS_LINE>', $codes);
  384. $js = str_replace(array("\n", '"'), array('<RAWJS_LINE>', '<RAWJS_QUOTE>'), $js);
  385. return "RAWJS<$js>RAWJS";
  386. }
  387. /**
  388. * @param string $str
  389. */
  390. public static function decodeRaw($str)
  391. {
  392. if(!str_contains($str, 'RAWJS')) return $str;
  393. return str_replace(array('<RAWJS_LINE>', '<RAWJS_QUOTE>', '"RAWJS<', '>RAWJS"'), array("\n", '"', '', ''), $str);
  394. }
  395. /**
  396. * Wrap js code with IIFE scope.
  397. * 使用立即执行函数作用域包装 JS 代码。
  398. *
  399. * @access public
  400. * @param string ...$codes Codes.
  401. * @return string
  402. */
  403. public static function scope(...$codes)
  404. {
  405. $js = new js(...$codes);
  406. return ';(function(){' . $js->toJS() . '}());';
  407. }
  408. /**
  409. * Encode php value to JS code and decode html special chars.
  410. * 将 PHP 值编码为 JS 代码,并解码 HTML 实体字符。
  411. *
  412. * @access public
  413. * @param mixed $data PHP value.
  414. * @return string
  415. */
  416. public static function value($data)
  417. {
  418. if($data instanceof js) return $data->toJS();
  419. $js = \zin\utils\jsonEncode($data, JSON_UNESCAPED_UNICODE);
  420. if(empty($js) && (is_array($data) || is_object($data))) return '[]';
  421. return static::decodeRaw($js);
  422. }
  423. /**
  424. * Create js var definition.
  425. * 创建 JS 变量定义。
  426. *
  427. * @access public
  428. * @param string $name Variable name.
  429. * @param mixed $value Variable value.
  430. * @return string
  431. */
  432. public static function defineVar($name, $value)
  433. {
  434. $js = new js();
  435. $js->var($name, $value);
  436. return $js->toJS();
  437. }
  438. /**
  439. * @param string $func
  440. * @param mixed[] $args
  441. */
  442. public static function defineJSCall($func, $args)
  443. {
  444. $js = new js();
  445. $js->call($func, ...$args);
  446. return $js->toJS();
  447. }
  448. /**
  449. * Create js context object.
  450. * 创建给定 JS 值的上下文操作辅助对象。
  451. *
  452. * @access public
  453. * @param mixed $value Value.
  454. * @param null|string|bool $name Name.
  455. * @param null|string|js|array ...$codes Codes.
  456. * @return jsContext
  457. */
  458. public static function context($value, $name = null, ...$codes)
  459. {
  460. return new jsContext($value, $name, ...$codes);
  461. }
  462. /**
  463. * Create zui context object.
  464. * 创建 ZUI 上下文操作辅助对象。
  465. *
  466. * @access public
  467. * @param null|string $name Name.
  468. * @return jsContext
  469. */
  470. public static function zui($name = null)
  471. {
  472. if(is_null($name)) return static::context('zui');
  473. return static::context("zui.{$name}");
  474. }
  475. /**
  476. * Create jquery context object.
  477. * 创建 jQuery 上下文操作辅助对象。
  478. *
  479. * @access public
  480. * @param string $selector Selector.
  481. * @param null|string $name Name.
  482. * @param null|string|js|array ...$codes Codes.
  483. * @return jquery
  484. */
  485. public static function jquery($selector, $name = null, ...$codes)
  486. {
  487. return new jquery($selector, $name, ...$codes);
  488. }
  489. /**
  490. * Create js callback object.
  491. * 创建 js 回调函数代码生成对象。
  492. *
  493. * @access public
  494. * @param string ...$args Function argument name list.
  495. * @return jsCallback
  496. * @static
  497. */
  498. public static function callback(...$args)
  499. {
  500. return new jsCallback(...$args);
  501. }
  502. /**
  503. * Create window variables context object.
  504. * 创建 window 变量上下文操作辅助对象。
  505. *
  506. * @access public
  507. * @param string $name Name.
  508. * @param mixed[] ...$args Arguments.
  509. */
  510. public static function __callStatic($name, $args)
  511. {
  512. $context = static::context("window.{$name}");
  513. if(empty($args)) return $context;
  514. return $context->call(...$args);
  515. }
  516. }
  517. /**
  518. * Create js object.
  519. * 创建 JS 对象。
  520. *
  521. * @access public
  522. * @param null|string|js|array ...$codes Codes.
  523. * @return js
  524. */
  525. function js(...$codes)
  526. {
  527. return new js(...$codes);
  528. }
  529. function jsRaw(...$codes)
  530. {
  531. return js::raw(...$codes);
  532. }