Skip to content

Commit 085e622

Browse files
authored
Merge pull request #31 from dmstr/feature/disable-editor
Added the option to disable a editor via property
2 parents 7af61da + 0e93bdf commit 085e622

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/JsonEditorWidget.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use yii\helpers\ArrayHelper;
1414
use yii\helpers\Html;
1515
use yii\helpers\Json;
16+
use yii\web\View;
1617
use yii\widgets\InputWidget as BaseWidget;
1718

1819
/**
@@ -78,6 +79,13 @@ class JsonEditorWidget extends BaseWidget
7879
*/
7980
private $_renderInput = true;
8081

82+
/**
83+
* Disable the editor and set it in a readonly state
84+
* @var bool
85+
* @link https://github.com/json-editor/json-editor#enable-and-disable-the-editor
86+
*/
87+
public $disabled = false;
88+
8189
/**
8290
* Initializes the widget
8391
* @inheritdoc
@@ -175,11 +183,15 @@ public function run()
175183

176184
$readyFunction = '';
177185
$readyFunction .= "{$widgetId}.on('change', function() { document.getElementById('{$inputId}').value = JSON.stringify({$widgetId}.getValue()); });\n";
178-
186+
if ($this->disabled) {
187+
// Disabled last added json editor
188+
$readyFunction .= 'window.jsonEditors.slice(-1)[0].disable()';
189+
}
179190
$widgetJs .= "{$widgetId}.on('ready', function() {\n{$readyFunction}\n});";
180191

181192
// Register js code
182193
$view->registerJs($widgetJs, $view::POS_READY);
194+
183195
$this->registerAdditionalLanguages();
184196

185197
parent::run();

0 commit comments

Comments
 (0)