forked from smaknsk/yii-csol
/
YiiCSOL.php
168 lines (147 loc) · 5.52 KB
/
YiiCSOL.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
<?php
/**
* Using Object Literal to organize your code for Yii
* http://github.com/smaknsk/yii-csol
*/
class YiiCSOL extends CClientScript
{
/**
* @var string Base url path script files
*/
public $baseUrl = '/js/';
public $revisionFile = 'clientscript.rev';
/**
* @var int default position for YiiCSOL::registerScriptInit
* value is CClientScript::POS_END,CClientScript::POS_READY and etc
*/
public $defaultPosition = 4;
/**
* @var string Number revisioin add to url (script.js?v=$revision)
*/
protected $revision = null;
/**
* Add js code for run Controller
*
* @param string || array $module
* @param string $controller
* @param string $action
* @param integer $position the position of the JavaScript code. Valid values include the following:
* <ul>
* <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
* <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
* <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
* <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
* <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
* </ul>
* @param array $data
* @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
*/
public function registerScriptInit($module = null, $controller = null, $action = null, $position = null, $data = array())
{
if(is_null($position)) {
$position = $this->defaultPosition;
}
if (is_array($module)) {
$data = $module;
$module = null;
}
$scriptId = array($module, $controller, $action, 'init');
$paramsJson = $data ? json_encode($data, JSON_FORCE_OBJECT) : '';
if (!$module && Yii::app()->getController()->module) {
$scriptId[0] = ucfirst(Yii::app()->getController()->module->id) . 'Module';
}
if (!$controller) {
$scriptId[1] = ucfirst(Yii::app()->getController()->id) . 'Controller';
}
if (!$action) {
$scriptId[2] = 'action' . ucfirst(Yii::app()->getController()->action->id);
}
if (!$scriptId[0]) {
array_shift($scriptId);
}
$scriptIdStr = implode('.', $scriptId);
$scriptChecking = array();
$tmp = '';
foreach($scriptId as $item) {
$tmp .= $item;
$scriptChecking[] = 'typeof(' . $tmp . ') != "undefined"';
$tmp .= '.';
}
$scriptChecking = implode(' && ', $scriptChecking);
$scriptChecking = 'if (' . $scriptChecking . '){' . $scriptIdStr . '(' . $paramsJson . ');' . '}';
$scriptFile = $this->baseUrl . $scriptId[0] . '.js';
if (file_exists(Yii::getPathOfAlias('webroot') . $scriptFile)) {
$this->registerScriptFile($scriptFile);
$this->registerScript($scriptIdStr, $scriptChecking, $position);
}
}
/**
* Get revision number from file application.runtime/clientscript.rev
* @return string
*/
public function getRevision()
{
if ($this->revision !== null) {
return $this->revision;
}
$this->revision = '';
$path = Yii::getPathOfAlias('application.runtime') . '/' . $this->revisionFile;
if (file_exists($path)) {
$this->revision = '?' . trim(file_get_contents($path));
}
return $this->revision;
}
/**
* Register CSS file and add revision to url
*
* @param string $url
* @param string $media
* @return CClientScript
*/
public function registerCssFile($url, $media = '')
{
$url = $url . $this->getRevision();
return parent::registerCssFile($url, $media);
}
/**
* Register JS file and add revision to url
*
* @param string $url
* @param string $position
* @param array $htmlOptions
* @param bool $isRevision
* @return CClientScript
*/
public function registerScriptFile($url, $position = null, array $htmlOptions = array(), $isRevision = true)
{
if ($isRevision) {
$url = $url . $this->getRevision();
}
return parent::registerScriptFile($url, $position, $htmlOptions);
}
/**
* Convert array to json and register as script
*
* @param $name
* @param $data
* @param integer $position
* <ul>
* <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
* <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
* <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
* <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
* <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
* </ul>
* @param array $htmlOptions
* @return CClientScript
*/
public function registerScriptData($name, $data, $position = null, array $htmlOptions = array())
{
return $this->registerScript($name, $name . ' = ' . json_encode($data) . ';', $position, $htmlOptions);
}
public function render(&$output)
{
$this->registerScriptInit();
parent::render($output);
}
}