/* * Util.js: miscellaneous helper functions */ /* * Gets the link pointing to a course details page. * @param course The course object. We'll use it's uid */ function SearchUtil_getCourseUrl(course) { return "search.action?uid="+course.getID() + "&view=details"; } /* * Adds the given text to the given input field. */ function Util_addContentsToField(id, abbrev) { var input = document.getElementById(id); input.value = abbrev + ", " + input.value; } /* * Formats a price (an integer) for display on the page. */ function Util_formatPrice(total_cents) { var dollars = parseInt(total_cents / 100, 10); var cents = parseInt(total_cents % 100, 10); if (cents < 10) cents = '0' + cents; return ('$' + dollars + '.' + cents); } /* * Adds an event to a DOM element in a cross-browser way. * @param object the DOM element on which to add the event * @param eventname the name of the event (e.g., 'click') * @param handler the handler function (or object) to use */ function Util_addEvent(object, eventname, handler) { /* IE uses proprietary attachEvent. Everybody else uses w3c addEventListener. */ if (object.attachEvent) object.attachEvent('on'+eventname, handler); else object.addEventListener(eventname, handler, false); } /* * Adds an Option to a Select field. * @param select the DOM Select object * @param option the DOM Option object */ function Util_addOptionToSelect(select, option) { /* * We're using the existence of attachEvent to determine if it's IE, * because it has the function we want, but requires a non-standard * way to call it. */ if (select.attachEvent) select.add(option); else select.add(option, null); } /* * Assuming that the following is public domain from * http://binnyva.blogspot.com/2005/10/dump-function-javascript-equivalent-of.html */ /** * Function : dump() * Arguments: The data - array,hash(associative array),object * The level - OPTIONAL * Returns : The textual representation of the array. * This function was inspired by the print_r function of PHP. * This will accept some data as the argument and return a * text that will be a more readable version of the * array/hash/object that is given. */ function Util_dump(arr,max,level) { var dumped_text = ""; if(!level) level = 0; if(!max) max = 5; if (max > level) return ''; //The padding given at the beginning of the line. var level_padding = ""; for(var j=0;j \"" + value + "\"\n"; } } } else { //Stings/Chars/Numbers etc. dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; } return dumped_text; } /* * If in debug mode, this brings up a Javascript alert when an assertion fails. */ function Util_assert(arg) { } function Util_max(a, b) { return (a > b ? a : b); } /* * Returns all the elements of [minuend] which are not also in [subtrahend]. */ function Util_setDifference(minuend, subtrahend) { var minuendHash = []; for (var ii in minuend) { minuendHash[minuend[ii]] = true; } for (var ii in subtrahend) { delete minuendHash[subtrahend[ii]]; } var result = []; for (var item in minuendHash) { result.push(item); } return result; } function Util_setUnion(a, b) { return Util_setDifference(a, b).concat(b); } function Util_map(f, list) { var result = new Array(list.length); for (var ii in list) { result[ii] = f(list[ii]); } return result; } function Util_filter(f, list) { var result = new Array(list.length); for (var ii in list) { if (f(list[ii])) { result[ii] = list[ii]; } } return result; } function Util_findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft curtop = obj.offsetTop while (obj = obj.offsetParent) { curleft += obj.offsetLeft curtop += obj.offsetTop } } return [curleft,curtop]; }