forked from ruikc/yii2-ueditor-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUeditor.php
115 lines (100 loc) · 2.32 KB
/
Ueditor.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
<?php namespace lxpgw\ueditor;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;
/**
* Ueditor Widget
*
* @package lxpgw\ueditor
* @version 0.1.0
*/
class Ueditor extends InputWidget
{
/**
* UE初始化目标ID
* @var string
*/
public $id;
/**
* UE默认值
* @var string
*/
public $value;
/**
* 表单字段名
* @var string
*/
public $name;
/**
* Tag/ScriptTag HtmlStyle
* @var style
*/
public $style;
/**
* 是否渲染Tag
* @var string/bool
*/
public $renderTag = true;
/**
* UE 参数
* @var array
*/
public $jsOptions = [];
/**
* UE.ready(function(){
* //nothing
* //alert('editor ready');
* });
* @var string
*/
public $readyEvent;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
if (empty($this->id)) {
$this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
if (empty($this->name)) {
$this->name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->id;
}
$attributeName = $this->attribute;
if (empty($this->value) && $this->hasModel()) {
$this->value = $this->model->$attributeName;
}
}
/**
* Renders the widget.
*/
public function run()
{
UeditorAssets::register($this->view);
$this->registerScripts();
if ($this->renderTag) {
echo $this->renderTag();
}
}
public function renderTag()
{
$id = $this->id;
$content = $this->value;
$name = $this->name;
$style = $this->style ? " style=\"{$this->style}\"" : '';
return <<<EOF
<script id="{$id}" name="{$name}"$style type="text/plain">{$content}</script>
EOF;
}
public function registerScripts()
{
$jsonOptions = Json::encode($this->jsOptions);
$script = "UE.getEditor('{$this->id}', " . $jsonOptions . ")";
if ($this->readyEvent) {
$script .= ".ready(function(){{$this->readyEvent}})";
}
$script .= ';';
$this->view->registerJs($script, View::POS_READY);
}
}