-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoader.php
More file actions
130 lines (113 loc) · 3.89 KB
/
AssetLoader.php
File metadata and controls
130 lines (113 loc) · 3.89 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
<?php
/*
@CODOLICENSE
*/
/**
* Loads all static assets - css & javascript
* Class AssetLoader
*/
require_once 'ITheme.php';
class AssetLoader implements ITheme
{
protected $asset;
protected $headCollection;
protected $bodyCollection;
/**
* Adds all required javascript libraries that are common.
* Page specific javascripts are defined in respective controllers
* @return mixed
*/
function addCommonJs()
{
$this->headCollection->addJS('jquery-3.2.1.min.js', array('type' => 'defer'))
->addJS('popper.min.js', array('type' => 'defer'))
->addJS('bootstrap.min.js', array('type' => 'defer'))
->addJS('fastclick.js', array('type' => 'defer'))
->addJS('notify.js', array('type' => 'defer'))
->addJS('app.js', array('type' => 'defer'))
->addJS('jquery.mmenu.min.js', array('type' => 'defer'))
->addJS('jquery.mmenu.dragopen.min.js', array('type' => 'defer'))
->addJS('hammer.min.js', array('type' => 'defer'));
}
function addCommonCss()
{
$globalLessFiles = array('mixins', 'bootstrap', 'font-awesome', 'general', 'search', 'jquery.mmenu', 'jquery.mmenu.dragopen',
'material-design');
$path = DEF_THEME_DIR . 'less';
foreach ($globalLessFiles as $file) {
$this->headCollection->addCSS("$path/$file.less");
}
}
/**
* Adds page specific less files defined in controller
* @param $pageCssFiles
* @param $themesByInheritance
*/
function addPageSpecificCss($pageCssFiles, $themesByInheritance)
{
if ($pageCssFiles == null) $pageCssFiles = array();
foreach ($pageCssFiles as $file) {
foreach ($themesByInheritance as $theme) {
$this->headCollection->addCSS(THEME_DIR . "$theme/less/$file.less");
}
}
}
/**
* Adds custom css files: colors.less
* @param $themesByInheritance
*/
function addColorDefinitions($themesByInheritance)
{
foreach ($themesByInheritance as $theme) {
$this->headCollection->addCSS(THEME_DIR . "$theme/less/colors.less");
}
}
/**
* Adds any common deferred js files
* @return mixed
*/
function addDeferredJs()
{
$this->bodyCollection->addJS('handlebars-v1.1.2.js', array('type' => 'defer'));
}
/**
* Adds any page specific js files defined in controller
* @return mixed
*/
function addPageSpecificJs($pageJsFiles)
{
if ($pageJsFiles === null) $pageJsFiles = array();
foreach ($pageJsFiles as $js_file) {
if (is_array($js_file)) {
$this->bodyCollection->addJS($js_file[0], $js_file[1]);
} else {
$this->bodyCollection->addJS($js_file);
}
}
}
/**
* Everything in constructor because all methods are always required
* to be executed.
* AssetLoader constructor.
* @param $pageCssFiles
* @param $pageJsFiles
*/
function __construct($pageCssFiles, $pageJsFiles)
{
$this->asset = new CODOF\Asset\Stream();
$this->headCollection = new \CODOF\Asset\Collection('head_col');
$this->bodyCollection = new \CODOF\Asset\Collection('head_col');
$this->bodyCollection->position = 'body';
$themesByInheritance = array_reverse(\CODOF\Util::getThemesByInhereitance());
//Add everything that goes in head
$this->addCommonJs();
$this->addCommonCss();
$this->addPageSpecificCss($pageCssFiles, $themesByInheritance);
$this->addColorDefinitions($themesByInheritance);
$this->asset->addCollection($this->headCollection);
//Add everything that goes in body
$this->addDeferredJs();
$this->addPageSpecificJs($pageJsFiles);
$this->asset->addCollection($this->bodyCollection);
}
}