Skip to content

Commit

Permalink
implement metadata formatting
Browse files Browse the repository at this point in the history
implements code mirror which creates a code editor
that allows proper inputting of metadata in json tree format

Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri committed Dec 21, 2023
1 parent 071500b commit c6e0269
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 115 deletions.
1 change: 1 addition & 0 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
"tablefooter",
"error",
"breadcrumb",
"metadatamodal",

"bootstrap",
"bootstraps",
Expand Down
42 changes: 42 additions & 0 deletions ui/web/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
SPDX-License-Identifier: Apache-2.0 */

@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300&family=Roboto:wght@400&family=Montserrat:wght@700&display=swap");
@import url('https://fonts.googleapis.com/css2?family=Inconsolata&display=swap');


:root {
--main-color: #003366;
Expand All @@ -15,6 +17,8 @@ SPDX-License-Identifier: Apache-2.0 */
--pill-color-1: #1d2231;
--pill-color-2: #003366;
--pill-color-3: #8390a2;

--codemirror-font: 'Inconsolata', monospace;
}

* {
Expand Down Expand Up @@ -585,6 +589,44 @@ button.edit-btn {
font-size: 1.25rem;
}

.string,
.cm-s-default .cm-string {
color: var(--main-color);
}

.key,
.cm-property {
color: red !important;
}

.number,
.cm-s-default .cm-number {
color: darkorange;
}

.boolean,
.cm-s-default .cm-atom {
color: green;
}

.null {
color: magenta;
}

.table-container .meta-div {
overflow: auto;
max-height: 5rem;
max-width: 100%;
}

.table-container .meta-col {
max-width: 50%;
}

.metadata-field span {
font-family: var(--codemirror-font) !important;
}

@media (max-width: 768px) {
.sidebar {
width: var(--sidebar-min);
Expand Down
51 changes: 51 additions & 0 deletions ui/web/static/js/formatjson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

function syntaxHighlight(json) {
json = json.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
var cls = "number";
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = "key";
} else {
cls = "string";
}
} else if (/true|false/.test(match)) {
cls = "boolean";
} else if (/null/.test(match)) {
cls = "null";
}
return '<span class="' + cls + '">' + match + "</span>";
},
);
}

function attachFormatJsonWithPrettifyListener(config) {
document.addEventListener("DOMContentLoaded", function () {
var meta = JSON.parse(config.metadata);
document.getElementById(config.id).innerHTML = syntaxHighlight(JSON.stringify(meta, null, 2));
});
}

function codeMirrorEditor(config) {
var editor = CodeMirror.fromTextArea(document.getElementById(config.textArea), {
mode: "application/json",
matchBrackets: true,
autoCloseBrackets: true,
lineWrapping: true,
lineNumbers: true,
lint: true,
gutters: ["CodeMirror-lint-markers"],
autoRefresh: true,
});
editor.setValue(JSON.stringify(config.value, null, 2));
editor.setSize("100%", 200);
document.getElementById(config.button).addEventListener("click", function () {
document.getElementById(config.textArea).value = editor.getValue();
console.log(document.getElementById(config.textArea));
console.log(document.getElementById(config.textArea).value);
});
}
4 changes: 2 additions & 2 deletions ui/web/static/js/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ function updateIdentity(config) {

function updateMetadata(config) {
const button = document.getElementById(config.button);

button.addEventListener("click", function (event) {
const updatedValue = config.cell.textContent.trim();
event.preventDefault();
const updatedValue = document.getElementById(config.textArea).value;
if (validateJSON(updatedValue, config.alertDiv, config.fieldName, event)) {
const url = `/${config.entity}/${config.id}`;
const data = { [config.field]: JSON.parse(updatedValue) };
Expand Down
46 changes: 33 additions & 13 deletions ui/web/template/channel.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,21 @@
</tr>
<tr>
<th>Metadata</th>
<td
class="editable metadata-field"
contenteditable="false"
data-field="metadata"
>
{{ toJSON .Channel.Metadata }}
<td>
<div>
<pre id="meta-data"></pre>
</div>
</td>
<td>
<button
type="button"
class="edit-btn"
id="edit-metadata"
data-bs-toggle="modal"
data-bs-target="#editMetadataModal"
{{ if not $editButton }}disabled{{ end }}
>
<i class="fas fa-pencil-alt"></i>
</button>
<div class="save-cancel-buttons" style="display: none">
<button class="save-btn" id="save-metadata">Save</button>
<button class="cancel-btn" id="cancel-metadata">Cancel</button>
</div>
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -143,7 +139,24 @@
</div>
</div>
</div>
<!-- Edit Metadata Modal -->
{{ template "metadatamodal" }}
</div>
<script>
var metadata = "{{ toJSON .Channel.Metadata }}";
var parsedMetadata = JSON.parse(metadata);

attachFormatJsonWithPrettifyListener({
id: "meta-data",
metadata: metadata,
});

codeMirrorEditor({
textArea: "metadataTextArea",
button: "save-metadata",
value: parsedMetadata,
});
</script>
<script type="module">
import { attachEditRowListener, updateName, updateDescription, updateMetadata} from "/js/update.js";

Expand All @@ -154,14 +167,21 @@
rows: {
name:updateName,
description: updateDescription,
metadata:updateMetadata,
},
errorDiv: "error-message",
fields:{
name: "name-field",
metadata: "metadata-field",
},
});
updateMetadata({
textArea: "metadataTextArea",
field:"metadata",
alertDiv: "metadataError",
fieldName: "metadata-field",
id: "{{ .Channel.ID }}",
entity: "channels",
button: "save-metadata",
});
</script>
</body>
</html>
Expand Down
31 changes: 18 additions & 13 deletions ui/web/template/channels.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ <h5 class="modal-title" id="addChannelModalLabel">Add Channel</h5>
</div>
<div class="mb-3">
<label for="metadata" class="form-label">Metadata</label>
<input
type="text"
class="form-control metadata-field"
name="metadata"
id="metadata"
value="{}"
/>
<div class="metadata-field">
<textarea name="metadata" id="metadata"></textarea>
</div>
<div id="metadataHelp" class="form-text">
Enter channel metadata in JSON format.
</div>
Expand Down Expand Up @@ -176,15 +172,18 @@ <h5 class="modal-title" id="addChannelsModalLabel">Add Channels</h5>
<td>{{ $c.Name }}</td>
<td class="desc-col">{{ $c.Description }}</td>
<td class="meta-col">
{{ range $k, $v := $c.Metadata }}
<span class="badge bg-success">
{{ $k }}:
{{ $v }}
</span>
{{ end }}
<div class="meta-div">
<pre id="meta-{{ $i }}"></pre>
</div>
</td>
<td class="created-col">{{ $c.CreatedAt }}</td>
</tr>
<script>
attachFormatJsonWithPrettifyListener({
metadata: '{{ toJSON $c.Metadata }}',
id: "meta-{{ $i }}",
})
</script>
{{ end }}
</tbody>
</table>
Expand All @@ -207,6 +206,12 @@ <h5 class="modal-title" id="addChannelsModalLabel">Add Channels</h5>
channelsModal.show();
}
}

codeMirrorEditor({
textArea: "metadata",
button: "create-channel-button",
value: {},
});
</script>
<script type="module">
import { attachValidationListener, validateName, validateJSON } from "/js/validation.js";
Expand Down
49 changes: 35 additions & 14 deletions ui/web/template/group.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,21 @@
</tr>
<tr>
<th>Metadata</th>
<td
class="editable metadata-field"
contenteditable="false"
data-field="metadata"
>
{{ toJSON .Group.Metadata }}
<td>
<div>
<pre id="meta-data"></pre>
</div>
</td>
<td>
<button
type="button"
class="edit-btn"
id="edit-metadata"
data-bs-toggle="modal"
data-bs-target="#editMetadataModal"
{{ if not $editButton }}disabled{{ end }}
>
<i class="fas fa-pencil-alt"></i>
</button>
<div class="save-cancel-buttons" style="display: none">
<button class="save-btn" id="save-metadata">Save</button>
<button class="cancel-btn" id="cancel-metadata">Cancel</button>
</div>
</td>
</tr>
</tbody>
Expand All @@ -127,8 +123,26 @@
</div>
</div>
</div>

<!-- Edit Metadata Modal -->
{{ template "metadatamodal" }}
</div>
<script type="module">
<script>
var metadata = "{{ toJSON .Group.Metadata }}";
var parsedMetadata = JSON.parse(metadata);

attachFormatJsonWithPrettifyListener({
id: "meta-data",
metadata: metadata,
});

codeMirrorEditor({
textArea: "metadataTextArea",
button: "save-metadata",
value: parsedMetadata,
});
</script>
<script type="module">
import { attachEditRowListener, updateName, updateDescription, updateMetadata} from "/js/update.js";

attachEditRowListener(
Expand All @@ -138,15 +152,22 @@
rows: {
name:updateName,
description: updateDescription,
metadata:updateMetadata,
},
errorDiv: "error-message",
fields: {
name: "name-field",
metadata: "metadata-field",
},
}
);
updateMetadata({
textArea: "metadataTextArea",
field:"metadata",
alertDiv: "metadataError",
fieldName: "metadata-field",
id: "{{ .Group.ID }}",
entity: "groups",
button: "save-metadata",
});
</script>
</body>
</html>
Expand Down
Loading

0 comments on commit c6e0269

Please sign in to comment.