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