/* * Cools dialog library * @author Alek$ * @date 06-09-2008 * * Thanks to ksnk, cruelangel and smartov */ var replace_alert = true; var replace_confirm = false; var replace_prompt = false; // Main dialog logics is placed here function BuildDialog(type, callback) { var myself = this; // // Internal function // // Build common dialog elements this.basicDialog = function () { // Create basic layer this.win = document.createElement('div'); this.win.className = "popup_dialog"; // Create header bar var headbar = document.createElement('div'); headbar.className = "popup_headerbar"; // Create header controls var controlClose = document.createElement('span'); controlClose.className = "popup_control"; controlClose.innerHTML = "X"; controlClose.onclick = function() { myself.close(); myself.callback(false); } headbar.appendChild(controlClose); this.win.appendChild(headbar); // Create text container this.cont = document.createElement('span'); this.cont.className = "popup_content"; this.win.appendChild(this.cont); // Add Escape handler this.win.onkeydown = function (e) { var keynum; if(window.event) keynum = event.keyCode; else if(e.which) keynum = e.which; if(keynum == 27) { myself.close(); myself.callback(false); } } } // Build elements for alert dialog this.buildAlert = function () { // Create button var buttonDiv = document.createElement('div'); buttonDiv.className = "popup_buttons"; // Create OK button var bOK = document.createElement('input'); bOK.type = "button"; bOK.value = "OK"; bOK.className = "form-control"; bOK.onclick = function() { myself.close(); } buttonDiv.appendChild(bOK); this.win.appendChild(buttonDiv); // Set focus function this.focus = function () { bOK.focus(); } } // Build elements for confirm dialog this.buildConfirm = function () { // Create button var buttonDiv = document.createElement('div'); buttonDiv.className = "popup_buttons"; // Create OK button var bOK = document.createElement('input'); bOK.type = "button"; bOK.value = "OK"; bOK.style.width = '50%'; bOK.style.float = 'left'; bOK.className = "form-control"; bOK.onclick = function() { myself.close(); myself.callback(true); } buttonDiv.appendChild(bOK); // Create Cancel button var bCancel = document.createElement('input'); bCancel.type = "button"; bCancel.value = "Cancel"; bCancel.className = "form-control"; bCancel.style.width = '50%'; bCancel.onclick = function() { myself.close(); myself.callback(false); } buttonDiv.appendChild(bCancel); this.win.appendChild(buttonDiv); // Set focus function this.focus = function () { bOK.focus(); } } // Build elements for prompt dialog this.buildPrompt = function () { // Create input var textInput = document.createElement('textarea'); textInput.style.width = '95%'; this.win.appendChild(textInput); // Create button var buttonDiv = document.createElement('div'); buttonDiv.className = "popup_buttons"; // Create OK button var bOK = document.createElement('input'); bOK.type = "button"; bOK.value = "OK"; bOK.className = "form-control"; bOK.style.width = '50%'; bOK.style.float = 'left'; bOK.onclick = function() { myself.close(); myself.callback(textInput.value); } buttonDiv.appendChild(bOK); // Create Cancel button var bCancel = document.createElement('input'); bCancel.type = "button"; bCancel.value = "Cancel"; bCancel.className = "form-control"; bCancel.style.width = '50%'; bCancel.onclick = function() { myself.close(); myself.callback(false); } buttonDiv.appendChild(bCancel); this.win.appendChild(buttonDiv); // Add Enter handler textInput.onkeydown = function (e) { var keynum; if(window.event) keynum = event.keyCode; else if(e.which) keynum = e.which; /*if(keynum == 13) { myself.close(); myself.callback(textInput.value); // Here is a little magic return false; }*/ } // Add specific function for seting text field value this.setValue = function (value) { textInput.value = value; } // Set focus function this.focus = function () { bOK.focus(); } } // Set dialog text this.setText = function (text) { this.cont.innerHTML = text; } // Display dialog this.show = function () { document.body.appendChild(this.win); this.win.style.top = '0px'; this.win.style.left = (document.body.clientWidth - 300)/2 + 'px'; this.win.focus(); this.focus(); } // Close dialog this.close = function () { document.body.removeChild(this.win); } this.focus = function () {}; // // Start building dialog // this.basicDialog(); // Set callback function if(callback == null) { callback = function(result) {}; } this.callback = callback; // Build specific elements for if(type == 'alert') { // alert this.buildAlert(); } else if(type == 'confirm') { // confirm this.buildConfirm(); } else if(type == 'prompt') { //prompt this.buildPrompt(); } } // Main alert function function myAlert (text) { var win = new BuildDialog('alert', null); win.setText(text); win.show(); //defaultAlert(text); } // Main confirm function function myConfirm(text, callback) { var win = new BuildDialog('confirm', callback); win.setText(text); win.show(); } // Main prompt function function myPrompt(text, value, callback) { var win = new BuildDialog('prompt', callback); win.setText(text); win.setValue(value); win.show(); } // // Replace default functions // // Store default functions window.defaultAlert = window.alert; window.defaultConfirm = window.alert; window.defaultPrompt = window.alert; // Replace if(replace_alert) { window.alert = myAlert; } if(replace_confirm) { window.confirm = myConfirm; } if(replace_prompt) { window.prompt = myPrompt; }