-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodemirrorWidget.php
111 lines (91 loc) · 2.35 KB
/
CodemirrorWidget.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
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 05.06.2015
*/
namespace skeeks\widget\codemirror;
use yii\helpers\Html;
use yii\web\JsExpression;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* Class CodemirrorWidget
* @package skeeks\widget\codemirror
*/
class CodemirrorWidget extends \yii\widgets\InputWidget
{
public $presetsDir;
public $assets = [];
public $clientOptions = [];
/**
* Preset name. php|javascript etc.
* @var string
*/
public $preset;
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel())
{
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else
{
echo Html::textarea($this->name, $this->value, $this->options);
}
$this->registerAssets();
$this->_registerPlugin();
}
/**
* Registers Assets
*/
public function registerAssets()
{
$view = $this->getView();
$settings = $this->clientOptions;
$assets = $this->assets;
if ($this->preset)
{
$preset = $this->getPreset($this->preset);
if(isset($preset['assets']))
{
$assets = ArrayHelper::merge($preset['assets'], $assets);
}
if(isset($preset['settings']))
{
$this->clientOptions = ArrayHelper::merge($preset['settings'], $settings);
}
}
CodemirrorAsset::register($this->view, $assets);
}
/**
* Registers CKEditor plugin
*/
protected function _registerPlugin()
{
$view = $this->getView();
$id = $this->options['id'];
$settings = Json::encode($this->clientOptions);
$js = "CodeMirror.fromTextArea(document.getElementById('$id'), $settings);";
$view->registerJs($js);
}
public function getPreset($name)
{
if($this->presetsDir)
$filename=$this->presetsDir.DIRECTORY_SEPARATOR.ucfirst($name).'Preset.php';
else
$filename=dirname(__FILE__).DIRECTORY_SEPARATOR.'presets'.DIRECTORY_SEPARATOR.ucfirst($name).'Preset.php';
return require $filename;
}
}