| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Lingr tools
Revision: 38
Author: dburkes
Date: 22 May 2007 13:09:58
Changes:Added lingr.js files
Files:| ... | ...@@ -1,5 +0,0 @@ | |
| 1 | Lingr API Javascript Sample | |
| 2 | ||
| 3 | api_demo.html contains a very simple Lingr API client in Javascript. It demonstrates how to call the Lingr API function room.getInfo using dynamic script tags (cf. doUpdateRoomInfo()), and how to parse the JSON return values (cf. updateRoomInfo()). | |
| 4 | ||
| 5 | For more details, tutorials, and community support, see the Lingr Developer Wiki at http://wiki.lingr.com. |
| ... | ...@@ -1,91 +0,0 @@ | |
| 1 | <html> | |
| 2 | ||
| 3 | <head> | |
| 4 | <script src="http://www.lingr.com/javascripts/prototype.js" type="text/javascript"></script> | |
| 5 | </head> | |
| 6 | ||
| 7 | <body> | |
| 8 | ||
| 9 | <h1>Lingr API Testing Page</h1> | |
| 10 | <p>Enter a room id and I'll generate a badge: | |
| 11 | <form onSubmit="doUpdateRoomInfo(); return false;"><input type="text" id="roomId"> <input type="submit" value="Go!"></form> | |
| 12 | </p> | |
| 13 | <div id="roomInfo" style="background: #ffe0e0; display:none;"> | |
| 14 | <p><a id="roomLink" href="#"><img id="roomImage" src="#" style="display:none; border:none;" width="32" height="32"><span id="roomName"></span></a></p> | |
| 15 | <p id="roomDescription"></p> | |
| 16 | <p id="roomStatus"></p> | |
| 17 | </div> | |
| 18 | <div id="roomInfoError" style="background: #ffe0e0; display:none"></div> | |
| 19 | ||
| 20 | <script type="text/javascript"> | |
| 21 | ||
| 22 | function dynamic_script_tag(url) | |
| 23 | { | |
| 24 | var script=document.createElement('script'); | |
| 25 | script.src=url; | |
| 26 | script.type="text/javascript"; | |
| 27 | ||
| 28 | document.getElementsByTagName('head')[0].appendChild(script); | |
| 29 | } | |
| 30 | ||
| 31 | function doUpdateRoomInfo() | |
| 32 | { | |
| 33 | url = 'http://www.lingr.com/api/room/get_info?api_key=foo&format=json&callback=updateRoomInfo&id=' + $F('roomId'); | |
| 34 | dynamic_script_tag(url); | |
| 35 | } | |
| 36 | ||
| 37 | function updateRoomInfo(info) | |
| 38 | { | |
| 39 | if (info.status == 'ok') | |
| 40 | { | |
| 41 | $('roomLink').href = info.room.url; | |
| 42 | ||
| 43 | $('roomImage').src = info.room.icon_url; | |
| 44 | Element.show('roomImage'); | |
| 45 | ||
| 46 | if (!info.room.name) info.room.name = ''; | |
| 47 | Element.update('roomName', info.room.name); | |
| 48 | ||
| 49 | if (!info.room.description) info.room.description = ''; | |
| 50 | Element.update('roomDescription', info.room.description); | |
| 51 | ||
| 52 | if (info.occupants.length == 0) | |
| 53 | { | |
| 54 | summary = 'No one is currently chatting'; | |
| 55 | } | |
| 56 | else if (info.occupants.length == 1) | |
| 57 | { | |
| 58 | summary = info.occupants[0].nickname + ' is waiting to chat'; | |
| 59 | } | |
| 60 | else | |
| 61 | { | |
| 62 | nicknames = ''; | |
| 63 | for (i = 0; i < info.occupants.length; i++) | |
| 64 | { | |
| 65 | if (nicknames.length > 0) | |
| 66 | { | |
| 67 | nicknames += ', '; | |
| 68 | } | |
| 69 | ||
| 70 | nicknames += info.occupants[i].nickname; | |
| 71 | } | |
| 72 | ||
| 73 | summary = info.occupants.length + ' people are chatting (' + nicknames + ')'; | |
| 74 | } | |
| 75 | ||
| 76 | Element.update('roomStatus', summary); | |
| 77 | ||
| 78 | Element.hide('roomInfoError'); | |
| 79 | Element.show('roomInfo'); | |
| 80 | } | |
| 81 | else | |
| 82 | { | |
| 83 | Element.update('roomInfoError', 'Could not get info for room ' + $F('roomId')); | |
| 84 | Element.hide('roomInfo'); | |
| 85 | Element.show('roomInfoError'); | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | </script> | |
| 90 | </body> | |
| 91 | </html> | |
| 92 | 0 | \ No newline at end of file |
| ... | ...@@ -0,0 +1,2006 @@ | |
| 1 | /* Prototype JavaScript framework, version 1.5.0_rc0 | |
| 2 | * (c) 2005 Sam Stephenson <sam@conio.net> | |
| 3 | * | |
| 4 | * Prototype is freely distributable under the terms of an MIT-style license. | |
| 5 | * For details, see the Prototype web site: http://prototype.conio.net/ | |
| 6 | * | |
| 7 | /*--------------------------------------------------------------------------*/ | |
| 8 | ||
| 9 | var Prototype = { | |
| 10 | Version: '1.5.0_rc0', | |
| 11 | ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', | |
| 12 | ||
| 13 | emptyFunction: function() {}, | |
| 14 | K: function(x) {return x} | |
| 15 | } | |
| 16 | ||
| 17 | var Class = { | |
| 18 | create: function() { | |
| 19 | return function() { | |
| 20 | this.initialize.apply(this, arguments); | |
| 21 | } | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | var Abstract = new Object(); | |
| 26 | ||
| 27 | Object.extend = function(destination, source) { | |
| 28 | for (var property in source) { | |
| 29 | destination[property] = source[property]; | |
| 30 | } | |
| 31 | return destination; | |
| 32 | } | |
| 33 | ||
| 34 | Object.inspect = function(object) { | |
| 35 | try { | |
| 36 | if (object == undefined) return 'undefined'; | |
| 37 | if (object == null) return 'null'; | |
| 38 | return object.inspect ? object.inspect() : object.toString(); | |
| 39 | } catch (e) { | |
| 40 | if (e instanceof RangeError) return '...'; | |
| 41 | throw e; | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | Function.prototype.bind = function() { | |
| 46 | var __method = this, args = $A(arguments), object = args.shift(); | |
| 47 | return function() { | |
| 48 | return __method.apply(object, args.concat($A(arguments))); | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | Function.prototype.bindAsEventListener = function(object) { | |
| 53 | var __method = this; | |
| 54 | return function(event) { | |
| 55 | return __method.call(object, event || window.event); | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | Object.extend(Number.prototype, { | |
| 60 | toColorPart: function() { | |
| 61 | var digits = this.toString(16); | |
| 62 | if (this < 16) return '0' + digits; | |
| 63 | return digits; | |
| 64 | }, | |
| 65 | ||
| 66 | succ: function() { | |
| 67 | return this + 1; | |
| 68 | }, | |
| 69 | ||
| 70 | times: function(iterator) { | |
| 71 | $R(0, this, true).each(iterator); | |
| 72 | return this; | |
| 73 | } | |
| 74 | }); | |
| 75 | ||
| 76 | var Try = { | |
| 77 | these: function() { | |
| 78 | var returnValue; | |
| 79 | ||
| 80 | for (var i = 0; i < arguments.length; i++) { | |
| 81 | var lambda = arguments[i]; | |
| 82 | try { | |
| 83 | returnValue = lambda(); | |
| 84 | break; | |
| 85 | } catch (e) {} | |
| 86 | } | |
| 87 | ||
| 88 | return returnValue; | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | /*--------------------------------------------------------------------------*/ | |
| 93 | ||
| 94 | var PeriodicalExecuter = Class.create(); | |
| 95 | PeriodicalExecuter.prototype = { | |
| 96 | initialize: function(callback, frequency) { | |
| 97 | this.callback = callback; | |
| 98 | this.frequency = frequency; | |
| 99 | this.currentlyExecuting = false; | |
| 100 | ||
| 101 | this.registerCallback(); | |
| 102 | }, | |
| 103 | ||
| 104 | registerCallback: function() { | |
| 105 | setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); | |
| 106 | }, | |
| 107 | ||
| 108 | onTimerEvent: function() { | |
| 109 | if (!this.currentlyExecuting) { | |
| 110 | try { | |
| 111 | this.currentlyExecuting = true; | |
| 112 | this.callback(); | |
| 113 | } finally { | |
| 114 | this.currentlyExecuting = false; | |
| 115 | } | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | Object.extend(String.prototype, { | |
| 120 | gsub: function(pattern, replacement) { | |
| 121 | var result = '', source = this, match; | |
| 122 | replacement = arguments.callee.prepareReplacement(replacement); | |
| 123 | ||
| 124 | while (source.length > 0) { | |
| 125 | if (match = source.match(pattern)) { | |
| 126 | result += source.slice(0, match.index); | |
| 127 | result += (replacement(match) || '').toString(); | |
| 128 | source = source.slice(match.index + match[0].length); | |
| 129 | } else { | |
| 130 | result += source, source = ''; | |
| 131 | } | |
| 132 | } | |
| 133 | return result; | |
| 134 | }, | |
| 135 | ||
| 136 | sub: function(pattern, replacement, count) { | |
| 137 | replacement = this.gsub.prepareReplacement(replacement); | |
| 138 | count = count === undefined ? 1 : count; | |
| 139 | ||
| 140 | return this.gsub(pattern, function(match) { | |
| 141 | if (--count < 0) return match[0]; | |
| 142 | return replacement(match); | |
| 143 | }); | |
| 144 | }, | |
| 145 | ||
| 146 | scan: function(pattern, iterator) { | |
| 147 | this.gsub(pattern, iterator); | |
| 148 | return this; | |
| 149 | }, | |
| 150 | ||
| 151 | truncate: function(length, truncation) { | |
| 152 | length = length || 30; | |
| 153 | truncation = truncation === undefined ? '...' : truncation; | |
| 154 | return this.length > length ? | |
| 155 | this.slice(0, length - truncation.length) + truncation : this; | |
| 156 | }, | |
| 157 | ||
| 158 | strip: function() { | |
| 159 | return this.replace(/^\s+/, '').replace(/\s+$/, ''); | |
| 160 | }, | |
| 161 | ||
| 162 | stripTags: function() { | |
| 163 | return this.replace(/<\/?[^>]+>/gi, ''); | |
| 164 | }, | |
| 165 | ||
| 166 | stripScripts: function() { | |
| 167 | return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); | |
| 168 | }, | |
| 169 | ||
| 170 | extractScripts: function() { | |
| 171 | var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); | |
| 172 | var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); | |
| 173 | return (this.match(matchAll) || []).map(function(scriptTag) { | |
| 174 | return (scriptTag.match(matchOne) || ['', ''])[1]; | |
| 175 | }); | |
| 176 | }, | |
| 177 | ||
| 178 | evalScripts: function() { | |
| 179 | return this.extractScripts().map(function(script) { return eval(script) }); | |
| 180 | }, | |
| 181 | ||
| 182 | escapeHTML: function() { | |
| 183 | var div = document.createElement('div'); | |
| 184 | var text = document.createTextNode(this); | |
| 185 | div.appendChild(text); | |
| 186 | return div.innerHTML; | |
| 187 | }, | |
| 188 | ||
| 189 | unescapeHTML: function() { | |
| 190 | var div = document.createElement('div'); | |
| 191 | div.innerHTML = this.stripTags(); | |
| 192 | return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; | |
| 193 | }, | |
| 194 | ||
| 195 | toQueryParams: function() { | |
| 196 | var pairs = this.match(/^\??(.*)$/)[1].split('&'); | |
| 197 | return pairs.inject({}, function(params, pairString) { | |
| 198 | var pair = pairString.split('='); | |
| 199 | params[pair[0]] = pair[1]; | |
| 200 | return params; | |
| 201 | }); | |
| 202 | }, | |
| 203 | ||
| 204 | toArray: function() { | |
| 205 | return this.split(''); | |
| 206 | }, | |
| 207 | ||
| 208 | camelize: function() { | |
| 209 | var oStringList = this.split('-'); | |
| 210 | if (oStringList.length == 1) return oStringList[0]; | |
| 211 | ||
| 212 | var camelizedString = this.indexOf('-') == 0 | |
| 213 | ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) | |
| 214 | : oStringList[0]; | |
| 215 | ||
| 216 | for (var i = 1, len = oStringList.length; i < len; i++) { | |
| 217 | var s = oStringList[i]; | |
| 218 | camelizedString += s.charAt(0).toUpperCase() + s.substring(1); | |
| 219 | } | |
| 220 | ||
| 221 | return camelizedString; | |
| 222 | }, | |
| 223 | ||
| 224 | inspect: function() { | |
| 225 | return "'" + this.replace(/\\/g, '\\\\').replace(/'/g, '\\\'') + "'"; | |
| 226 | } | |
| 227 | }); | |
| 228 | ||
| 229 | String.prototype.gsub.prepareReplacement = function(replacement) { | |
| 230 | if (typeof replacement == 'function') return replacement; | |
| 231 | var template = new Template(replacement); | |
| 232 | return function(match) { return template.evaluate(match) }; | |
| 233 | } | |
| 234 | ||
| 235 | String.prototype.parseQuery = String.prototype.toQueryParams; | |
| 236 | ||
| 237 | var Template = Class.create(); | |
| 238 | Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; | |
| 239 | Template.prototype = { | |
| 240 | initialize: function(template, pattern) { | |
| 241 | this.template = template.toString(); | |
| 242 | this.pattern = pattern || Template.Pattern; | |
| 243 | }, | |
| 244 | ||
| 245 | evaluate: function(object) { | |
| 246 | return this.template.gsub(this.pattern, function(match) { | |
| 247 | var before = match[1]; | |
| 248 | if (before == '\\') return match[2]; | |
| 249 | return before + (object[match[3]] || '').toString(); | |
| 250 | }); | |
| 251 | } | |
| 252 | } | |
| 253 | ||
| 254 | var $break = new Object(); | |
| 255 | var $continue = new Object(); | |
| 256 | ||
| 257 | var Enumerable = { | |
| 258 | each: function(iterator) { | |
| 259 | var index = 0; | |
| 260 | try { | |
| 261 | this._each(function(value) { | |
| 262 | try { | |
| 263 | iterator(value, index++); | |
| 264 | } catch (e) { | |
| 265 | if (e != $continue) throw e; | |
| 266 | } | |
| 267 | }); | |
| 268 | } catch (e) { | |
| 269 | if (e != $break) throw e; | |
| 270 | } | |
| 271 | }, | |
| 272 | ||
| 273 | all: function(iterator) { | |
| 274 | var result = true; | |
| 275 | this.each(function(value, index) { | |
| 276 | result = result && !!(iterator || Prototype.K)(value, index); | |
| 277 | if (!result) throw $break; | |
| 278 | }); | |
| 279 | return result; | |
| 280 | }, | |
| 281 | ||
| 282 | any: function(iterator) { | |
| 283 | var result = true; | |
| 284 | this.each(function(value, index) { | |
| 285 | if (result = !!(iterator || Prototype.K)(value, index)) | |
| 286 | throw $break; | |
| 287 | }); | |
| 288 | return result; | |
| 289 | }, | |
| 290 | ||
| 291 | collect: function(iterator) { | |
| 292 | var results = []; | |
| 293 | this.each(function(value, index) { | |
| 294 | results.push(iterator(value, index)); | |
| 295 | }); | |
| 296 | return results; | |
| 297 | }, | |
| 298 | ||
| 299 | detect: function (iterator) { | |
| 300 | var result; | |
| 301 | this.each(function(value, index) { | |
| 302 | if (iterator(value, index)) { | |
| 303 | result = value; | |
| 304 | throw $break; | |
| 305 | } | |
| 306 | }); | |
| 307 | return result; | |
| 308 | }, | |
| 309 | ||
| 310 | findAll: function(iterator) { | |
| 311 | var results = []; | |
| 312 | this.each(function(value, index) { | |
| 313 | if (iterator(value, index)) | |
| 314 | results.push(value); | |
| 315 | }); | |
| 316 | return results; | |
| 317 | }, | |
| 318 | ||
| 319 | grep: function(pattern, iterator) { | |
| 320 | var results = []; | |
| 321 | this.each(function(value, index) { | |
| 322 | var stringValue = value.toString(); | |
| 323 | if (stringValue.match(pattern)) | |
| 324 | results.push((iterator || Prototype.K)(value, index)); | |
| 325 | }) | |
| 326 | return results; | |
| 327 | }, | |
| 328 | ||
| 329 | include: function(object) { | |
| 330 | var found = false; | |
| 331 | this.each(function(value) { | |
| 332 | if (value == object) { | |
| 333 | found = true; | |
| 334 | throw $break; | |
| 335 | } | |
| 336 | }); | |
| 337 | return found; | |
| 338 | }, | |
| 339 | ||
| 340 | inject: function(memo, iterator) { | |
| 341 | this.each(function(value, index) { | |
| 342 | memo = iterator(memo, value, index); | |
| 343 | }); | |
| 344 | return memo; | |
| 345 | }, | |
| 346 | ||
| 347 | invoke: function(method) { | |
| 348 | var args = $A(arguments).slice(1); | |
| 349 | return this.collect(function(value) { | |
| 350 | return value[method].apply(value, args); | |
| 351 | }); | |
| 352 | }, | |
| 353 | ||
| 354 | max: function(iterator) { | |
| 355 | var result; | |
| 356 | this.each(function(value, index) { | |
| 357 | value = (iterator || Prototype.K)(value, index); | |
| 358 | if (result == undefined || value >= result) | |
| 359 | result = value; | |
| 360 | }); | |
| 361 | return result; | |
| 362 | }, | |
| 363 | ||
| 364 | min: function(iterator) { | |
| 365 | var result; | |
| 366 | this.each(function(value, index) { | |
| 367 | value = (iterator || Prototype.K)(value, index); | |
| 368 | if (result == undefined || value < result) | |
| 369 | result = value; | |
| 370 | }); | |
| 371 | return result; | |
| 372 | }, | |
| 373 | ||
| 374 | partition: function(iterator) { | |
| 375 | var trues = [], falses = []; | |
| 376 | this.each(function(value, index) { | |
| 377 | ((iterator || Prototype.K)(value, index) ? | |
| 378 | trues : falses).push(value); | |
| 379 | }); | |
| 380 | return [trues, falses]; | |
| 381 | }, | |
| 382 | ||
| 383 | pluck: function(property) { | |
| 384 | var results = []; | |
| 385 | this.each(function(value, index) { | |
| 386 | results.push(value[property]); | |
| 387 | }); | |
| 388 | return results; | |
| 389 | }, | |
| 390 | ||
| 391 | reject: function(iterator) { | |
| 392 | var results = []; | |
| 393 | this.each(function(value, index) { | |
| 394 | if (!iterator(value, index)) | |
| 395 | results.push(value); | |
| 396 | }); | |
| 397 | return results; | |
| 398 | }, | |
| 399 | ||
| 400 | sortBy: function(iterator) { | |
| 401 | return this.collect(function(value, index) { | |
| 402 | return {value: value, criteria: iterator(value, index)}; | |
| 403 | }).sort(function(left, right) { | |
| 404 | var a = left.criteria, b = right.criteria; | |
| 405 | return a < b ? -1 : a > b ? 1 : 0; | |
| 406 | }).pluck('value'); | |
| 407 | }, | |
| 408 | ||
| 409 | toArray: function() { | |
| 410 | return this.collect(Prototype.K); | |
| 411 | }, | |
| 412 | ||
| 413 | zip: function() { | |
| 414 | var iterator = Prototype.K, args = $A(arguments); | |
| 415 | if (typeof args.last() == 'function') | |
| 416 | iterator = args.pop(); | |
| 417 | ||
| 418 | var collections = [this].concat(args).map($A); | |
| 419 | return this.map(function(value, index) { | |
| 420 | return iterator(collections.pluck(index)); | |
| 421 | }); | |
| 422 | }, | |
| 423 | ||
| 424 | inspect: function() { | |
| 425 | return '#<Enumerable:' + this.toArray().inspect() + '>'; | |
| 426 | } | |
| 427 | } | |
| 428 | ||
| 429 | Object.extend(Enumerable, { | |
| 430 | map: Enumerable.collect, | |
| 431 | find: Enumerable.detect, | |
| 432 | select: Enumerable.findAll, | |
| 433 | member: Enumerable.include, | |
| 434 | entries: Enumerable.toArray | |
| 435 | }); | |
| 436 | var $A = Array.from = function(iterable) { | |
| 437 | if (!iterable) return []; | |
| 438 | if (iterable.toArray) { | |
| 439 | return iterable.toArray(); | |
| 440 | } else { | |
| 441 | var results = []; | |
| 442 | for (var i = 0; i < iterable.length; i++) | |
| 443 | results.push(iterable[i]); | |
| 444 | return results; | |
| 445 | } | |
| 446 | } | |
| 447 | ||
| 448 | Object.extend(Array.prototype, Enumerable); | |
| 449 | ||
| 450 | if (!Array.prototype._reverse) | |
| 451 | Array.prototype._reverse = Array.prototype.reverse; | |
| 452 | ||
| 453 | Object.extend(Array.prototype, { | |
| 454 | _each: function(iterator) { | |
| 455 | for (var i = 0; i < this.length; i++) | |
| 456 | iterator(this[i]); | |
| 457 | }, | |
| 458 | ||
| 459 | clear: function() { | |
| 460 | this.length = 0; | |
| 461 | return this; | |
| 462 | }, | |
| 463 | ||
| 464 | first: function() { | |
| 465 | return this[0]; | |
| 466 | }, | |
| 467 | ||
| 468 | last: function() { | |
| 469 | return this[this.length - 1]; | |
| 470 | }, | |
| 471 | ||
| 472 | compact: function() { | |
| 473 | return this.select(function(value) { | |
| 474 | return value != undefined || value != null; | |
| 475 | }); | |
| 476 | }, | |
| 477 | ||
| 478 | flatten: function() { | |
| 479 | return this.inject([], function(array, value) { | |
| 480 | return array.concat(value && value.constructor == Array ? | |
| 481 | value.flatten() : [value]); | |
| 482 | }); | |
| 483 | }, | |
| 484 | ||
| 485 | without: function() { | |
| 486 | var values = $A(arguments); | |
| 487 | return this.select(function(value) { | |
| 488 | return !values.include(value); | |
| 489 | }); | |
| 490 | }, | |
| 491 | ||
| 492 | indexOf: function(object) { | |
| 493 | for (var i = 0; i < this.length; i++) | |
| 494 | if (this[i] == object) return i; | |
| 495 | return -1; | |
| 496 | }, | |
| 497 | ||
| 498 | reverse: function(inline) { | |
| 499 | return (inline !== false ? this : this.toArray())._reverse(); | |
| 500 | }, | |
| 501 | ||
| 502 | inspect: function() { | |
| 503 | return '[' + this.map(Object.inspect).join(', ') + ']'; | |
| 504 | } | |
| 505 | }); | |
| 506 | var Hash = { | |
| 507 | _each: function(iterator) { | |
| 508 | for (var key in this) { | |
| 509 | var value = this[key]; | |
| 510 | if (typeof value == 'function') continue; | |
| 511 | ||
| 512 | var pair = [key, value]; | |
| 513 | pair.key = key; | |
| 514 | pair.value = value; | |
| 515 | iterator(pair); | |
| 516 | } | |
| 517 | }, | |
| 518 | ||
| 519 | keys: function() { | |
| 520 | return this.pluck('key'); | |
| 521 | }, | |
| 522 | ||
| 523 | values: function() { | |
| 524 | return this.pluck('value'); | |
| 525 | }, | |
| 526 | ||
| 527 | merge: function(hash) { | |
| 528 | return $H(hash).inject($H(this), function(mergedHash, pair) { | |
| 529 | mergedHash[pair.key] = pair.value; | |
| 530 | return mergedHash; | |
| 531 | }); | |
| 532 | }, | |
| 533 | ||
| 534 | toQueryString: function() { | |
| 535 | return this.map(function(pair) { | |
| 536 | return pair.map(encodeURIComponent).join('='); | |
| 537 | }).join('&'); | |
| 538 | }, | |
| 539 | ||
| 540 | inspect: function() { | |
| 541 | return '#<Hash:{' + this.map(function(pair) { | |
| 542 | return pair.map(Object.inspect).join(': '); | |
| 543 | }).join(', ') + '}>'; | |
| 544 | } | |
| 545 | } | |
| 546 | ||
| 547 | function $H(object) { | |
| 548 | var hash = Object.extend({}, object || {}); | |
| 549 | Object.extend(hash, Enumerable); | |
| 550 | Object.extend(hash, Hash); | |
| 551 | return hash; | |
| 552 | } | |
| 553 | ObjectRange = Class.create(); | |
| 554 | Object.extend(ObjectRange.prototype, Enumerable); | |
| 555 | Object.extend(ObjectRange.prototype, { | |
| 556 | initialize: function(start, end, exclusive) { | |
| 557 | this.start = start; | |
| 558 | this.end = end; | |
| 559 | this.exclusive = exclusive; | |
| 560 | }, | |
| 561 | ||
| 562 | _each: function(iterator) { | |
| 563 | var value = this.start; | |
| 564 | do { | |
| 565 | iterator(value); | |
| 566 | value = value.succ(); | |
| 567 | } while (this.include(value)); | |
| 568 | }, | |
| 569 | ||
| 570 | include: function(value) { | |
| 571 | if (value < this.start) | |
| 572 | return false; | |
| 573 | if (this.exclusive) | |
| 574 | return value < this.end; | |
| 575 | return value <= this.end; | |
| 576 | } | |
| 577 | }); | |
| 578 | ||
| 579 | var $R = function(start, end, exclusive) { | |
| 580 | return new ObjectRange(start, end, exclusive); | |
| 581 | } | |
| 582 | ||
| 583 | var Ajax = { | |
| 584 | getTransport: function() { | |
| 585 | return Try.these( | |
| 586 | function() {return new XMLHttpRequest()}, | |
| 587 | function() {return new ActiveXObject('Msxml2.XMLHTTP')}, | |
| 588 | function() {return new ActiveXObject('Microsoft.XMLHTTP')} | |
| 589 | ) || false; | |
| 590 | }, | |
| 591 | ||
| 592 | activeRequestCount: 0 | |
| 593 | } | |
| 594 | ||
| 595 | Ajax.Responders = { | |
| 596 | responders: [], | |
| 597 | ||
| 598 | _each: function(iterator) { | |
| 599 | this.responders._each(iterator); | |
| 600 | }, | |
| 601 | ||
| 602 | register: function(responderToAdd) { | |
| 603 | if (!this.include(responderToAdd)) | |
| 604 | this.responders.push(responderToAdd); | |
| 605 | }, | |
| 606 | ||
| 607 | unregister: function(responderToRemove) { | |
| 608 | this.responders = this.responders.without(responderToRemove); | |
| 609 | }, | |
| 610 | ||
| 611 | dispatch: function(callback, request, transport, json) { | |
| 612 | this.each(function(responder) { | |
| 613 | if (responder[callback] && typeof responder[callback] == 'function') { | |
| 614 | try { | |
| 615 | responder[callback].apply(responder, [request, transport, json]); | |
| 616 | } catch (e) {} | |
| 617 | } | |
| 618 | }); | |
| 619 | } | |
| 620 | }; | |
| 621 | ||
| 622 | Object.extend(Ajax.Responders, Enumerable); | |
| 623 | ||
| 624 | Ajax.Responders.register({ | |
| 625 | onCreate: function() { | |
| 626 | Ajax.activeRequestCount++; | |
| 627 | }, | |
| 628 | ||
| 629 | onComplete: function() { | |
| 630 | Ajax.activeRequestCount--; | |
| 631 | } | |
| 632 | }); | |
| 633 | ||
| 634 | Ajax.Base = function() {}; | |
| 635 | Ajax.Base.prototype = { | |
| 636 | setOptions: function(options) { | |
| 637 | this.options = { | |
| 638 | method: 'post', | |
| 639 | asynchronous: true, | |
| 640 | contentType: 'application/x-www-form-urlencoded', | |
| 641 | parameters: '' | |
| 642 | } | |
| 643 | Object.extend(this.options, options || {}); | |
| 644 | }, | |
| 645 | ||
| 646 | responseIsSuccess: function() { | |
| 647 | return this.transport.status == undefined | |
| 648 | || this.transport.status == 0 | |
| 649 | || (this.transport.status >= 200 && this.transport.status < 300); | |
| 650 | }, | |
| 651 | ||
| 652 | responseIsFailure: function() { | |
| 653 | return !this.responseIsSuccess(); | |
| 654 | } | |
| 655 | } | |
| 656 | ||
| 657 | Ajax.Request = Class.create(); | |
| 658 | Ajax.Request.Events = | |
| 659 | ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; | |
| 660 | ||
| 661 | Ajax.Request.prototype = Object.extend(new Ajax.Base(), { | |
| 662 | initialize: function(url, options) { | |
| 663 | this.transport = Ajax.getTransport(); | |
| 664 | this.setOptions(options); | |
| 665 | this.request(url); | |
| 666 | }, | |
| 667 | ||
| 668 | request: function(url) { | |
| 669 | var parameters = this.options.parameters || ''; | |
| 670 | if (parameters.length > 0) parameters += '&_='; | |
| 671 | ||
| 672 | try { | |
| 673 | this.url = url; | |
| 674 | if (this.options.method == 'get' && parameters.length > 0) | |
| 675 | this.url += (this.url.match(/\?/) ? '&' : '?') + parameters; | |
| 676 | ||
| 677 | Ajax.Responders.dispatch('onCreate', this, this.transport); | |
| 678 | ||
| 679 | this.transport.open(this.options.method, this.url, | |
| 680 | this.options.asynchronous); | |
| 681 | ||
| 682 | if (this.options.asynchronous) { | |
| 683 | this.transport.onreadystatechange = this.onStateChange.bind(this); | |
| 684 | setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10); | |
| 685 | } | |
| 686 | ||
| 687 | this.setRequestHeaders(); | |
| 688 | ||
| 689 | var body = this.options.postBody ? this.options.postBody : parameters; | |
| 690 | this.transport.send(this.options.method == 'post' ? body : null); | |
| 691 | ||
| 692 | } catch (e) { | |
| 693 | this.dispatchException(e); | |
| 694 | } | |
| 695 | }, | |
| 696 | ||
| 697 | setRequestHeaders: function() { | |
| 698 | var requestHeaders = | |
| 699 | ['X-Requested-With', 'XMLHttpRequest', | |
| 700 | 'X-Prototype-Version', Prototype.Version, | |
| 701 | 'Accept', 'text/javascript, text/html, application/xml, text/xml, */*']; | |
| 702 | ||
| 703 | if (this.options.method == 'post') { | |
| 704 | requestHeaders.push('Content-type', this.options.contentType); | |
| 705 | ||
| 706 | /* Force "Connection: close" for Mozilla browsers to work around | |
| 707 | * a bug where XMLHttpReqeuest sends an incorrect Content-length | |
| 708 | * header. See Mozilla Bugzilla #246651. | |
| 709 | */ | |
| 710 | if (this.transport.overrideMimeType) | |
| 711 | requestHeaders.push('Connection', 'close'); | |
| 712 | } | |
| 713 | ||
| 714 | if (this.options.requestHeaders) | |
| 715 | requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); | |
| 716 | ||
| 717 | for (var i = 0; i < requestHeaders.length; i += 2) | |
| 718 | this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); | |
| 719 | }, | |
| 720 | ||
| 721 | onStateChange: function() { | |
| 722 | var readyState = this.transport.readyState; | |
| 723 | if (readyState != 1) | |
| 724 | this.respondToReadyState(this.transport.readyState); | |
| 725 | }, | |
| 726 | ||
| 727 | header: function(name) { | |
| 728 | try { | |
| 729 | return this.transport.getResponseHeader(name); | |
| 730 | } catch (e) {} | |
| 731 | }, | |
| 732 | ||
| 733 | evalJSON: function() { | |
| 734 | try { | |
| 735 | return eval('(' + this.header('X-JSON') + ')'); | |
| 736 | } catch (e) {} | |
| 737 | }, | |
| 738 | ||
| 739 | evalResponse: function() { | |
| 740 | try { | |
| 741 | return eval(this.transport.responseText); | |
| 742 | } catch (e) { | |
| 743 | this.dispatchException(e); | |
| 744 | } | |
| 745 | }, | |
| 746 | ||
| 747 | respondToReadyState: function(readyState) { | |
| 748 | var event = Ajax.Request.Events[readyState]; | |
| 749 | var transport = this.transport, json = this.evalJSON(); | |
| 750 | ||
| 751 | if (event == 'Complete') { | |
| 752 | try { | |
| 753 | (this.options['on' + this.transport.status] | |
| 754 | || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] | |
| 755 | || Prototype.emptyFunction)(transport, json); | |
| 756 | } catch (e) { | |
| 757 | this.dispatchException(e); | |
| 758 | } | |
| 759 | ||
| 760 | if ((this.header('Content-type') || '').match(/^text\/javascript/i)) | |
| 761 | this.evalResponse(); | |
| 762 | } | |
| 763 | ||
| 764 | try { | |
| 765 | (this.options['on' + event] || Prototype.emptyFunction)(transport, json); | |
| 766 | Ajax.Responders.dispatch('on' + event, this, transport, json); | |
| 767 | } catch (e) { | |
| 768 | this.dispatchException(e); | |
| 769 | } | |
| 770 | ||
| 771 | /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ | |
| 772 | if (event == 'Complete') | |
| 773 | this.transport.onreadystatechange = Prototype.emptyFunction; | |
| 774 | }, | |
| 775 | ||
| 776 | dispatchException: function(exception) { | |
| 777 | (this.options.onException || Prototype.emptyFunction)(this, exception); | |
| 778 | Ajax.Responders.dispatch('onException', this, exception); | |
| 779 | } | |
| 780 | }); | |
| 781 | ||
| 782 | Ajax.Updater = Class.create(); | |
| 783 | ||
| 784 | Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { | |
| 785 | initialize: function(container, url, options) { | |
| 786 | this.containers = { | |
| 787 | success: container.success ? $(container.success) : $(container), | |
| 788 | failure: container.failure ? $(container.failure) : | |
| 789 | (container.success ? null : $(container)) | |
| 790 | } | |
| 791 | ||
| 792 | this.transport = Ajax.getTransport(); | |
| 793 | this.setOptions(options); | |
| 794 | ||
| 795 | var onComplete = this.options.onComplete || Prototype.emptyFunction; | |
| 796 | this.options.onComplete = (function(transport, object) { | |
| 797 | this.updateContent(); | |
| 798 | onComplete(transport, object); | |
| 799 | }).bind(this); | |
| 800 | ||
| 801 | this.request(url); | |
| 802 | }, | |
| 803 | ||
| 804 | updateContent: function() { | |
| 805 | var receiver = this.responseIsSuccess() ? | |
| 806 | this.containers.success : this.containers.failure; | |
| 807 | var response = this.transport.responseText; | |
| 808 | ||
| 809 | if (!this.options.evalScripts) | |
| 810 | response = response.stripScripts(); | |
| 811 | ||
| 812 | if (receiver) { | |
| 813 | if (this.options.insertion) { | |
| 814 | new this.options.insertion(receiver, response); | |
| 815 | } else { | |
| 816 | Element.update(receiver, response); | |
| 817 | } | |
| 818 | } | |
| 819 | ||
| 820 | if (this.responseIsSuccess()) { | |
| 821 | if (this.onComplete) | |
| 822 | setTimeout(this.onComplete.bind(this), 10); | |
| 823 | } | |
| 824 | } | |
| 825 | }); | |
| 826 | ||
| 827 | Ajax.PeriodicalUpdater = Class.create(); | |
| 828 | Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { | |
| 829 | initialize: function(container, url, options) { | |