v1.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'input' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'textarea' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'editor' . DS . 'v1.php';
  6. require_once dirname(__DIR__) . DS . 'checkbox' . DS . 'v1.php';
  7. require_once dirname(__DIR__) . DS . 'checklist' . DS . 'v1.php';
  8. require_once dirname(__DIR__) . DS . 'radiolist' . DS . 'v1.php';
  9. require_once dirname(__DIR__) . DS . 'select' . DS . 'v1.php';
  10. require_once dirname(__DIR__) . DS . 'inputcontrol' . DS . 'v1.php';
  11. require_once dirname(__DIR__) . DS . 'inputgroup' . DS . 'v1.php';
  12. require_once dirname(__DIR__) . DS . 'picker' . DS . 'v1.php';
  13. require_once dirname(__DIR__) . DS . 'datepicker' . DS . 'v1.php';
  14. require_once dirname(__DIR__) . DS . 'timepicker' . DS . 'v1.php';
  15. require_once dirname(__DIR__) . DS . 'pripicker' . DS . 'v1.php';
  16. require_once dirname(__DIR__) . DS . 'severitypicker' . DS . 'v1.php';
  17. require_once dirname(__DIR__) . DS . 'colorpicker' . DS . 'v1.php';
  18. require_once dirname(__DIR__) . DS . 'colorinput' . DS . 'v1.php';
  19. require_once dirname(__DIR__) . DS . 'datetimepicker' . DS . 'v1.php';
  20. require_once dirname(__DIR__) . DS . 'fileselector' . DS . 'v1.php';
  21. class control extends wg
  22. {
  23. /**
  24. * @var mixed[]
  25. */
  26. protected static $defineProps = array
  27. (
  28. 'control?: string', // 表单输入元素类型,值可以为:static, text, input, password, email, number, date, time, datetime, month, url, search, color, picker, pri, severity, select, checkbox, radio, checkboxList, radioList, checkboxListInline, radioListInline, file, image, textarea, editor
  29. 'type?: string', // 请使用 control 属性,如果已经指定 control 属性,则此属性作为具体控件的 type 属性。
  30. 'id?: string', // ID。
  31. 'name: string', // 控件名称,可能影响到表单提交的域名称,如果是多个值的表单控件,可能需要将名称定义为 `key[]` 的形式。
  32. 'value?: string', // 控件值。
  33. 'placeholder?: string', // 占位文本。
  34. 'text?: string', // 文本。
  35. 'readonly?: bool', // 是否只读。
  36. 'required?: bool', // 是否为必填。
  37. 'disabled?: bool', // 是否禁用。
  38. 'builder?: callable', // 自定义构建函数。
  39. 'items?: array' // 选项列表。
  40. );
  41. /**
  42. * @var mixed[]
  43. */
  44. protected static $inputTypes = array
  45. (
  46. 'button', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', 'number', 'password', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'url', 'week'
  47. );
  48. protected function checkErrors()
  49. {
  50. if($this->prop('control') == 'upload') trigger_error('[ZIN] The control property value "upload" is deprecated, use "file" instead.', E_USER_WARNING);
  51. }
  52. protected function created()
  53. {
  54. list($control, $type, $name, $id) = $this->prop(array('control', 'type', 'name', 'id'));
  55. if(is_null($control))
  56. {
  57. $control = $type;
  58. $type = null;
  59. $this->setProp('control', $control);
  60. $this->setProp('type', null);
  61. }
  62. if($control === 'static' && is_null($name))
  63. {
  64. $name = '';
  65. $this->setProp('name', '');
  66. }
  67. if(is_null($id) && $name !== null)
  68. {
  69. $id = substr($name, -2) == '[]' ? substr($name, 0, - 2) : $name;
  70. $this->setProp('id', $id);
  71. }
  72. }
  73. /**
  74. * Build control with static content.
  75. *
  76. * @return node
  77. */
  78. protected function buildStatic()
  79. {
  80. $name = $this->prop('name');
  81. return div
  82. (
  83. set::className('form-control-static'),
  84. set($this->props->skip(array('type', 'control', 'name', 'value', 'required', 'disabled', 'placeholder', 'items', 'required'))),
  85. $name ? set('data-name', $name) : null,
  86. $this->prop('value')
  87. );
  88. }
  89. protected function buildTextarea()
  90. {
  91. return new textarea(set($this->props->skip('control')));
  92. }
  93. protected function buildInputControl()
  94. {
  95. $controlProps = array();
  96. $allProps = $this->props->skip(array('control'));
  97. $propsNames = array_keys(inputControl::definedPropsList());
  98. foreach($propsNames as $propName)
  99. {
  100. if(!isset($allProps[$propName])) continue;
  101. $controlProps[$propName] = $allProps[$propName];
  102. unset($allProps[$propName]);
  103. }
  104. return new inputControl
  105. (
  106. set($controlProps),
  107. new input(set($allProps))
  108. );
  109. }
  110. protected function buildInputGroup()
  111. {
  112. return new inputGroup(set($this->props->skip(array('control', 'required', 'name'))));
  113. }
  114. protected function buildCheckbox()
  115. {
  116. if($this->hasProp('items')) return $this->buildCheckList();
  117. return new checkList
  118. (
  119. new checkbox(set($this->props->skip('control')))
  120. );
  121. }
  122. protected function buildCheckList()
  123. {
  124. return new checkList
  125. (
  126. set($this->props->skip('control'))
  127. );
  128. }
  129. protected function buildRadioList()
  130. {
  131. return new radioList
  132. (
  133. set($this->props->skip('control'))
  134. );
  135. }
  136. protected function buildCheckListInline()
  137. {
  138. return new checkList
  139. (
  140. set::inline(true),
  141. set($this->props->skip('control'))
  142. );
  143. }
  144. protected function buildRadioListInline()
  145. {
  146. return new radioList
  147. (
  148. set::inline(true),
  149. set($this->props->skip('control'))
  150. );
  151. }
  152. protected function buildDate()
  153. {
  154. return new datePicker(set($this->props->skip('control')));
  155. }
  156. protected function buildTime()
  157. {
  158. return new timePicker(set($this->props->skip('control')));
  159. }
  160. protected function buildPri()
  161. {
  162. return new priPicker(set($this->props->skip('control')));
  163. }
  164. protected function buildSeverity()
  165. {
  166. return new severityPicker(set($this->props->skip('control')));
  167. }
  168. protected function buildColor()
  169. {
  170. return new colorPicker(set($this->props->skip('control')));
  171. }
  172. protected function buildColorInput()
  173. {
  174. return new colorInput(set($this->props->skip('control')));
  175. }
  176. protected function buildFile()
  177. {
  178. return new fileSelector(set($this->props->skip('control')));
  179. }
  180. protected function buildFiles()
  181. {
  182. return $this->buildFile();
  183. }
  184. protected function buildHidden()
  185. {
  186. return new input(set::type('hidden'), set($this->props->skip('control')));
  187. }
  188. protected function buildText()
  189. {
  190. return new input(set::type('text'), set($this->props->skip('control')));
  191. }
  192. protected function buildHtml()
  193. {
  194. return div
  195. (
  196. setClass('article'),
  197. set($this->getRestProps()),
  198. html($this->prop('value'))
  199. );
  200. }
  201. protected function buildDropdown()
  202. {
  203. return new dropdown($this->prop('text'), set($this->props->skip('control,text,name,widget')));
  204. }
  205. protected function build()
  206. {
  207. $builder = $this->prop('builder');
  208. if(is_callable($builder)) return $builder($this->props->skip('builder'), $this->children());
  209. $control = $this->prop('control');
  210. if(empty($control)) $control = $this->hasProp('items') ? 'picker' : 'input';
  211. $methodName = "build{$control}";
  212. if(method_exists($this, $methodName)) return $this->$methodName();
  213. $wgName = "\\zin\\$control";
  214. if(class_exists($wgName)) return new $wgName(set($this->props->skip('control')), $this->children());
  215. if(!empty($control) && !in_array($control, static::$inputTypes)) return createWg($control, set($this->props->skip('control')), 'input');
  216. return new input(set::type($control), set($this->props->skip('control')));
  217. }
  218. }