/***************************************************************** * ATTENTION! * This version TransModal 0.0.4 is intended * for testing and debugging purpose only. * It is NOT intended for a practical use. *****************************************************************/ function TransModal() { /* Enforcing TransModal to be a singleton: * for thar locking the conventional way * of directly creating class instances. */ if (this instanceof TransModal) { return TransModal; } /* Checking if not some weird environment * where window host object is not available. * * "window" identifier is reserved for the host * object. This way typeof window == 'object' * guarantees that either 1) host object is here * or 2) it is not here but someone is * emulating it. In the latter case it is not * our concern how good or bad such emulation is. */ if (typeof window != 'object') { return null; } /* Check if DOM manipulations are available: * other words if the document is loaded. * If not then postpone the execution till * after window load event. * TransModal.isIE flag is preset on conditional * compilation outside of the TransModal body: * see "Conditional compilation" in this code. */ if (document.body == null) { if (TransModal.isIE) { window.attachEvent('onload', TransModal); } else { window.addEventListener('load', TransModal, false); } return null; } /* If TransModal was already once executed * then do not repeat the initialization. */ if (TransModal.isExecuted) { return null; } else { TransModal.isExecuted = true; } /* It seems that all excuses are explored so * nothing is left but start doing the job :-) */ /* If "modal" identifier is not already in use, * then set it as a shortcut alias for TransModal. * This way TransModal.dialog() and modal.dialog() * calls will be equivalent. */ if (typeof modal == 'undefined') { modal = TransModal; } /* Prototype.js compatibility. * If global $() function is already defined * then do not override it. Otherwise making * a lightweight replacement of it. * * We don't want a parasite closure in here, * so we are using Function constructor. */ // ! BUG bug 0.0.4#1 // Possible jQuery namespace slash as per // http://docs.jquery.com/Using_jQuery_with_Other_Libraries if (typeof $ != 'function') { $ = new Function('id', 'return document.getElementById(id)'); } /* It is nice to have button labels in user's preferred language * and not English only. * * navigator.browserLanguage (IE) and navigator.language * (some other UAs) may have different meanings: OS language, * or browser interface language, or the preferred language * in the browser settings. See also: * http://msdn.microsoft.com/en-us/library/ms533542(VS.85).aspx * http://developer.mozilla.org/en/docs/DOM:window.navigator.language * Either way it still allows us to make a good guess of what language * the current user would like to see. If the detected language is * not implemented yet then English is used by default. * For such basic lexicon as "Yes", "No", "Cancel" etc. * we dare to disregard country-specific variations, so * we are taking only two first letters from the language * code - so say "en", "en_US", "en_GB" all come to "en". */ /* If any of known locale detection mechanics is available * then use it; otherwise use English labels by default. */ if ('browserLanguage' in navigator) { var lang = navigator.browserLanguage.substring(0,2); } else if ('language' in navigator) { var lang = navigator.language.substring(0,2); } else { var lang = 'en'; } /* If there is translation for the detected language * then use it; otherwise fall back to English. */ if (lang in TransModal.buttonLabelSet) { TransModal.lang = lang; } else { TransModal.lang = 'en'; } /* Creating and adding the dialog window template. * The dialog window SHOULD NOT be styled * by external CSS rules. All needed styling * should be being made within script. */ // Main container: var wndDialog = document.createElement('DIV'); wndDialog.id = 'TransModalDialog'; with (wndDialog.style) { visibility = 'hidden'; position = 'absolute'; zIndex = '1002'; left = '0px'; top = '0px'; margin = '0px 0px'; padding = '0px 0px'; borderWidth = '0px'; cursor = 'default'; } // Title bar: var wndDialogTitleBar = document.createElement('DIV'); wndDialogTitleBar.id = 'TransModalDialogTitleBar'; with (wndDialogTitleBar.style) { visibility = 'inherit'; margin = '0px 0px'; padding = '4px 2px'; width = '100%'; fontFamily = 'sans-serif'; fontSize = 'smaller'; fontWeight = 'bolder'; color = 'CaptionText'; backgroundColor = 'ActiveCaption'; borderWidth = 'thin'; borderStyle = 'outset'; // Making top left / right rounded corners: if (TransModal.isIE) { // ! VML not implemented yet } else if (TransModal.isGecko) { MozBorderRadiusTopleft = '8px'; MozBorderRadiusTopright = '8px'; } else if (TransModal.isSafari) { WebkitBorderTopLeftRadius = '8px'; WebkitBorderTopRightRadius = '8px'; } else { borderTopLeftRadius = '8px'; borderTopRightRadius = '8px'; } } wndDialog.appendChild(wndDialogTitleBar); // Body container for dialog icon, prompt and buttons: var wndDialogBody = document.createElement('DIV'); with (wndDialogBody.style) { visibility = 'inherit'; margin = '0px 0px'; padding = '4px 2px'; width = '100%'; backgroundColor = 'Window'; borderLeft = 'thin outset'; borderRight = 'thin outset'; borderBottom = 'thin outset'; } // Container for dialog icon: var wndDialogIcon = document.createElement('DIV'); wndDialogIcon.id = 'TransModalDialogIcon'; with (wndDialogIcon.style) { margin = '4px 4px'; padding = '0px 0px'; (TransModal.isIE) ? (styleFloat = 'left') : (cssFloat = 'left'); } // IE6 doesn't support alpha channels for .png images // Working around by using AlphaImageLoader filter: if (TransModal.isOldIE) { wndDialogIcon.style.width = '32px'; wndDialogIcon.style.filter = ''.concat( 'progid:DXImageTransform.Microsoft.AlphaImageLoader(', 'sizingMethod=\'image\',', 'src=\'./images/crystal_clear/important.png\')'); } else { var wndDialogIconImage = document.createElement('IMG'); wndDialogIconImage.src = './images/crystal_clear/important.png'; wndDialogIcon.appendChild(wndDialogIconImage); } // Container for dialog prompt: var wndDialogPrompt = document.createElement('DIV'); wndDialogPrompt.id = 'TransModalDialogPrompt'; with (wndDialogPrompt.style) { margin = '4px 4px'; padding = '4px 4px'; } // Container for dialog buttons: var wndDialogButtons = document.createElement('DIV'); wndDialogButtons.id = 'TransModalDialogButtons'; with (wndDialogButtons.style) { margin = '4px 4px'; padding = '4px 4px'; whiteSpace = 'nowrap'; textAlign = 'center'; } wndDialogBody.appendChild(wndDialogIcon); wndDialogBody.appendChild(wndDialogPrompt); wndDialogBody.appendChild(wndDialogButtons); wndDialog.appendChild(wndDialogBody); document.body.appendChild(wndDialog); /* Creating and adding the cover sheet. * The cover sheet SHOULD NOT be styled * by external CSS rules. All needed styling * should be made by script. */ var wndCover = document.createElement('DIV'); wndCover.id = 'TransModalVeil'; /* Sometimes styling of a completely empty element * may make IE to act strange. To avoid that we are * setting the default content to NO-BREAK SPACE */ wndCover.appendChild(document.createTextNode('\u00A0')); if (s = wndCover.style) { s.visibility = 'hidden'; s.position = 'absolute'; s.zIndex = '1001'; s.left = '0px'; s.top = '0px'; s.margin = '0px 0px'; s.padding = '0px 0px'; s.borderStyle = 'none'; /* Opera as of current v. 9.27 still * doesn't support "not-allowed" cursor style. * Shame, shame on them! */ s.cursor = (!TransModal.isOpera) ? 'not-allowed' : 'wait'; } /* Pre-applying alpha filter for IE. */ if (TransModal.isIE) { wndCover.style.filter = ''.concat( 'progid:DXImageTransform.Microsoft.Alpha(Opacity=', Math.floor(TransModal.coverOpacity * 100), ',Style=0)'); } document.body.appendChild(wndCover); } /************** TransModal methods **************/ /* TransModal.dialog() method that * takes six optional arguments: * * dlgPrompt (String) : * HTML code used as the dialog prompt. * It should be a well-formed reasonably compact block, * otherwise no guarantees are given for a usable outcome. * By default displays library's name and version. * * dlgButtonSet (number) : * 0 - OK only * 1 - OK, Cancel * 2 - Abort, Retry, Ignore * 3 - Yes, No, Cancel * 4 - Yes, No * 5 - Retry, Cancel * By default displays OK only (0) * * dlgDefaultButton (number) : * Index of the button in the set (starting with 0) * that initially has the focus. * By default the leftmost button (0) * * dlgListener (Function reference) : * function to call on button being pressed or clicked. * The first argument contains system button label (String). * * dlgIcon (String) : * Crystal Clear icon path to use as the dialog icon. * Crystal Clear icon set by Everaldo Coelho * is used under GNU Lesser General Public License. * Please see /images/crystal_clear/ directory * of this package for more details. * By default displays "/apps/important.png". * ! Not fully implemented yet. * * dlgTitle (String) : * Dialog title. * By default displays "Program message". * ! Not fully implemented yet. */ TransModal.dialog = function( dlgPrompt , dlgButtonSet , dlgDefaultButton , dlgListener , dlgIcon , dlgTitle) { /* Setting defaults for missing arguments. */ if (typeof dlgPrompt != 'string') { dlgPrompt = '
TransModal v.'.concat( TransModal.reportVersion(), '
'); } if (typeof dlgButtonSet != 'number') { dlgButtonSet = 0; } /* How many buttons in the selected set? */ var len = TransModal.buttonSet[dlgButtonSet].length; if ((typeof dlgDefaultButton != 'number') || (dlgDefaultButton >= len)) { dlgDefaultButton = 0; } if (typeof dlgListener != 'function') { TransModal.notifyObserver = TransModal.loopHole; } else { TransModal.notifyObserver = dlgListener; } if (typeof dlgIcon != 'string') { dlgIcon = 'important.png'; } if (typeof dlgTitle != 'string') { dlgTitle = 'Program message'; } /* Getting DOM references to the cover and to the dialog. */ var cover = $('TransModalVeil'); var dialog = $('TransModalDialog'); var buttons = $('TransModalDialogButtons'); buttons.innerHTML = ''; var button, label, tmp; for (var i=0; i