-
Notifications
You must be signed in to change notification settings - Fork 152
/
ImperaviRedactorWidget.php
167 lines (147 loc) · 3.77 KB
/
ImperaviRedactorWidget.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
<?php
/**
* ImperaviRedactorWidget class file.
*
* @property string $assetsPath
* @property string $assetsUrl
* @property array $plugins
*
* @author Veaceslav Medvedev <slavcopost@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
*
* @link https://github.com/yiiext/imperavi-redactor-widget
* @link http://imperavi.com/redactor
* @license https://github.com/yiiext/imperavi-redactor-widget/blob/master/license.md
*/
class ImperaviRedactorWidget extends CInputWidget
{
/**
* Assets package ID.
*/
const PACKAGE_ID = 'imperavi-redactor';
/**
* @var array {@link http://imperavi.com/redactor/docs/ redactor options}.
*/
public $options = array();
/**
* @var string|null Selector pointing to textarea to initialize redactor for.
* Defaults to null meaning that textarea does not exist yet and will be
* rendered by this widget.
*/
public $selector;
/**
* @var array
*/
public $package = array();
/**
* @var array
*/
private $_plugins = array();
/**
* Init widget.
*/
public function init()
{
parent::init();
if ($this->selector === null) {
list($this->name, $this->id) = $this->resolveNameID();
$this->htmlOptions['id'] = $this->getId();
$this->selector = '#' . $this->getId();
if ($this->hasModel()) {
echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
} else {
echo CHtml::textArea($this->name, $this->value, $this->htmlOptions);
}
}
$this->registerClientScript();
}
/**
* Register CSS and Script.
*/
protected function registerClientScript()
{
// Prepare script package.
$this->package = array_merge(array(
'baseUrl' => $this->getAssetsUrl(),
'js' => array(
YII_DEBUG ? 'redactor.js' : 'redactor.min.js',
),
'css' => array(
'redactor.css',
),
'depends' => array(
'jquery',
),
), $this->package);
// Append language file to script package.
if (isset($this->options['lang']) && $this->options['lang'] !== 'en') {
$this->package['js'][] = 'lang/' . $this->options['lang'] . '.js';
}
// Add assets url to relative css.
if (isset($this->options['css'])) {
if (!is_array($this->options['css'])) {
$this->options['css'] = array($this->options['css']);
}
foreach ($this->options['css'] as $i => $css) {
if (strpos($css, '/') === false) {
$this->options['css'][$i] = $this->getAssetsUrl() . '/' . $css;
}
}
}
// Insert plugins in options
if (!empty($this->_plugins)) {
$this->options['plugins'] = array_keys($this->_plugins);
}
$clientScript = Yii::app()->getClientScript();
$selector = CJavaScript::encode($this->selector);
$options = CJavaScript::encode($this->options);
$clientScript
->addPackage(self::PACKAGE_ID, $this->package)
->registerPackage(self::PACKAGE_ID)
->registerScript(
$this->id,
'jQuery(' . $selector . ').redactor(' . $options . ');',
CClientScript::POS_READY
);
foreach ($this->getPlugins() as $id => $plugin) {
$clientScript
->addPackage(self::PACKAGE_ID . '-' . $id, $plugin)
->registerPackage(self::PACKAGE_ID . '-' . $id);
}
}
/**
* Get the assets path.
* @return string
*/
public function getAssetsPath()
{
return dirname(__FILE__) . '/assets';
}
/**
* Publish assets and return url.
* @return string
*/
public function getAssetsUrl()
{
return Yii::app()->getAssetManager()->publish($this->getAssetsPath());
}
/**
* @param array $plugins
*/
public function setPlugins(array $plugins)
{
foreach ($plugins as $id => $plugin) {
if (!isset($plugin['baseUrl']) && !isset($plugin['basePath'])) {
$plugin['baseUrl'] = $this->getAssetsUrl() . '/plugins/' . $id;
}
$this->_plugins[$id] = $plugin;
}
}
/**
* @return array
*/
public function getPlugins()
{
return $this->_plugins;
}
}