72 lines
2.7 KiB
JavaScript
72 lines
2.7 KiB
JavaScript
var MessageHandler = (function () {
|
|
// Handlers for each message type
|
|
let animation = "slideDown";
|
|
const handlers = {
|
|
announcement: function (data) {
|
|
toastr.info(`<div class="item announcemantToast">${data.message}</div>`, data.title, {
|
|
"closeButton": true,
|
|
"positionClass": "toast-bottom-right",
|
|
"newestOnTop": true,
|
|
"progressBar": true,
|
|
"preventDuplicates": false,
|
|
"onclick": null,
|
|
"showDuration": "30000",
|
|
"hideDuration": "1000",
|
|
"timeOut": "5000",
|
|
"extendedTimeOut": "1000",
|
|
"showEasing": "swing",
|
|
"hideEasing": "linear",
|
|
"showMethod": animation,
|
|
"hideMethod": "fadeOut"
|
|
});
|
|
$('.toast-info').css("background-color", "#008080");
|
|
},
|
|
bidNotification: function (data) {
|
|
//console.log("Bid product name" + data.productName);
|
|
toastr.success(`<div class="item bidToast"><p>${data.bidPrice}</p><p>${data.productName}</p></div>`, "New bid arrived", {
|
|
//"timeOut": 150000,
|
|
"closeButton": true,
|
|
"positionClass": "toast-bottom-right",
|
|
"newestOnTop": true,
|
|
"progressBar": true,
|
|
"preventDuplicates": false,
|
|
"onclick": null,
|
|
"showDuration": "30000",
|
|
"hideDuration": "1000",
|
|
"timeOut": "5000",
|
|
"extendedTimeOut": "1000",
|
|
"showEasing": "swing",
|
|
"hideEasing": "linear",
|
|
"showMethod": animation,
|
|
"hideMethod": "fadeOut"
|
|
|
|
});
|
|
$('.toast-success').css("background-color", "#4caf50");
|
|
},
|
|
// Add more handlers as needed
|
|
default: function (data) {
|
|
console.warn("Unhandled message type:", data);
|
|
}
|
|
};
|
|
|
|
// Message router to route to the appropriate handler based on message type
|
|
function messageRouter(message) {
|
|
// Parse the JSON message
|
|
try {
|
|
/*console.log(message);*/
|
|
const parsedMessage = JSON.parse(message);
|
|
const messageType = parsedMessage.messageType;
|
|
const messageData = parsedMessage.data;
|
|
console.log("Message type:" + messageType);
|
|
// Route to appropriate handler, default if no match
|
|
(handlers[messageType] || handlers.default)(messageData);
|
|
} catch (e) {
|
|
console.error("Error parsing message:", e);
|
|
}
|
|
}
|
|
|
|
return {
|
|
handle: messageRouter
|
|
};
|
|
})();
|