Skip to content

Commit

Permalink
JsonPreview: Always display JSON preview for MySQL JSON field type
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpp committed Oct 2, 2024
1 parent 468f79f commit 0019887
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions plugins/AdminerJsonPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public function head()
document.addEventListener("DOMContentLoaded", init, false);

function init() {
var links = document.querySelectorAll('a.json-icon');
const links = document.querySelectorAll('a.json-icon');

for (var i = 0; i < links.length; i++) {
for (let i = 0; i < links.length; i++) {
links[i].addEventListener("click", function(event) {
event.preventDefault();
toggleJson(this);
Expand All @@ -150,11 +150,10 @@ function init() {
}

function toggleJson(button) {
var index = button.dataset.index;
const index = button.dataset.index;

var obj = document.getElementById("json-code-" + index);
if (!obj)
return;
const obj = document.getElementById("json-code-" + index);
if (!obj) return;

if (obj.style.display === "none") {
button.className += " json-up";
Expand All @@ -170,35 +169,42 @@ function toggleJson(button) {
<?php
}

public function selectVal(&$val, $link, $field, $original)
public function selectVal(&$val, $link, array $field, $original)
{
static $counter = 1;

if (!$this->inTable) {
return;
}

if (is_string($original) && in_array(substr($original, 0, 1), ['{', '[']) && ($json = json_decode($original, true))) {
if ($this->isJson($field, $original) && ($json = json_decode($original, true)) !== null) {
$val = "<a class='icon json-icon' href='#' title='JSON' data-index='$counter'>JSON</a> " . $val;
$val .= $this->convertJson($json, 1, $counter++);
if (is_array($json)) {
$val .= $this->convertJson($json, 1, $counter++);
}
}
}

public function editInput($table, $field, $attrs, $value)
public function editInput($table, array $field, $attrs, $value)
{
static $counter = 1;

if (!$this->inEdit) {
return;
}

if (is_string($value) && in_array(substr($value, 0, 1), ['{', '[']) && ($json = json_decode($value, true))) {
if ($this->isJson($field, $value) && ($json = json_decode($value, true)) !== null && is_array($json)) {
echo "<a class='icon json-icon json-link' href='#' title='JSON' data-index='$counter'><span>JSON</span></a><br/>";
echo $this->convertJson($json, 1, $counter);
}
}

private function convertJson($json, $level = 1, $id = 0)
private function isJson(array $field, $value)
{
return $field["type"] == "json" || (is_string($value) && in_array(substr($value, 0, 1), ['{', '[']));
}

private function convertJson(array $json, $level = 1, $id = 0)
{
$value = "";

Expand Down Expand Up @@ -240,6 +246,10 @@ private function convertJson($json, $level = 1, $id = 0)
}
}

if (empty($json)) {
$value .= "<tr><td>   </td></tr>";
}

$value .= "</table>";

return $value;
Expand Down

0 comments on commit 0019887

Please sign in to comment.