Skip to content

Commit

Permalink
Find panels declared inside CompositePanels
Browse files Browse the repository at this point in the history
  • Loading branch information
barnettwilliam committed Dec 11, 2023
1 parent 26fcee0 commit 4510f84
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 66 deletions.
70 changes: 50 additions & 20 deletions platform/src/ActivityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ActivityManager {
*/
resolvePanelReference(activityId, panelRef){

const foundPanel = this.activities[activityId].panels.find( pnl => pnl.id == panelRef );
const foundPanel = this.findPanel(panelRef,this.activities[activityId].panels);

if ( foundPanel != undefined && typeof foundPanel.id == "string" ){

Expand All @@ -94,6 +94,26 @@ class ActivityManager {
}
}

/**
* Required to search within CompositePanels
*
* @param {*} panelRef
* @param {*} panelList
* @returns
*/
findPanel(panelRef, panelList) {
for(let panel of panelList) {
if (panel.id == panelRef) return panel;
else {
if (panel.childrenPanels) {
const pnl = this.findPanel(panelRef, panel.childrenPanels)
if (pnl) return pnl;
}
}
}
return undefined;
}

/**
* Fetches all the activities from activitiesUrl
* and populates the activities array
Expand Down Expand Up @@ -326,6 +346,34 @@ class ActivityManager {
return null;
}



/* resolve panel refs recursively because of CompositePanels */
resolveRef(panelList) {
for ( let apanel of panelList ){

if (apanel.file != null) {
apanel.url = new URL( apanel.file, this.activitiesUrl).href;
let file = this.fetchFile(apanel.file);
apanel.file = file.content;
apanel.sha = file.sha;
};

// Resolve the panel definition reference
if ( typeof apanel.ref == "string" ){
const panelDef = this.accessPanelDef(apanel.ref);

if (panelDef != null){
apanel.ref = panelDef;
}
}

if (apanel.childrenPanels) {
this.resolveRef(apanel.childrenPanels);
}
}
}

/**
* Fetches the contents of the activity with the provided ID
*/
Expand All @@ -335,25 +383,7 @@ class ActivityManager {

var activity = this.activities[id];

for ( let apanel of activity.panels ){

if (apanel.file != null) {
apanel.url = new URL( apanel.file, this.activitiesUrl).href;
let file = this.fetchFile(apanel.file);
apanel.file = file.content;
apanel.sha = file.sha;
};

// Resolve the panel definition reference
if ( typeof apanel.ref == "string" ){

const panelDef = this.accessPanelDef(apanel.ref);

if (panelDef != null){
apanel.ref = panelDef;
}
}
}
this.resolveRef(activity.panels);

return activity;
}
Expand Down
8 changes: 2 additions & 6 deletions platform/src/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ class Button {

} else if (buttonConfigObject["internal"] != undefined) {

if ((buttonConfigObject.targetPanel) && (buttonConfigObject.internal === "show" || buttonConfigObject.internal === "hide")) {
if (buttonConfigObject.internal === "hide") {
this.action = "hidePanelById( '" + buttonConfigObject.targetPanel + "Panel' )";
} else {
this.action = "showPanelById( '" + buttonConfigObject.targetPanel + "Panel' )";
}
if (buttonConfigObject.targetPanel && buttonConfigObject.internal === "toggle") {
this.action = "togglePanelById( '" + buttonConfigObject.targetPanel + "Panel' )";
} else {
this.action = buttonConfigObject.internal;
}
Expand Down
5 changes: 3 additions & 2 deletions platform/src/CompositePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class CompositePanel extends Panel {
super.initialize(editor)
this.childrenPanels.forEach(panel => {
panel.initialize(editor);
panel.parentComposite = this;
});
}


addPanel(panel) {
this.childrenPanels.push(panel);
panel.parentComposite = this; // Set a reference to the parent CompositePanel
}

removePanel(panel) {
Expand All @@ -31,14 +33,13 @@ class CompositePanel extends Panel {
root.setAttribute("data-role", "panel");
root.setAttribute("id", this.id + "Panel");


root.style.display = "flex";
root.style.flexDirection = "column";
root.style.flex = "1";

var childElementList = []
if (this.childrenPanels) {
this.childrenPanels.forEach(childPanel => {
this.childrenPanels.forEach(childPanel => {
var childElement = childPanel.getElement();
childElementList.push(childElement);
});
Expand Down
2 changes: 1 addition & 1 deletion platform/src/ConsolePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class ConsolePanel extends Panel {
root.setAttribute("id", this.id + "Panel");

var editor = document.createElement("div");
editor.setAttribute("id", this.id + "Editor");
editor.setAttribute("class", "editor");
editor.setAttribute("id", this.id + "Editor");

root.appendChild(editor);

Expand Down
1 change: 1 addition & 0 deletions platform/src/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Panel {
fontSize: "11pt",
useSoftTabs: true
});

} else {
this.editor = editor;
}
Expand Down
54 changes: 18 additions & 36 deletions platform/src/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,8 @@ function createPanelForDefinitionId(panel){

newPanel = new CompositePanel(newPanelId);
if (panel.childrenPanels) {
for (let childPanelId of panel.childrenPanels) {

var childPanelConfig = activity.panels.find( p => p.id === childPanelId )

if (childPanelConfig == null) {
console.error(`Error: No configuration found for child panel with ID ${childPanelId}`);
continue; // Skip this iteration and continue with the next
}

let childPanel = createPanelForDefinitionId(childPanelConfig);
for (let childPanelConfig of panel.childrenPanels) {
var childPanel = createPanelForDefinitionId(childPanelConfig);
newPanel.addPanel(childPanel);
}
}
Expand Down Expand Up @@ -740,11 +732,11 @@ function handleResponseActionFunction(action, requestPromise){
requestPromise.then( (responseText) => {

var response = JSON.parse(responseText);
var outputPanel = panels.find( pn => pn.id == action.output.id);
const outputPanel = activityManager.findPanel( action.output.id, panels);

var outputConsole;
if (action.outputConsole != null){
outputConsole = panels.find(pn => pn.id == action.outputConsole.id);
outputConsole = activityManager.findPanel(action.outputConsole.id, panels);
} else {
outputConsole = outputPanel;
}
Expand All @@ -757,7 +749,7 @@ function handleResponseActionFunction(action, requestPromise){

var responseDiagram = Object.keys(response).find( key => key.toLowerCase().includes("diagram") );

if (response.output != "") {
if (response.output) {
// Text
outputConsole.setValue(response.output)
}
Expand All @@ -769,14 +761,9 @@ function handleResponseActionFunction(action, requestPromise){


} else if (responseDiagram != undefined) {
// Diagrams
// outputPanel.hideEditor(); // TODO Showing diagram before and after renderDiagrams makes outputs image show in panel otherwise nothing.
// outputPanel.showDiagram();


outputPanel.renderDiagram( response[responseDiagram] );

// outputPanel.showDiagram();

} else if (response.generatedFiles) {
// Multiple text files
outputPanel.setGeneratedFiles(response.generatedFiles);
Expand All @@ -787,7 +774,13 @@ function handleResponseActionFunction(action, requestPromise){
switch (action.outputType){
case "code":
// Text
var editor = outputPanel.getEditor();
console.log("BEFORE THE CALL");
console.log(editor.getValue());
console.log(response.generatedText.trim());
outputPanel.getEditor().setValue(response.generatedText.trim(), 1);
console.log("AFTER THE CALL");
console.log(outputPanel.getEditor().getValue());
break;

case "html":
Expand Down Expand Up @@ -819,12 +812,9 @@ function handleResponseActionFunction(action, requestPromise){
krokiXhr.onreadystatechange = function () {
if (krokiXhr.readyState === 4) {
if (krokiXhr.status === 200) {
// outputPanel.hideEditor(); // TODO Showing diagram before and after renderDiagrams makes outputs image show in panel otherwise nothing.
// outputPanel.showDiagram();

outputPanel.renderDiagram(krokiXhr.responseText);

// outputPanel.showDiagram();
}
}
};
Expand Down Expand Up @@ -877,7 +867,7 @@ function runAction(source, sourceButton) {
const panelId = action.parameters[paramName].id;

if (panelId) {
const panel = panels.find( pn => pn.id == panelId );
const panel = activityManager.findPanel(panelId, panels);
param.type = panel.getType();
param.value = panel.getValue();

Expand Down Expand Up @@ -915,18 +905,11 @@ function runAction(source, sourceButton) {
}



function hidePanelById(elementId) {
const panelElement = document.getElementById(elementId);
if (panelElement) {
$("#" + panelElement.parentElement.id).hide();
}
}

function showPanelById(elementId) {
function togglePanelById(elementId) {
const panelElement = document.getElementById(elementId);
if (panelElement) {
$("#" + panelElement.parentElement.id).show();
const parentElement = panelElement.parentElement;
toggle(parentElement.id);
}
}

Expand Down Expand Up @@ -1082,12 +1065,11 @@ async function checkEditorReady(statusUrl, editorInstanceUrl, editorPanelId, edi
window.fit = fit;
window.updateGutterVisibility = updateGutterVisibility;
window.runAction = runAction;
window.hidePanelById = hidePanelById;
window.showPanelById = showPanelById;
window.panels = panels;
window.savePanelContents = savePanelContents;
window.backend = backend;
window.toggle = toggle;
window.togglePanelById = togglePanelById;
//window.renderDiagram = renderDiagram;
window.longNotification = longNotification;
window.getPanelTitle = getPanelTitle;
2 changes: 1 addition & 1 deletion platform/src/ProgramPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class ProgramPanel extends Panel {
root.setAttribute("id", this.id + "Panel");

var editor = document.createElement("div");
editor.setAttribute("id", this.id + "Editor");
editor.setAttribute("class", "editor");
editor.setAttribute("id", this.id + "Editor");

root.appendChild(editor);

Expand Down

0 comments on commit 4510f84

Please sign in to comment.