Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sliqric7053 committed Feb 16, 2024
1 parent 5228f52 commit 6c6d484
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 31 deletions.
60 changes: 60 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,66 @@ <h2 class="method-title">closeModalDialog</h2>
<br>
</div>
</section>

<!-- Section about onCanCancel function -->
<section class="collapsible-section">
<header class="section-header">
<h2 class="method-title">onCanCancel</h2>
<div class="collapsible-icon">˄</div>
</header>
<div class="content">
<p>
Das Ereignis onCanCancel dient als Registrierungsmechanismus, der während der Initialisierungsphase aufgerufen werden sollte. Sie ermöglicht die Entscheidung, ob der modale Dialog beim Auftreten eines ESC-Tastenereignisses innerhalb eines modalen Dialog-Add-ons beendet oder seine Anzeige beibehalten werden soll. Weitere Einzelheiten zur <a target="_blank"
href="https://help.optimal-systems.com/enaio_develop/x/-oCiBQ">Dokumentation</a>.

</p>
</p>
<p>
Syntax: <em class="bolder italic">lib.registerOnCanCancelCallback(() => value)</em>
</p>
<div class="content-button-container">
<p>Klicken Sie auf eine der Schaltflächen, um den Wert zuzuweisen, und drücken Sie dann die ESC-Taste, um das Verhalten zu beobachten.</p>
<div class="button-container">
<button id="onCanCancelEnable" class="inner-button">ESC-Tastenereignis einschalten (value = 1)</button>
<button id="onCanCancelDisable" class="inner-button">ESC-Tastenereignis deaktivieren (value = 2)</button>
</div>
</div>
<br>
</div>
</section>

<!-- Section about setDialogCaption method -->
<section class="collapsible-section">
<header class="section-header">
<h2 class="method-title">setDialogCaption</h2>
<div class="collapsible-icon">˄</div>
</header>
<div class="content">
<p>
Die Methode setDialogCaption ermöglicht Entwicklern, den Titel des Dialogfelds dynamisch anzupassen, selbst nachdem das modale Dialogfeld geöffnet wurde. Dies bietet eine höhere Flexibilität bei der Gestaltung der Benutzererfahrung im Kontext des Projekts.
Weitere Einzelheiten zur <a target="_blank"
href="https://help.optimal-systems.com/enaio_develop/x/QYGiBQ">Dokumentation</a>.
</p>
</p>
<p>
Syntax: <em class="bolder italic">setDialogCaption(value)</em>
</p>
<div class="content-button-container">
<div class="fields-container">
<div>
<label for="captionName">New Caption:&nbsp;</label>
<input type="text" name="New Caption" id="captionName">
</div>
</div>
<div class="button-container">
<button id="setDialogCaption" class="inner-button">Set Dialog Caption</button>
<button class="clear-button">Clear</button>
</div>
</div>
</div>
</section>


<!-- A generic close button to close the modal dialog for convenience -->
<div class="content-button-container">
<div class="button-container close-button-general">
Expand Down
51 changes: 49 additions & 2 deletions js/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ async function setFieldValueByInternal() {
await action.setFieldValueByInternal(internalFieldName, internalFieldValue);
}

/**
* Sets the caption of the dialog.
*
* This function retrieves the caption from an HTML element with the ID "captionName" and sets it as the dialog's caption.
*/
async function setDialogCaption() {
const newCaption = document.getElementById("captionName").value;
await action.setDialogCaption(newCaption);
}

/**
* Closes the modal dialog with a specific value.
*
Expand Down Expand Up @@ -78,7 +88,11 @@ document
.getElementById("setFieldValueByInternal")
.addEventListener("click", setFieldValueByInternal);

// Set up event listeners
document
.getElementById("setDialogCaption")
.addEventListener("click", setDialogCaption);

// Event listeners for closeModalDialog
document
.getElementById("closeModalSave")
.addEventListener("click", function () {
Expand All @@ -98,6 +112,39 @@ document
closeModalDialog(); // Here we pass the no parameter and the dialog will be closed with default value '1'
});




/**
* This function attempts to execute the onCanCancelMethod from the action object with a given value.
*
* @param {any} value - The value to be passed to the onCanCancelMethod of the action object.
* @throws {Error} If an error occurs during the execution of the onCanCancelMethod, it logs the error and rethrows it.
*/
function onCanCancelMethod(value) {
try {
action.onCanCancelMethod(value);
} catch (error) {
console.error("Error onCanCancel method:", error);
throw error;
}
}


// Event listeners for onCanCancel
document
.getElementById("onCanCancelEnable")
.addEventListener("click", function () {
onCanCancelMethod(1); // Here we pass the parameter '1'
});

document
.getElementById("onCanCancelDisable")
.addEventListener("click", function () {
onCanCancelMethod(2); // Here we pass the parameter '2'
});


/**
* Clears content and input values when a clear button is clicked.
*/
Expand All @@ -108,7 +155,7 @@ function setupClearButtonActions() {
clearContainer("getFieldValueByInternal_response");
clearContainer("setFieldValueByInternal_response");
clearContainer("getEnvironment_response");
clearInputFields("internalFieldName", "internalFieldValue", "internalSetFieldName");
clearInputFields("internalFieldName", "internalFieldValue", "internalSetFieldName", "captionName");
});
});
}
Expand Down
90 changes: 75 additions & 15 deletions js/library/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function openIndexData(inNewTab, mode, objectId, objectTypeId = undefined)
if (isModalDialog()) {
throw "Not implemented for modal dialog";
}

return sendClientMessage(["openIndexData", [inNewTab, mode, objectId, objectTypeId]]);
}

Expand All @@ -66,7 +66,7 @@ async function openLocation(inNewTab, objectId, objectTypeId = undefined, parent
if (isModalDialog()) {
throw "Not implemented for modal dialog";
}

await sendClientMessage(["openLocation", [inNewTab, objectId, objectTypeId, parentId, parentTypeId]]);
}

Expand All @@ -84,7 +84,7 @@ async function getSelectedObjects() {
if (isModalDialog()) {
throw "Not implemented for modal dialog";
}

return sendClientMessage(["getSelectedObjects", []]);
}

Expand All @@ -99,7 +99,7 @@ async function refreshHitListObjects(osIds) {
if (isModalDialog()) {
throw "Not implemented for modal dialog";
}

await sendClientMessage(["refreshHitListObjects", [osIds]]);
}

Expand All @@ -118,8 +118,8 @@ async function openHitListByIds(objects, inNewTab = false, title = "", subTitle
if (isModalDialog()) {
throw "Not implemented for modal dialog";
}
await sendClientMessage(["openHitListByIds", {

await sendClientMessage(["openHitListByIds", {
objects,
inNewTab,
title,
Expand All @@ -142,7 +142,7 @@ async function getFieldValueByInternal(json) {
if (!isModalDialog()) {
throw "Not implemented for dashlets";
}

return sendClientMessage(["getFieldValueByInternal", [jsonObjectToString(json)]]);
}

Expand Down Expand Up @@ -173,12 +173,27 @@ async function getEnvironment() {
if (!isModalDialog()) {
throw "Not implemented for dashlets";
}

return sendClientMessage(["getEnvironment", []]);
}

/**
* Closes the modal dialog
* This function is only available for modal dialogs. It sets the caption of the dialog to the provided value.
*
* @param {string} newDialogCaption - The caption to be set for the modal dialog. Defaults to an empty string if no value is provided.
* @throws {string} Throws a string error message if the function is used outside of a modal dialog context.
* @remarks The caption is set as an array for webclient compatibility. The rich client only accepts a string.
*/
function setDialogCaption(newDialogCaption = "") {
if (!isModalDialog()) {
throw "Not implemented for dashlets";
}

return sendClientMessage(["setDialogCaption", [newDialogCaption]]);
}

/**
* Cancel the modal dialog
*
* @param buttonScriptReturnValue The numeric value which should be sent to the button script
*/
Expand All @@ -201,8 +216,8 @@ async function sendClientMessage(payload) {
try {
if (window.osClient) {
return libRichClient.sendToRichClient(payload);
}
}

return libWebClient.sendWebclientMessage(payload);
} catch (error) {
console.log(`dashlet says: error caught in ${payload[0]}`, error);
Expand All @@ -218,7 +233,7 @@ async function sendClientMessage(payload) {
*/
function jsonObjectToString(jsonObject) {
if (!(jsonObject instanceof String) && typeof jsonObject !== "string") {
if (jsonObject.value instanceof Object && typeof jsonObject.value === "object") {
if (typeof jsonObject.value === "object" && !Array.isArray(jsonObject.value)) {
jsonObject.value = JSON.stringify(jsonObject.value);
}

Expand All @@ -238,19 +253,60 @@ function isModalDialog() {
if (window.osClient) {
return libRichClient.isModalDialog();
}

return libWebClient.isModalDialog();
}

// This will store the value for the onCanCancel behavior.
// It's initialized to a default value to ensure it's always callable.
let onCanCancelValue = 1;

/**
* Registers the callback for the ESC key event.
*
* @param {Function} valueFunction - A function that returns the current value for the callback.
*/
function registerOnCanCancelCallback(valueFunction) {
return new Promise((resolve, reject) => {
// Delay is necessary to ensure the availability of the function.
setTimeout(() => {
if (!isModalDialog()) {
reject("Not implemented for dashlets");
} else {
// We assign the function passed from main.js to onCanCancelValue.
// This allows the function to be updated dynamically from main.js.
onCanCancelValue = valueFunction;
resolve();
}
}, 1000);
});
}

// Event listener for the ESC key.
window.addEventListener('keydown', function (event) {
// Check if the ESC key was pressed, and the modal dialog is active.
if (event.key === "Escape" && !window.osClient && isModalDialog()) {
// Retrieve the current onCanCancelValue by calling the function.
const currentValue = typeof onCanCancelValue == 'function' ? onCanCancelValue() : onCanCancelValue;
// If the value is not 2, we close the modal dialog.
if (currentValue !== 2) {
closeModalDialog(currentValue);
} else {
console.warn("ESC key event is disabled.");
}
}
});

// Export functions to be used in other JavaScript files.
// Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
export {
// Event Callbacks
registerOnInitCallback,
registerOnUpdateCallback,
registerOnCanCancelCallback,

// Methods Dashlets
openIndexData,
openIndexData,
openLocation,
getSelectedObjects,
refreshHitListObjects,
Expand All @@ -260,5 +316,9 @@ export {
// Methods modal dialogs
getFieldValueByInternal,
setFieldValueByInternal,
getEnvironment
getEnvironment,
setDialogCaption,

// export for unit tests
isModalDialog
};
Loading

0 comments on commit 6c6d484

Please sign in to comment.