featherlight.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Grav\Plugin;
  3. use \Grav\Common\Plugin;
  4. use \Grav\Common\Grav;
  5. use \Grav\Common\Page\Page;
  6. class FeatherlightPlugin extends Plugin
  7. {
  8. protected $active = false;
  9. /**
  10. * @return array
  11. */
  12. public static function getSubscribedEvents()
  13. {
  14. return [
  15. 'onPluginsInitialized' => ['onPluginsInitialized', 0]
  16. ];
  17. }
  18. /**
  19. * Initialize configuration
  20. */
  21. public function onPluginsInitialized()
  22. {
  23. if ($this->isAdmin()) {
  24. $this->active = false;
  25. return;
  26. }
  27. $this->enable([
  28. 'onPageInitialized' => ['onPageInitialized', 0]
  29. ]);
  30. }
  31. /**
  32. * Initialize configuration
  33. */
  34. public function onPageInitialized()
  35. {
  36. $defaults = (array) $this->config->get('plugins.featherlight');
  37. /** @var Page $page */
  38. $page = $this->grav['page'];
  39. if (isset($page->header()->featherlight)) {
  40. $this->config->set('plugins.featherlight', array_merge($defaults, $page->header()->featherlight));
  41. }
  42. // take the old legacy `lightbox: true` setting into account
  43. if (isset($page->header()->lightbox) && $page->header()->lightbox == true) {
  44. $legacy = true;
  45. } else {
  46. $legacy = false;
  47. }
  48. $this->active = $this->config->get('plugins.featherlight.active') || $legacy;
  49. if ($this->active) {
  50. $this->enable([
  51. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  52. ]);
  53. }
  54. }
  55. /**
  56. * if enabled on this page, load the JS + CSS theme.
  57. */
  58. public function onTwigSiteVariables()
  59. {
  60. $config = $this->config->get('plugins.featherlight');
  61. if ($config['requirejs']) {
  62. $init = "define(\"featherlight\",['jquery', 'plugin/featherlight/js/featherlight.min' ], function($){
  63. var Lightbox = {
  64. Init : function() {
  65. $('a[rel=\"lightbox\"]').featherlight({
  66. openSpeed: {$config['openSpeed']},
  67. closeSpeed: {$config['closeSpeed']},
  68. closeOnClick: '{$config['closeOnClick']}',
  69. closeOnEsc: '{$config['closeOnEsc']}',
  70. root: '{$config['root']}'
  71. });
  72. }
  73. };
  74. return Lightbox;
  75. });";
  76. $this->grav['assets']
  77. ->addCss('plugin://featherlight/css/featherlight.min.css')
  78. ->addInlineJs($init);
  79. } elseif ($config['gallery']) {
  80. $init = $this->getInitJs($config);
  81. $this->grav['assets']
  82. ->addCss('plugin://featherlight/css/featherlight.min.css')
  83. ->addCss('plugin://featherlight/css/featherlight.gallery.min.css')
  84. ->add('jquery', 101)
  85. ->addJs('plugin://featherlight/js/featherlight.min.js')
  86. ->addJs('plugin://featherlight/js/featherlight.gallery.min.js')
  87. ->addInlineJs($init);
  88. } else {
  89. $init = $this->getInitJs($config);
  90. $this->grav['assets']
  91. ->addCss('plugin://featherlight/css/featherlight.min.css')
  92. ->add('jquery', 101)
  93. ->addJs('plugin://featherlight/js/featherlight.min.js')
  94. ->addInlineJs($init);
  95. }
  96. }
  97. protected function getInitJs($config) {
  98. $asset = $this->grav['locator']->findResource($config['initTemplate'], false);
  99. $init = file_get_contents(ROOT_DIR . $asset);
  100. $init = str_replace(
  101. array('{pluginName}', '{openSpeed}', '{closeSpeed}', '{closeOnClick}', '{closeOnEsc}', '{root}'),
  102. array(
  103. $config['gallery'] ? 'featherlightGallery' : 'featherlight',
  104. $config['openSpeed'],
  105. $config['closeSpeed'],
  106. $config['closeOnClick'],
  107. $config['closeOnEsc'],
  108. $config['root']
  109. ),
  110. $init
  111. );
  112. return $init;
  113. }
  114. }