-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssjs.php
314 lines (278 loc) · 8.3 KB
/
cssjs.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
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
<?php
/**
* css and js files controller
*
* Provide to include some css or js files to templates form controller
*
* @author lightsuner
* @version 1
* @see get_instance()
*/
class CssJs {
/**
* Singleton object
* @var null
*/
protected static $instance = null;
/**
* Array of css styles
*
* @var array
*/
protected $css_array = array();
/**
* Array of js files to header
*
* @var array
*/
protected $js_header_array = array();
/**
* Array of js files to footer
*
* @var array
*/
protected $js_footer_array = array();
/**
* Path to local css files
*
* @var string
*/
protected $css_path = '';
/**
* Path to js files
* @var string
*/
protected $js_path = '';
/**
* Version of css and js
*
* @var string
*/
protected $version = '?';
/**
* Protect Singleton from multi creation
*/
private function __construct() {
$ci = get_instance();
$ci->load->config('cssjs', true);
$this->js_path = base_url().$ci->config->item('js_path','cssjs');
$this->css_path = base_url().$ci->config->item('css_path','cssjs');
$this->version .= $ci->config->item('files_version','cssjs');
}
/**
* Protect Singleton from clone
*/
private function __clone() {}
/**
* Protect Singleton from wakeup
*/
private function __wakeup() {}
/**
* Get instance of this object (CssJs)
*
* @return CssJs
*/
public static function getInst() { // @return Singleton
if(is_null(static::$instance)) {
static::$instance = new static;
}
return self::$instance;
}
/**
* Add css
*
* You can use add_css('styles.css');
* add_css('.nav{display:none;}', 'inline');
* add_css('http://google.com/styles.css','external');
* OR use array
* add_css( array(
* 'styles.css',
* array('css' => '.nav{display:none;}', 'type' => 'inline'),
* array('css' => 'http://google.com/styles.css', 'type' => 'external'),
* ) );
*
*
* @param array|string $data
* @param null|external|inline $type
*
* @return CssJs
*/
public function add_css($data, $type = null) {
if(is_string($data)) {
$this->add_single_css($data, $type);
} elseif(is_array($data)) {
foreach($data as $_val) {
if(empty($_val)) {
continue;
}
if(is_string($_val)) {
$this->add_single_css($_val, $type);
} elseif(is_array($_val) && !empty($_val['css']) && isset($_val['type'])) {
$this->add_single_css($_val['css'], $_val['type']);
}
}
}
return $this;
}
/**
* Add single css to css array
*
* @param $css
* @param $type
*/
protected function add_single_css($css, $type) {
$out = '';
switch($type) {
case 'external':
$out = '<link rel="stylesheet" href="' . $css . '" type="text/css">';
break;
case 'inline':
$out = '<style type="text/css">' . $css . '</style>';
break;
default:
$link = $this->css_path . $css . $this->version;
$out = '<link rel="stylesheet" href="' . $link . '" type="text/css">';
break;
}
$this->css_array[] = $out;
}
/**
* Get all added css links
*
* @return string
*/
public function get_css() {
$out = '';
foreach($this->css_array as $_css) {
$out .= $_css.PHP_EOL;
}
return $out;
}
/**
* Add js
*
* You can use add_js('func.js');
* add_js('alert("Hellow World!");', 'inline');
* add_js('http://ajax.google.com/jquery.1.8.1.js','external');
* OR use array
* add_js( array(
* 'func.js',
* array('js' => 'alert("Hellow World!");', 'type' => 'inline', 'location' => 'header'),
* array('js' => 'http://ajax.google.com/jquery.1.8.1.js', 'type' => 'external'),
* array('js' => 'somescript.js', 'bottom' => true) - script with bottom options will be added last
* ) );
*
* @param $data
* @param null|external|inline $type
* @param footer|header $location
*
* @return bool|CssJs
*/
public function add_js($data, $type = null, $location = 'footer') {
$save_suffix = 'js_';
$save_prefix = '_array';
$save_to = $save_suffix . $location . $save_prefix;
if(!isset($this->{$save_to})) {
return FALSE;
}
if(is_string($data)) {
$this->add_single_js($data, $type, $save_to);
} elseif(is_array($data)) {
foreach($data as $_js) {
if(is_string($_js)) {
$this->add_single_js($_js, $type, $save_to);
} elseif(is_array($_js) && !empty($_js['js'])) {
$_local_save_to = $save_to;
if(isset($_js['location']) && isset($this->{$save_suffix . $_js['location'] . $save_prefix})) {
$_local_save_to = $save_suffix . $_js['location'] . $save_prefix;
}
if(!isset($_js['type'])){
$_js['type'] = null;
}
if(!isset($_js['bottom']) OR ! $_js['bottom']) {
$_js['bottom'] = false;
}
$this->add_single_js($_js['js'], $_js['type'], $_local_save_to, $_js['bottom']);
}
}
}
return $this;
}
/**
* Add js - equal controller-method path
*
* @param $controller
* @param $method
*/
public function c_js($controller = null, $method = null){
if(!$controller){
$controller = get_instance()->router->class;
}
if(!$method){
$method = get_instance()->router->method;
}
$this->add_js('controller/'.$controller.'/'.$method.'.js');
return $this;
}
/**
* Add single js file to array
*
* @param $data
* @param $type
* @param $location
*/
protected function add_single_js($js, $type, $location, $bottom = false) {
$out = '';
switch($type) {
case 'external':
$out = '<script type="text/javascript" src="' . $js . '" ></script>';
break;
case 'inline':
$out = '<script type="text/javascript">' . $js.'</script>';
break;
default:
$link = $this->js_path . $js . $this->version;
$out = '<script type="text/javascript" src="' . $link . '" ></script>';
break;
}
if ($bottom) {
$this->{$location}['bottom'][] = $out;
} else {
$this->{$location}[] = $out;
}
}
/**
* Get all js to header
*
* @return string
*/
public function get_header_js() {
$out = '';
foreach($this->js_header_array as $_js) {
if (is_array($_js)) continue;
$out .= $_js.PHP_EOL;
}
$bottom = isset($this->js_header_array['bottom']) ? $this->js_header_array['bottom'] : array();
foreach($bottom as $_js) {
$out .= $_js.PHP_EOL;
}
return $out;
}
/**
* Get all js to footer
*
* @return string
*/
public function get_footer_js() {
$out = '';
foreach($this->js_footer_array as $_js) {
if (is_array($_js)) continue;
$out .= $_js.PHP_EOL;
}
$bottom = isset($this->js_footer_array['bottom']) ? $this->js_footer_array['bottom'] : array();
foreach($bottom as $_js) {
$out .= $_js.PHP_EOL;
}
return $out;
}
}