Field.php 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Widgets\Form as WidgetForm;
  6. use Dcat\Admin\Form\Concerns;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Contracts\Support\Renderable;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Fluent;
  11. use Illuminate\Support\Str;
  12. use Illuminate\Support\Traits\Macroable;
  13. /**
  14. * Class Field.
  15. */
  16. class Field implements Renderable
  17. {
  18. use Macroable, Concerns\HasFieldValidator;
  19. const FILE_DELETE_FLAG = '_file_del_';
  20. /**
  21. * Element id.
  22. *
  23. * @var array|string
  24. */
  25. protected $id;
  26. /**
  27. * Element value.
  28. *
  29. * @var mixed
  30. */
  31. protected $value;
  32. /**
  33. * Data of all original columns of value.
  34. *
  35. * @var mixed
  36. */
  37. protected $data;
  38. /**
  39. * Field original value.
  40. *
  41. * @var mixed
  42. */
  43. protected $original;
  44. /**
  45. * Field default value.
  46. *
  47. * @var mixed
  48. */
  49. protected $default;
  50. /**
  51. * Element label.
  52. *
  53. * @var string
  54. */
  55. protected $label = '';
  56. /**
  57. * Column name.
  58. *
  59. * @var string|array
  60. */
  61. protected $column = '';
  62. /**
  63. * Form element name.
  64. *
  65. * @var string
  66. */
  67. protected $elementName = [];
  68. /**
  69. * Form element classes.
  70. *
  71. * @var array
  72. */
  73. protected $elementClass = [];
  74. /**
  75. * Variables of elements.
  76. *
  77. * @var array
  78. */
  79. protected $variables = [];
  80. /**
  81. * Options for specify elements.
  82. *
  83. * @var array
  84. */
  85. protected $options = [];
  86. /**
  87. * Checked for specify elements.
  88. *
  89. * @var array
  90. */
  91. protected $checked = [];
  92. /**
  93. * Css required by this field.
  94. *
  95. * @var array
  96. */
  97. protected static $css = [];
  98. /**
  99. * Js required by this field.
  100. *
  101. * @var array
  102. */
  103. protected static $js = [];
  104. /**
  105. * Script for field.
  106. *
  107. * @var string
  108. */
  109. protected $script = '';
  110. /**
  111. * Element attributes.
  112. *
  113. * @var array
  114. */
  115. protected $attributes = [];
  116. /**
  117. * Parent form.
  118. *
  119. * @var Form|WidgetForm
  120. */
  121. protected $form = null;
  122. /**
  123. * View for field to render.
  124. *
  125. * @var string
  126. */
  127. protected $view = '';
  128. /**
  129. * Help block.
  130. *
  131. * @var array
  132. */
  133. protected $help = [];
  134. /**
  135. * Key for errors.
  136. *
  137. * @var mixed
  138. */
  139. protected $errorKey;
  140. /**
  141. * Placeholder for this field.
  142. *
  143. * @var string|array
  144. */
  145. protected $placeholder;
  146. /**
  147. * Width for label and field.
  148. *
  149. * @var array
  150. */
  151. protected $width = [
  152. 'label' => 2,
  153. 'field' => 8,
  154. ];
  155. /**
  156. * If the form horizontal layout.
  157. *
  158. * @var bool
  159. */
  160. protected $horizontal = true;
  161. /**
  162. * column data format.
  163. *
  164. * @var \Closure
  165. */
  166. protected $customFormat = null;
  167. /**
  168. * @var bool
  169. */
  170. protected $display = true;
  171. /**
  172. * @var array
  173. */
  174. protected $labelClass = [];
  175. /**
  176. * @var \Closure
  177. */
  178. protected $prepareCallback;
  179. /**
  180. * Field constructor.
  181. *
  182. * @param $column
  183. * @param array $arguments
  184. */
  185. public function __construct($column, $arguments = [])
  186. {
  187. $this->column = $column;
  188. $this->label = $this->formatLabel($arguments);
  189. $this->id = $this->formatId($column);
  190. }
  191. /**
  192. * Get the field element id.
  193. *
  194. * @return string|array
  195. */
  196. public function getElementId()
  197. {
  198. return $this->id;
  199. }
  200. /**
  201. * Format the field element id.
  202. *
  203. * @param string|array $column
  204. *
  205. * @return string|array
  206. */
  207. protected function formatId($column)
  208. {
  209. $random = Str::random(5);
  210. if (is_array($column)) {
  211. $id = [];
  212. foreach (str_replace('.', '-', $column) as $k => $v) {
  213. $id[$k] = "{$v}-{$random}";
  214. }
  215. return $id;
  216. }
  217. return 'form-field-'.str_replace('.', '-', $column).'-'.$random;
  218. }
  219. /**
  220. * Format the label value.
  221. *
  222. * @param array $arguments
  223. *
  224. * @return string
  225. */
  226. protected function formatLabel($arguments = [])
  227. {
  228. $column = is_array($this->column) ? current($this->column) : $this->column;
  229. $label = isset($arguments[0]) ? $arguments[0] : ucfirst(admin_trans_field($column));
  230. return str_replace(['.', '_'], ' ', $label);
  231. }
  232. /**
  233. * Format the name of the field.
  234. *
  235. * @param string $column
  236. *
  237. * @return array|mixed|string
  238. */
  239. protected function formatName($column)
  240. {
  241. if (is_string($column)) {
  242. $name = explode('.', $column);
  243. if (count($name) == 1) {
  244. return $name[0];
  245. }
  246. $html = array_shift($name);
  247. foreach ($name as $piece) {
  248. $html .= "[$piece]";
  249. }
  250. return $html;
  251. }
  252. if (is_array($this->column)) {
  253. $names = [];
  254. foreach ($this->column as $key => $name) {
  255. $names[$key] = $this->formatName($name);
  256. }
  257. return $names;
  258. }
  259. return '';
  260. }
  261. /**
  262. * Set form element name.
  263. *
  264. * @param string $name
  265. *
  266. * @return $this
  267. *
  268. * @author Edwin Hui
  269. */
  270. public function setElementName($name)
  271. {
  272. $this->elementName = $name;
  273. return $this;
  274. }
  275. /**
  276. * Fill data to the field.
  277. *
  278. * @param array $data
  279. *
  280. * @return void
  281. */
  282. final public function fill($data)
  283. {
  284. $this->data($data);
  285. $this->value = $this->formatFieldData($data);
  286. $this->callCustomFormatter();
  287. }
  288. /**
  289. * Format field data.
  290. *
  291. * @param array $data
  292. * @return mixed
  293. */
  294. protected function formatFieldData($data)
  295. {
  296. if (is_array($this->column)) {
  297. $value = [];
  298. foreach ($this->column as $key => $column) {
  299. $value[$key] = Arr::get($data, $column);
  300. }
  301. return $value;
  302. }
  303. return Arr::get($data, $this->column, $this->value);
  304. }
  305. /**
  306. * custom format form column data when edit.
  307. *
  308. * @param \Closure $call
  309. *
  310. * @return $this
  311. */
  312. public function customFormat(\Closure $call)
  313. {
  314. $this->customFormat = $call;
  315. return $this;
  316. }
  317. /**
  318. * Set original value to the field.
  319. *
  320. * @param array $data
  321. * @return void
  322. */
  323. final public function setOriginal($data)
  324. {
  325. $this->original = $this->formatFieldData($data);
  326. $this->callCustomFormatter('original', new Fluent($data));
  327. }
  328. /**
  329. * @param string $key
  330. * @param Fluent|null $dataremoveField
  331. */
  332. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  333. {
  334. if ($this->customFormat) {
  335. $this->{$key} = $this->customFormat
  336. ->call(
  337. $data ?: $this->data(),
  338. $this->{$key},
  339. $this->column,
  340. $this
  341. );
  342. }
  343. }
  344. /**
  345. * @param Form|WidgetForm $form
  346. *
  347. * @return $this
  348. */
  349. public function setForm($form = null)
  350. {
  351. $this->form = $form;
  352. return $this;
  353. }
  354. /**
  355. * @return Fluent
  356. */
  357. public function getFormModel()
  358. {
  359. return $this->form ? $this->form->model() : new Fluent;
  360. }
  361. /**
  362. * Set width for field and label.
  363. *
  364. * @param int $field
  365. * @param int $label
  366. *
  367. * @return $this
  368. */
  369. public function setWidth($field = 8, $label = 2)
  370. {
  371. $this->width = [
  372. 'label' => $label,
  373. 'field' => $field,
  374. ];
  375. return $this;
  376. }
  377. /**
  378. * Set the field options.
  379. *
  380. * @param array $options
  381. *
  382. * @return $this
  383. */
  384. public function options($options = [])
  385. {
  386. if ($options instanceof Arrayable) {
  387. $options = $options->toArray();
  388. }
  389. $this->options = array_merge($this->options, $options);
  390. return $this;
  391. }
  392. /**
  393. * Set the field option checked.
  394. *
  395. * @param array $checked
  396. *
  397. * @return $this
  398. */
  399. public function checked($checked = [])
  400. {
  401. if ($checked instanceof Arrayable) {
  402. $checked = $checked->toArray();
  403. }
  404. $this->checked = array_merge($this->checked, (array)$checked);
  405. return $this;
  406. }
  407. /**
  408. * Get key for error message.
  409. *
  410. * @return string
  411. */
  412. public function getErrorKey()
  413. {
  414. return $this->errorKey ?: $this->column;
  415. }
  416. /**
  417. * Set key for error message.
  418. *
  419. * @param string $key
  420. *
  421. * @return $this
  422. */
  423. public function setErrorKey($key)
  424. {
  425. $this->errorKey = $key;
  426. return $this;
  427. }
  428. /**
  429. * Set or get value of the field.
  430. *
  431. * @param null $value
  432. *
  433. * @return mixed
  434. */
  435. public function value($value = null)
  436. {
  437. if (is_null($value)) {
  438. return is_null($this->value) ? $this->getDefault() : $this->value;
  439. }
  440. $this->value = $value;
  441. return $this;
  442. }
  443. /**
  444. * Set or get data.
  445. *
  446. * @param array $data
  447. *
  448. * @return $this
  449. */
  450. public function data(array $data = null)
  451. {
  452. if (is_null($data)) {
  453. return $this->data ?: ($this->data = new Fluent);
  454. }
  455. $this->data = new Fluent($data);
  456. return $this;
  457. }
  458. /**
  459. * Set default value for field.
  460. *
  461. * @param $default
  462. *
  463. * @return $this
  464. */
  465. public function default($default)
  466. {
  467. $this->default = $default;
  468. return $this;
  469. }
  470. /**
  471. * Get default value.
  472. *
  473. * @return mixed
  474. */
  475. public function getDefault()
  476. {
  477. if ($this->default instanceof \Closure) {
  478. return call_user_func($this->default, $this->form);
  479. }
  480. return $this->default;
  481. }
  482. /**
  483. * Set help block for current field.
  484. *
  485. * @param string $text
  486. * @param string $icon
  487. *
  488. * @return $this
  489. */
  490. public function help($text = '', $icon = 'fa-info-circle')
  491. {
  492. $this->help = compact('text', 'icon');
  493. return $this;
  494. }
  495. /**
  496. * Get column of the field.
  497. *
  498. * @return string|array
  499. */
  500. public function column()
  501. {
  502. return $this->column;
  503. }
  504. /**
  505. * Get or set label of the field.
  506. *
  507. * @param null $label
  508. * @return $this|string
  509. */
  510. public function label($label = null)
  511. {
  512. if ($label == null) {
  513. return $this->label;
  514. }
  515. if ($label instanceof \Closure) {
  516. $label = $label($this->label);
  517. }
  518. $this->label = $label;
  519. return $this;
  520. }
  521. public function old()
  522. {
  523. return old($this->column, $this->value());
  524. }
  525. /**
  526. * Get original value of the field.
  527. *
  528. * @return mixed
  529. */
  530. public function original()
  531. {
  532. return $this->original;
  533. }
  534. /**
  535. * Sanitize input data.
  536. *
  537. * @param array $input
  538. * @param string $column
  539. *
  540. * @return array
  541. */
  542. protected function sanitizeInput($input, $column)
  543. {
  544. if ($this instanceof Field\MultipleSelect) {
  545. $value = Arr::get($input, $column);
  546. Arr::set($input, $column, array_filter($value));
  547. }
  548. return $input;
  549. }
  550. /**
  551. * Add html attributes to elements.
  552. *
  553. * @param array|string $attribute
  554. * @param mixed $value
  555. *
  556. * @return $this
  557. */
  558. public function attribute($attribute, $value = null)
  559. {
  560. if (is_array($attribute)) {
  561. $this->attributes = array_merge($this->attributes, $attribute);
  562. } else {
  563. $this->attributes[$attribute] = (string) $value;
  564. }
  565. return $this;
  566. }
  567. /**
  568. * Specifies a regular expression against which to validate the value of the input.
  569. *
  570. * @param string $error
  571. * @param string $regexp
  572. *
  573. * @return $this
  574. */
  575. public function pattern($regexp, $error = null)
  576. {
  577. if ($error) {
  578. $this->attribute('data-pattern-error', $error);
  579. }
  580. return $this->attribute('pattern', $regexp);
  581. }
  582. /**
  583. * set the input filed required.
  584. *
  585. * @param bool $isLabelAsterisked
  586. *
  587. * @return $this
  588. */
  589. public function required($isLabelAsterisked = true)
  590. {
  591. if ($isLabelAsterisked) {
  592. $this->setLabelClass(['asterisk']);
  593. }
  594. $this->rules('required');
  595. return $this->attribute('required', true);
  596. }
  597. /**
  598. * Set the field automatically get focus.
  599. *
  600. * @return $this
  601. */
  602. public function autofocus()
  603. {
  604. return $this->attribute('autofocus', true);
  605. }
  606. /**
  607. * Set the field as readonly mode.
  608. *
  609. * @return $this
  610. */
  611. public function readOnly()
  612. {
  613. return $this->attribute('readonly', true);
  614. }
  615. /**
  616. * Set field as disabled.
  617. *
  618. * @return $this
  619. */
  620. public function disable()
  621. {
  622. return $this->attribute('disabled', true);
  623. }
  624. /**
  625. * Set field placeholder.
  626. *
  627. * @param string $placeholder
  628. *
  629. * @return $this
  630. */
  631. public function placeholder($placeholder = '')
  632. {
  633. $this->placeholder = $placeholder;
  634. return $this;
  635. }
  636. /**
  637. * Get placeholder.
  638. *
  639. * @return string
  640. */
  641. public function getPlaceholder()
  642. {
  643. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  644. }
  645. /**
  646. * Prepare for a field value before update or insert.
  647. *
  648. * @param mixed $value
  649. * @return mixed
  650. */
  651. protected function prepareToSave($value)
  652. {
  653. return $value;
  654. }
  655. /**
  656. * @param \Closure $closure
  657. * @return $this
  658. */
  659. public function saving(\Closure $closure)
  660. {
  661. $this->prepareCallback = $closure;
  662. return $this;
  663. }
  664. /**
  665. * Prepare for a field value before update or insert.
  666. *
  667. * @param mixed $value
  668. * @return mixed
  669. */
  670. final public function prepare($value)
  671. {
  672. $value = $this->prepareToSave($value);
  673. if ($handler = $this->prepareCallback) {
  674. $handler->bindTo($this->data);
  675. return $handler($value);
  676. }
  677. return $value;
  678. }
  679. /**
  680. * Format the field attributes.
  681. *
  682. * @return string
  683. */
  684. protected function formatAttributes()
  685. {
  686. $html = [];
  687. foreach ($this->attributes as $name => $value) {
  688. $html[] = $name.'="'.e($value).'"';
  689. }
  690. return implode(' ', $html);
  691. }
  692. /**
  693. * @return $this
  694. */
  695. public function disableHorizontal()
  696. {
  697. $this->horizontal = false;
  698. return $this;
  699. }
  700. /**
  701. * @return array
  702. */
  703. public function getViewElementClasses()
  704. {
  705. if ($this->horizontal) {
  706. return [
  707. 'label' => "col-sm-{$this->width['label']} {$this->getLabelClass()}",
  708. 'field' => "col-sm-{$this->width['field']}",
  709. 'form-group' => 'form-group ',
  710. ];
  711. }
  712. return ['label' => $this->getLabelClass(), 'field' => '', 'form-group' => ''];
  713. }
  714. /**
  715. * Set form element class.
  716. *
  717. * @param string|array $class
  718. *
  719. * @return $this
  720. */
  721. public function setElementClass($class)
  722. {
  723. $this->elementClass = array_merge($this->elementClass, (array) $class);
  724. return $this;
  725. }
  726. /**
  727. * Get element class.
  728. *
  729. * @return array
  730. */
  731. protected function getElementClass()
  732. {
  733. if (!$this->elementClass) {
  734. $name = $this->elementName ?: $this->formatName($this->column);
  735. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  736. }
  737. return $this->elementClass;
  738. }
  739. /**
  740. * Get element class string.
  741. *
  742. * @return mixed
  743. */
  744. protected function getElementClassString()
  745. {
  746. $elementClass = $this->getElementClass();
  747. if (Arr::isAssoc($elementClass)) {
  748. $classes = [];
  749. foreach ($elementClass as $index => $class) {
  750. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  751. }
  752. return $classes;
  753. }
  754. return implode(' ', $elementClass);
  755. }
  756. /**
  757. * Get element class selector.
  758. *
  759. * @return string|array
  760. */
  761. protected function getElementClassSelector()
  762. {
  763. $elementClass = $this->getElementClass();
  764. $formId = $this->getFormId();
  765. $formId = $formId ? '#'.$formId : '';
  766. if (Arr::isAssoc($elementClass)) {
  767. $classes = [];
  768. foreach ($elementClass as $index => $class) {
  769. $classes[$index] = $formId . ' .'.(is_array($class) ? implode('.', $class) : $class);
  770. }
  771. return $classes;
  772. }
  773. return $formId . ' .'.implode('.', $elementClass);
  774. }
  775. /**
  776. * Remove the field in modal.
  777. *
  778. * @return $this
  779. */
  780. public function hideInModal()
  781. {
  782. if (
  783. $this->form instanceof Form
  784. && $this->form->inModal()
  785. ) {
  786. $this->setDisplay(false);
  787. }
  788. return $this;
  789. }
  790. /**
  791. * @return string|null
  792. */
  793. protected function getFormId()
  794. {
  795. return $this->form ? $this->form->getFormId() : null;
  796. }
  797. /**
  798. * Add the element class.
  799. *
  800. * @param $class
  801. *
  802. * @return $this
  803. */
  804. public function addElementClass($class)
  805. {
  806. if (is_array($class) || is_string($class)) {
  807. $this->elementClass = array_merge($this->elementClass, (array) $class);
  808. $this->elementClass = array_unique($this->elementClass);
  809. }
  810. return $this;
  811. }
  812. /**
  813. * Remove element class.
  814. *
  815. * @param $class
  816. *
  817. * @return $this
  818. */
  819. public function removeElementClass($class)
  820. {
  821. $delClass = [];
  822. if (is_string($class) || is_array($class)) {
  823. $delClass = (array) $class;
  824. }
  825. foreach ($delClass as $del) {
  826. if (($key = array_search($del, $this->elementClass))) {
  827. unset($this->elementClass[$key]);
  828. }
  829. }
  830. return $this;
  831. }
  832. /**
  833. * Add variables to field view.
  834. *
  835. * @param array $variables
  836. *
  837. * @return $this
  838. */
  839. protected function addVariables(array $variables = [])
  840. {
  841. $this->variables = array_merge($this->variables, $variables);
  842. return $this;
  843. }
  844. /**
  845. * @return string
  846. */
  847. public function getLabelClass(): string
  848. {
  849. return implode(' ', $this->labelClass);
  850. }
  851. /**
  852. * @param array $labelClass
  853. *
  854. * @return $this
  855. */
  856. public function setLabelClass(array $labelClass)
  857. {
  858. $this->labelClass = $labelClass;
  859. return $this;
  860. }
  861. /**
  862. * @return string
  863. */
  864. public function getElementName()
  865. {
  866. return $this->elementName ?: $this->formatName($this->column);
  867. }
  868. /**
  869. * Get the view variables of this field.
  870. *
  871. * @return array
  872. */
  873. public function variables()
  874. {
  875. return array_merge($this->variables, [
  876. 'id' => $this->id,
  877. 'name' => $this->getElementName(),
  878. 'help' => $this->help,
  879. 'class' => $this->getElementClassString(),
  880. 'value' => $this->value(),
  881. 'label' => $this->label,
  882. 'viewClass' => $this->getViewElementClasses(),
  883. 'column' => $this->column,
  884. 'errorKey' => $this->getErrorKey(),
  885. 'attributes' => $this->formatAttributes(),
  886. 'placeholder' => $this->getPlaceholder(),
  887. 'disabled' => $this->attributes['disabled'] ?? false,
  888. 'formId' => $this->getFormId(),
  889. ]);
  890. }
  891. /**
  892. * Get view of this field.
  893. *
  894. * @return string
  895. */
  896. public function getView()
  897. {
  898. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  899. }
  900. /**
  901. * Set view of current field.
  902. *
  903. * @return string
  904. */
  905. public function setView($view)
  906. {
  907. $this->view = $view;
  908. return $this;
  909. }
  910. /**
  911. * Get script of current field.
  912. *
  913. * @return string
  914. */
  915. public function getScript()
  916. {
  917. return $this->script;
  918. }
  919. /**
  920. * Set script of current field.
  921. *
  922. * @return self
  923. */
  924. public function setScript($script)
  925. {
  926. $this->script = $script;
  927. return $this;
  928. }
  929. /**
  930. * To set this field should render or not.
  931. *
  932. * @return self
  933. */
  934. public function setDisplay(bool $display)
  935. {
  936. $this->display = $display;
  937. return $this;
  938. }
  939. /**
  940. * If this field should render.
  941. *
  942. * @return bool
  943. */
  944. protected function shouldRender()
  945. {
  946. return $this->display;
  947. }
  948. /**
  949. * Collect assets required by this field.
  950. */
  951. public static function collectAssets()
  952. {
  953. static::$js && Admin::js(static::$js);
  954. static::$css && Admin::css(static::$css);
  955. }
  956. /**
  957. * Render this filed.
  958. *
  959. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  960. */
  961. public function render()
  962. {
  963. if (!$this->shouldRender()) {
  964. return '';
  965. }
  966. Admin::script($this->script);
  967. return view($this->getView(), $this->variables());
  968. }
  969. /**
  970. * @return string
  971. */
  972. public function __toString()
  973. {
  974. return $this->render()->render();
  975. }
  976. }