jquery.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. /**
  3. * The jQuery context 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 __DIR__ . DS . 'jscontext.class.php';
  13. require_once dirname(__DIR__) . DS . 'utils' . DS . 'classlist.class.php';
  14. use zin\utils\classlist;
  15. /**
  16. * Class for generating jQuery code with context.
  17. * 用于生成带上下文的 jQuery 代码的类。
  18. */
  19. class jQuery extends jsContext
  20. {
  21. /**
  22. * 构造函数。
  23. *
  24. * @access public
  25. * @param string $selector The jQuery selector. jQuery 选择器。
  26. * @param string|null $name The jQuery name. jQuery 名称。
  27. * @param string|array|js|null ...$codes The jQuery codes. jQuery 代码。
  28. */
  29. public function __construct($selector, $name = null, ...$codes)
  30. {
  31. parent::__construct(static::selector($selector), $name, ...$codes);
  32. }
  33. /**
  34. * Add class to the element.
  35. * 为元素添加类。
  36. *
  37. * @access public
  38. * @param mixed ...$classList The class list. 类列表。
  39. * @return self
  40. */
  41. public function addClass(...$classList)
  42. {
  43. return $this->call('addClass', classlist::format(...$classList));
  44. }
  45. /**
  46. * Set class to the element.
  47. * 为元素设置类。
  48. *
  49. * @access public
  50. * @param mixed ...$classList The class list. 类列表。
  51. * @return self
  52. */
  53. public function setClass(...$classList)
  54. {
  55. return $this->call('setClass', classlist::format(...$classList));
  56. }
  57. /**
  58. * Remove class from the element.
  59. * 为元素移除类。
  60. *
  61. * @access public
  62. * @param mixed ...$classList The class list. 类列表。
  63. * @return self
  64. */
  65. public function removeClass(...$classList)
  66. {
  67. return $this->call('removeClass', classlist::format(...$classList));
  68. }
  69. /**
  70. * Toggle class to the element.
  71. * 为元素切换类。
  72. *
  73. * @access public
  74. * @param mixed $classList The class list. 类列表。
  75. * @param string $condition The condition. 条件。
  76. * @return self
  77. */
  78. public function toggleClass($classList, $condition)
  79. {
  80. return $this->call('toggleClass', classlist::format($classList), $condition);
  81. }
  82. /**
  83. * Set css style to the element.
  84. * 为元素设置 CSS 样式。
  85. *
  86. * @access public
  87. * @param string|array|object $name The css name. CSS 名称。
  88. * @param mixed $value The css value. CSS 值。
  89. * @return self
  90. */
  91. public function css($name, $value = null)
  92. {
  93. if(is_string($name)) return $this->call('css', $name, $value);
  94. return $this->call('css', $name);
  95. }
  96. /**
  97. * Set HTML attribute to the element.
  98. * 为元素设置 HTML 属性。
  99. *
  100. * @access public
  101. * @param string|array|object $name The attribute name. 属性名称。
  102. * @param mixed $value The attribute value. 属性值。
  103. * @return self
  104. */
  105. public function attr($name, $value = null)
  106. {
  107. if(is_string($name)) return $this->call('attr', $name, $value);
  108. return $this->call('attr', $name);
  109. }
  110. /**
  111. * Remove attribute from the element.
  112. * 为元素移除 HTML 属性。
  113. *
  114. * @access public
  115. * @param string $name The attribute name. 属性名称。
  116. * @return self
  117. */
  118. public function removeAttr($name)
  119. {
  120. return $this->call('removeAttr', $name);
  121. }
  122. /**
  123. * Set DOM property to the element.
  124. * 为元素设置 DOM 属性。
  125. *
  126. * @access public
  127. * @param string|array|object $name The property name. 属性名称。
  128. * @param mixed $value The property value. 属性值。
  129. * @return self
  130. */
  131. public function prop($name, $value = null)
  132. {
  133. if(is_string($name)) return $this->call('prop', $name, $value);
  134. return $this->call('prop', $name);
  135. }
  136. /**
  137. * Remove property from the element.
  138. * 为元素移除 DOM 属性。
  139. *
  140. * @access public
  141. * @param string $name The property name. 属性名称。
  142. * @return self
  143. */
  144. public function removeProp($name)
  145. {
  146. return $this->call('removeProp', $name);
  147. }
  148. /**
  149. * Show element.
  150. * 将元素设置为可见。
  151. *
  152. * @access public
  153. * @return self
  154. */
  155. public function show()
  156. {
  157. return $this->call('show');
  158. }
  159. /**
  160. * Hide element.
  161. * 将元素设置为不可见。
  162. *
  163. * @access public
  164. * @return self
  165. */
  166. public function hide()
  167. {
  168. return $this->call('hide');
  169. }
  170. /**
  171. * Toggle element.
  172. * 切换元素的可见性。
  173. *
  174. * @access public
  175. * @param bool|null $toggle The toggle value. 切换值。
  176. * @return self
  177. */
  178. public function toggle($toggle = null)
  179. {
  180. if(is_null($toggle)) return $this->call('toggle');
  181. return $this->call('toggle', $toggle);
  182. }
  183. /**
  184. * Trigger event on the element.
  185. * 在元素上触发事件。
  186. *
  187. * @access public
  188. * @param string $event The event name. 事件名称。
  189. * @param mixed ...$args The event arguments. 事件参数。
  190. * @return self
  191. */
  192. public function trigger($event, ...$args)
  193. {
  194. return $this->call('trigger', $event, ...$args);
  195. }
  196. /**
  197. * Bind event on element.
  198. * 为元素绑定事件。
  199. *
  200. * @access public
  201. * @param string $event The event name. 事件名称。
  202. * @param string $selectorOrHandler The selector or handler. 选择器或处理器。
  203. * @param string|null $handler The handler. 处理器。
  204. * @return self
  205. */
  206. public function on($event, $selectorOrHandler, $handler = null)
  207. {
  208. $selector = null;
  209. if(is_null($handler)) $handler = $selectorOrHandler;
  210. else $selector = $selectorOrHandler;
  211. if(is_string($handler)) $handler = jsRaw($handler);
  212. if(is_null($selector))
  213. {
  214. return $this->call('on', $event, $handler);
  215. }
  216. return $this->call('on', $event, $selector, $handler);
  217. }
  218. /**
  219. * Unbind event on element.
  220. * 为元素解绑事件。
  221. *
  222. * @access public
  223. * @param string $event The event name. 事件名称。
  224. * @return self
  225. */
  226. public function off($event)
  227. {
  228. return $this->call('off', $event);
  229. }
  230. /**
  231. * Set element inner HTML.
  232. * 设置元素内部 HTML 内容。
  233. *
  234. * @access public
  235. * @param string $html The HTML content. HTML 内容。
  236. * @return self
  237. */
  238. public function html($html)
  239. {
  240. return $this->call('html', $html);
  241. }
  242. /**
  243. * Set element inner text.
  244. * 设置元素内部文本内容。
  245. *
  246. * @access public
  247. * @param string $text The text content. 文本内容。
  248. * @return self
  249. */
  250. public function text($text)
  251. {
  252. return $this->call('text', $text);
  253. }
  254. /**
  255. * Set element value.
  256. * 设置元素值。
  257. *
  258. * @access public
  259. * @param mixed $value The value. 值。
  260. * @return self
  261. */
  262. public function val($value)
  263. {
  264. return $this->call('val', $value);
  265. }
  266. /**
  267. * Empty element.
  268. * 清空元素。
  269. *
  270. * @access public
  271. * @return self
  272. */
  273. public function empty()
  274. {
  275. return $this->call('empty');
  276. }
  277. /**
  278. * Remove element from DOM.
  279. * 将元素从 DOM 中移除。
  280. *
  281. * @access public
  282. * @return self
  283. */
  284. public function remove()
  285. {
  286. return $this->call('remove');
  287. }
  288. /**
  289. * Call callback on each element.
  290. * 在每个元素上调用回调。
  291. *
  292. * @access public
  293. * @param string $callback The callback. 回调。
  294. * @return self
  295. */
  296. public function each($callback)
  297. {
  298. if(is_string($callback)) $callback = jsRaw($callback);
  299. return $this->call('each', $callback);
  300. }
  301. /**
  302. * Insert element after the selector.
  303. * 将元素插入到选择器后面。
  304. *
  305. * @access public
  306. * @param string $selector The selector. 选择器。
  307. * @return self
  308. */
  309. public function insertAfter($selector)
  310. {
  311. return $this->call('insertAfter', $selector);
  312. }
  313. /**
  314. * Insert element before the selector.
  315. * 将元素插入到选择器前面。
  316. *
  317. * @access public
  318. * @param string $selector The selector. 选择器。
  319. * @return self
  320. */
  321. public function insertBefore($selector)
  322. {
  323. return $this->call('insertBefore', $selector);
  324. }
  325. /**
  326. * Insert content after the element.
  327. * 将内容插入到元素后面。
  328. *
  329. * @access public
  330. * @param string $selectorOrContent The selector or content. 选择器或内容。
  331. * @return self
  332. */
  333. public function after($selectorOrContent)
  334. {
  335. return $this->call('after', $selectorOrContent);
  336. }
  337. /**
  338. * Insert content before the element.
  339. * 将内容插入到元素前面。
  340. *
  341. * @access public
  342. * @param string $selectorOrContent The selector or content. 选择器或内容。
  343. * @return self
  344. */
  345. public function before($selectorOrContent)
  346. {
  347. return $this->call('before', $selectorOrContent);
  348. }
  349. /**
  350. * Append content to the element.
  351. * 将内容追加到元素。
  352. *
  353. * @access public
  354. * @param string $selectorOrContent The selector or content. 选择器或内容。
  355. * @return self
  356. */
  357. public function append($selectorOrContent)
  358. {
  359. return $this->call('append', $selectorOrContent);
  360. }
  361. /**
  362. * Prepend content to the element.
  363. * 将内容前置到元素。
  364. *
  365. * @access public
  366. * @param string $selectorOrContent The selector or content. 选择器或内容。
  367. * @return self
  368. */
  369. public function prepend($selectorOrContent)
  370. {
  371. return $this->call('prepend', $selectorOrContent);
  372. }
  373. /**
  374. * Append element to the selector.
  375. * 将元素追加到选择器。
  376. *
  377. * @access public
  378. * @param string $selector The selector. 选择器。
  379. * @return self
  380. */
  381. public function appendTo($selector)
  382. {
  383. return $this->call('appendTo', $selector);
  384. }
  385. /**
  386. * Prepend element to the selector.
  387. * 将元素前置到选择器。
  388. *
  389. * @access public
  390. * @param string $selector The selector. 选择器。
  391. * @return self
  392. */
  393. public function prependTo($selector)
  394. {
  395. return $this->call('prependTo', $selector);
  396. }
  397. /**
  398. * Replace element with content.
  399. * 用内容替换元素。
  400. *
  401. * @access public
  402. * $selectorOrContent The selector or content. 选择器或内容。
  403. * @return self
  404. * @param string $selectorOrContent
  405. */
  406. public function replaceWith($selectorOrContent)
  407. {
  408. return $this->call('replaceWith', $selectorOrContent);
  409. }
  410. /**
  411. * Set width to the element.
  412. * 设置元素宽度。
  413. *
  414. * @access public
  415. * @param string|int $width The width. 宽度。
  416. * @return self
  417. */
  418. public function width($width)
  419. {
  420. if(is_numeric($width)) $width .= 'px';
  421. return $this->call('width', $width);
  422. }
  423. /**
  424. * Set height to the element.
  425. * 设置元素高度。
  426. *
  427. * @access public
  428. * @param string|int $height The height. 高度。
  429. * @return self
  430. */
  431. public function height($height)
  432. {
  433. if(is_numeric($height)) $height .= 'px';
  434. return $this->call('height', $height);
  435. }
  436. /**
  437. * Generate code for creating jQuery object with selector.
  438. * 根据选择器生成创建 jQuery 对象的代码。
  439. *
  440. * @access public
  441. * @param string $selector The selector. 选择器。
  442. * @return string
  443. * @static
  444. */
  445. public static function selector($selector)
  446. {
  447. if(is_string($selector) && str_starts_with($selector, 'RAWJS<') && str_ends_with($selector, '>RAWJS')) return substr($selector, 6, -6);
  448. if(str_starts_with($selector, '$')) return $selector;
  449. if($selector !== 'window' && $selector !== 'document')
  450. {
  451. $selector = '\'' . str_replace('\'', "\'", $selector) . '\'';
  452. }
  453. return "\$($selector)";
  454. }
  455. }
  456. /**
  457. * Create a jQuery object.
  458. * 创建一个 jQuery 对象。
  459. *
  460. * @access public
  461. * @param string $selector The jQuery selector. jQuery 选择器。
  462. * @param string|null $name The jQuery name. jQuery 名称。
  463. * @param string|array|js|null ...$codes The jQuery codes. jQuery 代码。
  464. * @return jQuery
  465. */
  466. function jQuery($selector, $name = null, ...$codes)
  467. {
  468. return new jQuery($selector, $name, ...$codes);
  469. }