Skip to content

Commit

Permalink
Merge pull request #31 from dmstr/feature/disable-editor
Browse files Browse the repository at this point in the history
Added the option to disable a editor via property
  • Loading branch information
schmunk42 authored Oct 18, 2022
2 parents 7af61da + 0e93bdf commit 085e622
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/JsonEditorWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 085e622

Please sign in to comment.