/
Summernote.php
70 lines (57 loc) · 1.4 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
<?php
/**
* @author: Zaini Afzan
* @created: 23/02/2014 3:05
* @file: Summernote
*/
namespace zainiafzan\widget;
use yii\helpers\Html;
use yii\helpers\Json;
class Summernote extends \yii\base\Widget{
public $options = [];
/**
* @var array the html options.
*/
public $htmlOptions = [];
/*
* @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 = '';
public function init()
{
\Yii::setAlias('@summernote', dirname(__FILE__));
if(!isset($this->htmlOptions['id'])){
$this->htmlOptions['id'] = $this->getId();
}
parent::init();
}
public function run(){
$this->selector = '#' . $this->getId();
if(!is_null($this->model)){
echo Html::activeTextarea($this->model, $this->attribute, $this->htmlOptions);
}else{
echo Html::textarea($this->attribute, $this->value, $this->htmlOptions);
}
SummernoteAsset::register($this->getView());
$this->registerClientScript();
}
public function registerClientScript()
{
$view = $this->getView();
$options = empty($this->options) ? '' : Json::encode($this->options);
$js = "jQuery('" . $this->selector . "').summernote($options);";
$view->registerJs($js);
}
}