Lazy.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Contracts\LazyRenderable;
  4. use Dcat\Admin\Traits\InteractsWithRenderApi;
  5. use Illuminate\Support\Str;
  6. class Lazy extends Widget
  7. {
  8. use InteractsWithRenderApi;
  9. protected $target = 'lazy';
  10. protected $load = true;
  11. public function __construct(LazyRenderable $renderable = null, bool $load = true)
  12. {
  13. $this->setRenderable($renderable);
  14. $this->load($load);
  15. $this->class($this->elementClass = 'lazy-box');
  16. }
  17. /**
  18. * 设置是否立即加载.
  19. *
  20. * @param bool $value
  21. *
  22. * @return $this
  23. */
  24. public function load(bool $value)
  25. {
  26. $this->load = $value;
  27. return $this;
  28. }
  29. protected function addScript()
  30. {
  31. $loader = $this->load ? "target.trigger('{$this->target}:load')" : '';
  32. $this->script = <<<JS
  33. Dcat.init('{$this->getElementSelector()}', function (target) {
  34. var body = target;
  35. {$this->getRenderableScript()}
  36. body.html('<div style="min-height:150px"></div>').loading();
  37. {$loader}
  38. });
  39. JS;
  40. }
  41. public function html()
  42. {
  43. $this->addScript();
  44. return <<<HTML
  45. <div {$this->formatHtmlAttributes()}></div>
  46. HTML;
  47. }
  48. }