/* * Lert v1.0 * by Jeffrey Sambells - http://JeffreySambells.com * For more information on this script, visit http://JeffreySambells.com/openprojects/lert/ * Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/ * Icons from Tango Desktop Project http://tango.freedesktop.org/Tango_Desktop_Project */ function Lert(title, message, buttons, options) { this.title_ = title; this.message_ = message; this.buttons_ = buttons; this.defaultButton_ = options.defaultButton || this.buttons_[0]; this.icon_ = options.icon || null; } Lert.prototype.display = function() { var body = document.getElementsByTagName ('BODY')[0]; //create the overlay if necessary var overlay = document.getElementById('lertOverlay'); if(!overlay) { var overlay = document.createElement("div"); overlay.setAttribute('id','lertOverlay'); overlay.style.display = 'none'; body.appendChild(overlay); } //position and show the overlay overlay.style.height='100%'; overlay.style.display='block'; //create the container if necessary var container = document.getElementById('lertContainer'); if(!container) { var container = document.createElement("div"); container.setAttribute('id','lertContainer'); container.style.display = 'none'; body.appendChild(container); } //position and show the container container.style.top = '35%'; container.style.display = 'block'; //create the window var win = document.createElement('div'); win.setAttribute('id','lertWindow'); var topBorder = document.createElement('div'); topBorder.setAttribute('id','topBorder'); topBorder.innerHTML = this.title_; var iconClose = document.createElement('img'); iconClose.setAttribute('src', 'js/lert/images/close_button.gif'); iconClose.setAttribute('id','closeIcon'); iconClose.setAttribute('alt',''); iconClose.setAttribute('style','cursor: pointer;'); iconClose.setAttribute('onclick', "document.getElementById('lertOverlay').style.display='none';document.getElementById('lertContainer').style.display='none';document.getElementById('lertContainer').innerHTML = '';"); topBorder.appendChild(iconClose); win.appendChild(topBorder); //create the optional icon if(this.icon_ != null) { var icon = document.createElement('img'); icon.setAttribute('src',this.icon_); icon.setAttribute('id','lertIcon'); icon.setAttribute('alt',''); win.appendChild(icon); } //create the message space var message = document.createElement('p'); message.setAttribute('id','lertMessage'); message.innerHTML = this.message_; win.appendChild(message); //create the button space var buttons = document.createElement('div'); buttons.setAttribute('id','lertButtons'); var oldKeyDown = document.onkeydown; //add each button for(i in this.buttons_) { var button = this.buttons_[i]; if(button.getDom) { var domButton = button.getDom(function() { container.style.display = 'none'; overlay.style.display = 'none'; document.onkeydown=oldKeyDown; container.innerHTML = ''; button.onclick_; },this.defaultButton_); buttons.appendChild(domButton); } } win.appendChild(buttons); document.onkeydown = this.keyboardControls; //append the window container.appendChild(win); } Lert.prototype.keyboardControls = function(e) { if (e == null) { keycode = event.keyCode; } // ie else { keycode = e.which; } // mozilla if(keycode==13){ document.getElementById('lertDefaultButton').onclick(); } } function LertButton(label, event, options) { this.label_ = label; this.onclick_ = event; this.eventClick = function() {}; } LertButton.prototype.getDom = function(eventCleanup,defaultButton) { var button = document.createElement('a'); //button.setAttribute('href','javascript:void(0);'); button.className = 'lertButton'; if(this == defaultButton) button.setAttribute('id','lertDefaultButton'); button.innerHTML = this.label_; var eventOnclick = this.onclick_; button.onclick = function() { eventCleanup(); eventOnclick(); } this.eventClick = button.onclick; return button; }