/* Utility functions */ function bindEventHandler(element, eventName, handler) { if (element.addEventListener) { // The standard way element.addEventListener(eventName, handler, false); } else if (element.attachEvent) { // The Microsoft way element.attachEvent('on' + eventName, handler); } } /** * The modal dialog class * @constructor */ function Dialog(options) { this.options = { width: 400, top: 120, openOnCreate: true, destroyOnClose: true, escHandler: this.close, buttons: {button1:{title : 'Ok', 'action': this.close}} }; // Overwrite the default options for (var option in options) { this.options[option] = options[option]; } this.parentElement = (this.options.parent) ? (this.options.parent) : document.body; // Creamos el frame var divshim = document.getElementById('DivShim'); if (!divshim) { var iframe = document.createElement("iframe"); iframe.id = "DivShim"; iframe.src = "javascript:false;"; iframe.scrolling = "no"; iframe.frameBorder = 0; iframe.style.display = "none"; iframe.style.top = "0px"; iframe.style.left = "0px"; this.divshim = this.parentElement.appendChild(iframe); } else this.divshim = divshim; // Create dialog dom this._makeNodes(); if (this.options.openOnCreate) { this.open(); } } Dialog.prototype = { /* handles to the dom nodes */ container: null, header: null, body: null, content: null, actions: null, _overlay: null, _wrapper: null, _zIndex: 0, _escHandler: null, /** * Shows the dialog */ open: function() { this._makeTop(); var ws = this._wrapper.style; if (this.options.modal) this._overlay.style.display = 'block'; ws.display = 'block'; this._wrapper.focus(); if (this.options.notcentered) { ws.left = this.options.left + "px"; ws.top = this.options.top + "px"; } else { ws.left = (this.parentElement.clientWidth - this.options.width) / 2 + 'px'; if (this.options.parent) ws.top = (this.parentElement.clientHeight - this._wrapper.offsetHeight) / 2 + 'px'; else ws.top = (this.parentElement.scrollTop || document.documentElement.scrollTop) + this.options.top + 'px'; } this.updateContent(); if (this.options.focus) { var input = document.getElementById(this.options.focus); if (input) { input.focus(); } } }, /** * Closes the dialog */ close: function() { if (this.options.destroyOnClose) { this._destroy(); } else { if (this.options.modal) this._overlay.style.display = 'none'; this._wrapper.style.display = 'none'; } var IfrRef = document.getElementById('DivShim'); IfrRef.style.display = "none"; }, /** * Add buttons to the dialog actions panel after creation * @param {object} buttons Object with property name as button text and value as click handler * @param {boolean} prepend If true, buttons will be prepended to the panel instead of being appended */ addButtons: function(buttons, prepend) { var actions = this.actions; var buttonArray = this._makeButtons(buttons); var first = null; if (prepend && (first = actions.firstChild) != null) { for (var i in buttonArray) { actions.insertBefore(buttonArray[i], first); } } else { for (var i in buttonArray) { actions.appendChild(buttonArray[i]); } } }, isValidEmail : function(str) { if (str.indexOf("localhost") > 2) return (str.indexOf("@") > 0) && (str.indexOf(",") == -1) && (str.indexOf(";") == -1); else return (str.indexOf(".") > 2) && (str.indexOf("@") > 0) && (str.indexOf(",") == -1) && (str.indexOf(";") == -1); }, trimAll : function(sString) { while (sString.substring(0,1) == ' ') sString = sString.substring(1, sString.length); while (sString.substring(sString.length-1, sString.length) == ' ') sString = sString.substring(0,sString.length-1); return sString; }, updateContent : function() { // Aqui modificamos el script para que se situe encima del applet this.divshim.style.position = 'absolute'; this.divshim.style.width = this._wrapper.style.width; this.divshim.style.height = this._wrapper.offsetHeight + "px"; this.divshim.style.top = this._wrapper.style.top; this.divshim.style.left = this._wrapper.style.left; this.divshim.style.zIndex = this._wrapper.style.zIndex - 1; this.divshim.style.display = "block"; }, /** * Change (or set) title after creation * @param {string} title The dialog title */ setTitle: function(title) { if (!this.header) { var header = document.createElement('div'); header.className = 'dialog-header'; this.container.insertBefore(header, this.body); this.header = header; } this.header.innerHTML = title; }, /** * Makes the dom tree for the dialog */ _makeNodes: function() { if ((this._overlay && this.options.modal) || this._wrapper) { return; // Avoid duplicate invocation } if (this.options.modal) { // Make overlay this._overlay = document.createElement('div'); this._overlay.className = 'dialog-overlay'; this.parentElement.appendChild(this._overlay); } if (typeof this.options.title == 'string' && this.options.title != '') { var header = document.createElement('div'); header.className = 'dialog-header'; header.innerHTML = this.options.title; this.header = header; } // {begin dialog body var content = document.createElement('div'); if (!this.options.notbuttons) content.className = 'dialog-content'; content.innerHTML = this.options.content; this.content = content; // {begin actions panel var actions = document.createElement('div'); if (!this.options.notbuttons) actions.className = 'dialog-actions'; if (!this.options.notbuttons) { var buttons = this._makeButtons(this.options.buttons); if (buttons.length > 0) { for (var i in buttons) { actions.appendChild(buttons[i]); } } } this.actions = actions; // }end actions panel var body = document.createElement('div'); body.className = 'dialog-body'; body.appendChild(content); body.appendChild(actions); this.body = body; // }end dialog body var container = document.createElement('div'); container.className = 'dialog'; if (this.header) { container.appendChild(header); } container.appendChild(body); this.container = container; var wrapper = document.createElement('div'); wrapper.className = 'dialog-wrapper'; var ws = wrapper.style; ws.position = 'absolute'; ws.width = this.options.width + 'px'; if (this.options.width) ws.display = 'none'; ws.outline = 'none'; wrapper.appendChild(container); // register keydown event if (this.options.escHandler) { wrapper.tabIndex = -1; this._onKeydown = this._makeHandler(function(e) { if (!e) { e = window.event; } if (e.keyCode && e.keyCode == 27) { this.options.escHandler.apply(this); } }, this); bindEventHandler(wrapper, 'keydown', this._onKeydown); } this._wrapper = this.parentElement.appendChild(wrapper); if (Dialog.needIEFix) { this._fixIE(); } }, /** * Removes the nodes from document * @param {object} buttons Object with property name as button text and value as click handler * @return {Array} Array of buttons as dom nodes */ _makeButtons: function(buttons) { var buttonArray = new Array(); for (var buttonText in buttons) { var button = document.createElement('button'); button.className = 'dialog-button'; button.innerHTML = buttons[buttonText].title; bindEventHandler(button, 'click', this._makeHandler(buttons[buttonText].action, this)); buttonArray.push(button); } return buttonArray; }, /** A helper function used by makeButtons */ _makeHandler: function(method, obj) { return function(e) { method.call(obj, e); } }, /** A helper function used by open */ _makeTop: function() { if (this._zIndex < Dialog.Manager.currentZIndex) { if (this.options.modal) this._overlay.style.zIndex = Dialog.Manager.newZIndex(); this._zIndex = this._wrapper.style.zIndex = Dialog.Manager.newZIndex(); } }, _fixIE: function() { var width = document.documentElement["scrollWidth"] + 'px'; var height = document.documentElement["scrollHeight"] + 'px'; if (this.options.modal) { var os = this._overlay.style; os.position = 'absolute'; os.width = width; os.height = height; } var iframe = document.createElement('iframe'); iframe.className = 'iefix'; iframe.style.width = width; iframe.style.height = height; this._wrapper.appendChild(iframe); }, /** * Removes the nodes from document */ _destroy: function() { this.parentElement.removeChild(this._wrapper); if (this.options.modal) this.parentElement.removeChild(this._overlay); this.container = null; this.header = null; this.body = null; this.content = null; this.actions = null; this._overlay = null; this._wrapper = null; } }; Dialog.needIEFix = (function () { var userAgent = navigator.userAgent.toLowerCase(); return /msie/.test(userAgent) && !/opera/.test(userAgent) && !window.XMLHttpRequest; })(); /** This simple object manages the z indices */ Dialog.Manager = { currentZIndex: 3000, newZIndex: function() { return ++this.currentZIndex; } };