Passed
Push — master ( 9dbdd9...d5a428 )
by Alexander
04:15
created

framework/web/View.php (3 issues)

1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
use yii\helpers\Html;
14
use yii\helpers\Url;
15
16
/**
17
 * View represents a view object in the MVC pattern.
18
 *
19
 * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
20
 *
21
 * View is configured as an application component in [[\yii\base\Application]] by default.
22
 * You can access that instance via `Yii::$app->view`.
23
 *
24
 * You can modify its configuration by adding an array to your application config under `components`
25
 * as it is shown in the following example:
26
 *
27
 * ```php
28
 * 'view' => [
29
 *     'theme' => 'app\themes\MyTheme',
30
 *     'renderers' => [
31
 *         // you may add Smarty or Twig renderer here
32
 *     ]
33
 *     // ...
34
 * ]
35
 * ```
36
 *
37
 * For more details and usage information on View, see the [guide article on views](guide:structure-views).
38
 *
39
 * @property \yii\web\AssetManager $assetManager The asset manager. Defaults to the "assetManager" application
40
 * component.
41
 *
42
 * @author Qiang Xue <[email protected]>
43
 * @since 2.0
44
 */
45
class View extends \yii\base\View
46
{
47
    /**
48
     * @event Event an event that is triggered by [[beginBody()]].
49
     */
50
    const EVENT_BEGIN_BODY = 'beginBody';
51
    /**
52
     * @event Event an event that is triggered by [[endBody()]].
53
     */
54
    const EVENT_END_BODY = 'endBody';
55
    /**
56
     * The location of registered JavaScript code block or files.
57
     * This means the location is in the head section.
58
     */
59
    const POS_HEAD = 1;
60
    /**
61
     * The location of registered JavaScript code block or files.
62
     * This means the location is at the beginning of the body section.
63
     */
64
    const POS_BEGIN = 2;
65
    /**
66
     * The location of registered JavaScript code block or files.
67
     * This means the location is at the end of the body section.
68
     */
69
    const POS_END = 3;
70
    /**
71
     * The location of registered JavaScript code block.
72
     * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`.
73
     */
74
    const POS_READY = 4;
75
    /**
76
     * The location of registered JavaScript code block.
77
     * This means the JavaScript code block will be enclosed within `jQuery(window).load()`.
78
     */
79
    const POS_LOAD = 5;
80
    /**
81
     * This is internally used as the placeholder for receiving the content registered for the head section.
82
     */
83
    const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
84
    /**
85
     * This is internally used as the placeholder for receiving the content registered for the beginning of the body section.
86
     */
87
    const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
88
    /**
89
     * This is internally used as the placeholder for receiving the content registered for the end of the body section.
90
     */
91
    const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
92
93
    /**
94
     * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values
95
     * are the registered [[AssetBundle]] objects.
96
     * @see registerAssetBundle()
97
     */
98
    public $assetBundles = [];
99
    /**
100
     * @var string the page title
101
     */
102
    public $title;
103
    /**
104
     * @var array the registered meta tags.
105
     * @see registerMetaTag()
106
     */
107
    public $metaTags = [];
108
    /**
109
     * @var array the registered link tags.
110
     * @see registerLinkTag()
111
     */
112
    public $linkTags = [];
113
    /**
114
     * @var array the registered CSS code blocks.
115
     * @see registerCss()
116
     */
117
    public $css = [];
118
    /**
119
     * @var array the registered CSS files.
120
     * @see registerCssFile()
121
     */
122
    public $cssFiles = [];
123
    /**
124
     * @var array the registered JS code blocks
125
     * @see registerJs()
126
     */
127
    public $js = [];
128
    /**
129
     * @var array the registered JS files.
130
     * @see registerJsFile()
131
     */
132
    public $jsFiles = [];
133
134
    private $_assetManager;
135
136
137
    /**
138
     * Marks the position of an HTML head section.
139
     */
140 51
    public function head()
141
    {
142 51
        echo self::PH_HEAD;
143 51
    }
144
145
    /**
146
     * Marks the beginning of an HTML body section.
147
     */
148 51
    public function beginBody()
149
    {
150 51
        echo self::PH_BODY_BEGIN;
151 51
        $this->trigger(self::EVENT_BEGIN_BODY);
152 51
    }
153
154
    /**
155
     * Marks the ending of an HTML body section.
156
     */
157 55
    public function endBody()
158
    {
159 55
        $this->trigger(self::EVENT_END_BODY);
160 55
        echo self::PH_BODY_END;
161
162 55
        foreach (array_keys($this->assetBundles) as $bundle) {
163 15
            $this->registerAssetFiles($bundle);
164
        }
165 55
    }
166
167
    /**
168
     * Marks the ending of an HTML page.
169
     * @param bool $ajaxMode whether the view is rendering in AJAX mode.
170
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
171
     * will be rendered at the end of the view like normal scripts.
172
     */
173 55
    public function endPage($ajaxMode = false)
174
    {
175 55
        $this->trigger(self::EVENT_END_PAGE);
176
177 55
        $content = ob_get_clean();
178
179 55
        echo strtr($content, [
180 55
            self::PH_HEAD => $this->renderHeadHtml(),
181 55
            self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
182 55
            self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
183
        ]);
184
185 55
        $this->clear();
186 55
    }
187
188
    /**
189
     * Renders a view in response to an AJAX request.
190
     *
191
     * This method is similar to [[render()]] except that it will surround the view being rendered
192
     * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]].
193
     * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files
194
     * that are registered with the view.
195
     *
196
     * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter.
197
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
198
     * @param object $context the context that the view should use for rendering the view. If null,
199
     * existing [[context]] will be used.
200
     * @return string the rendering result
201
     * @see render()
202
     */
203
    public function renderAjax($view, $params = [], $context = null)
204
    {
205
        $viewFile = $this->findViewFile($view, $context);
206
207
        ob_start();
208
        ob_implicit_flush(false);
209
210
        $this->beginPage();
211
        $this->head();
212
        $this->beginBody();
213
        echo $this->renderFile($viewFile, $params, $context);
214
        $this->endBody();
215
        $this->endPage(true);
216
217
        return ob_get_clean();
218
    }
219
220
    /**
221
     * Registers the asset manager being used by this view object.
222
     * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component.
223
     */
224 59
    public function getAssetManager()
225
    {
226 59
        return $this->_assetManager ?: Yii::$app->getAssetManager();
227
    }
228
229
    /**
230
     * Sets the asset manager.
231
     * @param \yii\web\AssetManager $value the asset manager
232
     */
233 79
    public function setAssetManager($value)
234
    {
235 79
        $this->_assetManager = $value;
236 79
    }
237
238
    /**
239
     * Clears up the registered meta tags, link tags, css/js scripts and files.
240
     */
241 57
    public function clear()
242
    {
243 57
        $this->metaTags = [];
244 57
        $this->linkTags = [];
245 57
        $this->css = [];
246 57
        $this->cssFiles = [];
247 57
        $this->js = [];
248 57
        $this->jsFiles = [];
249 57
        $this->assetBundles = [];
250 57
    }
251
252
    /**
253
     * Registers all files provided by an asset bundle including depending bundles files.
254
     * Removes a bundle from [[assetBundles]] once files are registered.
255
     * @param string $name name of the bundle to register
256
     */
257 15
    protected function registerAssetFiles($name)
258
    {
259 15
        if (!isset($this->assetBundles[$name])) {
260 12
            return;
261
        }
262 15
        $bundle = $this->assetBundles[$name];
263 15
        if ($bundle) {
264 15
            foreach ($bundle->depends as $dep) {
265 12
                $this->registerAssetFiles($dep);
266
            }
267 15
            $bundle->registerAssetFiles($this);
268
        }
269 15
        unset($this->assetBundles[$name]);
270 15
    }
271
272
    /**
273
     * Registers the named asset bundle.
274
     * All dependent asset bundles will be registered.
275
     * @param string $name the class name of the asset bundle (without the leading backslash)
276
     * @param int|null $position if set, this forces a minimum position for javascript files.
277
     * This will adjust depending assets javascript file position or fail if requirement can not be met.
278
     * If this is null, asset bundles position settings will not be changed.
279
     * See [[registerJsFile]] for more details on javascript position.
280
     * @return AssetBundle the registered asset bundle instance
281
     * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
282
     */
283 35
    public function registerAssetBundle($name, $position = null)
284
    {
285 35
        if (!isset($this->assetBundles[$name])) {
286 34
            $am = $this->getAssetManager();
287 34
            $bundle = $am->getBundle($name);
288 34
            $this->assetBundles[$name] = false;
289
            // register dependencies
290 34
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
291 34
            foreach ($bundle->depends as $dep) {
292 24
                $this->registerAssetBundle($dep, $pos);
293
            }
294 33
            $this->assetBundles[$name] = $bundle;
295 19
        } elseif ($this->assetBundles[$name] === false) {
296 1
            throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
297
        } else {
298 18
            $bundle = $this->assetBundles[$name];
299
        }
300
301 34
        if ($position !== null) {
302 12
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
303 12
            if ($pos === null) {
304 12
                $bundle->jsOptions['position'] = $pos = $position;
305 6
            } elseif ($pos > $position) {
306 6
                throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'.");
307
            }
308
            // update position for all dependencies
309 12
            foreach ($bundle->depends as $dep) {
310 6
                $this->registerAssetBundle($dep, $pos);
311
            }
312
        }
313
314 34
        return $bundle;
315
    }
316
317
    /**
318
     * Registers a meta tag.
319
     *
320
     * For example, a description meta tag can be added like the following:
321
     *
322
     * ```php
323
     * $view->registerMetaTag([
324
     *     'name' => 'description',
325
     *     'content' => 'This website is about funny raccoons.'
326
     * ]);
327
     * ```
328
     *
329
     * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.
330
     *
331
     * @param array $options the HTML attributes for the meta tag.
332
     * @param string $key the key that identifies the meta tag. If two meta tags are registered
333
     * with the same key, the latter will overwrite the former. If this is null, the new meta tag
334
     * will be appended to the existing ones.
335
     */
336
    public function registerMetaTag($options, $key = null)
337
    {
338
        if ($key === null) {
339
            $this->metaTags[] = Html::tag('meta', '', $options);
340
        } else {
341
            $this->metaTags[$key] = Html::tag('meta', '', $options);
342
        }
343
    }
344
345
    /**
346
     * Registers CSRF meta tags.
347
     * They are rendered dynamically to retrieve a new CSRF token for each request.
348
     *
349
     * ```php
350
     * $view->registerCsrfMetaTags();
351
     * ```
352
     *
353
     * The above code will result in `<meta name="csrf-param" content="[yii\web\Request::$csrfParam]">`
354
     * and `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page.
355
     *
356
     * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()`
357
     * from `yii.js`.
358
     *
359
     * @since 2.0.13
360
     */
361 1
    public function registerCsrfMetaTags()
362
    {
363 1
        $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return yii\helpers\Html::csrfMetaTags();');
364 1
    }
365
366
    /**
367
     * Registers a link tag.
368
     *
369
     * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon)
370
     * can be added like the following:
371
     *
372
     * ```php
373
     * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
374
     * ```
375
     *
376
     * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.
377
     *
378
     * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which
379
     * has more options for this kind of link tag.
380
     *
381
     * @param array $options the HTML attributes for the link tag.
382
     * @param string $key the key that identifies the link tag. If two link tags are registered
383
     * with the same key, the latter will overwrite the former. If this is null, the new link tag
384
     * will be appended to the existing ones.
385
     */
386
    public function registerLinkTag($options, $key = null)
387
    {
388
        if ($key === null) {
389
            $this->linkTags[] = Html::tag('link', '', $options);
390
        } else {
391
            $this->linkTags[$key] = Html::tag('link', '', $options);
392
        }
393
    }
394
395
    /**
396
     * Registers a CSS code block.
397
     * @param string $css the content of the CSS code block to be registered
398
     * @param array $options the HTML attributes for the `<style>`-tag.
399
     * @param string $key the key that identifies the CSS code block. If null, it will use
400
     * $css as the key. If two CSS code blocks are registered with the same key, the latter
401
     * will overwrite the former.
402
     */
403
    public function registerCss($css, $options = [], $key = null)
404
    {
405
        $key = $key ?: md5($css);
406
        $this->css[$key] = Html::style($css, $options);
407
    }
408
409
    /**
410
     * Registers a CSS file.
411
     *
412
     * This method should be used for simple registration of CSS files. If you want to use features of
413
     * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]]
414
     * and [[registerAssetBundle()]] instead.
415
     *
416
     * @param string $url the CSS file to be registered.
417
     * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for
418
     * the supported options. The following options are specially handled and are not treated as HTML attributes:
419
     *
420
     * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
421
     * - `appendTimestamp`: bool whether to append a timestamp to the URL.
422
     *
423
     * @param string $key the key that identifies the CSS script file. If null, it will use
424
     * $url as the key. If two CSS files are registered with the same key, the latter
425
     * will overwrite the former.
426
     * @throws InvalidConfigException
427
     */
428 20
    public function registerCssFile($url, $options = [], $key = null)
429
    {
430 20
        $this->registerFile('css', $url, $options, $key);
431 20
    }
432
433
    /**
434
     * Registers a JS code block.
435
     * @param string $js the JS code block to be registered
436
     * @param int $position the position at which the JS script tag should be inserted
437
     * in a page. The possible values are:
438
     *
439
     * - [[POS_HEAD]]: in the head section
440
     * - [[POS_BEGIN]]: at the beginning of the body section
441
     * - [[POS_END]]: at the end of the body section
442
     * - [[POS_LOAD]]: enclosed within jQuery(window).load().
443
     *   Note that by using this position, the method will automatically register the jQuery js file.
444
     * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
445
     *   Note that by using this position, the method will automatically register the jQuery js file.
446
     *
447
     * @param string $key the key that identifies the JS code block. If null, it will use
448
     * $js as the key. If two JS code blocks are registered with the same key, the latter
449
     * will overwrite the former.
450
     */
451 10
    public function registerJs($js, $position = self::POS_READY, $key = null)
452
    {
453 10
        $key = $key ?: md5($js);
454 10
        $this->js[$position][$key] = $js;
455 10
        if ($position === self::POS_READY || $position === self::POS_LOAD) {
456 8
            JqueryAsset::register($this);
457
        }
458 10
    }
459
460
    /**
461
     * Registers a JS or CSS file.
462
     *
463
     * @param string $url the JS file to be registered.
464
     * @param string $type type (js or css) of the file.
465
     * @param array $options the HTML attributes for the script tag. The following options are specially handled
466
     * and are not treated as HTML attributes:
467
     *
468
     * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
469
     * - `appendTimestamp`: bool whether to append a timestamp to the URL.
470
     *
471
     * @param string $key the key that identifies the JS script file. If null, it will use
472
     * $url as the key. If two JS files are registered with the same key at the same position, the latter
473
     * will overwrite the former. Note that position option takes precedence, thus files registered with the same key,
474
     * but different position option will not override each other.
475
     * @throws InvalidConfigException
476
     */
477 36
    private function registerFile($type, $url, $options = [], $key = null)
478
    {
479 36
        $url = Yii::getAlias($url);
480 36
        $key = $key ?: $url;
481 36
        $depends = ArrayHelper::remove($options, 'depends', []);
482 36
        $originalOptions = $options;
483 36
        $position = ArrayHelper::remove($options, 'position', self::POS_END);
484
485
        try {
486 36
            $assetManagerAppendTimestamp = $this->getAssetManager()->appendTimestamp;
487
        } catch (InvalidConfigException $e) {
488
            $depends = null; // the AssetManager is not available
489
            $assetManagerAppendTimestamp = false;
490
        }
491 36
        $appendTimestamp = ArrayHelper::remove($options, 'appendTimestamp', $assetManagerAppendTimestamp);
492
493 36
        if (empty($depends)) {
494
            // register directly without AssetManager
495 36
            if ($appendTimestamp && Url::isRelative($url)) {
496 5
                $prefix = Yii::getAlias('@web');
497 5
                $prefixLength = strlen($prefix);
498 5
                $trimmedUrl = ltrim((substr($url, 0, $prefixLength) === $prefix) ? substr($url, $prefixLength) : $url, '/');
499 5
                $timestamp = @filemtime(Yii::getAlias('@webroot/' . $trimmedUrl, false));
500 5
                if ($timestamp > 0) {
501 2
                    $url = $timestamp ? "$url?v=$timestamp" : $url;
502
                }
503
            }
504 36
            if ($type === 'js') {
505 24
                $this->jsFiles[$position][$key] = Html::jsFile($url, $options);
0 ignored issues
show
It seems like $url can also be of type false; however, parameter $url of yii\helpers\BaseHtml::jsFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

505
                $this->jsFiles[$position][$key] = Html::jsFile(/** @scrutinizer ignore-type */ $url, $options);
Loading history...
506
            } else {
507 36
                $this->cssFiles[$key] = Html::cssFile($url, $options);
0 ignored issues
show
It seems like $url can also be of type false; however, parameter $url of yii\helpers\BaseHtml::cssFile() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

507
                $this->cssFiles[$key] = Html::cssFile(/** @scrutinizer ignore-type */ $url, $options);
Loading history...
508
            }
509
        } else {
510 4
            $this->getAssetManager()->bundles[$key] = Yii::createObject([
511 4
                'class' => AssetBundle::className(),
512 4
                'baseUrl' => '',
513 4
                'basePath' => '@webroot',
514 4
                (string)$type => [ArrayHelper::merge([!Url::isRelative($url) ? $url : ltrim($url, '/')], $originalOptions)],
515 4
                "{$type}Options" => $options,
516 4
                'depends' => (array)$depends,
517
            ]);
518 4
            $this->registerAssetBundle($key);
0 ignored issues
show
It seems like $key can also be of type false; however, parameter $name of yii\web\View::registerAssetBundle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

518
            $this->registerAssetBundle(/** @scrutinizer ignore-type */ $key);
Loading history...
519
        }
520 36
    }
521
522
    /**
523
     * Registers a JS file.
524
     *
525
     * This method should be used for simple registration of JS files. If you want to use features of
526
     * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]]
527
     * and [[registerAssetBundle()]] instead.
528
     *
529
     * @param string $url the JS file to be registered.
530
     * @param array $options the HTML attributes for the script tag. The following options are specially handled
531
     * and are not treated as HTML attributes:
532
     *
533
     * - `depends`: array, specifies the names of the asset bundles that this JS file depends on.
534
     * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
535
     *     * [[POS_HEAD]]: in the head section
536
     *     * [[POS_BEGIN]]: at the beginning of the body section
537
     *     * [[POS_END]]: at the end of the body section. This is the default value.
538
     * - `appendTimestamp`: bool whether to append a timestamp to the URL.
539
     *
540
     * Please refer to [[Html::jsFile()]] for other supported options.
541
     *
542
     * @param string $key the key that identifies the JS script file. If null, it will use
543
     * $url as the key. If two JS files are registered with the same key at the same position, the latter
544
     * will overwrite the former. Note that position option takes precedence, thus files registered with the same key,
545
     * but different position option will not override each other.
546
     * @throws InvalidConfigException
547
     */
548 24
    public function registerJsFile($url, $options = [], $key = null)
549
    {
550 24
        $this->registerFile('js', $url, $options, $key);
551 24
    }
552
553
    /**
554
     * Registers a JS code block defining a variable. The name of variable will be
555
     * used as key, preventing duplicated variable names.
556
     *
557
     * @param string $name Name of the variable
558
     * @param array|string $value Value of the variable
559
     * @param int $position the position in a page at which the JavaScript variable should be inserted.
560
     * The possible values are:
561
     *
562
     * - [[POS_HEAD]]: in the head section. This is the default value.
563
     * - [[POS_BEGIN]]: at the beginning of the body section.
564
     * - [[POS_END]]: at the end of the body section.
565
     * - [[POS_LOAD]]: enclosed within jQuery(window).load().
566
     *   Note that by using this position, the method will automatically register the jQuery js file.
567
     * - [[POS_READY]]: enclosed within jQuery(document).ready().
568
     *   Note that by using this position, the method will automatically register the jQuery js file.
569
     *
570
     * @since 2.0.14
571
     */
572 1
    public function registerJsVar($name, $value, $position = self::POS_HEAD)
573
    {
574 1
        $js = sprintf('var %s = %s;', $name, \yii\helpers\Json::htmlEncode($value));
575 1
        $this->registerJs($js, $position, $name);
576 1
    }
577
578
    /**
579
     * Renders the content to be inserted in the head section.
580
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
581
     * @return string the rendered content
582
     */
583 55
    protected function renderHeadHtml()
584
    {
585 55
        $lines = [];
586 55
        if (!empty($this->metaTags)) {
587 1
            $lines[] = implode("\n", $this->metaTags);
588
        }
589
590 55
        if (!empty($this->linkTags)) {
591
            $lines[] = implode("\n", $this->linkTags);
592
        }
593 55
        if (!empty($this->cssFiles)) {
594 20
            $lines[] = implode("\n", $this->cssFiles);
595
        }
596 55
        if (!empty($this->css)) {
597
            $lines[] = implode("\n", $this->css);
598
        }
599 55
        if (!empty($this->jsFiles[self::POS_HEAD])) {
600 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
601
        }
602 55
        if (!empty($this->js[self::POS_HEAD])) {
603 1
            $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]));
604
        }
605
606 55
        return empty($lines) ? '' : implode("\n", $lines);
607
    }
608
609
    /**
610
     * Renders the content to be inserted at the beginning of the body section.
611
     * The content is rendered using the registered JS code blocks and files.
612
     * @return string the rendered content
613
     */
614 55
    protected function renderBodyBeginHtml()
615
    {
616 55
        $lines = [];
617 55
        if (!empty($this->jsFiles[self::POS_BEGIN])) {
618 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]);
619
        }
620 55
        if (!empty($this->js[self::POS_BEGIN])) {
621
            $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]));
622
        }
623
624 55
        return empty($lines) ? '' : implode("\n", $lines);
625
    }
626
627
    /**
628
     * Renders the content to be inserted at the end of the body section.
629
     * The content is rendered using the registered JS code blocks and files.
630
     * @param bool $ajaxMode whether the view is rendering in AJAX mode.
631
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
632
     * will be rendered at the end of the view like normal scripts.
633
     * @return string the rendered content
634
     */
635 55
    protected function renderBodyEndHtml($ajaxMode)
636
    {
637 55
        $lines = [];
638
639 55
        if (!empty($this->jsFiles[self::POS_END])) {
640 19
            $lines[] = implode("\n", $this->jsFiles[self::POS_END]);
641
        }
642
643 55
        if ($ajaxMode) {
644
            $scripts = [];
645
            if (!empty($this->js[self::POS_END])) {
646
                $scripts[] = implode("\n", $this->js[self::POS_END]);
647
            }
648
            if (!empty($this->js[self::POS_READY])) {
649
                $scripts[] = implode("\n", $this->js[self::POS_READY]);
650
            }
651
            if (!empty($this->js[self::POS_LOAD])) {
652
                $scripts[] = implode("\n", $this->js[self::POS_LOAD]);
653
            }
654
            if (!empty($scripts)) {
655
                $lines[] = Html::script(implode("\n", $scripts));
656
            }
657
        } else {
658 55
            if (!empty($this->js[self::POS_END])) {
659
                $lines[] = Html::script(implode("\n", $this->js[self::POS_END]));
660
            }
661 55
            if (!empty($this->js[self::POS_READY])) {
662
                $js = "jQuery(function ($) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
663
                $lines[] = Html::script($js);
664
            }
665 55
            if (!empty($this->js[self::POS_LOAD])) {
666
                $js = "jQuery(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
667
                $lines[] = Html::script($js);
668
            }
669
        }
670
671 55
        return empty($lines) ? '' : implode("\n", $lines);
672
    }
673
}
674