diff --git a/src/JsonEditorWidget.php b/src/JsonEditorWidget.php index 3da7d03..a867f4c 100755 --- a/src/JsonEditorWidget.php +++ b/src/JsonEditorWidget.php @@ -13,6 +13,7 @@ use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\helpers\Json; +use yii\web\View; use yii\widgets\InputWidget as BaseWidget; /** @@ -78,6 +79,13 @@ class JsonEditorWidget extends BaseWidget */ private $_renderInput = true; + /** + * Disable the editor and set it in a readonly state + * @var bool + * @link https://github.com/json-editor/json-editor#enable-and-disable-the-editor + */ + public $disabled = false; + /** * Initializes the widget * @inheritdoc @@ -175,11 +183,15 @@ public function run() $readyFunction = ''; $readyFunction .= "{$widgetId}.on('change', function() { document.getElementById('{$inputId}').value = JSON.stringify({$widgetId}.getValue()); });\n"; - + if ($this->disabled) { + // Disabled last added json editor + $readyFunction .= 'window.jsonEditors.slice(-1)[0].disable()'; + } $widgetJs .= "{$widgetId}.on('ready', function() {\n{$readyFunction}\n});"; // Register js code $view->registerJs($widgetJs, $view::POS_READY); + $this->registerAdditionalLanguages(); parent::run();