Color.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. class Color extends Text
  5. {
  6. protected static $css = [
  7. '@admin/dcat/plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css',
  8. ];
  9. protected static $js = [
  10. '@admin/dcat/plugins/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js',
  11. ];
  12. protected $view = 'admin::form.color';
  13. /**
  14. * Use `hex` format.
  15. *
  16. * @return $this
  17. */
  18. public function hex()
  19. {
  20. return $this->options(['format' => 'hex']);
  21. }
  22. /**
  23. * Use `rgb` format.
  24. *
  25. * @return $this
  26. */
  27. public function rgb()
  28. {
  29. return $this->options(['format' => 'rgb']);
  30. }
  31. /**
  32. * Use `rgba` format.
  33. *
  34. * @return $this
  35. */
  36. public function rgba()
  37. {
  38. return $this->options(['format' => 'rgba']);
  39. }
  40. protected function addScript()
  41. {
  42. $options = json_encode($this->options);
  43. $this->script = <<<JS
  44. $('{$this->getElementClassSelector()}').colorpicker($options).on('colorpickerChange', function(event) {
  45. $(this).parents('.input-group').find('.input-group-prepend i').css('background-color', event.color.toString());
  46. });
  47. JS;
  48. }
  49. /**
  50. * Render this filed.
  51. *
  52. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  53. */
  54. public function render()
  55. {
  56. Admin::style('.popover{z-index:29891015}');
  57. $this->addScript();
  58. $this->defaultAttribute('style', 'width: 160px;flex:none');
  59. return parent::render();
  60. }
  61. }