// o---------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net | // | | // | RGraph is dual-licensed under the Open Source GPL license. This means | // | that it's free to use for any purpose. The GPL license does have | // | consequences on the license of the software that you include it in, | // | however. If this is not desirable, then there's an inexpensive commercial | // | license option available. See the RGraph website for more details. | // o---------------------------------------------------------------------------o ModalDialog = Modal = {}; Modal.notify = {}; Modal.Open = Modal.open = Modal.Show = Modal.show = Modal.Draw = Modal.draw = function (conf) { // // First, some functions that draw the dialog and its // background. // // // Draws the background // var drawBackground = function () { // Create the background Modal.background = document.createElement('DIV'); Modal.background.id = 'Modal_background'; Modal.background.className = 'Modal_background'; Modal.background.style.position = 'fixed'; Modal.background.style.top = '-50px'; Modal.background.style.left = '-50px'; Modal.background.style.width = 'calc(100% + 100px)'; Modal.background.style.height = 'calc(100% + 100px)'; Modal.background.style.backgroundColor = '#cccc'; Modal.background.style.opacity = Number(Modal.properties.opacityStart); Modal.background.style.transition = Modal.properties.effectShowDuration + "ms opacity ease-out"; Modal.container.appendChild(Modal.background); // If the relevant property is set add an event listener // for it. if (Modal.properties.hideOnBackground) { Modal.background.addEventListener('click', function (e) { Modal.hide(); }, false); } }; // // Draws the dialog // var drawDialog = function () { // Allow the number to be be both a string or a number. // If its a number then treat as milliseconds if (typeof Modal.properties.effectDuration === 'number' || (typeof Modal.properties.effectDuration === 'string' && Modal.properties.effectDuration.match(/^[0-9]+$/))) { Modal.properties.effectDuration = parseInt(Modal.properties.effectDuration); } // Create the dialog div Modal.dialog = document.createElement('div'); Modal.dialog.id = 'Modal_dialog'; Modal.dialog.className = 'Modal_dialog'; Modal.dialog.style.borderRadius = '5px'; Modal.dialog.style.position = 'fixed'; Modal.dialog.style.backgroundColor = 'white'; Modal.dialog.style.border = '0'; Modal.dialog.style.padding = '5px'; Modal.dialog.style.opacity = Number(Modal.properties.opacityStart), Modal.dialog.style.transition = Modal.properties.effectShowDuration + 'ms opacity ease-out, ' + Modal.properties.effectShowDuration + 'ms transform ease-out'; Modal.dialog.style.transform = 'scale(' + Modal.properties.zoomFactorStart + ') rotate(' + Modal.properties.rotationStart + ') translate(' + Modal.properties.translateStart + ') skew(' + Modal.properties.skewStart + ')'; Modal.dialog.style.left = 0; Modal.dialog.style.top = 0; Modal.container.appendChild(Modal.dialog); // // Add the content DIV that the given content sits inside // var content = document.createElement('DIV'); content.id = 'Modal_content'; content.className = 'Modal_content '; content.style.width = '100%'; content.style.height = '100%'; Modal.dialog.appendChild(content); if (Modal.id.toLowerCase().substring(0, 7) == 'string:') { content.innerHTML = Modal.id.substring(7); } else { content.innerHTML = document.getElementById(Modal.id).innerHTML; } // // Show the dialog // Modal.dialog.style.visibility = 'visible'; // Fade the dialog in and position it now that the width // is known. setTimeout(function () { // Position the dialog // var position = Modal.properties.position.trim().toLowerCase().split(/\s+/); // // Determine the X position. // var xUnits = '%'; if (position[0].trim() === 'left') { var x = 25; } else if (position[0].trim() === 'right') { var x = 75; } else if (position[0].trim() === 'center') { var x = 50; } else if (position[0].trim().match(/^([.0-9]+)%?$/)) { var x = parseFloat(RegExp.$1); } else if (position[0].trim().match(/^([-.0-9]+)px$/)) { xUnits = 'px'; var x = parseFloat(RegExp.$1); } // // Determine the Y position // var yUnits = '%'; if (position[1].trim() === 'top') { var y = 25; } else if (position[1].trim() === 'bottom') { var y = 75; } else if (position[1].trim() === 'center') { var y = 50; } else if (position[1].trim().match(/^([.0-9]+)%?$/)) { var y = parseFloat(RegExp.$1); } else if (position[1].trim().match(/^([-.0-9]+)px$/)) { yUnits = 'px'; var y = parseFloat(RegExp.$1); } // // Calculate the horizontal off set because of // horizontal allignment. // var align = Modal.properties.align.match(/^\s*(left|center|right)\s*(top|center|bottom)\s*$/); var halign = RegExp.$1; var valign = RegExp.$2; var hoffset = 0; var voffset = 0; // // Horizontal offset // if (halign === 'left') { hoffset = 0; } else if (halign === 'right') { hoffset = Modal.dialog.offsetWidth; } else if (halign === 'center') { hoffset = Modal.dialog.offsetWidth / 2; } // // Vertical offset // if (valign === 'top') { voffset = 0; } else if (valign === 'bottom') { voffset = Modal.dialog.offsetHeight; } else if (valign === 'center') { voffset = Modal.dialog.offsetHeight / 2; } Modal.dialog.style.left = 'calc(' + x + xUnits + ' - ' + hoffset + 'px)' Modal.dialog.style.top = 'calc(' + y + yUnits + ' - ' + voffset + 'px)' Modal.dialog.style.transform = ''; Modal.dialog.style.opacity = 1; Modal.background.style.opacity = 1; }, 15); }; /////////////////////////////////////////// // THE ACTUAL CODE THAT RUNS STARTS HERE // /////////////////////////////////////////// // Some object properties Modal.id = conf.id; Modal.uid = Modal.random(Date.now(), 9999999999999); Modal.dialog = null; Modal.background = null; Modal.eventListeners = []; Modal.properties = { zoomFactorEnd: 0, zoomFactorStart: 0, hideOnBackground: true, pageScroll: false, rotationStart: '45deg', rotationEnd: '45deg', translateStart: '0px, 0px', translateEnd: '0px, 0px', skewStart: '0deg, 0deg', skewEnd: '0deg, 0deg', opacityStart: 0, opacityEnd: 0, position: 'center center', align: 'center center', style: [], effectShowDuration: 500, effectHideDuration: 500, zIndex: 10000 }; // Copy the given config on to the properties object if (conf.options) { conf.properties = conf.options; } // // Add the reverse look-up table for property names // so that property names can be specified in any case. // Modal.properties_lowercase_map = []; for (var j in Modal.properties) { if (typeof j === 'string') { Modal.properties_lowercase_map[j.toLowerCase()] = j; } } for (i in conf.properties) { var value = conf.properties[i]; // // Allow the translate properties to be with or without units // if (i === 'translateStart' || i === 'translateEnd') { value = String(value); value.match(/^\s*([-.0-9]+)\s*(?:px)?(?:[, ]+([-.0-9]+)\s*(?:px)?)?\s*$/) value = RegExp.$1 + 'px, ' + Number(RegExp.$2 || 0) + 'px'; } // // Allow the skew properties to be with or without units // if (i === 'skewStart' || i === 'skewEnd') { value = String(value); value.match(/^\s*([-.0-9]+)\s*(deg|rad)?(?:[, ]+([-.0-9]+)\s*(deg|rad)?)?\s*$/) value = RegExp.$1 + (RegExp.$2 || 'deg') + ', ' + Number(RegExp.$3 || 0) + (RegExp.$4 || 'deg'); } // // Allow the skew properties to be with or without units // if (i === 'rotationStart' || i === 'rotationEnd') { value = String(value); value.match(/^\s*([-.0-9]+)\s*(deg|rad)?\s*$/) value = RegExp.$1 + (RegExp.$2 || 'deg'); } if (Modal.properties_lowercase_map[i.toLowerCase()]) { i = Modal.properties_lowercase_map[i.toLowerCase()]; } Modal.properties[i] = value; } // Create a CONTAINER DIV that the dialog and the background // are added to. var container = document.createElement('DIV'); container.id = 'Modal_container_' + Modal.uid; container.className = 'Modal_container'; container.style.position = 'relative'; container.style.zIndex = Modal.properties.zIndex; document.body.appendChild(container); Modal.container = container; drawBackground(); drawDialog(); // // Now add any style that's defined in the style option. // Prefix each entry with "div#Modal_container". // if (Modal.properties.style) { for (var i=0; i' + (properties.header ? properties.header : '') + properties.text + '
 
' + (properties.footer ? properties.footer : '') + '', options: { rotationStart: 0, rotationEnd: 0, zoomFactorStart: 1, zoomFactorEnd: 1, hideOnBackground: false, ...properties.dialog } }); // Make the OK button the default var okButton = document.getElementById('rgraph-modal-ok-button'); if (okButton) { okButton.focus(); } // // This function runs when the OK button on the dialog // is pressed. // Modal.confirmConfirmationModal = function () { if (typeof properties.callbacks.ok === 'function') { (properties.callbacks.ok)(Modal.dialog); } // Get rid of the dialog Modal.hide(); }; // // This function runs when the dialog is cancelled // Modal.cancelConfirmationModal = function () { if (typeof properties.callbacks.cancel === 'function') { (properties.callbacks.cancel)(Modal.dialog); } // Get rid of the dialog Modal.hide(); }; // If the Esc key is pressed then do this as well as // hiding the dialog. document.body.addEventListener('keydown', Modal.confirm.keyDownListener = function (e) { if (e.keyCode === 13) { Modal.confirmConfirmationModal(); e.stopPropagation(); e.preventDefault(); } else if (e.keyCode === 27) { Modal.cancelConfirmationModal(); e.stopPropagation(); e.preventDefault(); } }, false); }; // // This function hows a modal dialog success dialog // Modal.notify.success = function (properties) { if (typeof properties === 'string') { properties = {text: properties}; } Modal.confirm({ text: '
 Success
' + properties.text, dialog: { style: [ ', button {font-size: 20pt;}', 'button#rgraph-modal-success-button {font-size: 20pt;}', 'button#rgraph-modal-cancel-button {display: none;}' ], ...properties.dialog } }); }; // // This function hows a modal dialog warning dialog // Modal.notify.warning = function (properties) { if (typeof properties === 'string') { properties = {text: properties}; } Modal.confirm({ text: '
 Warning
' + properties.text, dialog: { style: [ ', button {font-size: 20pt;}', 'button#rgraph-modal-success-button {font-size: 20pt;}', 'button#rgraph-modal-cancel-button {display: none;}' ], ...properties.dialog } }); }; // // This function hows a modal dialog information dialog // Modal.notify.info = Modal.notify.information = function (properties) { if (typeof properties === 'string') { properties = {text: properties}; } Modal.confirm({ text: '
 Information
' + properties.text, dialog: { style: [ ', button {font-size: 20pt;}', 'button#rgraph-modal-success-button {font-size: 20pt;}', 'button#rgraph-modal-cancel-button {display: none;}' ], ...properties.dialog } }); }; // // This function hows a modal dialog error dialog // Modal.notify.error = function (properties) { if (typeof properties === 'string') { properties = {text: properties}; } Modal.confirm({ text: '
 Error
' + properties.text, dialog: { style: [ '{min-width: 300px;}', ', button {font-size: 20pt;}', 'button#rgraph-modal-success-button {font-size: 20pt;}', 'button#rgraph-modal-cancel-button {display: none;}' ], ...properties.dialog } }); }; // // Adds a style declaration to the document. You give the whole // selector/style string and it will add it. // // If a style element hasn't already been created // it will create one and then reuse it on subsequent // calls to this function. // // @param string style The styles to assign to the // new CSS. An example: // // table tr td {color: red;} // // @param object styleElement The style element to which the // style is attached. // @param object parent If given (default is null), // this is the node to which the // style element is attached. If // not given then the style element // is added to the . // Modal.addCss = function (style, styleElement = null, parent = null) { // Create a style element and add the CSS var el = styleElement || document.createElement('style'); el.insertAdjacentHTML('beforeend', style); // Append the style element to the document? Only // do this on newly created style elements. You can // optionally pass in an element on which to attach // the style element instead of the head. if (parent) { parent.appendChild(el); } else { document.head.appendChild(el); } return el; }; // // An updated clone function that works better. // // @param array mixed The variable to clone and // return a copy of. Doesn't // clone objects. // @param objects boolean Whether to clone objects or not, // default is not to. // Modal.arrayClone = function (array, objects = false, maxdepth = 5) { // This is here to limit recurusion if (maxdepth < 0) { return; } var ret = null; // Account for undefined values. if (typeof array === 'undefined') { return undefined; } // Account for null values. if (typeof array === 'object' && !array) { return null; } switch(typeof array) { case 'string': ret = (function (str){return String(str)})(array); break; case 'number': ret = (function (num){return Number(num)})(array); break; case 'boolean': ret = array; break; case 'object': if (array.constructor.toString().indexOf('Array') >= 0) { ret = new Array(); for (var i=0; i 0 && objects) { ret = new Object(); for (var i in array) { ret[i] = Modal.arrayClone(array[i], true, maxdepth - 1); } } break; case 'function': ret = array; break; } return ret; }; // // Determines if the given object is an array or not. // // @param mixed value The variable to test. // @return boolean Whether the given value is an array // or not. // Modal.isArray = function (value) { if (value && value.constructor) { var pos = value.constructor.toString().indexOf('Array'); } else { return false; } return value != null && typeof pos === 'number' && pos > 0 && pos < 20; }; // // Generates a random number between the minimum and maximum. // // @param number min The minimum value. // @param number max The maximum value. // @param number OPTIONAL Number of decimal places. // Modal.random = function (min, max, decimals = 0) { var r = Math.random(); return Number((((max - min) * r) + min).toFixed(decimals)); }; // // Debug functions // window.$a = alert; // // Loosely mimicks the PHP function print_r(). // // @param mixed obj The variable you want to see. // @param boolean notify Whether to use the alert() function // to notify you or not. Defaults to // true. // @param string indent The indent to yse when showing nested // properties. Defaults to 4 spaces. // @param number counter Used internally to control nesting. // You won't need to give this. // window.$p = function (obj, notify = true, indent = ' ', counter = 0) { var str = ''; if (counter >= 3) { return 'INDENT TOO MUCH!'; } switch (typeof obj) { case 'string': str += obj + ' (' + (typeof obj) + ', ' + obj.length + ')'; break; case 'number': str += obj + ' (' + (typeof obj) + ')'; break; case 'boolean': str += obj + ' (' + (typeof obj) + ')'; break; case 'function': str += 'function () {}'; break; case 'undefined': str += 'undefined'; break; case 'null': str += 'null'; break; case 'object': // In case of null if (RGraph.isNullish(obj)) { str += 'null(ish)'; } else { str += 'Object {' + '\n' for (var j in obj) { str += indent + ' ' + j + ' => ' + RGraph.pr(obj[j], false, indent + ' ', counter + 1) + '\n'; } str += indent + '}'; } break; default: str += 'Unknown type: ' + typeof obj + ''; break; } // // Finished, now either return if we're in a recursed // call, or alert() if we're not. // if (notify) { alert(str); } return str; };