Field.php 22 KB

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