1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\components; |
4
|
|
|
|
5
|
|
|
use yii\helpers\Html; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class View extends \yii\web\View { |
9
|
|
|
/** |
10
|
|
|
* @var array the registered JSON code blocks |
11
|
|
|
* @see registerJson() |
12
|
|
|
*/ |
13
|
|
|
public $json; |
14
|
|
|
|
15
|
|
|
// this is the key new function here |
16
|
|
|
public function registerJson(array $array, $key = null, $options = 0, $depth = 512) |
17
|
|
|
{ |
18
|
|
|
$json = json_encode($array, $options, $depth); |
19
|
|
|
$key = $key ?: md5($json); |
20
|
|
|
$this->json[self::POS_READY][$key] = $json; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Renders the content to be inserted at the end of the body section. |
25
|
|
|
* The content is rendered using the registered JS code blocks and files. |
26
|
|
|
* @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
27
|
|
|
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
28
|
|
|
* will be rendered at the end of the view like normal scripts. |
29
|
|
|
* @return string the rendered content |
30
|
|
|
*/ |
31
|
|
|
protected function renderBodyEndHtml($ajaxMode) |
32
|
|
|
{ |
33
|
|
|
$lines = []; |
34
|
|
|
|
35
|
|
|
if (!empty($this->jsFiles[self::POS_END])) { |
36
|
|
|
$lines[] = implode("\n", $this->jsFiles[self::POS_END]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!empty($this->json)) { |
40
|
|
|
foreach($this->json[self::POS_READY] as $json_name => $json_value) { |
41
|
|
|
$lines[] = Html::script($json_value, ['type' => 'application/json', 'id' => $json_name]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($ajaxMode) { |
46
|
|
|
$scripts = []; |
47
|
|
|
if (!empty($this->js[self::POS_END])) { |
48
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_END]); |
49
|
|
|
} |
50
|
|
|
if (!empty($this->js[self::POS_READY])) { |
51
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_READY]); |
52
|
|
|
} |
53
|
|
|
if (!empty($this->js[self::POS_LOAD])) { |
54
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_LOAD]); |
55
|
|
|
} |
56
|
|
|
if (!empty($scripts)) { |
57
|
|
|
$lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
if (!empty($this->js[self::POS_END])) { |
61
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']); |
62
|
|
|
} |
63
|
|
|
if (!empty($this->js[self::POS_READY])) { |
64
|
|
|
$js = "jQuery(document).ready(function () {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; |
65
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
66
|
|
|
} |
67
|
|
|
if (!empty($this->js[self::POS_LOAD])) { |
68
|
|
|
$js = "jQuery(window).load(function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; |
69
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return empty($lines) ? '' : implode("\n", $lines); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|