source: Dev/branches/Cartis/Tiles preview/js/generalScripts.js @ 258

Last change on this file since 258 was 258, checked in by cartis, 13 years ago

Blauwe UI

File size: 8.8 KB
RevLine 
[258]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
7var 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
20function 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
27function addClass(ele,cls) {
28    if (!this.hasClass(ele,cls)) ele.className += " "+cls;
29}
30
31// Remove a given class from an HTMLElement
32function 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.
41function 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 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.
79function ajaxInfoRequest(uid, el, type) {
80    // Info panel update.
81   
82    var c = "uid="+uid;
83    c += "&type="+type;
84    var u = "getInfo.php";
85    newAjaxRequest(c, u, function(result) {
86        el.innerHTML = result.responseText;
87    }, true);
88}
89
90// Function to save the object currently being edited.
91// !! Still needs to be generalized !!
92function saveObject (confirmSave) {
93    var answer = false;
94    if (confirmSave==true) {
95        answer = confirm("Save changes to pipeline?");
96    }
97    else {
98        answer = true;
99    }
100   
101   
102    if (answer == true) {
103        var pipeline = document.getElementById("pipelineStringField").value;
104        pipeline = pipeline.slice(0, pipeline.length - 1);
105        var types = document.getElementById("pipelineTypeField").value;
106        types = types.slice(0, types.length - 1);
107        var session = document.getElementById("sessionField").value;
108        var requestString = "uids="+pipeline+"&types="+types+"&sessionUid="+session;
109        console.log(requestString);
110       
111       
112        var success;
113        newAjaxRequest(requestString, "savesession.php", function(result){
114            success = result.responseText;
115        }, true);
116        console.log(success);
117    }
118}
119
120// Convert a string to an array and remove any empty items (from trailing commas)
121function stringToArray(s, c) {
122    var a = s.split(c);
123    for (var i = 0; i < a.length; i++) {    // remove empty items
124        if (a[i] == "") {
125            a.splice(i, 1);
126            i--;
127        }
128    }
129    return a;
130}
131
132// Convert an array to a string, separated by character "c"
133function arrayToString(a, c) {
134    var s = "";
135    for (var i = 0; i < a.length; i++) {
136        if (a[i] != "") {
137            s += a[i]+c;
138        }
139    }
140    return s;
141}
142
143// Remove all newlines, carriage returns and tabs from a given string
144function removeNL(s){
145    return s.replace(/[\n\r\t]/g,"");
146}
147
148// Shorthand for document.createElement, because that pops up about three times every line of the main program
149function ce(s) {
150    return document.createElement(s);
151}
152
153// Shorthand for document.getElementById, same reason as above
154function ge(s) {
155    return document.getElementById(s);
156}
157
158// Function for getting the absolute position of the top left corner of an element, relative to the window
159function getPos(element) {
160    var posX = posY = 0;
161    if (element.offsetParent) {
162        do {
163            posX += element.offsetLeft;
164            posY += element.offsetTop;
165        } while (element = element.offsetParent);
166    }
167   
168    var result = {
169        X: posX,
170        Y: posY
171    }
172    return result;
173}
174
175// 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...
176// Fix/remove as soon as possible
177function getWrongPos(element) {
178    var posX = posY = 0;
179    if (element.offsetParent) {
180        do {
181            posX += element.offsetLeft;
182            posY += element.offsetTop;
183        } while (element = element.offSetParent);
184    }
185   
186    var result = {
187        X: posX,
188        Y: posY
189    }
190    return result;
191}
192
193// Returns the computed width of an element
194function getWidth(element) {
195    var width = 0;
196    if (element.offsetWidth) {
197        width = element.offsetWidth;
198    }
199   
200    return width;
201}
202
203// Returns the computer size of an element
204function getSize(element) {
205    var size = {
206        X: null,
207        Y: null
208    }
209    if (element.offsetWidth) {
210        size.X = element.offsetWidth;
211        size.Y = element.offsetHeight;
212    }
213   
214    return size;
215}
216
217// Drop down menu implementation. Supports three levels: Base button, 1st level categories, and 2nd level links
218// 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?
219function DDMenu() {
220    // Initialize function, setting all needed variables.
221    var instance = this;
222    this.closeTimer = 0;
223    this.ddMenuItem = null;
224    this.timeout = 350;
225    this.visible = false;
226    this.menuElement = null;
227    this.parentButton = null
228   
229    this.Init = function(id1, id2) {   
230        instance.menuElement = ge(id1);
231        instance.parentButton = ge(id2);
232    }
233   
234    this.SetCloseTimer = function() {
235        //debugger;
236        instance.closeTimer = window.setTimeout(instance.Close, instance.timeout);
237    }
238   
239    this.Close = function() {
240        if (instance.ddMenuItem) {
241            instance.ddMenuItem.style.visibility = "hidden";
242        }
243        instance.Toggle();
244       
245    }
246   
247    this.CancelCloseTimer = function() {
248        if (instance.closeTimer) {
249            window.clearTimeout(instance.closeTimer);
250            instance.closeTimer = null;
251        }
252    }
253   
254    this.Open = function(id) {
255        instance.CancelCloseTimer();
256        if (instance.ddMenuItem) {
257            instance.ddMenuItem.style.visibility = "hidden";
258        }
259       
260        instance.ddMenuItem = ge(id);
261        instance.ddMenuItem.style.visibility = "visible";
262        var parentPos = getWrongPos(instance.ddMenuItem.parentNode);
263        var parentWidth = getWidth(instance.ddMenuItem.parentNode);
264        instance.ddMenuItem.style.left = (parentPos.X + parentWidth)+"px";
265        instance.ddMenuItem.style.top = parentPos.Y+"px";
266       
267    }
268   
269    this.Toggle = function() {
270        //debugger;
271        if (instance.visible) {
272            // Hide the menu
273            instance.menuElement.style.visibility = "hidden";
274            instance.parentButton.className = "";
275            instance.visible = false;
276        }
277        else{
278            //Show the menu
279            if (instance.menuElement) {
280                instance.menuElement.style.visibility = "visible";
281                instance.parentButton.className = "down";
282            }
283            instance.visible = true;
284        }
285    }
286}
287
288function manualEventFire(element, rawEvent) {
289    // Attempts to fire a raw DOM event on an element
290   
291    try {
292        element = document.getElementById(element);
293        if (element.fireEvent) {
294            element.fireEvent("on"+rawEvent.type, rawEvent);
295            return true;
296        }
297        else if (element.dispatchEvent) {
298            element.dispatchEvent(rawEvent);
299            return true;
300        }
301    } catch (e) {
302    }
303    return false;
304}
Note: See TracBrowser for help on using the repository browser.