v1.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'checkbox' . DS . 'v1.php';
  4. class checkList extends wg
  5. {
  6. /**
  7. * @var mixed[]
  8. */
  9. protected static $defineProps = array(
  10. 'primary: bool=true',
  11. 'className?: string',
  12. 'type: string="checkbox"',
  13. 'name?: string',
  14. 'value?: string|array',
  15. 'items?: array',
  16. 'inline?: bool',
  17. 'disabled?: bool',
  18. 'title?: string'
  19. );
  20. public function getValueList()
  21. {
  22. $value = $this->prop('value');
  23. if(is_null($value)) return array();
  24. if($this->prop('type') === 'checkbox') return is_array($value) ? $value : explode(',', $value);
  25. return array($value);
  26. }
  27. public function onBuildItem($item)
  28. {
  29. if($item instanceof item) $item = $item->props->toJSON();
  30. if(!isset($item['checked']))
  31. {
  32. $value = isset($item['value']) ? $item['value'] : '';
  33. $valueList = $this->getValueList();
  34. $item['checked'] = in_array($value, $valueList);
  35. $item['disabled'] = $this->prop('disabled');
  36. }
  37. $props = $this->props->pick(array('primary', 'type', 'name', 'disabled'));
  38. if(!empty($props['name']) && isset($item['value'])) $props['id'] = str_replace('[]', '', $props['name']) . (is_null($item['value']) ? '' : $item['value']);
  39. return $this->buildItem(array_merge($props, $item));
  40. }
  41. /**
  42. * @param mixed[] $props
  43. */
  44. public function buildItem($props)
  45. {
  46. return new checkbox(set($props));
  47. }
  48. protected function build()
  49. {
  50. list($items, $inline, $disabled, $className, $title) = $this->prop(array('items', 'inline', 'disabled', 'className', 'title'));
  51. if(!empty($items))
  52. {
  53. $valueList = $this->getValueList();
  54. foreach($items as $key => $item)
  55. {
  56. if(!is_array($item)) $item = array('text' => $item, 'value' => $key);
  57. if(!isset($item['checked'])) $item['checked'] = in_array($item['value'], $valueList);
  58. if(!empty($item[$title])) $item['title'] = $item[$title];
  59. $items[$key] = $this->onBuildItem($item);
  60. }
  61. }
  62. return div
  63. (
  64. setClass($inline ? 'check-list-inline' : 'check-list', $className, $disabled ? 'disabled' : ''),
  65. set($this->getRestProps()),
  66. $items,
  67. $this->children()
  68. );
  69. }
  70. }