v1.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace zin;
  3. requireWg('textarea');
  4. requireWg('checkbox');
  5. class thinkBaseCheckbox extends wg
  6. {
  7. /**
  8. * @var mixed[]
  9. */
  10. protected static $defineProps = array(
  11. 'primary: bool=true',
  12. 'type: string="checkbox"',
  13. 'name?: string',
  14. 'value?: string|array',
  15. 'items?: array',
  16. 'inline?: bool',
  17. 'disabled?: bool'
  18. );
  19. public static function getPageCSS()
  20. {
  21. return file_get_contents(__DIR__ . DS . 'css' . DS . 'v1.css');
  22. }
  23. public static function getPageJS()
  24. {
  25. return file_get_contents(__DIR__ . DS . 'js' . DS . 'v1.js');
  26. }
  27. public function getValueList()
  28. {
  29. $value = $this->prop('value');
  30. if(is_null($value)) return array();
  31. if($this->prop('type') === 'checkbox') return is_array($value) ? $value : explode(',', $value);
  32. return [$value];
  33. }
  34. /**
  35. * @return \zin\wg|\zin\node
  36. */
  37. public function onBuildItem($item)
  38. {
  39. global $lang;
  40. if($item instanceof item) $item = $item->props->toJSON();
  41. $disabled = $this->prop('disabled');
  42. if(!isset($item['checked']))
  43. {
  44. $value = isset($item['value']) ? $item['value'] : '';
  45. $valueList = $this->getValueList();
  46. $item['checked'] = !empty($value) && in_array($value, $valueList);
  47. $item['disabled'] = $this->prop('disabled');
  48. }
  49. $props = $this->props->pick(['primary', 'type', 'name', 'disabled']);
  50. if(!empty($props['name']) && !empty($item['value'])) $props['id'] = $props['name'] . $item['value'];
  51. $itemClass = '';
  52. $text = $item['text'];
  53. unset($item['text']);
  54. if(isset($item['checked']) && $item['checked']) $itemClass = 'is-checked';
  55. if(!empty($item['isOther']))
  56. {
  57. return div
  58. (
  59. setClass('item-control has-input w-full py-2 px-3 flex gap-3 items-center justify-between border cursor-pointer rounded ' . $itemClass),
  60. setData('type', $this->prop('type')),
  61. !$disabled ? on::click('toggleChecked') : null,
  62. div(setClass('flex items-start text-md gap-1.5 flex-1'), div(setStyle(array('min-width' => '60px')), setClass('mt-1'), $text), new textarea
  63. (
  64. set(array(
  65. 'rows' => 1,
  66. 'class' => 'run-other' . (isset($item['checked']) && $item['checked'] ? '' : ' hidden'),
  67. 'name' => 'other',
  68. 'value' => isset($item['other']) ? $item['other'] : '',
  69. 'placeholder' => $lang->thinkrun->placeholder->otherOption,
  70. 'disabled' => $disabled
  71. )),
  72. on::input('inputOther'),
  73. on::click("event.stopPropagation(); if($('.run-other-error')) $('.run-other-error').removeClass('run-other-error');")
  74. )),
  75. new checkbox
  76. (
  77. set($props),
  78. set($item),
  79. $this->prop('type') == 'radio' ? on::click('e.stopPropagation()') : null,
  80. isset($item['checked']) && $item['checked'] ? set::rootClass('checked') : null
  81. )
  82. );
  83. }
  84. return div
  85. (
  86. setData('type', $this->prop('type')),
  87. set::title(!empty($item['title']) ? $item['title'] : null),
  88. !$disabled ? on::click('toggleChecked') : null,
  89. setClass('item-control w-full py-2 px-3 flex gap-3 items-center justify-between border cursor-pointer rounded ' . $itemClass),
  90. div(setClass('text-md flex-1 break-all'), $text),
  91. new checkbox
  92. (
  93. set($props),
  94. set($item),
  95. $this->prop('type') == 'radio' ? on::click('e.stopPropagation()') : null,
  96. isset($item['checked']) && $item['checked'] ? set::rootClass('checked') : null
  97. )
  98. );
  99. }
  100. protected function build()
  101. {
  102. list($items, $inline, $disabled) = $this->prop(['items', 'inline', 'disabled']);
  103. if(!empty($items))
  104. {
  105. $valueList = $this->getValueList();
  106. foreach($items as $key => $item)
  107. {
  108. $prefix = '';
  109. $index = $key;
  110. while($index >= 0)
  111. {
  112. $prefix = chr(65 + ($index % 26)) . $prefix;
  113. $index = floor($index / 26) - 1;
  114. }
  115. if(!is_array($item)) $item = array('text' => $item, 'value' => $key);
  116. if(!isset($item['checked'])) $item['checked'] = !empty($item['value']) && in_array($item['value'], $valueList);
  117. $item['text'] = !empty($item['disabledPrefix']) ? $item['text'] : $prefix . '. ' . $item['text'];
  118. $items[$key] = $this->onBuildItem($item);
  119. }
  120. }
  121. return div
  122. (
  123. setClass('think-check-list check-items', $inline ? 'check-list-inline' : 'check-list'),
  124. set($this->getRestProps()),
  125. $disabled ? set('disabled', 'disabled') : '',
  126. $items,
  127. $this->children()
  128. );
  129. }
  130. }