Js.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Assets;
  9. use Grav\Common\Utils;
  10. /**
  11. * Class Js
  12. * @package Grav\Common\Assets
  13. */
  14. class Js extends BaseAsset
  15. {
  16. /**
  17. * Js constructor.
  18. * @param array $elements
  19. * @param string|null $key
  20. */
  21. public function __construct(array $elements = [], $key = null)
  22. {
  23. $base_options = [
  24. 'asset_type' => 'js',
  25. ];
  26. $merged_attributes = Utils::arrayMergeRecursiveUnique($base_options, $elements);
  27. parent::__construct($merged_attributes, $key);
  28. }
  29. /**
  30. * @return string
  31. */
  32. public function render()
  33. {
  34. if (isset($this->attributes['loading']) && $this->attributes['loading'] === 'inline') {
  35. $buffer = $this->gatherLinks([$this], self::JS_ASSET);
  36. return '<script' . $this->renderAttributes() . ">\n" . trim($buffer) . "\n</script>\n";
  37. }
  38. return '<script src="' . trim($this->asset) . $this->renderQueryString() . '"' . $this->renderAttributes() . $this->integrityHash($this->asset) . "></script>\n";
  39. }
  40. }