1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\View; |
5
|
|
|
|
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Yiisoft\Asset\AssetBundle; |
8
|
|
|
use Yiisoft\Asset\AssetManager; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
11
|
|
|
use Yiisoft\View\Event\BodyBegin; |
12
|
|
|
use Yiisoft\View\Event\BodyEnd; |
13
|
|
|
use Yiisoft\View\Event\PageEnd; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* View represents a view object in the MVC pattern. |
17
|
|
|
* |
18
|
|
|
* View provides a set of methods (e.g. {@see render()} for rendering purpose. |
19
|
|
|
* |
20
|
|
|
* You can modify its configuration by adding an array to your application config under `components` as it is shown in |
21
|
|
|
* the following example: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* 'view' => [ |
25
|
|
|
* 'theme' => 'app\themes\MyTheme', |
26
|
|
|
* 'renderers' => [ |
27
|
|
|
* // you may add Smarty or Twig renderer here |
28
|
|
|
* ] |
29
|
|
|
* // ... |
30
|
|
|
* ] |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* For more details and usage information on View, see the [guide article on views](guide:structure-views). |
34
|
|
|
*/ |
35
|
|
|
class WebView extends View |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* The location of registered JavaScript code block or files. |
39
|
|
|
* This means the location is in the head section. |
40
|
|
|
*/ |
41
|
|
|
public const POS_HEAD = 1; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The location of registered JavaScript code block or files. |
45
|
|
|
* This means the location is at the beginning of the body section. |
46
|
|
|
*/ |
47
|
|
|
public const POS_BEGIN = 2; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The location of registered JavaScript code block or files. |
51
|
|
|
* This means the location is at the end of the body section. |
52
|
|
|
*/ |
53
|
|
|
public const POS_END = 3; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The location of registered JavaScript code block. |
57
|
|
|
* This means the JavaScript code block will be executed when HTML document composition is ready. |
58
|
|
|
*/ |
59
|
|
|
public const POS_READY = 4; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The location of registered JavaScript code block. |
63
|
|
|
* This means the JavaScript code block will be executed when HTML page is completely loaded. |
64
|
|
|
*/ |
65
|
|
|
public const POS_LOAD = 5; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This is internally used as the placeholder for receiving the content registered for the head section. |
69
|
|
|
*/ |
70
|
|
|
private const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>'; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This is internally used as the placeholder for receiving the content registered for the beginning of the body |
74
|
|
|
* section. |
75
|
|
|
*/ |
76
|
|
|
private const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* This is internally used as the placeholder for receiving the content registered for the end of the body section. |
80
|
|
|
*/ |
81
|
|
|
private const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values |
85
|
|
|
* are the registered {@see AssetBundle} objects. |
86
|
|
|
* |
87
|
|
|
* {@see registerAssetBundle()} |
88
|
|
|
*/ |
89
|
|
|
private $assetBundles = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var AssetManager $assetManager |
93
|
|
|
*/ |
94
|
|
|
private $assetManager; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var string the page title |
98
|
|
|
*/ |
99
|
|
|
private $title; |
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var array the registered meta tags. |
103
|
|
|
* |
104
|
|
|
* {@see registerMetaTag()} |
105
|
|
|
*/ |
106
|
|
|
private $metaTags = []; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var array the registered link tags. |
110
|
|
|
* |
111
|
|
|
* {@see registerLinkTag()} |
112
|
|
|
*/ |
113
|
|
|
private $linkTags = []; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var array the registered CSS code blocks. |
117
|
|
|
* |
118
|
|
|
* {@see registerCss()} |
119
|
|
|
*/ |
120
|
|
|
private $css = []; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var array the registered CSS files. |
124
|
|
|
* |
125
|
|
|
* {@see registerCssFile()} |
126
|
|
|
*/ |
127
|
|
|
private $cssFiles = []; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var array the registered JS code blocks |
131
|
|
|
* |
132
|
|
|
* {@see registerJs()} |
133
|
|
|
*/ |
134
|
|
|
private $js = []; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var array the registered JS files. |
138
|
|
|
* |
139
|
|
|
* {@see registerJsFile()} |
140
|
|
|
*/ |
141
|
|
|
private $jsFiles = []; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Marks the position of an HTML head section. |
145
|
|
|
*/ |
146
|
32 |
|
public function head(): void |
147
|
|
|
{ |
148
|
32 |
|
echo self::PH_HEAD; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Marks the beginning of an HTML body section. |
153
|
|
|
*/ |
154
|
32 |
|
public function beginBody(): void |
155
|
|
|
{ |
156
|
32 |
|
echo self::PH_BODY_BEGIN; |
157
|
32 |
|
$this->eventDispatcher->dispatch(new BodyBegin($this->getViewFile())); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Marks the ending of an HTML body section. |
162
|
|
|
*/ |
163
|
32 |
|
public function endBody(): void |
164
|
|
|
{ |
165
|
32 |
|
$this->eventDispatcher->dispatch(new BodyEnd($this->getViewFile())); |
|
|
|
|
166
|
32 |
|
echo self::PH_BODY_END; |
167
|
|
|
|
168
|
32 |
|
foreach (array_keys($this->assetBundles) as $bundle) { |
169
|
8 |
|
$this->registerAssetFiles($bundle); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Marks the ending of an HTML page. |
175
|
|
|
* |
176
|
|
|
* @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
177
|
|
|
* {@see POS_READY} and {@see POS_LOAD} positions will be rendered at the end of the view like |
178
|
|
|
* normal scripts. |
179
|
|
|
*/ |
180
|
32 |
|
public function endPage($ajaxMode = false): void |
181
|
|
|
{ |
182
|
32 |
|
$this->eventDispatcher->dispatch(new PageEnd($this->getViewFile())); |
|
|
|
|
183
|
|
|
|
184
|
32 |
|
$content = ob_get_clean(); |
185
|
|
|
|
186
|
32 |
|
echo strtr($content, [ |
187
|
32 |
|
self::PH_HEAD => $this->renderHeadHtml(), |
188
|
32 |
|
self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
189
|
32 |
|
self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
190
|
|
|
]); |
191
|
|
|
|
192
|
32 |
|
$this->clear(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Renders a view in response to an AJAX request. |
197
|
|
|
* |
198
|
|
|
* This method is similar to {@see render()} except that it will surround the view being rendered with the calls of |
199
|
|
|
* {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the |
200
|
|
|
* method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the |
201
|
|
|
* view. |
202
|
|
|
* |
203
|
|
|
* @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
204
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view |
205
|
|
|
* file. |
206
|
|
|
* @param object $context the context that the view should use for rendering the view. If null, existing [[context]] |
207
|
|
|
* will be used. |
208
|
|
|
* |
209
|
|
|
* @return string the rendering result |
210
|
|
|
* |
211
|
|
|
* {@see render()} |
212
|
|
|
*/ |
213
|
|
|
public function renderAjax(string $view, array $params = [], $context = null): string |
214
|
|
|
{ |
215
|
|
|
$viewFile = $this->findViewFile($view, $context); |
|
|
|
|
216
|
|
|
|
217
|
|
|
ob_start(); |
218
|
|
|
ob_implicit_flush(0); |
219
|
|
|
|
220
|
|
|
$this->beginPage(); |
221
|
|
|
$this->head(); |
222
|
|
|
$this->beginBody(); |
223
|
|
|
echo $this->renderFile($viewFile, $params, $context); |
224
|
|
|
$this->endBody(); |
225
|
|
|
$this->endPage(true); |
226
|
|
|
|
227
|
|
|
return ob_get_clean(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Registers the asset manager being used by this view object. |
232
|
|
|
* |
233
|
|
|
* @return array the asset manager. Defaults to the "assetManager" application component. |
234
|
|
|
*/ |
235
|
10 |
|
public function getAssetBundles(): array |
236
|
|
|
{ |
237
|
10 |
|
return $this->assetBundles; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Registers the asset manager being used by this view object. |
242
|
|
|
* |
243
|
|
|
* @return AssetManager the asset manager. Defaults to the "assetManager" application component. |
244
|
|
|
*/ |
245
|
33 |
|
public function getAssetManager(): AssetManager |
246
|
|
|
{ |
247
|
33 |
|
return $this->assetManager; |
248
|
|
|
} |
249
|
|
|
|
250
|
27 |
|
public function getEventDispatcher(): EventDispatcherInterface |
251
|
|
|
{ |
252
|
27 |
|
return $this->eventDispatcher; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Sets the asset manager. |
257
|
|
|
* |
258
|
|
|
* @param AssetManager $value the asset manager |
259
|
|
|
* |
260
|
|
|
* @return void |
261
|
|
|
*/ |
262
|
81 |
|
public function setAssetManager(AssetManager $value): void |
263
|
|
|
{ |
264
|
81 |
|
$this->assetManager = $value; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Clears up the registered meta tags, link tags, css/js scripts and files. |
269
|
|
|
* |
270
|
|
|
* @return void |
271
|
|
|
*/ |
272
|
32 |
|
public function clear(): void |
273
|
|
|
{ |
274
|
32 |
|
$this->metaTags = []; |
275
|
32 |
|
$this->linkTags = []; |
276
|
32 |
|
$this->css = []; |
277
|
32 |
|
$this->cssFiles = []; |
278
|
32 |
|
$this->js = []; |
279
|
32 |
|
$this->jsFiles = []; |
280
|
32 |
|
$this->assetBundles = []; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Registers all files provided by an asset bundle including depending bundles files. |
285
|
|
|
* |
286
|
|
|
* Removes a bundle from {@see assetBundles} once files are registered. |
287
|
|
|
* |
288
|
|
|
* @param string $name name of the bundle to register |
289
|
|
|
* |
290
|
|
|
* @return void |
291
|
|
|
*/ |
292
|
8 |
|
protected function registerAssetFiles(string $name): void |
293
|
|
|
{ |
294
|
8 |
|
if (!isset($this->assetBundles[$name])) { |
295
|
7 |
|
return; |
296
|
|
|
} |
297
|
|
|
|
298
|
8 |
|
$bundle = $this->assetBundles[$name]; |
299
|
|
|
|
300
|
8 |
|
if ($bundle) { |
|
|
|
|
301
|
8 |
|
foreach ($bundle->depends as $dep) { |
302
|
7 |
|
$this->registerAssetFiles($dep); |
303
|
|
|
} |
304
|
8 |
|
$bundle->registerAssetFiles($this); |
305
|
|
|
} |
306
|
|
|
|
307
|
8 |
|
unset($this->assetBundles[$name]); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Registers the named asset bundle. |
312
|
|
|
* |
313
|
|
|
* All dependent asset bundles will be registered. |
314
|
|
|
* |
315
|
|
|
* @param string $name the class name of the asset bundle (without the leading backslash) |
316
|
|
|
* @param int|null $position if set, this forces a minimum position for javascript files. This will adjust depending |
317
|
|
|
* assets javascript file position or fail if requirement can not be met. If this is null, asset |
318
|
|
|
* bundles position settings will not be changed. |
319
|
|
|
* |
320
|
|
|
* {@see registerJsFile()} for more details on javascript position. |
321
|
|
|
* |
322
|
|
|
* @throws \RuntimeException if the asset bundle does not exist or a circular dependency is detected |
323
|
|
|
* |
324
|
|
|
* @return AssetBundle the registered asset bundle instance |
325
|
|
|
*/ |
326
|
12 |
|
public function registerAssetBundle(string $name, ?int $position = null): AssetBundle |
327
|
|
|
{ |
328
|
12 |
|
if (!isset($this->assetBundles[$name])) { |
329
|
12 |
|
$bundle = $this->getAssetManager()->getBundle($name); |
330
|
|
|
|
331
|
12 |
|
$this->assetBundles[$name] = false; |
332
|
|
|
|
333
|
|
|
// register dependencies |
334
|
|
|
|
335
|
12 |
|
$pos = $bundle->jsOptions['position'] ?? null; |
336
|
|
|
|
337
|
12 |
|
foreach ($bundle->depends as $dep) { |
338
|
10 |
|
$this->registerAssetBundle($dep, $pos); |
339
|
|
|
} |
340
|
|
|
|
341
|
11 |
|
$this->assetBundles[$name] = $bundle; |
342
|
8 |
|
} elseif ($this->assetBundles[$name] === false) { |
|
|
|
|
343
|
1 |
|
throw new \RuntimeException("A circular dependency is detected for bundle '$name'."); |
344
|
|
|
} else { |
345
|
7 |
|
$bundle = $this->assetBundles[$name]; |
346
|
|
|
} |
347
|
|
|
|
348
|
11 |
|
if ($position !== null) { |
349
|
8 |
|
$pos = $bundle->jsOptions['position'] ?? null; |
350
|
|
|
|
351
|
8 |
|
if ($pos === null) { |
352
|
8 |
|
$bundle->jsOptions['position'] = $pos = $position; |
353
|
2 |
|
} elseif ($pos > $position) { |
354
|
2 |
|
throw new \RuntimeException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
// update position for all dependencies |
358
|
8 |
|
foreach ($bundle->depends as $dep) { |
359
|
6 |
|
$this->registerAssetBundle($dep, $pos); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
11 |
|
return $bundle; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Registers a meta tag. |
368
|
|
|
* |
369
|
|
|
* For example, a description meta tag can be added like the following: |
370
|
|
|
* |
371
|
|
|
* ```php |
372
|
|
|
* $view->registerMetaTag([ |
373
|
|
|
* 'name' => 'description', |
374
|
|
|
* 'content' => 'This website is about funny raccoons.' |
375
|
|
|
* ]); |
376
|
|
|
* ``` |
377
|
|
|
* |
378
|
|
|
* will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
379
|
|
|
* |
380
|
|
|
* @param array $options the HTML attributes for the meta tag. |
381
|
|
|
* @param string $key the key that identifies the meta tag. If two meta tags are registered with the same key, the |
382
|
|
|
* latter will overwrite the former. If this is null, the new meta tag will be appended to the |
383
|
|
|
* existing ones. |
384
|
|
|
* |
385
|
|
|
* @return void |
386
|
|
|
*/ |
387
|
|
|
public function registerMetaTag(array $options, string $key = null): void |
388
|
|
|
{ |
389
|
|
|
if ($key === null) { |
390
|
|
|
$this->metaTags[] = Html::tag('meta', '', $options); |
391
|
|
|
} else { |
392
|
|
|
$this->metaTags[$key] = Html::tag('meta', '', $options); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Registers a link tag. |
398
|
|
|
* |
399
|
|
|
* For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the |
400
|
|
|
* following: |
401
|
|
|
* |
402
|
|
|
* ```php |
403
|
|
|
* $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
404
|
|
|
* ``` |
405
|
|
|
* |
406
|
|
|
* which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
407
|
|
|
* |
408
|
|
|
* **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which has more options |
409
|
|
|
* for this kind of link tag. |
410
|
|
|
* |
411
|
|
|
* @param array $options the HTML attributes for the link tag. |
412
|
|
|
* @param string|null $key the key that identifies the link tag. If two link tags are registered with the same |
413
|
|
|
* key, the latter will overwrite the former. If this is null, the new link tag will be appended |
414
|
|
|
* to the existing ones. |
415
|
|
|
*/ |
416
|
|
|
public function registerLinkTag(array $options, ?string $key = null): void |
417
|
|
|
{ |
418
|
|
|
if ($key === null) { |
419
|
|
|
$this->linkTags[] = Html::tag('link', '', $options); |
420
|
|
|
} else { |
421
|
|
|
$this->linkTags[$key] = Html::tag('link', '', $options); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Registers CSRF meta tags. |
427
|
|
|
* |
428
|
|
|
* They are rendered dynamically to retrieve a new CSRF token for each request. |
429
|
|
|
* |
430
|
|
|
* ```php |
431
|
|
|
* $view->registerCsrfMetaTags(); |
432
|
|
|
* ``` |
433
|
|
|
* |
434
|
|
|
* The above code will result in `<meta name="csrf-param" content="[Yiisoft\Web\Request::$csrfParam]">` and |
435
|
|
|
* `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page. |
436
|
|
|
* |
437
|
|
|
* Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` |
438
|
|
|
* from `yii.js`. |
439
|
|
|
*/ |
440
|
|
|
public function registerCsrfMetaTags(): void |
441
|
|
|
{ |
442
|
|
|
$this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return Yiisoft\Html\Html::csrfMetaTags();'); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Registers a CSS code block. |
447
|
|
|
* |
448
|
|
|
* @param string $css the content of the CSS code block to be registered |
449
|
|
|
* @param array $options the HTML attributes for the `<style>`-tag. |
450
|
|
|
* @param string $key the key that identifies the CSS code block. If null, it will use $css as the key. If two CSS |
451
|
|
|
* code blocks are registered with the same key, the latter will overwrite the former. |
452
|
|
|
*/ |
453
|
|
|
public function registerCss(string $css, array $options = [], string $key = null): void |
454
|
|
|
{ |
455
|
|
|
$key = $key ?: md5($css); |
456
|
|
|
$this->css[$key] = Html::style($css, $options); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Registers a CSS file. |
461
|
|
|
* |
462
|
|
|
* This method should be used for simple registration of CSS files. If you want to use features of |
463
|
|
|
* {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle} |
464
|
|
|
* and {@see registerAssetBundle()} instead. |
465
|
|
|
* |
466
|
|
|
* @param string $url the CSS file to be registered. |
467
|
|
|
* @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} |
468
|
|
|
* for the supported options. The following options are specially handled and are not treated as HTML |
469
|
|
|
* attributes: |
470
|
|
|
* |
471
|
|
|
* - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
472
|
|
|
* |
473
|
|
|
* @param string $key the key that identifies the CSS script file. If null, it will use $url as the key. If two CSS |
474
|
|
|
* files are registered with the same key, the latter will overwrite the former. |
475
|
|
|
* |
476
|
|
|
* @return void |
477
|
|
|
*/ |
478
|
18 |
|
public function registerCssFile(string $url, array $options = [], string $key = null): void |
479
|
|
|
{ |
480
|
18 |
|
$key = $key ?: $url; |
481
|
|
|
|
482
|
18 |
|
$depends = ArrayHelper::remove($options, 'depends', []); |
483
|
|
|
|
484
|
18 |
|
if (empty($depends)) { |
485
|
18 |
|
$this->cssFiles[$key] = Html::cssFile($url, $options); |
486
|
|
|
} else { |
487
|
|
|
$bundle = $this->createBundle([ |
488
|
|
|
'baseUrl' => '', |
489
|
|
|
'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
490
|
|
|
'cssOptions' => $options, |
491
|
|
|
'depends' => (array) $depends, |
492
|
|
|
]); |
493
|
|
|
$bundles = [$key => $bundle]; |
|
|
|
|
494
|
|
|
|
495
|
|
|
$this->registerAssetBundle($key); |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Registers a JS code block. |
501
|
|
|
* |
502
|
|
|
* @param string $js the JS code block to be registered |
503
|
|
|
* @param int $position the position at which the JS script tag should be inserted in a page. |
504
|
|
|
* |
505
|
|
|
* The possible values are: |
506
|
|
|
* |
507
|
|
|
* - [[POS_HEAD]]: in the head section |
508
|
|
|
* - [[POS_BEGIN]]: at the beginning of the body section |
509
|
|
|
* - [[POS_END]]: at the end of the body section. This is the default value. |
510
|
|
|
* - [[POS_LOAD]]: executed when HTML page is completely loaded. |
511
|
|
|
* - [[POS_READY]]: executed when HTML document composition is ready. |
512
|
|
|
* |
513
|
|
|
* @param string $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS code |
514
|
|
|
* blocks are registered with the same key, the latter will overwrite the former. |
515
|
|
|
* |
516
|
|
|
* @return void |
517
|
|
|
*/ |
518
|
1 |
|
public function registerJs(string $js, int $position = self::POS_END, string $key = null): void |
519
|
|
|
{ |
520
|
1 |
|
$key = $key ?: md5($js); |
521
|
1 |
|
$this->js[$position][$key] = $js; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Registers a JS file. |
526
|
|
|
* |
527
|
|
|
* This method should be used for simple registration of JS files. If you want to use features of |
528
|
|
|
* {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle} |
529
|
|
|
* and {@see registerAssetBundle()} instead. |
530
|
|
|
* |
531
|
|
|
* @param string $url the JS file to be registered. |
532
|
|
|
* @param array $options the HTML attributes for the script tag. The following options are specially handled and |
533
|
|
|
* are not treated as HTML attributes: |
534
|
|
|
* |
535
|
|
|
* - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
536
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
537
|
|
|
* * [[POS_HEAD]]: in the head section |
538
|
|
|
* * [[POS_BEGIN]]: at the beginning of the body section |
539
|
|
|
* * [[POS_END]]: at the end of the body section. This is the default value. |
540
|
|
|
* |
541
|
|
|
* Please refer to {@see \Yiisoft\Html\Html::jsFile()} for other supported options. |
542
|
|
|
* |
543
|
|
|
* @param string $key the key that identifies the JS script file. If null, it will use $url as the key. If two JS |
544
|
|
|
* files are registered with the same key at the same position, the latter will overwrite the former. |
545
|
|
|
* Note that position option takes precedence, thus files registered with the same key, but different |
546
|
|
|
* position option will not override each other. |
547
|
|
|
* |
548
|
|
|
* @return void |
549
|
|
|
*/ |
550
|
19 |
|
public function registerJsFile(string $url, array $options = [], string $key = null): void |
551
|
|
|
{ |
552
|
19 |
|
$key = $key ?: $url; |
553
|
|
|
|
554
|
19 |
|
$depends = ArrayHelper::remove($options, 'depends', []); |
555
|
|
|
|
556
|
19 |
|
if (empty($depends)) { |
557
|
19 |
|
$position = ArrayHelper::remove($options, 'position', self::POS_END); |
558
|
19 |
|
$this->jsFiles[$position][$key] = Html::jsFile($url, $options); |
559
|
|
|
} else { |
560
|
|
|
$bundle = $this->createBundle([ |
561
|
|
|
'baseUrl' => '', |
562
|
|
|
'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
563
|
|
|
'jsOptions' => $options, |
564
|
|
|
'depends' => (array) $depends, |
565
|
|
|
]); |
566
|
|
|
$bundles = [$key => $bundle]; |
|
|
|
|
567
|
|
|
$this->registerAssetBundle($key); |
568
|
|
|
} |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated |
573
|
|
|
* variable names. |
574
|
|
|
* |
575
|
|
|
* @param string $name Name of the variable |
576
|
|
|
* @param array|string $value Value of the variable |
577
|
|
|
* @param int $position the position in a page at which the JavaScript variable should be inserted. |
578
|
|
|
* |
579
|
|
|
* The possible values are: |
580
|
|
|
* |
581
|
|
|
* - [[POS_HEAD]]: in the head section. This is the default value. |
582
|
|
|
* - [[POS_BEGIN]]: at the beginning of the body section. |
583
|
|
|
* - [[POS_END]]: at the end of the body section. |
584
|
|
|
* - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
585
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
586
|
|
|
* - [[POS_READY]]: enclosed within jQuery(document).ready(). |
587
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
588
|
|
|
*/ |
589
|
1 |
|
public function registerJsVar(string $name, $value, int $position = self::POS_HEAD): void |
590
|
|
|
{ |
591
|
1 |
|
$js = sprintf('var %s = %s;', $name, \Yiisoft\Json\Json::htmlEncode($value)); |
592
|
1 |
|
$this->registerJs($js, $position, $name); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Renders the content to be inserted in the head section. |
597
|
|
|
* |
598
|
|
|
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
599
|
|
|
* |
600
|
|
|
* @return string the rendered content |
601
|
|
|
*/ |
602
|
32 |
|
protected function renderHeadHtml(): string |
603
|
|
|
{ |
604
|
32 |
|
$lines = []; |
605
|
32 |
|
if (!empty($this->metaTags)) { |
606
|
|
|
$lines[] = implode("\n", $this->metaTags); |
607
|
|
|
} |
608
|
|
|
|
609
|
32 |
|
if (!empty($this->linkTags)) { |
610
|
|
|
$lines[] = implode("\n", $this->linkTags); |
611
|
|
|
} |
612
|
32 |
|
if (!empty($this->cssFiles)) { |
613
|
18 |
|
$lines[] = implode("\n", $this->cssFiles); |
614
|
|
|
} |
615
|
32 |
|
if (!empty($this->css)) { |
616
|
|
|
$lines[] = implode("\n", $this->css); |
617
|
|
|
} |
618
|
32 |
|
if (!empty($this->jsFiles[self::POS_HEAD])) { |
619
|
3 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); |
620
|
|
|
} |
621
|
32 |
|
if (!empty($this->js[self::POS_HEAD])) { |
622
|
1 |
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD])); |
623
|
|
|
} |
624
|
|
|
|
625
|
32 |
|
return empty($lines) ? '' : implode("\n", $lines); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Renders the content to be inserted at the beginning of the body section. |
630
|
|
|
* |
631
|
|
|
* The content is rendered using the registered JS code blocks and files. |
632
|
|
|
* |
633
|
|
|
* @return string the rendered content |
634
|
|
|
*/ |
635
|
32 |
|
protected function renderBodyBeginHtml(): string |
636
|
|
|
{ |
637
|
32 |
|
$lines = []; |
638
|
32 |
|
if (!empty($this->jsFiles[self::POS_BEGIN])) { |
639
|
3 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]); |
640
|
|
|
} |
641
|
32 |
|
if (!empty($this->js[self::POS_BEGIN])) { |
642
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN])); |
643
|
|
|
} |
644
|
|
|
|
645
|
32 |
|
return empty($lines) ? '' : implode("\n", $lines); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Renders the content to be inserted at the end of the body section. |
650
|
|
|
* |
651
|
|
|
* The content is rendered using the registered JS code blocks and files. |
652
|
|
|
* |
653
|
|
|
* @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
654
|
|
|
* [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts. |
655
|
|
|
* |
656
|
|
|
* @return string the rendered content |
657
|
|
|
*/ |
658
|
32 |
|
protected function renderBodyEndHtml(bool $ajaxMode): string |
659
|
|
|
{ |
660
|
32 |
|
$lines = []; |
661
|
|
|
|
662
|
32 |
|
if (!empty($this->jsFiles[self::POS_END])) { |
663
|
15 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_END]); |
664
|
|
|
} |
665
|
|
|
|
666
|
32 |
|
if ($ajaxMode) { |
667
|
|
|
$scripts = []; |
668
|
|
|
if (!empty($this->js[self::POS_END])) { |
669
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_END]); |
670
|
|
|
} |
671
|
|
|
if (!empty($this->js[self::POS_READY])) { |
672
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_READY]); |
673
|
|
|
} |
674
|
|
|
if (!empty($this->js[self::POS_LOAD])) { |
675
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_LOAD]); |
676
|
|
|
} |
677
|
|
|
if (!empty($scripts)) { |
678
|
|
|
$lines[] = Html::script(implode("\n", $scripts)); |
679
|
|
|
} |
680
|
|
|
} else { |
681
|
32 |
|
if (!empty($this->js[self::POS_END])) { |
682
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_END])); |
683
|
|
|
} |
684
|
32 |
|
if (!empty($this->js[self::POS_READY])) { |
685
|
|
|
$js = "document.addEventListener('DOMContentLoaded', function(event) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; |
686
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
687
|
|
|
} |
688
|
32 |
|
if (!empty($this->js[self::POS_LOAD])) { |
689
|
|
|
$js = "window.addEventListener('load', function (event) {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; |
690
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
32 |
|
return empty($lines) ? '' : implode("\n", $lines); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* @param array $options |
699
|
|
|
* |
700
|
|
|
* @return AssetBundle |
701
|
|
|
*/ |
702
|
|
|
private function createBundle(array $options): AssetBundle |
703
|
|
|
{ |
704
|
|
|
$bundle = new AssetBundle(); |
705
|
|
|
$bundle->baseUrl = $options['baseUrl']; |
706
|
|
|
$bundle->js = $options['js']; |
707
|
|
|
$bundle->jsOptions = $options['jsOptions']; |
708
|
|
|
$bundle->depends = $options['depends']; |
709
|
|
|
|
710
|
|
|
return $bundle; |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
|