diff --git a/htdocs/admin/tools/ui/class/documentation.class.php b/htdocs/admin/tools/ui/class/documentation.class.php
index a64bd36b3ca..6d451f43084 100644
--- a/htdocs/admin/tools/ui/class/documentation.class.php
+++ b/htdocs/admin/tools/ui/class/documentation.class.php
@@ -256,7 +256,13 @@ class Documentation
'url' => dol_buildpath($this->baseUrl.'/experimental/experiments/dolibarr-context/index.php', 1),
'icon' => 'fas fa-flask',
'submenu' => array(),
- 'summary' => array(),
+ 'summary' => array(
+ 'Introduction' => '#titlesection-basicusage',
+ 'ConsoleHelp' => '#titlesection-console-help',
+ 'JSDolibarrhooks' => '#titlesection-hooks',
+ 'ExampleOfCreatingNewContextTool' => '#titlesection-create-tool-example',
+ 'SetEventMessageTool' => '#titlesection-tool-seteventmessage',
+ ),
),
)
);
diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js
new file mode 100644
index 00000000000..8d5d7649d56
--- /dev/null
+++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js
@@ -0,0 +1,29 @@
+// This file is purely for IDE autocompletion and developer convenience.
+// It is never executed or loaded in Dolibarr itself.
+
+// MOCK DEFINITION: Dolibarr.tools
+// This mock helps your code editor understand the structure of Dolibarr.tools
+// and provides autocomplete hints, parameter hints, and inline documentation.
+// You can safely edit this file to add all standard Dolibarr tools for autocompletion.
+
+var Dolibarr = {
+ tools: {
+
+ /**
+ * Displays a Dolibarr notification message (success, warning, or error).
+ * This is the JavaScript equivalent of the PHP setEventMessage tool.
+ *
+ * @param {string} msg The message text to display
+ * @param {string=} type Optional: 'mesgs' (default), 'warnings', or 'errors'
+ * @param {boolean=} sticky Optional: true if the message should stay until manually closed
+ *
+ * Example usage in your IDE:
+ * Dolibarr.tools.setEventMessage('Operation successful', 'success');
+ */
+ setEventMessage: function(msg, type, sticky) {},
+
+ // You can add more standard Dolibarr tools here for IDE autocompletion.
+ // Example:
+ // alertUser: function(msg) {},
+ }
+};
diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js
index ac2f38b621b..fe4ed7c9292 100644
--- a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js
+++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js
@@ -16,6 +16,9 @@
* Dolibarr Global Context (UMD)
* Provides a secure global object window.Dolibarr
* with non-replaceable tools, events and debug mode.
+ *
+ * See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.
+ *
*/
(function (root, factory) {
// Support AMD
@@ -61,6 +64,8 @@
* @param {string} name Name of the tool
* @param {*} value Function, class or object
* @param {boolean} overwrite Explicitly allow overwriting an existing tool
+ *
+ * See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.
*/
defineTool(name, value, overwrite = false, triggerHook = true) {
// Prevent silent overrides unless "overwrite" is true
@@ -76,7 +81,7 @@
enumerable: true,
});
- this.log(`Tool defined: ${name}, triggerHook: ${triggerHook}`);
+ this.log(`Tool defined: ${name}, triggerHook: ${triggerHook}, overwrite: ${overwrite} `);
if(triggerHook) {
Dolibarr.executeHook('defineTool', { toolName: name, overwrite: overwrite });
}
@@ -124,7 +129,7 @@
// Dispatch on internal EventTarget
_events.dispatchEvent(ev);
- // Dispatch globally on document so document.addEventListener('DolibarrHook:' + hookName) can catch it
+ // Dispatch globally on document so document.addEventListener('Dolibarr:' + hookName) can catch it
if (typeof document !== "undefined") {
document.dispatchEvent(new CustomEvent('Dolibarr:' + hookName, { detail: data }));
}
@@ -192,7 +197,7 @@
Dolibarr.tools.showConsoleHelp();
- // Trigger DolibarrContext:init as DOM ready
+ // Trigger Dolibarr:Ready as DOM ready
(function triggerContextInit() {
const initHook = () => {
Dolibarr.executeHook('Ready', { context: Dolibarr });
diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js
new file mode 100644
index 00000000000..2d6d58e2d43
--- /dev/null
+++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js
@@ -0,0 +1,34 @@
+document.addEventListener('Dolibarr:Ready', function(e) {
+
+ // this tool allow overwrite because of DISABLE_JQUERY_JNOTIFY conf
+ /**
+ * status : 'mesgs' by default, 'warnings', 'errors'
+ */
+ Dolibarr.defineTool('setEventMessage', (msg, status = 'mesgs', sticky = false) =>{
+
+ // Normalize status to match jNotify expected values
+ const normalizeStatus = (s) => {
+ s = (s || '').toLowerCase();
+ if (s === 'error' || s === 'errors') return 'error';
+ if (s === 'warning' || s === 'warnings') return 'warning';
+ return '';
+ };
+
+ let jnotifyConf = {
+ delay: 1500 // the default time to show each notification (in milliseconds)
+ , type : normalizeStatus(status)
+ , sticky: sticky // determines if the message should be considered "sticky" (user must manually close notification)
+ , closeLabel: "×" // the HTML to use for the "Close" link
+ , showClose: true // determines if the "Close" link should be shown if notification is also sticky
+ , fadeSpeed: 150 // the speed to fade messages out (in milliseconds)
+ , slideSpeed: 250 // the speed used to slide messages out (in milliseconds)
+ }
+
+ if(msg.length > 0){
+ $.jnotify(msg, jnotifyConf);
+ }
+ else{
+ Dolibarr.log('setEventMessage : Message is empty');
+ }
+ }, true);
+});
diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php
index 39bd757cf5c..6bc367f311a 100644
--- a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php
+++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php
@@ -49,6 +49,7 @@ $js = [
'/includes/ace/src/ext-statusbar.js',
'/includes/ace/src/ext-language_tools.js',
$experimentAssetsPath . '/dolibarr-context.umd.js',
+ $experimentAssetsPath . '/dolibarr-tool.seteventmessage.js',
];
$css = [];
@@ -72,7 +73,7 @@ $documentation->showSidebar(); ?>
showSummary(); ?>
-
Introduction
+
Introduction
DolibarrContext is a secure global JavaScript context for Dolibarr.
@@ -108,7 +109,7 @@ $documentation->showSidebar(); ?>
-
Console help
+
Console help
Open your browser console with F12 to view the available commands.
@@ -117,7 +118,7 @@ $documentation->showSidebar(); ?>
- You can define reusable and protected tools in the Dolibarr context using Dolibarr.defineTool:
+ You can define reusable and protected tools in the Dolibarr context using Dolibarr.defineTool.
+
See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.
showSidebar(); ?>
+
+
Set event message tool
+
+
+ Instead of calling JNotify directly in your code, use Dolibarr’s setEventMessage tool.
+ Dolibarr provides the configuration option DISABLE_JQUERY_JNOTIFY, which disables the jQuery JNotify system, usually because another notification library will be used instead.
+
+
+
+ If you rely on Dolibarr.tools.setEventMessage(), your code remains compatible even if the underlying notification system changes.
+ The setEventMessage tool can be replaced internally without requiring any changes in your modules or custom scripts.
+
+
+ This means all developers can write features without worrying about frontend compatibility or future library replacements. Enjoy!
+
+