/** * Class to manage a queue of messages to show the user in a message bar */ cambrient.UserMessaging = function() { var MESSAGE_BAR_HEIGHT = 40; var MESSAGE_BAR_BGCOLOR = 'ffff00'; var self; // pointer to instance of this class var mb; // the message bar div var messageQueue = new Array(); var messageBarOpen = false; var tOpen, tClose; // tweening animations for open and close function constructorFn() { self = this; registerOnLoad(self.init); } // initialize constructorFn.prototype.init = function() { mb = $('message_bar'); if (mb) { mb.style.display = 'none'; mb.style.visibility = 'hidden'; mb.style.zIndex = 1000; mb.onclick = function(evt) { self.closeBar(); } // define opening animation tOpen = new Tween(mb.style, 'height', Tween.strongEaseOut, 0, 40, 1, 'px'); tOpen.onMotionFinished = function() { var tFade = new ColorTween(mb.style, 'backgroundColor', Tween.bounceEaseOut, 'ffffff', MESSAGE_BAR_BGCOLOR, 3); tFade.start(); } // define closing animation tClose = new Tween(mb.style, 'height', Tween.strongEaseOut, MESSAGE_BAR_HEIGHT, 0, 1, 'px'); tClose.onMotionFinished = function() { mb.style.display = 'none'; mb.style.visibility = 'hidden'; } } self.processMessageQueue(); } // Routinely checks for messages in the message queue and displays them constructorFn.prototype.processMessageQueue = function() { if (!messageBarOpen) { if (messageQueue.length > 0) { var msg = messageQueue.shift(); if (mb) { if (msg.type == 'error') { MESSAGE_BAR_BGCOLOR = 'ff0000'; } else { MESSAGE_BAR_BGCOLOR = 'ffff00'; } // redefine the color tween in case the background color changed tOpen.onMotionFinished = function() { var tFade = new ColorTween(mb.style, 'backgroundColor', Tween.bounceEaseOut, 'ffffff', MESSAGE_BAR_BGCOLOR, 3); tFade.start(); } mb.innerHTML = msg.message; self.openBar(); } else { //alert("UNHANDLED MESSAGING" + msg.message); if(window.console) { console.log("UNHANDLED MESSAGING " + msg.message); } } } } setTimeout(self.processMessageQueue, 1000); } // Close the message bar constructorFn.prototype.closeBar = function() { tOpen.stop(); // if it's still running messageBarOpen = false; tClose.start(); } // Open the message bar constructorFn.prototype.openBar = function() { tClose.stop(); messageBarOpen = true; mb.style.backgroundColor = MESSAGE_BAR_BGCOLOR; mb.style.height = 0; //MESSAGE_BAR_HEIGHT; mb.style.display = 'block'; mb.style.visibility = 'visible'; tOpen.start(); } // adds a message to the message queue constructorFn.prototype.addMessage = function(msg) { var hasMsg = false; messageQueue.each(function(item) { if (item.message != null && item.message == msg.message) { hasMsg = true; } }); if (!hasMsg) { messageQueue.push(msg); } } // Display informational message to user constructorFn.prototype.info = function(msg) { self.addMessage({ "message": msg, "type": "info" }); } // Display error message to user constructorFn.prototype.error = function(msg) { self.addMessage({ "message": msg, "type": "error" }); } // Display error message to user constructorFn.prototype.warning = function(msg) { self.addMessage({ "message": msg, "type": "warning" }); } // Display error message to user constructorFn.prototype.confirm = function(msg) { console.log('ERROR: ' + msg); self.addMessage({ "message": msg, "type": "confirm" }); } // Display debug message to developer constructorFn.prototype.debug = function(msg) { self.addMessage({ "message": msg, "type": "debug" }); } constructorFn.prototype.test = function() { cambrient.userMessaging.info("Hello world!"); cambrient.userMessaging.info("Hello world!"); cambrient.userMessaging.info("Hello world!"); cambrient.userMessaging.error("ERROR: There was an unknown error trying to parse the error for display. Just kidding!"); cambrient.userMessaging.info("Hello world 2!"); cambrient.userMessaging.info("Hello world 2!"); cambrient.userMessaging.info("Hello world 2!"); } return new constructorFn(); } cambrient.userMessaging = new cambrient.UserMessaging();