v1.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * The dropPicker widget class file of zin module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author sunhao<sunhao@easycorp.ltd>
  8. * @package zin
  9. * @link http://www.zentao.net
  10. */
  11. namespace zin;
  12. require_once dirname(__DIR__) . DS . 'dropdown' . DS . 'v1.php';
  13. require_once dirname(__DIR__) . DS . 'btn' . DS . 'v1.php';
  14. /**
  15. * 下拉菜单形式的选择器。
  16. * The dropPicker widget class.
  17. *
  18. * @author Hao Sun
  19. */
  20. class dropPicker extends wg
  21. {
  22. /**
  23. * Define the properties.
  24. *
  25. * @var array
  26. * @access protected
  27. */
  28. protected static $defineProps = array
  29. (
  30. 'btnClass' => 'string="w-full"',
  31. 'text' => 'string',
  32. 'value' => 'string',
  33. 'name' => 'string',
  34. 'items' => 'array',
  35. 'menu' => 'array'
  36. );
  37. /**
  38. * Override the build method.
  39. *
  40. * @access protected
  41. * @return array
  42. */
  43. protected function build()
  44. {
  45. list($items, $text, $value, $name, $btnClass, $menu) = $this->prop(array('items', 'text', 'value', 'name', 'btnClass', 'menu'));
  46. $btnID = $this->gid;
  47. $menuProps = array();
  48. $menuProps['getItem'] = jsRaw(<<<JS
  49. function(item)
  50. {
  51. const selected = document.getElementById('{$btnID}').querySelector('input').value;
  52. item.selected = String(item.value) === selected;
  53. return item;
  54. }
  55. JS
  56. );
  57. $menuProps['onClickItem'] = jsRaw(<<<JS
  58. function(e)
  59. {
  60. if(e.item.value === undefined) return;
  61. const btn = document.getElementById('{$btnID}');
  62. const input = btn.querySelector('input');
  63. btn.querySelector('.text').innerText = e.item.text;
  64. input.value = e.item.value;
  65. \$(input).trigger('change');
  66. }
  67. JS
  68. );
  69. if(is_array($menu)) $menuProps = array_merge($menuProps, $menu);
  70. return new dropdown
  71. (
  72. new btn
  73. (
  74. $text,
  75. setClass('justify-between', $btnClass),
  76. setID($btnID),
  77. h::formHidden($name, $value)
  78. ),
  79. set::items($items),
  80. set::width('100%'),
  81. set::menu($menuProps),
  82. set($this->getRestProps())
  83. );
  84. }
  85. }