|
| 1 | + |
| 2 | +function $(element) { |
| 3 | + if (typeof element == "string") { |
| 4 | + element=document.getElementById(element); |
| 5 | + } |
| 6 | + if (element) |
| 7 | + extend_instance(element,Element); |
| 8 | + return element; |
| 9 | +} |
| 10 | + |
| 11 | +function extend_instance(instance,hash) { |
| 12 | + for (var name in hash) { |
| 13 | + instance[name] = hash[name]; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +var Element = { |
| 18 | + "hide": function () { |
| 19 | + this.setStyle("display","none") |
| 20 | + }, |
| 21 | + "show": function () { |
| 22 | + this.setStyle("display","block") |
| 23 | + }, |
| 24 | + "visible": function () { |
| 25 | + return (this.getStyle("display") != "none"); |
| 26 | + }, |
| 27 | + "toggle": function () { |
| 28 | + if (this.visible) { |
| 29 | + this.hide(); |
| 30 | + } else { |
| 31 | + this.show(); |
| 32 | + } |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +function encodeURIComponent(str) { |
| 37 | + if (typeof(str) == "string") { |
| 38 | + return str.replace(/=/g,'%3D').replace(/&/g,'%26'); |
| 39 | + } |
| 40 | + //checkboxes and radio buttons return objects instead of a string |
| 41 | + else if(typeof(str) == "object"){ |
| 42 | + for (prop in str) |
| 43 | + { |
| 44 | + return str[prop].replace(/=/g,'%3D').replace(/&/g,'%26'); |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +var Form = {}; |
| 50 | +Form.serialize = function(form_element) { |
| 51 | + elements=$(form_element).serialize(); |
| 52 | + param_string=""; |
| 53 | + for (var name in elements) { |
| 54 | + if (param_string) |
| 55 | + param_string += "&"; |
| 56 | + param_string += encodeURIComponent(name)+"="+encodeURIComponent(elements[name]); |
| 57 | + } |
| 58 | + return param_string; |
| 59 | +}; |
| 60 | + |
| 61 | +Ajax.Updater = function (container,url,options) { |
| 62 | + this.container = container; |
| 63 | + this.url=url; |
| 64 | + this.ajax = new Ajax(); |
| 65 | + this.ajax.requireLogin = 1; |
| 66 | + if (options["onSuccess"]) { |
| 67 | + this.ajax.responseType = Ajax.JSON; |
| 68 | + this.ajax.ondone = options["onSuccess"]; |
| 69 | + } else { |
| 70 | + this.ajax.responseType = Ajax.FBML; |
| 71 | + this.ajax.ondone = function(data) { |
| 72 | + $(container).setInnerFBML(data); |
| 73 | + } |
| 74 | + } |
| 75 | + if (options["onFailure"]) { |
| 76 | + this.ajax.onerror = options["onFailure"]; |
| 77 | + } |
| 78 | + |
| 79 | + // Yes, this is an excercise in undoing what we just did |
| 80 | + // FB doesn't provide encodeURI, but they will encode things passed as a hash |
| 81 | + // so we turn it into a string, esaping & and = |
| 82 | + // then we split it all back out here |
| 83 | + // this could be killed if encodeURIComponent was available |
| 84 | + parameters={}; |
| 85 | + if (options['parameters']) { |
| 86 | + pairs=options['parameters'].split('&'); |
| 87 | + for (var i=0; i<pairs.length; i++) { |
| 88 | + kv=pairs[i].split('='); |
| 89 | + key=kv[0].replace(/%3D/g,'=').replace(/%26/g,'&'); |
| 90 | + val=kv[1].replace(/%3D/g,'=').replace(/%26/g,'&'); |
| 91 | + parameters[key]=val; |
| 92 | + } |
| 93 | + } |
| 94 | + this.ajax.post(url,parameters); |
| 95 | + if (options["onLoading"]) { |
| 96 | + options["onLoading"].call() |
| 97 | + } |
| 98 | +}; |
| 99 | +Ajax.Request = function(url,options) { |
| 100 | + Ajax.Updater('unused',url,options); |
| 101 | +}; |
| 102 | + |
| 103 | +PeriodicalExecuter = function (callback, frequency) { |
| 104 | + setTimeout(callback, frequency *1000); |
| 105 | + setTimeout(function() { new PeriodicalExecuter(callback,frequency); }, frequency*1000); |
| 106 | +}; |
0 commit comments