-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJsoneditor.php
57 lines (51 loc) · 1.78 KB
/
Jsoneditor.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
<?php
namespace devgroup\jsoneditor;
use yii\helpers\BaseInflector;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
class Jsoneditor extends InputWidget
{
public $editorOptions = [
'mode' => 'tree',
'modes' => ['code', 'form', 'text', 'tree', 'view'],
];
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->id;
} else {
$this->id = $this->options['id'];
}
if (!isset($this->options['style'])) {
$this->options['style'] = 'height: 250px;';
}
if (is_object($this->model)) {
$this->value = Html::getAttributeValue($this->model, $this->attribute);
$this->name = Html::getInputName($this->model, $this->attribute);
}
if (empty($this->value)) {
$this->value = '{}';
}
$this->options['id'] .= '-jsoneditor';
}
public function run()
{
$view = $this->getView();
JsoneditorAsset::register($view);
$editorName = BaseInflector::camelize($this->id) . 'Editor';
$view->registerJs(
"var container = document.getElementById('" . $this->options['id'] . "');
var options = " . Json::encode($this->editorOptions) . ";
var json = " . $this->value . ";
" . $editorName . " = new JSONEditor(container, options, json);
jQuery('#" . $this->id . "').parents('form').eq(0).submit(function() {
jQuery('#" . $this->id . "').val(" . $editorName . ".getText());
return true;
});"
);
echo Html::hiddenInput($this->name, $this->value, ['id' => $this->id]);
echo Html::tag('div', '', $this->options);
}
}