-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJBox.php
199 lines (173 loc) · 4.96 KB
/
JBox.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
<?php
/**
* Created by JetBrains PhpStorm.
* User: yiqing
* Date: 12-3-16
* Time: 上午9:38
*------------------------------------------------------------
* _ _
* (_) (_)
* _ __ __ .--. _ __ _ .--. .--./)
* [ \ [ ][ |/ /'`\' ][ | [ `.-. | / /'`\;
* \ '/ / | || \__/ | | | | | | | \ \._//
* [\_: / [___]\__.; | [___][___||__].',__`
* \__.' |__] ( ( __))
*
*--------------------------------------------------------------
* To change this template use File | Settings | File Templates.
*/
class JBox extends CWidget
{
/**
* @static
* @param bool $hashByName
* @return string
* return the widget assetsUrl
*/
public static function getAssetsUrl($hashByName = false)
{
// return CHtml::asset(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets', $hashByName);
return Yii::app()->getAssetManager()->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets', $hashByName, -1, YII_DEBUG);
}
/**
* @var CClientScript
*/
protected $cs;
/**
* @var bool
*/
public $manualInit = true;
/**
* @var bool
*/
public $debug;
/**
* @var array
*/
public $options = array();
/**
* @var string
* 取值 Skins1|Skin
*/
public $skinBase = 'Skins';
/**
* @var string
* 取值参考皮肤文件夹名称
*/
public $skin = 'Blue';
/**
* @var array
* language code and the corresponding js name ,the js file is under i18n dir.
* eg: array('zh_cn'=>jquery.jBox-zh-CN.js)
* 如果有其他语言添加映射 并将翻译后的文件夹放至/jBox/i18n目录下 并在这里添加映射
*/
public $langMap = array(
'zh_cn' => 'jquery.jBox-zh-CN.js',
);
/**
* @var string
* 默认语言文件名
*/
protected $defaultLangFile = 'jquery.jBox-zh-CN.js';
/**
* @var string
*/
protected $baseUrl = '';
/**
* @var string
*/
protected $assetsPath = '';
/**
* @return void
* --------------------------
* best practice for write the init method:
* init(){
* //preConditionCheck();
* parent::init();
* //defaultSettings();
* }
*
* ---------------------------
*/
public function init()
{
parent::init();
if (!isset($this->debug)) {
$this->debug = defined(YII_DEBUG) ? YII_DEBUG : true;
}
$this->assetsPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
$this->cs = Yii::app()->getClientScript();
}
/**
* @return void
*/
public function run()
{
$this->publishAssets()
->registerClientScripts();
}
/**
* @return JBox
*/
public function publishAssets()
{
$assetsDir = $this->assetsPath; //dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
$this->baseUrl = Yii::app()->getAssetManager()->publish($assetsDir, false, -1, $this->debug);
return $this;
}
/**
* @return JBox
*/
public function registerClientScripts()
{
//> .register js file;
$cs = $this->cs;
$cs->registerCoreScript('jquery')
->registerScriptFile($this->baseUrl . '/jBox/jquery.jBox-2.3.min.js');
$this->handleI18n();
//> register css file
$skinPath = "/jBox/{$this->skinBase}/{$this->skin}/jbox.css";
$cs->registerCssFile($this->baseUrl . $skinPath);
//> if you want to manually setup this plugin , i will only register the necessary css and js file.
if ($this->manualInit == true) {
return $this;
}
//不准备过多封装 这里就截止 下面应该是插件的初始化js代码注册
return $this;
}
/**
* @return JBox
*/
public function handleI18n()
{
$language = Yii::app()->language;
if (isset($this->langMap[$language])) {
$jsFileName = $this->langMap[$language];
} else {
$jsFileName = $this->defaultLangFile;
}
$jsFilePath = $this->assetsPath . DIRECTORY_SEPARATOR . 'jBox' . DIRECTORY_SEPARATOR . 'i18n' . DIRECTORY_SEPARATOR . $jsFileName;
if (is_file($jsFilePath)) {
$jsFileUrl = $this->baseUrl . "/jBox/i18n/{$jsFileName}";
$this->cs->registerScriptFile($jsFileUrl);
} else {
//注册默认的
$jsFileUrl = $this->baseUrl . "/jBox/i18n/{$this->defaultLangFile}";
$this->cs->registerScriptFile($jsFileUrl);
}
return $this;
}
/**
* @param $name
* @param $value
*/
public function __set($name, $value)
{
try {
//shouldn't swallow the parent ' __set operation
parent::__set($name, $value);
} catch (Exception $e) {
$this->options[$name] = $value;
}
}
}