77 lines
3.0 KiB
JavaScript
77 lines
3.0 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) {
|
|
toastr.success(`<div class="item bidToast"><p>${data.bidPrice}</p><p>${data.productName}</p></div>`, "New bid arrived", {
|
|
"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");
|
|
},
|
|
auctionUpdate: function (data) {
|
|
const widgetPriceElement = document.getElementById("WidgetPrice");
|
|
if (widgetPriceElement) {
|
|
widgetPriceElement.textContent = data.bidPrice; // Update the price
|
|
console.log(`WidgetPrice updated to: ${data.bidPrice}`);
|
|
} else {
|
|
console.warn("Element with ID 'WidgetPrice' not found in the DOM.");
|
|
}
|
|
},
|
|
// 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 {
|
|
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
|
|
};
|
|
})();
|