-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkupComponents.module.php
More file actions
436 lines (407 loc) · 12.8 KB
/
MarkupComponents.module.php
File metadata and controls
436 lines (407 loc) · 12.8 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php namespace ProcessWire;
/**
* Components/snippets system inspired by Kirby’s `snippet()` helper function
*
* Copyright (c) 2023 EPRC
* Licensed under MIT License, see LICENSE
*
* https://eprc.studio
*
* For ProcessWire 3.x
* Copyright (c) 2021 by Ryan Cramer
* Licensed under GNU/GPL v2
*
* https://www.processwire.com
*
*/
class MarkupComponents extends WireData implements Module, ConfigurableModule {
private WireArray $components;
private WireArray $scriptsHead;
private WireArray $scripts;
private WireArray $styles;
public function __construct() {
parent::__construct();
$this->set("autoAddAssets", 0);
$this->set("autoFuel", 0);
$this->set("fuelName", "mc");
$this->set("functionsApi", 0);
$this->set("useConfig", 0);
$this->set("useConfig", 0);
}
public function init() {
$this->components = new WireArray();
$this->scriptsHead = new WireArray();
$this->scripts = new WireArray();
$this->styles = new WireArray();
if($this->autoAddAssets) {
$this->addHookAfter("PageRender::renderPage", $this, "addAssets");
}
if($this->autoFuel) {
if($this->wire($this->fuelName)) {
$this->set("fuelError", $this->fuelName);
$this->set("fuelName", "mc");
$this->modules->saveConfig($this, "fuelName", "mc");
} else {
$this->wire($this->fuelName ?: "mc", $this);
}
}
if($this->functionsApi && $this->page->template != "admin") {
include_once __DIR__ . "/MarkupComponentsFunctions.php";
}
}
protected function addAssets(HookEvent $event) {
$parentEvent = $event->arguments(0);
$html = $parentEvent->return;
$scriptsHead = $this->printScripts(true);
$scripts = $this->printScripts();
$styles = $this->printStyles();
$html = str_replace("</head>", "{$styles}{$scriptsHead}</head>", $html);
$html = str_replace("</body>", "{$scripts}</body>", $html);
if($this->overwriteAjax && $event->config->ajax) {
header("Content-Type: application/json");
$json = [
"html" => $html,
"styles" => [...$this->styles->each(["src", "attr"])],
"scripts" => [
...$this->scriptsHead->each(["src", "attr"]),
...$this->scripts->each(["src", "attr"])
]
];
$parentEvent->return = json_encode($json);
} else {
$parentEvent->return = $html;
}
}
public function getComponents() {
return $this->components;
}
/**
* Returns the components’ name as a string, using a separator and quotes
*
* @return string
*
*/
public function listComponents($options = []) {
$defaultOptions = [
"separator" => ",",
"quote" => "\"",
"closingQuote" => "",
"prepend" => "",
"append" => ""
];
$options = array_merge($defaultOptions, $options);
if(!$options["closingQuote"]) $options["closingQuote"] = $options["quote"];
$separator = $options["closingQuote"] . $options["separator"] . $options["quote"];
return $this->components->implode($separator, "", [
"prepend" => $options["prepend"] . $options["quote"],
"append" => $options["closingQuote"] . $options["append"]
]);
}
/**
* Adds a `<script>` inside either `<head>` or `<body>` tags
*
* You can also specify attributes, e.g. `type="module"`, with an array:
* `["type" => "module"]`
*
* If an array is set as the second argument, it will be used as `$attr`
* and `$addToHead` will be set to `false`
*
* @param string $filename Filename or URL pointing to the script file
* @param bool|array $addToHead Add to `<head>`?
* @param array $attr Associative array converted into tag’s attributes
*
*/
public function script($filename, $addToHead = false, $attr = []) {
if(!$filename) return;
if(strpos($filename, "http") !== false) {
$fullPath = $filename;
} else {
if(strpos($filename, ".js") === false) {
$filename .= ".js";
}
[$path, $url] = $this->getPathAndUrl($filename);
if(!file_exists($path)) return;
$fullPath = "$url?v=" . filemtime($path);
}
if(is_array($addToHead)) {
$attr = $addToHead;
$addToHead = false;
}
if($addToHead) {
if(!$this->scriptsHead->has("src=$fullPath")) {
$this->scriptsHead->add(WireData([
"src" => $fullPath,
"attr" => $this->attrToString($attr)
]));
}
} else {
if(!$this->scripts->has("src=$fullPath")) {
$this->scripts->add(WireData([
"src" => $fullPath,
"attr" => $this->attrToString($attr)
]));
}
}
if($this->useConfig) {
$this->config->scripts->add($fullPath);
}
}
/**
* Shorter function call for `script`
*
* @param string $filename Filename or URL pointing to the script file
* @param bool|array $addToHead Add to `<head>`?
* @param array $attr Associative array converted into tag’s attributes
*
*/
public function js($filename, $addToHead = false, $attr = []) {
$this->script($filename, $addToHead, $attr);
}
/**
* @return WireArray
*
*/
public function getScripts($head = false) {
return $head ? $this->scriptsHead : $this->scripts;
}
/**
* Prints the `<script>` tags
*
* @var bool $head Print the head scripts?
* @return string
*
*/
public function printScripts($head = false) {
$str = "";
foreach($this->getScripts($head) as $script) {
$str .= "<script src=\"$script->src\" $script->attr></script>";
}
return $str;
}
/**
* Shorter function call for `printScripts`
*
* @var bool $head Print the head scripts?
* @return string
*
*/
public function scripts($head = false) {
return $this->printScripts($head);
}
/**
* Adds a `<style>` inside the `<head>` tag
*
* You can also specify attributes, e.g. `media="print"`, with an array:
* `["media" => "print"]`
*
* @param string $filename Filename or URL pointing to the style file
* @param array $attr Associative array converted into tag’s attributes
*
*/
public function style($filename, $attr = []) {
if(!$filename) return;
if(strpos($filename, "http") !== false) {
$fullPath = $filename;
} else {
if(strpos($filename, ".css") === false) {
$filename .= ".css";
}
[$path, $url] = $this->getPathAndUrl($filename);
if(!file_exists($path)) return;
$fullPath = "$url?v=" . filemtime($path);
}
if(!$this->styles->has("src=$fullPath")) {
$this->styles->add(WireData([
"src" => $fullPath,
"attr" => $this->attrToString($attr)
]));
}
if($this->useConfig) {
$this->config->styles->add($fullPath);
}
}
/**
* Shorter function call for `style`
*
* @param string $filename Filename or URL pointing to the style file
* @param array $attr Associative array converted into tag’s attributes
*
*/
public function css($filename, $attr = []) {
$this->style($filename, $attr);
}
/**
* @return WireArray
*
*/
public function getStyles() {
return $this->styles;
}
/**
* Prints the `<style>` tags
*
* @return string
*
*/
public function printStyles() {
$str = "";
foreach($this->styles as $style) {
$str .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style->src\" $style->attr>";
}
return $str;
}
/**
* Shorter function call for `printStyles`
*
* @return string
*
*/
public function styles() {
return $this->printStyles();
}
private function attrToString($attr = []) {
if(empty($attr)) return "";
if(is_string($attr)) $attr = [$attr => ""];
$str = "";
foreach($attr as $key => $value) {
if(is_int($key)) {
$str .= "$value ";
} else {
$str .= "$key=\"$value\" ";
}
}
return trim($str);
}
private function getPathAndUrl($filename = "") {
$tplPath = $this->config->paths->templates;
$tplUrl = $this->config->urls->templates;
if(strpos($filename, "site") === 0) $filename = "/$filename";
if(strpos($filename, $tplPath) !== false) {
$path = $filename;
$url = str_replace($tplPath, $tplUrl, $filename);
} elseif(strpos($filename, $tplUrl) !== false) {
$path = str_replace($tplUrl, $tplPath, $filename);
$url = $filename;
} else {
$path = "{$tplPath}$filename";
$url = "{$tplUrl}$filename";
}
return [$path, $url];
}
/**
* Render a component from /site/templates/components
*
* It will automatically add the relevant css/js files if they share the
* same name. You can specify subfolders as well, e.g. "folder/component"
*
* @param string $name
* @param array $vars Associative array of variables sent to the component
* file. The keys "attrScript" / "attrStyle" will be used as attributes for
* `<script>` and `<link>` tags
* @param bool $isSnippet
* @return string Rendered component file
* @throws WireException Thrown if the component file doesn’t exists
*
*/
public function component($name, $vars = [], $isSnippet = false) {
if(!$name) return "";
$path = $isSnippet ? "snippets" : "components";
$tpl = $this->config->paths->templates;
$folders = explode("/", $name);
$name = $folders[count($folders) - 1];
for($i = 0; $i < count($folders) - 1; $i++) {
$path .= "/$folders[$i]";
}
if(file_exists("{$tpl}$path/$name/$name.php")) {
$path .= "/$name";
}
if(!$this->components->has("$path/$name")) {
if(file_exists("{$tpl}$path/$name.js")) {
$this->script("$path/$name.js", $vars["attrScript"] ?? []);
}
if(file_exists("{$tpl}$path/$name.css")) {
$this->style("$path/$name.css", $vars["attrStyle"] ?? []);
}
$this->components->add("$path/$name");
}
return wireRenderFile("{$tpl}$path/$name.php", $vars);
}
/**
* Render a snippet from /site/templates/snippets
*
* It will automatically add the relevant css/js files if they share the
* same name. You can specify subfolders as well, e.g. "folder/snippet"
*
* @param string $name
* @param array $vars Associative array of variables sent to the component
* file. The keys "attrScript" / "attrStyle" will be used as attributes for
* `<script>` and `<link>` tags
* @return string Rendered snippet file
* @throws WireException Thrown if the snippet file doesn’t exists
*
*/
public function snippet($name = "", $vars = []) {
if(!$name) return "";
return $this->component($name, $vars, true);
}
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
$modules = $this->modules;
/** @var InputfieldCheckbox $f */
$f = $modules->get("InputfieldCheckbox");
$f->attr("name", "autoFuel");
$f->columnWidth = 50;
$f->label = $this->_("Automatically instanciate this module?");
$f->label2 = $this->_("Yes");
$f->description = $this->_("The module will be instanciated and made available in your template code through the `\$$this->fuelName` variable");
$f->checked = !!$this->autoFuel;
$inputfields->add($f);
/** @var InputfieldText $f */
$f = $modules->get("InputfieldText");
$f->attr("name", "fuelName");
$f->columnWidth = 50;
$f->label = $this->_("Customize variable name");
$f->description = $this->_("Default: `\$mc`");
$f->showIf = "autoFuel=1";
if($this->fuelError) {
$f->error($this->_("Please choose a variable name that is not already used by Processwire or another module"));
$f->attr("value", $this->fuelError);
} else {
$f->attr("value", $this->fuelName);
}
$inputfields->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get("InputfieldCheckbox");
$f->attr("name", "functionsApi");
$f->label = $this->_("Use Functions API?");
$f->label2 = $this->_("Yes");
$f->description = sprintf(
$this->_("This allows you to use the module’s functions without having instanciating it, e.g.: `component()` instead of `%s->component()`"),
$this->autoFuel ? "\$$this->fuelName" : "\$modules->get('MarkupComponents')"
);
$f->checked = !!$this->functionsApi;
$inputfields->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get("InputfieldCheckbox");
$f->attr("name", "autoAddAssets");
$f->label = $this->_("Automatically add .css and .js files on page render?");
$f->label2 = $this->_("Yes");
$f->checked = !!$this->autoAddAssets;
$inputfields->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get("InputfieldCheckbox");
$f->attr("name", "overwriteAjax");
$f->label = $this->_("Overwrite ajax calls?");
$f->label2 = $this->_("Yes");
$f->description = $this->_("If you make an ajax call (using `$.ajax()` or `fetch()`), you may use this option to overwrite the page render process and wrap its output in a json, along with the `<script>` and `<style>` added by MarkupComponents.");
$f->checked = !!$this->overwriteAjax;
$inputfields->add($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get("InputfieldCheckbox");
$f->attr("name", "useConfig");
$f->label = $this->_("Add .css and .js to \$config?");
$f->label2 = $this->_("Yes");
$f->description = $this->_("When calling `component()` or `snippet()`, associated .css and .js files are added to an internal WireArray that you can output using `printStyles()` and `printScripts()`. This option allows you to have these added to `\$config->styles` and `\$config->scripts` as well.");
$f->checked = !!$this->useConfig;
$inputfields->add($f);
}
}