forked from marqu3s/yii2-summernote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummernote.php
87 lines (72 loc) · 2.42 KB
/
Summernote.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
<?php
namespace marqu3s\summernote;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
class Summernote extends InputWidget
{
/** @var array */
private $defaultOptions = ['class' => 'form-control'];
/** @var array */
private $defaultClientOptions = [
'height' => 200,
'codemirror' => [
'theme' => 'monokai'
]
];
/** @var array */
public $options = [];
/** @var array */
public $clientOptions = [];
/**
* @inheritdoc
*/
public function init()
{
$this->options = array_merge($this->defaultOptions, $this->options);
$this->clientOptions = array_merge($this->defaultClientOptions, $this->clientOptions);
parent::init();
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();
echo $this->hasModel()
? Html::activeTextarea($this->model, $this->attribute, $this->options)
: Html::textarea($this->name, $this->value, $this->options);
$callbacks = $this->getExtendsParams('callbacks');
$buttons = $this->getExtendsParams('buttons');
$modules = $this->getExtendsParams('modules');
$clientOptions = empty($this->clientOptions)
? null
: Json::encode($this->clientOptions);
$this->getView()->registerJs('
var params = '.$clientOptions.';'
. (empty($callbacks)?'':'params.callbacks = { '.$callbacks.' };')
. (empty($buttons)?'':'params.buttons = { '.$buttons.' };')
. (empty($modules)?'':'params.modules = { '.$modules.' };')
. 'jQuery( "#' . $this->options['id'] . '" ).summernote( params );
');
}
private function registerAssets()
{
$view = $this->getView();
if (ArrayHelper::getValue($this->clientOptions, 'codemirror')) {
CodemirrorAsset::register($view);
}
SummernoteAsset::register($view);
if ($language = ArrayHelper::getValue($this->clientOptions, 'lang', null)) {
SummernoteLanguageAsset::register($view)->language = $language;
}
}
private function getExtendsParams($param){
$result = '';
foreach (ArrayHelper::remove($this->clientOptions, $param, []) as $val => $key){
$result .= (empty($result)?'':',').$val.': '.$key;
}
return $result;
}
}