/* * General functions, shared by most different scripts and elements. */ // Object literal version of a timer script, can be used to benchmark script running time. var timeDiff = { setStartTime:function (){ d = new Date(); time = d.getTime(); }, getDiff:function (){ d = new Date(); return (d.getTime()-time); } } // Check if a certain HTMLElement has a given classname function hasClass(ele,cls) { if (ele.className) return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); } // Add a given classname to an HTMLElement function addClass(ele,cls) { if (!this.hasClass(ele,cls)) ele.className += " "+cls; } // Remove a given class from an HTMLElement function removeClass(ele,cls) { if (hasClass(ele,cls)) { var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); ele.className=ele.className.replace(reg,' '); } } // General function to handle AJAX functionality. // Specify the request string, url to send to, the function to execute on response and whether to run in the background or wait for a response before continuing. function newAjaxRequest(c, u, cb, async) { var xml; var content = c; //an array of strings, in "key=value" format. // assign a compatible request format if (window.XMLHttpRequest) { //Not IE5, IE6 xml = new XMLHttpRequest(); } else { //IE5, IE6 xml = new ActiveXObject("Microsoft.XMLHTTP"); } // subscribe the callback function to a response event xml.onreadystatechange = function() { xml.responseText = "Processing..."; if (xml.readyState == 4 && xml.status == 200) { cb(xml); } }; // initialize XMLRequest xml.open("POST", u, async); xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var contentString = ""; //iterate through parameters passed in variable c if (typeof(content)=='object'&&(input instanceof Array)) { // parameters were passed as an array of key=value strings for (var i = 0; i < content.length; i++) { contentString += content[i]; if (i != (content.length - 1)) { contentString += "&"; } } } else { // single parameter or string already formatted by calling function contentString = content; } // finally send the formatted request xml.send(contentString); } // Function that handles updating of info panel via AJAX getInfo.php. Currently operates on HTML to paste into #infoPanelContent, should eventually involve JSON arrays, to have more control over formatting. function ajaxInfoRequest(uid, el, type) { // Info panel update. var c = "uid="+uid; c += "&type="+type; var u = "getInfo.php"; newAjaxRequest(c, u, function(result) { el.innerHTML = result.responseText; }, true); } // Function to save the object currently being edited. // !! Still needs to be generalized !! function saveObject (confirmSave) { var answer = false; if (confirmSave==true) { answer = confirm("Save changes to pipeline?"); } else { answer = true; } if (answer == true) { var pipeline = document.getElementById("pipelineStringField").value; pipeline = pipeline.slice(0, pipeline.length - 1); var types = document.getElementById("pipelineTypeField").value; types = types.slice(0, types.length - 1); var session = document.getElementById("sessionField").value; var requestString = "uids="+pipeline+"&types="+types+"&sessionUid="+session; console.log(requestString); var success; newAjaxRequest(requestString, "savesession.php", function(result){ success = result.responseText; }, true); console.log(success); } } // Convert a string to an array and remove any empty items (from trailing commas) function stringToArray(s, c) { var a = s.split(c); for (var i = 0; i < a.length; i++) { // remove empty items if (a[i] == "") { a.splice(i, 1); i--; } } return a; } // Convert an array to a string, separated by character "c" function arrayToString(a, c) { var s = ""; for (var i = 0; i < a.length; i++) { if (a[i] != "") { s += a[i]+c; } } return s; } // Remove all newlines, carriage returns and tabs from a given string function removeNL(s){ return s.replace(/[\n\r\t]/g,""); } // Shorthand for document.createElement, because that pops up about three times every line of the main program function ce(s) { return document.createElement(s); } // Shorthand for document.getElementById, same reason as above function ge(s) { return document.getElementById(s); } // Function for getting the absolute position of the top left corner of an element, relative to the window function getPos(element) { var posX = posY = 0; if (element.offsetParent) { do { posX += element.offsetLeft; posY += element.offsetTop; } while (element = element.offsetParent); } var result = { X: posX, Y: posY } return result; } // TODO: this is a broken version of the above function, to be used during the demo ONLY! (Else the dropdown menu does not work correctly... // Fix/remove as soon as possible function getWrongPos(element) { var posX = posY = 0; if (element.offsetParent) { do { posX += element.offsetLeft; posY += element.offsetTop; } while (element = element.offSetParent); } var result = { X: posX, Y: posY } return result; } // Returns the computed width of an element function getWidth(element) { var width = 0; if (element.offsetWidth) { width = element.offsetWidth; } return width; } // Returns the computer size of an element function getSize(element) { var size = { X: null, Y: null } if (element.offsetWidth) { size.X = element.offsetWidth; size.Y = element.offsetHeight; } return size; } // Drop down menu implementation. Supports three levels: Base button, 1st level categories, and 2nd level links // TODO: getPos functie werkte eerst niet goed, menu offset is nog beetje whack. Betere manier van dropdown menu vinden en voor nu even de getPos functie stuk laten? function DDMenu() { // Initialize function, setting all needed variables. var instance = this; this.closeTimer = 0; this.ddMenuItem = null; this.timeout = 350; this.visible = false; this.menuElement = null; this.parentButton = null this.Init = function(id1, id2) { instance.menuElement = ge(id1); instance.parentButton = ge(id2); } this.SetCloseTimer = function() { //debugger; instance.closeTimer = window.setTimeout(instance.Close, instance.timeout); } this.Close = function() { if (instance.ddMenuItem) { instance.ddMenuItem.style.visibility = "hidden"; } instance.Toggle(); } this.CancelCloseTimer = function() { if (instance.closeTimer) { window.clearTimeout(instance.closeTimer); instance.closeTimer = null; } } this.Open = function(id) { instance.CancelCloseTimer(); if (instance.ddMenuItem) { instance.ddMenuItem.style.visibility = "hidden"; } instance.ddMenuItem = ge(id); instance.ddMenuItem.style.visibility = "visible"; var parentPos = getWrongPos(instance.ddMenuItem.parentNode); var parentWidth = getWidth(instance.ddMenuItem.parentNode); instance.ddMenuItem.style.left = (parentPos.X + parentWidth)+"px"; instance.ddMenuItem.style.top = parentPos.Y+"px"; } this.Toggle = function() { //debugger; if (instance.visible) { // Hide the menu instance.menuElement.style.visibility = "hidden"; instance.parentButton.className = ""; instance.visible = false; } else{ //Show the menu if (instance.menuElement) { instance.menuElement.style.visibility = "visible"; instance.parentButton.className = "down"; } instance.visible = true; } } } function manualEventFire(element, rawEvent) { // Attempts to fire a raw DOM event on an element try { element = document.getElementById(element); if (element.fireEvent) { element.fireEvent("on"+rawEvent.type, rawEvent); return true; } else if (element.dispatchEvent) { element.dispatchEvent(rawEvent); return true; } } catch (e) { } return false; }