/**
* Messages server module
* @module messages-server
*/
/**
* Adds a message to the request, in order to be sent to the client
* @function module:messages-server~internalAddMessage
* @param {String} type Message type (fatal, error, etc...)
* @param {String} message Message text
* @param {String} title Message title
* @param {Exception} exception An exception used to retrieve script file name and line number for logging
* @see Defined in Java code fr.dz.webjs.scripts.WebJSScriptLibrary
* @private
*/
/**
* Adds a message to the request, in order to be sent to the client
* @function module:messages-server~addMessage
* @param {String} type Message type (fatal, error, etc...)
* @param {String} message Message text
* @param {String} title Message title
* @param {Integer} callNumber Number of calls in the stack trace to the non-Messages first piece of code
* @private
*/
function addMessage(type, message, title, callNumber) {
try {
throw new Packages.java.lang.Exception("JS logger exception");
} catch(e) {
return internalAddMessage(type, message, title, e, callNumber + 1);
}
}
/**
* Messages server utility
* @namespace
*/
var Messages = {
/**
* Fatal message
* @type {Object}
* @constant
* @default
*/
FATAL: {
name: "fatal",
},
/**
* Error message
* @type {Object}
* @constant
* @default
*/
ERROR: {
name: "error",
},
/**
* Warn message
* @type {Object}
* @constant
* @default
*/
WARN: {
name: "warn",
},
/**
* Info message
* @type {Object}
* @constant
* @default
*/
INFO: {
name: "info",
},
/**
* Debug message
* @type {Object}
* @constant
* @default
*/
DEBUG: {
name: "debug",
},
/**
* Available messages types
* @type {Array}
* @constant
* @default
*/
TYPES: [],
/**
* Initialization :
* <ul><li>constants initialization</li></ul>
* @private
*/
init: function() {
this.TYPES = [this.DEBUG, this.INFO, this.WARN, this.ERROR, this.FATAL];
},
/**
* Displays a fatal message
* @param {String} message Message
* @param {String} title Title
*/
fatal: function(message, title) {
this.sendMessage(this.FATAL, message, title, 1);
},
/**
* Displays an error message
* @param {String} message Message
* @param {String} title Title
*/
error: function(message, title) {
this.sendMessage(this.ERROR, message, title, 1);
},
/**
* Displays a warn message
* @param {String} message Message
* @param {String} title Title
*/
warn: function(message, title) {
this.sendMessage(this.WARN, message, title, 1);
},
/**
* Displays an info message
* @param {String} message Message
* @param {String} title Title
*/
info: function(message, title) {
this.sendMessage(this.INFO, message, title, 1);
},
/**
* Displays a debug message
* @param {String} message Message
* @param {String} title Title
*/
debug: function(message, title) {
this.sendMessage(this.DEBUG, message, title, 1);
},
/**
* Creates a message
* @param {String} type Message type
* @param {String} message Message
* @param {String} title Title
*/
createMessage: function(type, message, title) {
return {type: type, message: message, title: title, date: new Date()};
},
/**
* Sends a message
* @param {String} type Message type
* @param {String} message Message
* @param {String} title Title
* @param {Integer} callNumber Number of calls in the stack trace to the non-Messages first piece of code
* @private
*/
sendMessage: function(type, message, title, callNumber) {
addMessage(type.name, message, title, callNumber + 1);
},
/**
* Sends multiple messages
* @param {Array} messages List of messages objects
*/
sendMessages: function(messages) {
$.each(messages, function() {
addMessage(this.type.name, this.message, this.title, 1);
});
},
};
// Initialization
Messages.init();