-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageimageSourceConfig.php
More file actions
192 lines (163 loc) · 4.88 KB
/
PageimageSourceConfig.php
File metadata and controls
192 lines (163 loc) · 4.88 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php namespace ProcessWire;
/**
* Pageimage Source Configuration
*
*/
class PageimageSourceConfig extends ModuleConfig {
/**
* Returns default values for module variables
*
* @return array
*
*/
public function getDefaults() {
return [
'useLazy' => 1,
'usePicture' => 0,
'webp' => 0,
'webpQuality' => 90,
];
}
/**
* Returns inputs for module configuration
*
* @return InputfieldWrapper
*
*/
public function getInputfields() {
$config = $this->wire()->config;
$modules = $this->wire()->modules;
$inputfields = parent::getInputfields();
if($modules->isInstalled('ProcessWireAPI')) {
// Add prism for code colouring
$urlProcessWireAPI = $config->urls('ProcessWireAPI');
$config->styles->add("$urlProcessWireAPI/prism.css");
$config->scripts->add("$urlProcessWireAPI/prism.js");
}
// Get the module this configures
$module = $modules->get(str_replace('Config', '', $this->className));
$a2nl = function(array $a, $b = 1) {
return implode(str_repeat("\n", $b), $a);
};
$preview = '';
$example = '{width}x{height} {inherentwidth}w|{resolution}x';
if($module->defaultSets) {
// Get generated sets
$attr = [];
foreach($module->getSets() as $rule => $dimensions) {
$attr[] = "image.{$dimensions[0]}x{$dimensions[1]}-" . $module::suffix . ".jpg $rule";
}
// Get defaultSets as a string and order correctly
$args = explode("\n", $module->defaultSets);
usort($args, function($a, $b) {
$sanitizer = $this->wire()->sanitizer;
return $sanitizer->int(explode(' ', $a)[0]) <=> $sanitizer->int(explode(' ', $b)[0]);
});
$preview .= $a2nl([
'// ' . $this->_('Sets generated'),
$a2nl($attr),
'',
'// ' . $this->_('Equivalent method call'),
'$srcset = $pageimage->srcset("'. implode(", ", $args) . '");',
'',
]);
} else {
$preview .= $a2nl([
'/*',
$this->_('Please configure the default set rules') . '.',
$this->_('Set rules should use the following format') . ':',
$example,
$this->_('Only `width` is required') . '.',
$this->_('Example') . ':',
$a2nl(['320', '640', '768x480 960w', '1024', '2048 2x']),
'*/',
], 2);
}
$inputfields->add([
'type' => 'textarea',
'name' => 'defaultSets',
'label' => $this->_('Default Set Rules'),
'placeholder' => $example,
'required' => !$this->wire()->input->post->bool('uninstall'),
'notes' => $this->_('Each set rule should be entered on a new line.'),
'icon' => 'arrows-alt',
'rows' => substr_count($preview, "\n"), // Adjust the textarea based on preview
'columnWidth' => 50,
]);
$inputfields->add([
'type' => 'markup',
'name' => 'previewSets',
'label' => $this->_('Preview'),
'value' => "<pre class=language-php><code class=language-php>$preview</code></pre>",
'notes' => $this->_('A set rule will only be used if the original image is wider or higher than the set dimensions.'),
'icon' => 'eye',
'columnWidth' => 50,
]);
$inputfields->add([
'type' => 'checkbox',
'name' => 'allSets',
'label' => $this->_('Use for all dimensions?'),
'value' => 1,
'notes' => $this->_('If enabled, a set rule will be used regardless of whether it is wider or higher than the dimensions of the original image.'),
'icon' => 'object-group',
'collapsed' => 2,
]);
$toggle = $modules->isInstalled('InputfieldToggle') ? 'toggle' : 'checkbox';
// WebP
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->label = $this->_('WebP');
$fieldset->icon = 'picture-o';
// WebP
$fieldset->add([
'type' => $toggle,
'name' => 'webp',
'label' => $this->_('Enable WebP Images?'),
'icon' => 'toggle-on',
'columnWidth' => 50,
]);
// WebP Quality
$fieldset->add([
'type' => 'integer',
'name' => 'webpQuality',
'label' => $this->_('WebP Quality'),
'icon' => 'search-plus',
'columnWidth' => 50,
'showIf' => 'webp=1',
]);
$inputfields->add($fieldset);
// Rendering
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->label = $this->_('Rendering');
$fieldset->icon = 'picture-o';
// Lazy loading
$fieldset->add([
'type' => $toggle,
'name' => 'useLazy',
'label' => $this->_('Use Lazy Loading?'),
'icon' => 'eye',
'columnWidth' => 50,
]);
// Use <picture>
$fieldset->add([
'type' => $toggle,
'name' => 'usePicture',
'label' => sprintf($this->_('Use the %s element?'), '<picture>'),
'icon' => 'code',
'showIf' => 'webp=1',
'columnWidth' => 50,
]);
$inputfields->add($fieldset);
// Remove Variations
$inputfields->add([
'type' => 'checkbox',
'name' => 'removeVariations',
'label' => $this->_('Remove Variations'),
'description' => $this->_('When checked, all variations generated by the module will be removed.'),
'notes' => $this->_('This may take a long time on large sites.'),
'value' => null,
'icon' => 'trash',
'collapsed' => 1,
]);
return $inputfields;
}
}