This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Widget.php
170 lines (145 loc) · 4.54 KB
/
Widget.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\imperavi;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\AssetBundle;
/**
* Imperavi Redactor Widget For Yii2 class file.
*
* @property array $plugins
* @property \yii\web\AssetBundle $assetBundle Imperavi Redactor asset bundle
*
* @author Veaceslav Medvedev <[email protected]>
* @author Alexander Makarov <[email protected]>
* @author Alexander Yaremchuk <[email protected]>
*
* @version 2.0.1
*
* @link https://github.com/asofter/yii2-imperavi-redactor
* @link http://imperavi.com/redactor
* @license https://github.com/asofter/yii2-imperavi-redactor/blob/master/LICENSE.md
*/
class Widget extends \yii\base\Widget
{
/**
* @var array the options for the Imperavi Redactor.
* Please refer to the corresponding [Imperavi Web page](http://imperavi.com/redactor/docs/) for possible options.
*/
public $options = [];
/**
* @var array the html options.
*/
public $htmlOptions = [];
/**
* @var array plugins that you want to use
*/
public $plugins = [];
/*
* @var object model for active text area
*/
public $model = null;
/*
* @var string selector for init js scripts
*/
protected $selector = null;
/*
* @var string name of textarea tag or name of attribute
*/
public $attribute = null;
/*
* @var string value for text area (without model)
*/
public $value = '';
/**
* @var \yii\web\AssetBundle|null Imperavi Redactor Asset bundle
*/
protected $_assetBundle = null;
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
if (!isset($this->htmlOptions['id'])) {
$this->htmlOptions['id'] = $this->getId();
}
}
/**
* Renders the widget.
*/
public function run()
{
$this->selector = '#' . $this->htmlOptions['id'];
if (!is_null($this->model)) {
echo Html::activeTextarea($this->model, $this->attribute, $this->htmlOptions);
} else {
echo Html::textarea($this->attribute, $this->value, $this->htmlOptions);
}
$this->registerRedactorAsset();
$this->registerClientScript();
}
/**
* Registers Imperavi Redactor asset bundle
*/
protected function registerRedactorAsset()
{
$this->_assetBundle = ImperaviRedactorAsset::register($this->getView());
}
/**
* Returns current asset bundle
* @return \yii\web\AssetBundle current asset bundle for Redactor
*/
protected function getAssetBundle()
{
if (!($this->_assetBundle instanceof AssetBundle)) {
$this->registerRedactorAsset();
}
return $this->_assetBundle;
}
/**
* Registers Imperavi Redactor JS
*/
protected function registerClientScript()
{
$view = $this->getView();
/*
* Language fix
* @author <https://github.com/sim2github>
*/
if (!isset($this->options['lang']) || empty($this->options['lang'])) {
$this->options['lang'] = strtolower(substr(Yii::$app->language, 0, 2));
}
// Kudos to yiidoc/yii2-redactor for this solution
$this->assetBundle->js[] = 'lang/' . $this->options['lang'] . '.js';
// Insert plugins in options
if (!empty($this->plugins)) {
$this->options['plugins'] = $this->plugins;
foreach ($this->options['plugins'] as $plugin) {
$this->registerPlugin($plugin);
}
}
$options = empty($this->options) ? '' : Json::encode($this->options);
$js = "jQuery('" . $this->selector . "').redactor($options);";
$view->registerJs($js);
}
/**
* Registers a specific Imperavi plugin and the related events
* @param string $name the name of the Imperavi plugin
*/
protected function registerPlugin($name)
{
$asset = "yii\\imperavi\\" . ucfirst($name) . "ImperaviRedactorPluginAsset";
// check exists file before register (it may be custom plugin with not standard file placement)
$sourcePath = Yii::$app->vendorPath . '/asofter/yii2-imperavi-redactor/' . str_replace('\\', '/', $asset) . '.php';
if (is_file($sourcePath)) {
$asset::register($this->getView());
}
}
}