-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlpineJS.module
75 lines (63 loc) · 1.93 KB
/
AlpineJS.module
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
<?php namespace ProcessWire;
class AlpineJS extends ModuleJS implements Module
{
public static function getModuleInfo()
{
return array(
'title' => 'Alpine.js',
'version' => 005,
'href' => 'https://github.com/elabx/AlpineJS',
'autoload' => true
);
}
public function ready()
{
$config = $this->config;
if( $this->page->template == "admin") {
$this->wire()->adminTheme->addExtraMarkup('head', "<script src='{$this->getVersionUrl()}' defer></script>");
}
}
// Update when https://github.com/processwire/processwire/pull/210 is merged
public function url($path)
{
$path = $this->path($path);
$config = $this->wire->config;
return str_replace($config->paths->root, $config->urls->root, $path);
}
/**
* Given any file or directory path or url convert it to an absolute path
* @param string $path
* @return string
*/
public function path($path)
{
$path = Paths::normalizeSeparators($path);
$config = $this->wire->config;
if (strpos($path, $config->paths->root) !== 0) {
// path is not within pw root
// so we assume it is a relative path and add the pw root
$url = ltrim($path, "/");
$path = $config->paths->root . $url;
}
return $path;
}
public function getVersionUrl()
{
$class = $this->className();
$versions_path = "{$this->config->paths->$class}alpinejs/";
if (!$this->version) {
$files = $this->files->find($versions_path, [
'recursive' => 1,
'allowDirs' => true,
'returnRelative' => true
]);
if (count($files)) {
$last_version_directory = end($files);
$version_url = "{$this->config->urls->$class}alpinejs/". $last_version_directory . "alpine.js";
return $version_url;
}
} else {
return "{$this->config->urls->$class}alpinejs/{$this->version}/alpine.js";
}
}
}