jshelper.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * The js helper 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 . 'js.class.php';
  13. require_once __DIR__ . DS . 'jquery.class.php';
  14. /**
  15. * Class for generating js code.
  16. * 用于生成 js 代码的助手,可以方便的操作 DOM 元素。
  17. */
  18. class jsHelper extends js
  19. {
  20. /**
  21. * Hide the specified elements.
  22. * 隐藏指定的元素。
  23. *
  24. * @access public
  25. * @param string $selector The selector of the elements.
  26. * @return self
  27. */
  28. public function toggleHide($selector)
  29. {
  30. $target = jQuery::selector($selector);
  31. return $this->appendLine("$target.hide();");
  32. }
  33. /**
  34. * Toggle the specified elements, if not specified, auto detect.
  35. * 切换显示或隐藏指定的元素,如果不指定参数则自动判断。
  36. *
  37. * @access public
  38. * @param string $selector The selector of the elements.
  39. * @param string $toggleCode Whether to show or hide the elements.
  40. * @return self
  41. */
  42. public function toggleShow($selector, $toggleCode = null)
  43. {
  44. $target = jQuery::selector($selector);
  45. if(is_null($toggleCode)) return $this->appendLine("$target.toggle();");
  46. if(!is_string($toggleCode)) $toggleCode = json_encode($toggleCode);
  47. return $this->appendLine("$target.toggle($toggleCode);");
  48. }
  49. /**
  50. * Add class to the specified elements.
  51. * 给指定的元素添加 CSS 类。
  52. *
  53. * @access public
  54. * @param string $selector The selector of the elements.
  55. * @param string $class The class to add.
  56. * @return self
  57. */
  58. public function addClass($selector, $class)
  59. {
  60. $target = jQuery::selector($selector);
  61. return $this->appendLine("$target.addClass(\"{$class}\");");
  62. }
  63. /**
  64. * Remove class from the specified elements.
  65. * 从指定的元素中移除 CSS 类。
  66. *
  67. * @access public
  68. * @param string $selector The selector of the elements.
  69. * @param string $class The class to remove.
  70. * @return self
  71. */
  72. public function removeClass($selector, $class)
  73. {
  74. $target = jQuery::selector($selector);
  75. return $this->appendLine("$target.removeClass(\"{$class}\");");
  76. }
  77. /**
  78. * Toggle class of the specified elements.
  79. * 切换指定的元素的 CSS 类。
  80. *
  81. * @access public
  82. * @param string $selector The selector of the elements.
  83. * @param string $class The class to toggle.
  84. * @param string $toggleCode Whether to add or remove the class.
  85. * @return self
  86. */
  87. public function toggleClass($selector, $class, $toggleCode = null)
  88. {
  89. $target = jQuery::selector($selector);
  90. if(is_null($toggleCode)) return $this->appendLine("$target.toggleClass(\"{$class}\");");
  91. return $this->appendLine("$target.toggleClass(\"{$class}\", $toggleCode);");
  92. }
  93. /**
  94. * Set the html of the specified elements.
  95. * 设置指定元素的 html。
  96. *
  97. * @access public
  98. * @param string $selector The selector of the elements.
  99. * @param string $html The html to set.
  100. * @return self
  101. */
  102. public function setHtml($selector, $html)
  103. {
  104. $target = jQuery::selector($selector);
  105. return $this->appendLine("$target.html(", json_encode($html), ");");
  106. }
  107. /**
  108. * Set the text of the specified elements.
  109. * 设置指定元素的 text。
  110. *
  111. * @access public
  112. * @param string $selector The selector of the elements.
  113. * @param string $text The text to set.
  114. * @return self
  115. */
  116. public function setText($selector, $text)
  117. {
  118. $target = jQuery::selector($selector);
  119. return $this->appendLine("$target.text(", json_encode($text), ");");
  120. }
  121. /**
  122. * Set the value of the specified elements.
  123. * 设置指定元素的 value。
  124. *
  125. * @access public
  126. * @param string $selector The selector of the elements.
  127. * @param string $value The value to set.
  128. * @return self
  129. */
  130. public function setVal($selector, $value)
  131. {
  132. $target = jQuery::selector($selector);
  133. return $this->appendLine("$target.val(", json_encode($value), ");");
  134. }
  135. /**
  136. * Set the css of the specified elements.
  137. * 设置指定元素的 css。
  138. *
  139. * @access public
  140. * @param string $selector The selector of the elements.
  141. * @param string $nameOrStyle The name of the css or the css style object.
  142. * @param string $value The value of the css.
  143. * @return self
  144. */
  145. public function setCss($selector, $nameOrStyle, $value = null)
  146. {
  147. $target = jQuery::selector($selector);
  148. if(is_array($nameOrStyle)) return $this->appendLine("$target.css(", json_encode($nameOrStyle), ");");
  149. return $this->appendLine("$target.css(\"{$nameOrStyle}\", ", json_encode($value), ");");
  150. }
  151. /**
  152. * Set the attr of the specified elements.
  153. * 设置指定元素的 attr。
  154. *
  155. * @access public
  156. * @param string $selector The selector of the elements.
  157. * @param string $nameOrAttrs The name of the attr or the attr object.
  158. * @param string $value The value of the attr.
  159. * @return self
  160. */
  161. public function setAttr($selector, $nameOrAttrs, $value = null)
  162. {
  163. $target = jQuery::selector($selector);
  164. if(is_array($nameOrAttrs)) return $this->appendLine("$target.attr(", json_encode($nameOrAttrs), ");");
  165. return $this->appendLine("$target.attr(\"{$nameOrAttrs}\", ", json_encode($value), ");");
  166. }
  167. /**
  168. * Remove the attr of the specified elements.
  169. * 移除指定元素的 attr。
  170. *
  171. * @access public
  172. * @param string $selector The selector of the elements.
  173. * @param string $name The name of the attr.
  174. * @return self
  175. */
  176. public function removeAttr($selector, $name)
  177. {
  178. $target = jQuery::selector($selector);
  179. return $this->appendLine("$target.removeAttr(\"{$name}\");");
  180. }
  181. /**
  182. * Set the prop of the specified elements.
  183. * 设置指定元素的 prop。
  184. *
  185. * @access public
  186. * @param string $selector The selector of the elements.
  187. * @param string $nameOrProps The name of the prop or the prop object.
  188. * @param string $value The value of the prop.
  189. * @return self
  190. */
  191. public function setProp($selector, $nameOrProps, $value = null)
  192. {
  193. $target = jQuery::selector($selector);
  194. if(is_array($nameOrProps)) return $this->appendLine("$target.prop(", json_encode($nameOrProps), ");");
  195. return $this->appendLine("$target.prop(\"{$nameOrProps}\", ", json_encode($value), ");");
  196. }
  197. /**
  198. * Remove the prop of the specified elements.
  199. * 移除指定元素的 prop。
  200. *
  201. * @access public
  202. * @param string $selector The selector of the elements.
  203. * @param string $name The name of the prop.
  204. * @return self
  205. */
  206. public function removeProp($selector, $name)
  207. {
  208. $target = jQuery::selector($selector);
  209. return $this->appendLine("$target.removeProp(\"{$name}\");");
  210. }
  211. /**
  212. * Trigger the specified event of the specified elements.
  213. * 触发指定元素的指定事件。
  214. *
  215. * @access public
  216. * @param string $selector The selector of the elements.
  217. * @param string $event The event to trigger.
  218. * @param mixed $data The data to pass to the event handler.
  219. * @return self
  220. */
  221. public function triggerEvent($selector, $event, $data = null)
  222. {
  223. $target = jQuery::selector($selector);
  224. return $this->appendLine("$target.trigger(\"{$event}\", ", json_encode($data), ");");
  225. }
  226. /**
  227. * Bind the specified event to the specified elements.
  228. * 给指定元素绑定指定事件。
  229. *
  230. * @access public
  231. * @param string $selector The selector of the elements.
  232. * @param string $event The event to bind.
  233. * @param string $targetsOrHandler The targets or handler to bind.
  234. * @param string $handler The handler to bind.
  235. * @return self
  236. */
  237. public function onEvent($selector, $event, $targetsOrHandler, $handler = null)
  238. {
  239. if(is_null($handler))
  240. {
  241. $targets = null;
  242. $handler = $targetsOrHandler;
  243. }
  244. else
  245. {
  246. $targets = $targetsOrHandler;
  247. }
  248. $target = jQuery::selector($selector);
  249. if(is_null($targets))
  250. {
  251. return $this->appendLine("$target.on(\"{$event}\", {$handler});");
  252. }
  253. $targets = str_replace('"', '\\"', $targets);
  254. return $this->appendLine("$target.on(\"{$event}\", \"{$targets}\", {$handler});");
  255. }
  256. /**
  257. * Unbind the specified event from the specified elements.
  258. * 给指定元素解绑指定事件。
  259. *
  260. * @access public
  261. * @param string $selector The selector of the elements.
  262. * @param string $event The event to unbind.
  263. * @param string $targetsOrHandler The targets or handler to unbind.
  264. * @param string $handler The handler to unbind.
  265. * @return self
  266. */
  267. public function offEvent($selector, $event, $targetsOrHandler = null, $handler = null)
  268. {
  269. if(is_null($handler))
  270. {
  271. $targets = null;
  272. $handler = $targetsOrHandler;
  273. }
  274. else
  275. {
  276. $targets = $targetsOrHandler;
  277. }
  278. $target = jQuery::selector($selector);
  279. if(is_null($targets))
  280. {
  281. if(is_null($handler)) return $this->appendLine("$target.off(\"{$event}\");");
  282. return $this->appendLine("$target.off(\"{$event}\", {$handler});");
  283. }
  284. $targets = str_replace('"', '\\"', $targets);
  285. if(is_null($handler)) return $this->appendLine("$target.off(\"{$event}\", \"{$targets}\");");
  286. return $this->appendLine("$target.off(\"{$event}\", \"{$targets}\", {$handler});");
  287. }
  288. }
  289. /**
  290. * Create a new js helper.
  291. * 创建一个新的 js 助手。
  292. *
  293. * @access public
  294. * @param string|array|js|null ...$codes The codes to append.
  295. * @return jsHelper
  296. */
  297. function jsHelper(...$codes)
  298. {
  299. return new jsHelper(...$codes);
  300. }