Tooltip.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Contracts\Support\Renderable;
  5. class Tooltip extends Widget
  6. {
  7. protected static $style = '.tooltip-inner{padding:7px 13px;border-radius:2px;font-size:13px;max-width:250px}';
  8. protected $selector;
  9. protected $title;
  10. protected $background;
  11. protected $maxWidth = 210;
  12. protected $placement = 1;
  13. protected $built;
  14. public function __construct(string $selector = '')
  15. {
  16. $this->selector($selector);
  17. $this->autoRender();
  18. }
  19. public function selector(string $selector)
  20. {
  21. $this->selector = $selector;
  22. return $this;
  23. }
  24. public function maxWidth(int $width)
  25. {
  26. $this->maxWidth = $width;
  27. return $this;
  28. }
  29. /**
  30. * @param string|Renderable|\Closure $content
  31. *
  32. * @return $this
  33. */
  34. public function title($content)
  35. {
  36. $this->title = $this->toString($content);
  37. return $this;
  38. }
  39. public function background(string $color)
  40. {
  41. $this->background = $color;
  42. return $this;
  43. }
  44. public function green()
  45. {
  46. return $this->background(Admin::color()->success());
  47. }
  48. public function blue()
  49. {
  50. return $this->background(Admin::color()->blue());
  51. }
  52. public function red()
  53. {
  54. return $this->background(Admin::color()->danger());
  55. }
  56. public function purple()
  57. {
  58. return $this->background(Admin::color()->purple());
  59. }
  60. public function left()
  61. {
  62. return $this->placement('left');
  63. }
  64. public function right()
  65. {
  66. return $this->placement('right');
  67. }
  68. public function top()
  69. {
  70. return $this->placement('top');
  71. }
  72. public function bottom()
  73. {
  74. return $this->placement('bottom');
  75. }
  76. public function placement(string $placement = 'auto')
  77. {
  78. $map = [
  79. 'top' => 1,
  80. 'right' => 2,
  81. 'bottom' => 3,
  82. 'left' => 4,
  83. ];
  84. $this->placement = $map[$placement] ?? 1;
  85. return $this;
  86. }
  87. protected function setupScript()
  88. {
  89. $background = $this->background ?: Admin::color()->primary(-5);
  90. $title = $this->title;
  91. Admin::script(
  92. <<<JS
  93. $('{$this->selector}').on('mouseover', function () {
  94. var title = '{$title}' || $(this).data('title');
  95. var idx = layer.tips(title, this, {
  96. tips: ['{$this->placement}', '{$background}'],
  97. time: 0,
  98. maxWidth: {$this->maxWidth},
  99. });
  100. $(this).attr('layer-idx', idx);
  101. }).on('mouseleave', function () {
  102. layer.close($(this).attr('layer-idx'));
  103. $(this).attr('layer-idx', '');
  104. });
  105. JS
  106. );
  107. }
  108. public function render()
  109. {
  110. if ($this->built) {
  111. return;
  112. }
  113. $this->built = true;
  114. $this->setupScript();
  115. }
  116. }