-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathERedactorWidget.php
executable file
·104 lines (93 loc) · 2.24 KB
/
ERedactorWidget.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
<?php
/**
* Redactor widget
*
* @author Jani Mikkonen <[email protected]>
* @version 1.2.0
* @license public domain (http://unlicense.org)
* @package extensions.redactor
* @link http://imperavi.com/redactor/
*/
class ERedactorWidget extends CInputWidget {
/**
* Assets package ID.
*/
const PACKAGE_ID = 'redactor-widget';
/**
* @var string path to assets
*/
protected $assetsPath;
/**
* @var string URL to assets
*/
protected $assetsUrl;
/**
* @var array redactor options
* @see http://redactorjs.com/docs
*/
public $options = array();
/**
* @var string|null textarea selector for jQuery
*/
public $selector;
/**
* Init widget
*/
public function init()
{
parent::init();
if ($this->assetsPath === null) {
$this->assetsPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'assets';
}
if ($this->assetsUrl === null) {
$this->assetsUrl = Yii::app()->assetManager->publish($this->assetsPath);
}
if ($this->selector === null) {
list($this->name, $this->id) = $this->resolveNameId();
$this->selector = '#' . $this->id;
}
$this->registerClientScript();
}
/**
* Run widget.
*/
public function run()
{
if ($this->hasModel()) {
echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
} else if ($this->selector !== null) {
echo CHtml::textArea($this->name, $this->value, $this->htmlOptions);
}
}
/**
* Register CSS and scripts.
*/
protected function registerClientScript()
{
$cs = Yii::app()->clientScript;
if (!isset($cs->packages[self::PACKAGE_ID])) {
$cs->packages[self::PACKAGE_ID] = array(
'basePath' => $this->assetsPath,
'baseUrl' => $this->assetsUrl,
'js' => array(
'js/redactor' . (YII_DEBUG ? '' : '.min') . '.js',
),
'css' => array(
'css/redactor.css',
),
'depends' => array(
'jquery',
),
);
}
$cs->registerPackage(self::PACKAGE_ID);
if (isset($this->options['lang']) && $this->options['lang'] != 'en') {
$cs->registerScriptFile($this->assetsUrl . '/js/lang/' . $this->options['lang'] . '.js');
}
$cs->registerScript(
__CLASS__ . '#' . $this->id,
'jQuery('. CJavaScript::encode($this->selector) .').redactor('. CJavaScript::encode($this->options) .');',
CClientScript::POS_READY
);
}
}