/
KindEditorWidget.php
316 lines (296 loc) · 12.3 KB
/
KindEditorWidget.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace anrhkk\kindeditor;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\JsExpression;
use yii\widgets\InputWidget;
class KindEditorWidget extends InputWidget
{
/**
* The name of this widget.
*/
const PLUGIN_NAME = 'KindEditor';
const THEME_TYPE_DEFAULT = 'default';
const THEME_TYPE_QQ = 'qq';
const THEME_TYPE_SIMPLE = 'simple';
const LANG_TYPE_AR = 'ar';
const LANG_TYPE_EN = 'en';
const LANG_TYPE_KO = 'ko';
const LANG_TYPE_ZH_CN = 'zh_CN';
const LANG_TYPE_ZH_TW = 'zh_TW';
/**
* @var array the KindEditor plugin options.
* @see http://kindeditor.net/doc.php
*/
public $clientOptions = [];
/**
* csrf cookie parameter
* @var string
*/
public $csrfCookieParam = '_csrfCookie';
/**
* @var boolean
*/
public $render = true;
/**
* @var string 编辑器类型
* textEditor:普通编辑器
* uploadButton:自定义上传按钮
* colorPicker:取色器
* fileManager:浏览服务器
* dialog:弹窗
* imageDialog:上传图片
* multiImageDialog:批量上传图片
* fileDialog:文件上传
*/
public $editorType = 'textEditor';
/**
* 默认图片
* @var string
*/
public $defaultImg = 'http://stc.weimob.com//img/template/lib/home-300200.jpg';
public function init(){
parent::init();
$this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
$this->name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerClientScript();
if ($this->render) {
if ($this->hasModel()) {
switch ($this->editorType) {
case 'uploadButton':
return Html::activeInput('text', $this->model, $this->attribute, $this->options) . '<input type="button" id="uploadButton" value="Upload" />';
break;
case 'colorPicker':
return Html::activeInput('text', $this->model, $this->attribute, $this->options) . '<input type="button" id="colorpicker" value="打开取色器" />';
break;
case 'fileManager':
return Html::activeInput('text', $this->model, $this->attribute, $this->options) . '<input type="button" id="filemanager" value="浏览服务器" />';
break;
case 'imageDialog':
$value = $this->model->isNewRecord ? $this->defaultImg : Html::getAttributeValue($this->model, $this->attribute);
return "<img id='thumb-img-$this->id' type='img' src='$value' style='max-height:100px;' />
<input type='hidden' id='thumb-$this->id' name='$this->name' value='$value' class='input-medium' data-rule-url='true' />
<a id='img-$this->id' class='btn insertimage'>选择图片</a>";
break;
case 'fileDialog':
return Html::activeInput('text', $this->model, $this->attribute, $this->options) . '<input type="button" id="insertfile" value="选择文件" />';
break;
default:
return Html::activeTextarea($this->model, $this->attribute, $this->options);
break;
}
} else {
switch ($this->editorType) {
case 'uploadButton':
return Html::input('text', $this->id, $this->value, $this->options) . '<input type="button" id="uploadButton" value="Upload" />';
break;
case 'colorPicker':
return Html::input('text', $this->id, $this->value, $this->options) . '<input type="button" id="colorpicker" value="打开取色器" />';
break;
case 'fileManager':
return Html::input('text', $this->id, $this->value, $this->options) . '<input type="button" id="filemanager" value="浏览服务器" />';
break;
case 'imageDialog':
$value = $this->model->isNewRecord ? get_params('UPLOAD_DEFAULT_IMG') : Html::getAttributeValue($this->model, $this->attribute);
return "<img id='thumb_img_$this->id' type='img' src='$value' style='max-height:100px;' />
<input type='hidden' id='thumb-$this->id' name='$this->name' value='' class='input-medium' data-rule-url='true' />
<a id='img-$this->id' class='btn insertimage'>选择图片</a>";
break;
case 'fileDialog':
return Html::input('text', $this->id, $this->value, $this->options) . '<input type="button" id="insertfile" value="选择文件" />';
break;
default:
return Html::textarea($this->id, $this->value, $this->options);
break;
}
}
}
}
/**
* Registers the needed client script and options.
*/
public function registerClientScript()
{
$view = $this->getView();
$this->initClientOptions();
$asset = KindEditorAsset::register($view);
$preJs = '';
$uploadJson = $this->clientOptions['uploadJson'];
$fileManagerJson = $this->clientOptions['fileManagerJson'];
switch ($this->editorType) {
case 'uploadButton':
$preJs = <<<EOT
var uploadbutton = K.uploadbutton({
button : K('#uploadButton')[0],
fieldName : 'imgFile',
url : '{$uploadJson}',
afterUpload : function(data) {
if (data.error === 0) {
var url = K.formatUrl(data.url, 'absolute');
K('#{$this->id}').val(url);
} else {
alert(data.message);
}
},
afterError : function(str) {
alert('自定义错误信息: ' + str);
}
});
uploadbutton.fileBox.change(function(e) {
uploadbutton.submit();
});
EOT;
break;
case 'colorPicker':
$preJs = <<<EOT
var colorpicker;
K('#colorpicker').bind('click', function(e) {
e.stopPropagation();
if (colorpicker) {
colorpicker.remove();
colorpicker = null;
return;
}
var colorpickerPos = K('#colorpicker').pos();
colorpicker = K.colorpicker({
x : colorpickerPos.x,
y : colorpickerPos.y + K('#colorpicker').height(),
z : 19811214,
selectedColor : 'default',
noColor : '无颜色',
click : function(color) {
K('#{$this->id}').val(color);
colorpicker.remove();
colorpicker = null;
}
});
});
K(document).click(function() {
if (colorpicker) {
colorpicker.remove();
colorpicker = null;
}
});
EOT;
break;
case 'fileManager':
$preJs = <<<EOT
var editor = K.editor({
fileManagerJson : '{$fileManagerJson}'
});
K('#filemanager').click(function() {
editor.loadPlugin('filemanager', function() {
editor.plugin.filemanagerDialog({
viewType : 'VIEW',
dirName : 'image',
clickFn : function(url, title) {
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
EOT;
break;
case 'imageDialog':
$preJs = <<<EOT
var editor = K.editor({
allowFileManager : true,
"uploadJson":"{$uploadJson}",
"fileManagerJson":"{$fileManagerJson}",
});
K('#img-{$this->id}').click(function(e) {
editor.loadPlugin('image', function() {
editor.plugin.imageDialog({
imageUrl : $(e.target).prev().val(),
clickFn : function(url, title, width, height, border, align) {
$(e.target).prev().val(url);
if ('img' == $(e.target).prev().prev().attr('type')) {
$(e.target).prev().hide();
$(e.target).prev().prev().attr('src', url);
$(e.target).prev().prev().show();
}
editor.hideDialog();
}
});
});
});
EOT;
break;
case 'fileDialog':
$preJs = <<<EOT
var editor = K.editor({
allowFileManager : true,
"uploadJson":"{$uploadJson}",
"fileManagerJson":"{$fileManagerJson}",
});
K('#insertfile').click(function() {
editor.loadPlugin('insertfile', function() {
editor.plugin.fileDialog({
fileUrl : K('#{$this->id}').val(),
clickFn : function(url, title) {
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
EOT;
break;
default:
$preJs = "
K.create('#{$this->id}', ".Json::encode($this->clientOptions).");";
break;
}
$view->registerJsFile($asset->baseUrl . '/lang/' . $this->clientOptions['langType'] . '.js', ['depends' => '\kindeditor\KindEditorAsset']);
$js = "
KindEditor.ready(function(K) {
{$preJs};
});
";
$view->registerJs($js);
}
/**
* Initializes client options
*/
protected function initClientOptions()
{
$options = array_merge($this->defaultOptions(), $this->clientOptions);
// $_POST['_csrf'] = ...
$options['extraFileUploadParams'][Yii::$app->request->csrfParam] = Yii::$app->request->getCsrfToken();
// $_POST['PHPSESSID'] = ...
$options['extraFileUploadParams'][Yii::$app->session->name] = Yii::$app->session->id;
if (Yii::$app->request->enableCsrfCookie) {
// $_POST['_csrfCookie'] = ...
$options['extraFileUploadParams'][$this->csrfCookieParam] = $_COOKIE[Yii::$app->request->csrfParam];
}
$this->clientOptions = $options;
}
/**
* Default client options
* @return array
*/
protected function defaultOptions()
{
return [
'id' => $this->id,
'name' => $this->name,
'fileManagerJson' => Url::to(['upload/index', 'action' => 'fileManagerJson']),
'uploadJson' => Url::to(['upload/index', 'action' => 'uploadJson']),
'width' => '80%',
'height' => '400px',
'allowFileManager' => true,
'themeType' => self::THEME_TYPE_DEFAULT,
'langType' => self::LANG_TYPE_ZH_CN,
'afterChange' => new JsExpression('function(){this.sync();}'),
];
}
}