[181] | 1 | /* |
---|
| 2 | * General functions, shared by most different scripts and elements. |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | // Object literal version of a timer script, can be used to benchmark script running time. |
---|
| 6 | |
---|
| 7 | var timeDiff = { |
---|
| 8 | setStartTime:function (){ |
---|
| 9 | d = new Date(); |
---|
| 10 | time = d.getTime(); |
---|
| 11 | }, |
---|
| 12 | |
---|
| 13 | getDiff:function (){ |
---|
| 14 | d = new Date(); |
---|
| 15 | return (d.getTime()-time); |
---|
| 16 | } |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | // Check if a certain HTMLElement has a given classname |
---|
| 20 | function hasClass(ele,cls) { |
---|
| 21 | if (ele.className) |
---|
| 22 | return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); |
---|
| 23 | |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | // Add a given classname to an HTMLElement |
---|
| 27 | function addClass(ele,cls) { |
---|
| 28 | if (!this.hasClass(ele,cls)) ele.className += " "+cls; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | // Remove a given class from an HTMLElement |
---|
| 32 | function removeClass(ele,cls) { |
---|
| 33 | if (hasClass(ele,cls)) { |
---|
| 34 | var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); |
---|
| 35 | ele.className=ele.className.replace(reg,' '); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | // General function to handle AJAX functionality. |
---|
| 40 | // 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. |
---|
| 41 | function newAjaxRequest(c, u, cb, async) { |
---|
| 42 | var xml; |
---|
| 43 | var content = c; //an array of strings, in "key=value" format. |
---|
| 44 | // assign a compatible request format |
---|
| 45 | if (window.XMLHttpRequest) { //Not IE5, IE6 |
---|
| 46 | xml = new XMLHttpRequest(); |
---|
| 47 | } |
---|
| 48 | else { //IE5, IE6 |
---|
| 49 | xml = new ActiveXObject("Microsoft.XMLHTTP"); |
---|
| 50 | } |
---|
| 51 | // subscribe the callback function to a response event |
---|
| 52 | xml.onreadystatechange = function() { |
---|
| 53 | xml.responseText = "Processing..."; |
---|
| 54 | if (xml.readyState == 4 && xml.status == 200) { |
---|
| 55 | cb(xml); |
---|
| 56 | } |
---|
| 57 | }; |
---|
| 58 | // initialize XMLRequest |
---|
| 59 | xml.open("POST", u, async); |
---|
| 60 | xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
---|
| 61 | var contentString = ""; |
---|
| 62 | //iterate through parameters passed in variable c |
---|
| 63 | if (typeof(content)=='object'&&(input instanceof Array)) { // parameters were passed as an array of key=value strings |
---|
| 64 | for (var i = 0; i < content.length; i++) { |
---|
| 65 | contentString += content[i]; |
---|
| 66 | if (i != (content.length - 1)) { |
---|
| 67 | contentString += "&"; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | else { // single parameter or string already formatted by calling function |
---|
| 72 | contentString = content; |
---|
| 73 | } |
---|
| 74 | // finally send the formatted request |
---|
| 75 | xml.send(contentString); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | // Function to save the object currently being edited. |
---|
| 79 | // !! Still needs to be generalized !! |
---|
| 80 | function saveObject (confirmSave) { |
---|
| 81 | var answer = false; |
---|
| 82 | if (confirmSave==true) { |
---|
| 83 | answer = confirm("Save changes to pipeline?"); |
---|
| 84 | } |
---|
| 85 | else { |
---|
| 86 | answer = true; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | if (answer == true) { |
---|
| 91 | var pipeline = document.getElementById("pipelineStringField").value; |
---|
| 92 | pipeline = pipeline.slice(0, pipeline.length - 1); |
---|
| 93 | var types = document.getElementById("pipelineTypeField").value; |
---|
| 94 | types = types.slice(0, types.length - 1); |
---|
| 95 | var session = document.getElementById("sessionField").value; |
---|
| 96 | var requestString = "uids="+pipeline+"&types="+types+"&sessionUid="+session; |
---|
| 97 | console.log(requestString); |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | var success; |
---|
| 101 | newAjaxRequest(requestString, "savesession.php", function(result){ |
---|
| 102 | success = result.responseText; |
---|
| 103 | }, true); |
---|
| 104 | console.log(success); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // Convert a string to an array and remove any empty items (from trailing commas) |
---|
| 109 | function stringToArray(s, c) { |
---|
| 110 | var a = s.split(c); |
---|
| 111 | for (var i = 0; i < a.length; i++) { // remove empty items |
---|
| 112 | if (a[i] == "") { |
---|
| 113 | a.splice(i, 1); |
---|
| 114 | i--; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | return a; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // Convert an array to a string, separated by character "c" |
---|
| 121 | function arrayToString(a, c) { |
---|
| 122 | var s = ""; |
---|
| 123 | for (var i = 0; i < a.length; i++) { |
---|
| 124 | if (a[i] != "") { |
---|
| 125 | s += a[i]+c; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | return s; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | // Remove all newlines, carriage returns and tabs from a given string |
---|
| 132 | function removeNL(s){ |
---|
| 133 | return s.replace(/[\n\r\t]/g,""); |
---|
| 134 | } |
---|