/
Widget.php
103 lines (89 loc) · 2.93 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
<?php
namespace aiddroid\yii2tinymce;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use aiddroid\yii2tinymce\TinyMceAsset;
use aiddroid\yii2tinymce\TinyMcePluginAsset;
class Widget extends \yii\base\Widget
{
/**
* @var array the options for the IinyMce.
*/
public $options = [];
/**
* @var array the html options.
*/
public $htmlOptions = [];
/*
* @var object model for active text area
*/
public $model = null;
/*
* @var string name of textarea tag or name of attribute
*/
public $attribute = null;
/*
* @var string value for text area (without model)
*/
public $value = '';
/**
* 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()
{
if (!is_null($this->model)) {
echo Html::activeTextarea($this->model, $this->attribute, $this->htmlOptions);
} else {
echo Html::textarea($this->attribute, $this->value, $this->htmlOptions);
}
TinyMceAsset::register($this->getView())->language = $this->options['language'];
$this->registerClientScript();
}
/**
* Registers TinyMce JS
*/
protected function registerClientScript()
{
$view = $this->getView();
if (!isset($this->options['language']) || empty($this->options['language'])) {
$this->options['language'] = strtolower(substr(Yii::$app->language, 0, 2));
}
// Insert plugins in options
if (isset($this->options['plugins'])) {
foreach ($this->options['plugins'] as $pluginLine) {
foreach(explode(' ', $pluginLine) as $plugin){
//if load elfinder plugin ,load elfinder asserts first
if($plugin == 'elfinder'){
\mihaildev\elfinder\Assets::register($this->getView());
\mihaildev\elfinder\Assets::addLangFile($this->options['language'],$this->getView());
}
$this->registerPlugin($plugin);
}
}
}
$options = empty($this->options) ? '' : Json::encode($this->options);
$js = "tinymce.init($options);";
$view->registerJs($js);
}
/**
* Registers a specific TinyMce plugin and the related events
* @param string $name the name of the TinyMce plugin
*/
protected function registerPlugin($name)
{
//register plugin
TinyMcePluginAsset::register($this->getView())->js[] = 'plugins/'.$name.'/plugin.min.js';
}
}