| | 33 | var Prototype = { |
|---|
| | 34 | Version: '1.4.0', |
|---|
| | 35 | ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', |
|---|
| | 36 | |
|---|
| | 37 | emptyFunction: function() {}, |
|---|
| | 38 | K: function(x) {return x} |
|---|
| | 39 | } |
|---|
| | 40 | |
|---|
| | 41 | var Class = { |
|---|
| | 42 | create: function() { |
|---|
| | 43 | return function() { |
|---|
| | 44 | this.initialize.apply(this, arguments); |
|---|
| | 45 | } |
|---|
| | 46 | } |
|---|
| | 47 | } |
|---|
| | 48 | |
|---|
| | 49 | var Abstract = new Object(); |
|---|
| | 50 | |
|---|
| | 51 | Object.extend = function(destination, source) { |
|---|
| | 52 | for (property in source) { |
|---|
| | 53 | destination[property] = source[property]; |
|---|
| | 54 | } |
|---|
| | 55 | return destination; |
|---|
| | 56 | } |
|---|
| | 57 | |
|---|
| | 58 | Object.inspect = function(object) { |
|---|
| | 59 | try { |
|---|
| | 60 | if (object == undefined) return 'undefined'; |
|---|
| | 61 | if (object == null) return 'null'; |
|---|
| | 62 | return object.inspect ? object.inspect() : object.toString(); |
|---|
| | 63 | } catch (e) { |
|---|
| | 64 | if (e instanceof RangeError) return '...'; |
|---|
| | 65 | throw e; |
|---|
| | 66 | } |
|---|
| | 67 | } |
|---|
| | 68 | |
|---|
| | 69 | Function.prototype.bind = function() { |
|---|
| | 70 | var __method = this, args = $A(arguments), object = args.shift(); |
|---|
| | 71 | return function() { |
|---|
| | 72 | return __method.apply(object, args.concat($A(arguments))); |
|---|
| | 73 | } |
|---|
| | 74 | } |
|---|
| | 75 | |
|---|
| | 76 | Function.prototype.bindAsEventListener = function(object) { |
|---|
| | 77 | var __method = this; |
|---|
| | 78 | return function(event) { |
|---|
| | 79 | return __method.call(object, event || window.event); |
|---|
| | 80 | } |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | Object.extend(Number.prototype, { |
|---|
| | 84 | toColorPart: function() { |
|---|
| | 85 | var digits = this.toString(16); |
|---|
| | 86 | if (this < 16) return '0' + digits; |
|---|
| | 87 | return digits; |
|---|
| | 88 | }, |
|---|
| | 89 | |
|---|
| | 90 | succ: function() { |
|---|
| | 91 | return this + 1; |
|---|
| | 92 | }, |
|---|
| | 93 | |
|---|
| | 94 | times: function(iterator) { |
|---|
| | 95 | $R(0, this, true).each(iterator); |
|---|
| | 96 | return this; |
|---|
| | 97 | } |
|---|
| | 98 | }); |
|---|
| | 99 | |
|---|
| | 100 | var Try = { |
|---|
| | 101 | these: function() { |
|---|
| | 102 | var returnValue; |
|---|
| | 103 | |
|---|
| | 104 | for (var i = 0; i < arguments.length; i++) { |
|---|
| | 105 | var lambda = arguments[i]; |
|---|
| | 106 | try { |
|---|
| | 107 | returnValue = lambda(); |
|---|
| | 108 | break; |
|---|
| | 109 | } catch (e) {} |
|---|
| | 110 | } |
|---|
| | 111 | |
|---|
| | 112 | return returnValue; |
|---|
| | 113 | } |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| | 116 | /*--------------------------------------------------------------------------*/ |
|---|
| | 117 | |
|---|
| | 118 | var PeriodicalExecuter = Class.create(); |
|---|
| | 119 | PeriodicalExecuter.prototype = { |
|---|
| | 120 | initialize: function(callback, frequency) { |
|---|
| | 121 | this.callback = callback; |
|---|
| | 122 | this.frequency = frequency; |
|---|
| | 123 | this.currentlyExecuting = false; |
|---|
| | 124 | |
|---|
| | 125 | this.registerCallback(); |
|---|
| | 126 | }, |
|---|
| | 127 | |
|---|
| | 128 | registerCallback: function() { |
|---|
| | 129 | setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); |
|---|
| | 130 | }, |
|---|
| | 131 | |
|---|
| | 132 | onTimerEvent: function() { |
|---|
| | 133 | if (!this.currentlyExecuting) { |
|---|
| | 134 | try { |
|---|
| | 135 | this.currentlyExecuting = true; |
|---|
| | 136 | this.callback(); |
|---|
| | 137 | } finally { |
|---|
| | 138 | this.currentlyExecuting = false; |
|---|
| | 139 | } |
|---|
| | 140 | } |
|---|
| | 141 | } |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| | 144 | /*--------------------------------------------------------------------------*/ |
|---|
| | 145 | |
|---|
| | 146 | function $() { |
|---|
| | 147 | var elements = new Array(); |
|---|
| | 148 | |
|---|
| | 149 | for (var i = 0; i < arguments.length; i++) { |
|---|
| | 150 | var element = arguments[i]; |
|---|
| | 151 | if (typeof element == 'string') |
|---|
| | 152 | element = document.getElementById(element); |
|---|
| | 153 | |
|---|
| | 154 | if (arguments.length == 1) |
|---|
| | 155 | return element; |
|---|
| | 156 | |
|---|
| | 157 | elements.push(element); |
|---|
| | 158 | } |
|---|
| | 159 | |
|---|
| | 160 | return elements; |
|---|
| | 161 | } |
|---|
| | 162 | Object.extend(String.prototype, { |
|---|
| | 163 | stripTags: function() { |
|---|
| | 164 | return this.replace(/<\/?[^>]+>/gi, ''); |
|---|
| | 165 | }, |
|---|
| | 166 | |
|---|
| | 167 | stripScripts: function() { |
|---|
| | 168 | return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); |
|---|
| | 169 | }, |
|---|
| | 170 | |
|---|
| | 171 | extractScripts: function() { |
|---|
| | 172 | var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); |
|---|
| | 173 | var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); |
|---|
| | 174 | return (this.match(matchAll) || []).map(function(scriptTag) { |
|---|
| | 175 | return (scriptTag.match(matchOne) || ['', ''])[1]; |
|---|
| | 176 | }); |
|---|
| | 177 | }, |
|---|
| | 178 | |
|---|
| | 179 | evalScripts: function() { |
|---|
| | 180 | return this.extractScripts().map(eval); |
|---|
| | 181 | }, |
|---|
| | 182 | |
|---|
| | 183 | escapeHTML: function() { |
|---|
| | 184 | var div = document.createElement('div'); |
|---|
| | 185 | var text = document.createTextNode(this); |
|---|
| | 186 | div.appendChild(text); |
|---|
| | 187 | return div.innerHTML; |
|---|
| | 188 | }, |
|---|
| | 189 | |
|---|
| | 190 | unescapeHTML: function() { |
|---|
| | 191 | var div = document.createElement('div'); |
|---|
| | 192 | div.innerHTML = this.stripTags(); |
|---|
| | 193 | return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; |
|---|
| | 194 | }, |
|---|
| | 195 | |
|---|
| | 196 | toQueryParams: function() { |
|---|
| | 197 | var pairs = this.match(/^\??(.*)$/)[1].split('&'); |
|---|
| | 198 | return pairs.inject({}, function(params, pairString) { |
|---|
| | 199 | var pair = pairString.split('='); |
|---|
| | 200 | params[pair[0]] = pair[1]; |
|---|
| | 201 | return params; |
|---|
| | 202 | }); |
|---|
| | 203 | }, |
|---|
| | 204 | |
|---|
| | 205 | toArray: function() { |
|---|
| | 206 | return this.split(''); |
|---|
| | 207 | }, |
|---|
| | 208 | |
|---|
| | 209 | camelize: function() { |
|---|
| | 210 | var oStringList = this.split('-'); |
|---|
| | 211 | if (oStringList.length == 1) return oStringList[0]; |
|---|
| | 212 | |
|---|
| | 213 | var camelizedString = this.indexOf('-') == 0 |
|---|
| | 214 | ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) |
|---|
| | 215 | : oStringList[0]; |
|---|
| | 216 | |
|---|
| | 217 | for (var i = 1, len = oStringList.length; i < len; i++) { |
|---|
| | 218 | var s = oStringList[i]; |
|---|
| | 219 | camelizedString += s.charAt(0).toUpperCase() + s.substring(1); |
|---|
| | 220 | } |
|---|
| | 221 | |
|---|
| | 222 | return camelizedString; |
|---|
| | 223 | }, |
|---|
| | 224 | |
|---|
| | 225 | inspect: function() { |
|---|
| | 226 | return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'"; |
|---|
| | 227 | } |
|---|
| | 228 | }); |
|---|
| | 229 | |
|---|
| | 230 | String.prototype.parseQuery = String.prototype.toQueryParams; |
|---|
| | 231 | |
|---|
| | 232 | var $break = new Object(); |
|---|
| | 233 | var $continue = new Object(); |
|---|
| | 234 | |
|---|
| | 235 | var Enumerable = { |
|---|
| | 236 | each: function(iterator) { |
|---|
| | 237 | var index = 0; |
|---|
| | 238 | try { |
|---|
| | 239 | this._each(function(value) { |
|---|
| | 240 | try { |
|---|
| | 241 | iterator(value, index++); |
|---|
| | 242 | } catch (e) { |
|---|
| | 243 | if (e != $continue) throw e; |
|---|
| | 244 | } |
|---|
| | 245 | }); |
|---|
| | 246 | } catch (e) { |
|---|
| | 247 | if (e != $break) throw e; |
|---|
| | 248 | } |
|---|
| | 249 | }, |
|---|
| | 250 | |
|---|
| | 251 | all: function(iterator) { |
|---|
| | 252 | var result = true; |
|---|
| | 253 | this.each(function(value, index) { |
|---|
| | 254 | result = result && !!(iterator || Prototype.K)(value, index); |
|---|
| | 255 | if (!result) throw $break; |
|---|
| | 256 | }); |
|---|
| | 257 | return result; |
|---|
| | 258 | }, |
|---|
| | 259 | |
|---|
| | 260 | any: function(iterator) { |
|---|
| | 261 | var result = true; |
|---|
| | 262 | this.each(function(value, index) { |
|---|
| | 263 | if (result = !!(iterator || Prototype.K)(value, index)) |
|---|
| | 264 | throw $break; |
|---|
| | 265 | }); |
|---|
| | 266 | return result; |
|---|
| | 267 | }, |
|---|
| | 268 | |
|---|
| | 269 | collect: function(iterator) { |
|---|
| | 270 | var results = []; |
|---|
| | 271 | this.each(function(value, index) { |
|---|
| | 272 | results.push(iterator(value, index)); |
|---|
| | 273 | }); |
|---|
| | 274 | return results; |
|---|
| | 275 | }, |
|---|
| | 276 | |
|---|
| | 277 | detect: function (iterator) { |
|---|
| | 278 | var result; |
|---|
| | 279 | this.each(function(value, index) { |
|---|
| | 280 | if (iterator(value, index)) { |
|---|
| | 281 | result = value; |
|---|
| | 282 | throw $break; |
|---|
| | 283 | } |
|---|
| | 284 | }); |
|---|
| | 285 | return result; |
|---|
| | 286 | }, |
|---|
| | 287 | |
|---|
| | 288 | findAll: function(iterator) { |
|---|
| | 289 | var results = []; |
|---|
| | 290 | this.each(function(value, index) { |
|---|
| | 291 | if (iterator(value, index)) |
|---|
| | 292 | results.push(value); |
|---|
| | 293 | }); |
|---|
| | 294 | return results; |
|---|
| | 295 | }, |
|---|
| | 296 | |
|---|
| | 297 | grep: function(pattern, iterator) { |
|---|
| | 298 | var results = []; |
|---|
| | 299 | this.each(function(value, index) { |
|---|
| | 300 | var stringValue = value.toString(); |
|---|
| | 301 | if (stringValue.match(pattern)) |
|---|
| | 302 | results.push((iterator || Prototype.K)(value, index)); |
|---|
| | 303 | }) |
|---|
| | 304 | return results; |
|---|
| | 305 | }, |
|---|
| | 306 | |
|---|
| | 307 | include: function(object) { |
|---|
| | 308 | var found = false; |
|---|
| | 309 | this.each(function(value) { |
|---|
| | 310 | if (value == object) { |
|---|
| | 311 | found = true; |
|---|
| | 312 | throw $break; |
|---|
| | 313 | } |
|---|
| | 314 | }); |
|---|
| | 315 | return found; |
|---|
| | 316 | }, |
|---|
| | 317 | |
|---|
| | 318 | inject: function(memo, iterator) { |
|---|
| | 319 | this.each(function(value, index) { |
|---|
| | 320 | memo = iterator(memo, value, index); |
|---|
| | 321 | }); |
|---|
| | 322 | return memo; |
|---|
| | 323 | }, |
|---|
| | 324 | |
|---|
| | 325 | invoke: function(method) { |
|---|
| | 326 | var args = $A(arguments).slice(1); |
|---|
| | 327 | return this.collect(function(value) { |
|---|
| | 328 | return value[method].apply(value, args); |
|---|
| | 329 | }); |
|---|
| | 330 | }, |
|---|
| | 331 | |
|---|
| | 332 | max: function(iterator) { |
|---|
| | 333 | var result; |
|---|
| | 334 | this.each(function(value, index) { |
|---|
| | 335 | value = (iterator || Prototype.K)(value, index); |
|---|
| | 336 | if (value >= (result || value)) |
|---|
| | 337 | result = value; |
|---|
| | 338 | }); |
|---|
| | 339 | return result; |
|---|
| | 340 | }, |
|---|
| | 341 | |
|---|
| | 342 | min: function(iterator) { |
|---|
| | 343 | var result; |
|---|
| | 344 | this.each(function(value, index) { |
|---|
| | 345 | value = (iterator || Prototype.K)(value, index); |
|---|
| | 346 | if (value <= (result || value)) |
|---|
| | 347 | result = value; |
|---|
| | 348 | }); |
|---|
| | 349 | return result; |
|---|
| | 350 | }, |
|---|
| | 351 | |
|---|
| | 352 | partition: function(iterator) { |
|---|
| | 353 | var trues = [], falses = []; |
|---|
| | 354 | this.each(function(value, index) { |
|---|
| | 355 | ((iterator || Prototype.K)(value, index) ? |
|---|
| | 356 | trues : falses).push(value); |
|---|
| | 357 | }); |
|---|
| | 358 | return [trues, falses]; |
|---|
| | 359 | }, |
|---|
| | 360 | |
|---|
| | 361 | pluck: function(property) { |
|---|
| | 362 | var results = []; |
|---|
| | 363 | this.each(function(value, index) { |
|---|
| | 364 | results.push(value[property]); |
|---|
| | 365 | }); |
|---|
| | 366 | return results; |
|---|
| | 367 | }, |
|---|
| | 368 | |
|---|
| | 369 | reject: function(iterator) { |
|---|
| | 370 | var results = []; |
|---|
| | 371 | this.each(function(value, index) { |
|---|
| | 372 | if (!iterator(value, index)) |
|---|
| | 373 | results.push(value); |
|---|
| | 374 | }); |
|---|
| | 375 | return results; |
|---|
| | 376 | }, |
|---|
| | 377 | |
|---|
| | 378 | sortBy: function(iterator) { |
|---|
| | 379 | return this.collect(function(value, index) { |
|---|
| | 380 | return {value: value, criteria: iterator(value, index)}; |
|---|
| | 381 | }).sort(function(left, right) { |
|---|
| | 382 | var a = left.criteria, b = right.criteria; |
|---|
| | 383 | return a < b ? -1 : a > b ? 1 : 0; |
|---|
| | 384 | }).pluck('value'); |
|---|
| | 385 | }, |
|---|
| | 386 | |
|---|
| | 387 | toArray: function() { |
|---|
| | 388 | return this.collect(Prototype.K); |
|---|
| | 389 | }, |
|---|
| | 390 | |
|---|
| | 391 | zip: function() { |
|---|
| | 392 | var iterator = Prototype.K, args = $A(arguments); |
|---|
| | 393 | if (typeof args.last() == 'function') |
|---|
| | 394 | iterator = args.pop(); |
|---|
| | 395 | |
|---|
| | 396 | var collections = [this].concat(args).map($A); |
|---|
| | 397 | return this.map(function(value, index) { |
|---|
| | 398 | iterator(value = collections.pluck(index)); |
|---|
| | 399 | return value; |
|---|
| | 400 | }); |
|---|
| | 401 | }, |
|---|
| | 402 | |
|---|
| | 403 | inspect: function() { |
|---|
| | 404 | return '#<Enumerable:' + this.toArray().inspect() + '>'; |
|---|
| | 405 | } |
|---|
| | 406 | } |
|---|
| | 407 | |
|---|
| | 408 | Object.extend(Enumerable, { |
|---|
| | 409 | map: Enumerable.collect, |
|---|
| | 410 | find: Enumerable.detect, |
|---|
| | 411 | select: Enumerable.findAll, |
|---|
| | 412 | member: Enumerable.include, |
|---|
| | 413 | entries: Enumerable.toArray |
|---|
| | 414 | }); |
|---|
| | 415 | var $A = Array.from = function(iterable) { |
|---|
| | 416 | if (!iterable) return []; |
|---|
| | 417 | if (iterable.toArray) { |
|---|
| | 418 | return iterable.toArray(); |
|---|
| | 419 | } else { |
|---|
| | 420 | var results = []; |
|---|
| | 421 | for (var i = 0; i < iterable.length; i++) |
|---|
| | 422 | results.push(iterable[i]); |
|---|
| | 423 | return results; |
|---|
| | 424 | } |
|---|
| | 425 | } |
|---|
| | 426 | |
|---|
| | 427 | Object.extend(Array.prototype, Enumerable); |
|---|
| | 428 | |
|---|
| | 429 | Array.prototype._reverse = Array.prototype.reverse; |
|---|
| | 430 | |
|---|
| | 431 | Object.extend(Array.prototype, { |
|---|
| | 432 | _each: function(iterator) { |
|---|
| | 433 | for (var i = 0; i < this.length; i++) |
|---|
| | 434 | iterator(this[i]); |
|---|
| | 435 | }, |
|---|
| | 436 | |
|---|
| | 437 | clear: function() { |
|---|
| | 438 | this.length = 0; |
|---|
| | 439 | return this; |
|---|
| | 440 | }, |
|---|
| | 441 | |
|---|
| | 442 | first: function() { |
|---|
| | 443 | return this[0]; |
|---|
| | 444 | }, |
|---|
| | 445 | |
|---|
| | 446 | last: function() { |
|---|
| | 447 | return this[this.length - 1]; |
|---|
| | 448 | }, |
|---|
| | 449 | |
|---|
| | 450 | compact: function() { |
|---|
| | 451 | return this.select(function(value) { |
|---|
| | 452 | return value != undefined || value != null; |
|---|
| | 453 | }); |
|---|
| | 454 | }, |
|---|
| | 455 | |
|---|
| | 456 | flatten: function() { |
|---|
| | 457 | return this.inject([], function(array, value) { |
|---|
| | 458 | return array.concat(value.constructor == Array ? |
|---|
| | 459 | value.flatten() : [value]); |
|---|
| | 460 | }); |
|---|
| | 461 | }, |
|---|
| | 462 | |
|---|
| | 463 | without: function() { |
|---|
| | 464 | var values = $A(arguments); |
|---|
| | 465 | return this.select(function(value) { |
|---|
| | 466 | return !values.include(value); |
|---|
| | 467 | }); |
|---|
| | 468 | }, |
|---|
| | 469 | |
|---|
| | 470 | indexOf: function(object) { |
|---|
| | 471 | for (var i = 0; i < this.length; i++) |
|---|
| | 472 | if (this[i] == object) return i; |
|---|
| | 473 | return -1; |
|---|
| | 474 | }, |
|---|
| | 475 | |
|---|
| | 476 | reverse: function(inline) { |
|---|
| | 477 | return (inline !== false ? this : this.toArray())._reverse(); |
|---|
| | 478 | }, |
|---|
| | 479 | |
|---|
| | 480 | shift: function() { |
|---|
| | 481 | var result = this[0]; |
|---|
| | 482 | for (var i = 0; i < this.length - 1; i++) |
|---|
| | 483 | this[i] = this[i + 1]; |
|---|
| | 484 | this.length--; |
|---|
| | 485 | return result; |
|---|
| | 486 | }, |
|---|
| | 487 | |
|---|
| | 488 | inspect: function() { |
|---|
| | 489 | return '[' + this.map(Object.inspect).join(', ') + ']'; |
|---|
| | 490 | } |
|---|
| | 491 | }); |
|---|
| | 492 | var Hash = { |
|---|
| | 493 | _each: function(iterator) { |
|---|
| | 494 | for (key in this) { |
|---|
| | 495 | var value = this[key]; |
|---|
| | 496 | if (typeof value == 'function') continue; |
|---|
| | 497 | |
|---|
| | 498 | var pair = [key, value]; |
|---|
| | 499 | pair.key = key; |
|---|
| | 500 | pair.value = value; |
|---|
| | 501 | iterator(pair); |
|---|
| | 502 | } |
|---|
| | 503 | }, |
|---|
| | 504 | |
|---|
| | 505 | keys: function() { |
|---|
| | 506 | return this.pluck('key'); |
|---|
| | 507 | }, |
|---|
| | 508 | |
|---|
| | 509 | values: function() { |
|---|
| | 510 | return this.pluck('value'); |
|---|
| | 511 | }, |
|---|
| | 512 | |
|---|
| | 513 | merge: function(hash) { |
|---|
| | 514 | return $H(hash).inject($H(this), function(mergedHash, pair) { |
|---|
| | 515 | mergedHash[pair.key] = pair.value; |
|---|
| | 516 | return mergedHash; |
|---|
| | 517 | }); |
|---|
| | 518 | }, |
|---|
| | 519 | |
|---|
| | 520 | toQueryString: function() { |
|---|
| | 521 | return this.map(function(pair) { |
|---|
| | 522 | return pair.map(encodeURIComponent).join('='); |
|---|
| | 523 | }).join('&'); |
|---|
| | 524 | }, |
|---|
| | 525 | |
|---|
| | 526 | inspect: function() { |
|---|
| | 527 | return '#<Hash:{' + this.map(function(pair) { |
|---|
| | 528 | return pair.map(Object.inspect).join(': '); |
|---|
| | 529 | }).join(', ') + '}>'; |
|---|
| | 530 | } |
|---|
| | 531 | } |
|---|
| | 532 | |
|---|
| | 533 | function $H(object) { |
|---|
| | 534 | var hash = Object.extend({}, object || {}); |
|---|
| | 535 | Object.extend(hash, Enumerable); |
|---|
| | 536 | Object.extend(hash, Hash); |
|---|
| | 537 | return hash; |
|---|
| | 538 | } |
|---|
| | 539 | ObjectRange = Class.create(); |
|---|
| | 540 | Object.extend(ObjectRange.prototype, Enumerable); |
|---|
| | 541 | Object.extend(ObjectRange.prototype, { |
|---|
| | 542 | initialize: function(start, end, exclusive) { |
|---|
| | 543 | this.start = start; |
|---|
| | 544 | this.end = end; |
|---|
| | 545 | this.exclusive = exclusive; |
|---|
| | 546 | }, |
|---|
| | 547 | |
|---|
| | 548 | _each: function(iterator) { |
|---|
| | 549 | var value = this.start; |
|---|
| | 550 | do { |
|---|
| | 551 | iterator(value); |
|---|
| | 552 | value = value.succ(); |
|---|
| | 553 | } while (this.include(value)); |
|---|
| | 554 | }, |
|---|
| | 555 | |
|---|
| | 556 | include: function(value) { |
|---|
| | 557 | if (value < this.start) |
|---|
| | 558 | return false; |
|---|
| | 559 | if (this.exclusive) |
|---|
| | 560 | return value < this.end; |
|---|
| | 561 | return value <= this.end; |
|---|
| | 562 | } |
|---|
| | 563 | }); |
|---|
| | 564 | |
|---|
| | 565 | var $R = function(start, end, exclusive) { |
|---|
| | 566 | return new ObjectRange(start, end, exclusive); |
|---|
| | 567 | } |
|---|
| | 568 | |
|---|
| | 569 | var Ajax = { |
|---|
| | 570 | getTransport: function() { |
|---|
| | 571 | return Try.these( |
|---|
| | 572 | function() {return new ActiveXObject('Msxml2.XMLHTTP')}, |
|---|
| | 573 | function() {return new ActiveXObject('Microsoft.XMLHTTP')}, |
|---|
| | 574 | function() {return new XMLHttpRequest()} |
|---|
| | 575 | ) || false; |
|---|
| | 576 | }, |
|---|
| | 577 | |
|---|
| | 578 | activeRequestCount: 0 |
|---|
| | 579 | } |
|---|
| | 580 | |
|---|
| | 581 | Ajax.Responders = { |
|---|
| | 582 | responders: [], |
|---|
| | 583 | |
|---|
| | 584 | _each: function(iterator) { |
|---|
| | 585 | this.responders._each(iterator); |
|---|
| | 586 | }, |
|---|
| | 587 | |
|---|
| | 588 | register: function(responderToAdd) { |
|---|
| | 589 | if (!this.include(responderToAdd)) |
|---|
| | 590 | this.responders.push(responderToAdd); |
|---|
| | 591 | }, |
|---|
| | 592 | |
|---|
| | 593 | unregister: function(responderToRemove) { |
|---|
| | 594 | this.responders = this.responders.without(responderToRemove); |
|---|
| | 595 | }, |
|---|
| | 596 | |
|---|
| | 597 | dispatch: function(callback, request, transport, json) { |
|---|
| | 598 | this.each(function(responder) { |
|---|
| | 599 | if (responder[callback] && typeof responder[callback] == 'function') { |
|---|
| | 600 | try { |
|---|
| | 601 | responder[callback].apply(responder, [request, transport, json]); |
|---|
| | 602 | } catch (e) {} |
|---|
| | 603 | } |
|---|
| | 604 | }); |
|---|
| | 605 | } |
|---|
| | 606 | }; |
|---|
| | 607 | |
|---|
| | 608 | Object.extend(Ajax.Responders, Enumerable); |
|---|
| | 609 | |
|---|
| | 610 | Ajax.Responders.register({ |
|---|
| | 611 | onCreate: function() { |
|---|
| | 612 | Ajax.activeRequestCount++; |
|---|
| | 613 | }, |
|---|
| | 614 | |
|---|
| | 615 | onComplete: function() { |
|---|
| | 616 | Ajax.activeRequestCount--; |
|---|
| | 617 | } |
|---|
| | 618 | }); |
|---|
| | 619 | |
|---|
| | 620 | Ajax.Base = function() {}; |
|---|
| | 621 | Ajax.Base.prototype = { |
|---|
| | 622 | setOptions: function(options) { |
|---|
| | 623 | this.options = { |
|---|
| | 624 | method: 'post', |
|---|
| | 625 | asynchronous: true, |
|---|
| | 626 | parameters: '' |
|---|
| | 627 | } |
|---|
| | 628 | Object.extend(this.options, options || {}); |
|---|
| | 629 | }, |
|---|
| | 630 | |
|---|
| | 631 | responseIsSuccess: function() { |
|---|
| | 632 | return this.transport.status == undefined |
|---|
| | 633 | || this.transport.status == 0 |
|---|
| | 634 | || (this.transport.status >= 200 && this.transport.status < 300); |
|---|
| | 635 | }, |
|---|
| | 636 | |
|---|
| | 637 | responseIsFailure: function() { |
|---|
| | 638 | return !this.responseIsSuccess(); |
|---|
| | 639 | } |
|---|
| | 640 | } |
|---|
| | 641 | |
|---|
| | 642 | Ajax.Request = Class.create(); |
|---|
| | 643 | Ajax.Request.Events = |
|---|
| | 644 | ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; |
|---|
| | 645 | |
|---|
| | 646 | Ajax.Request.prototype = Object.extend(new Ajax.Base(), { |
|---|
| | 647 | initialize: function(url, options) { |
|---|
| | 648 | this.transport = Ajax.getTransport(); |
|---|
| | 649 | this.setOptions(options); |
|---|
| | 650 | this.request(url); |
|---|
| | 651 | }, |
|---|
| | 652 | |
|---|
| | 653 | request: function(url) { |
|---|
| | 654 | var parameters = this.options.parameters || ''; |
|---|
| | 655 | if (parameters.length > 0) parameters += '&_='; |
|---|
| | 656 | |
|---|
| | 657 | try { |
|---|
| | 658 | this.url = url; |
|---|
| | 659 | if (this.options.method == 'get' && parameters.length > 0) |
|---|
| | 660 | this.url += (this.url.match(/\?/) ? '&' : '?') + parameters; |
|---|
| | 661 | |
|---|
| | 662 | Ajax.Responders.dispatch('onCreate', this, this.transport); |
|---|
| | 663 | |
|---|
| | 664 | this.transport.open(this.options.method, this.url, |
|---|
| | 665 | this.options.asynchronous); |
|---|
| | 666 | |
|---|
| | 667 | if (this.options.asynchronous) { |
|---|
| | 668 | this.transport.onreadystatechange = this.onStateChange.bind(this); |
|---|
| | 669 | setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10); |
|---|
| | 670 | } |
|---|
| | 671 | |
|---|
| | 672 | this.setRequestHeaders(); |
|---|
| | 673 | |
|---|
| | 674 | var body = this.options.postBody ? this.options.postBody : parameters; |
|---|
| | 675 | this.transport.send(this.options.method == 'post' ? body : null); |
|---|
| | 676 | |
|---|
| | 677 | } catch (e) { |
|---|
| | 678 | this.dispatchException(e); |
|---|
| | 679 | } |
|---|
| | 680 | }, |
|---|
| | 681 | |
|---|
| | 682 | setRequestHeaders: function() { |
|---|
| | 683 | var requestHeaders = |
|---|
| | 684 | ['X-Requested-With', 'XMLHttpRequest', |
|---|
| | 685 | 'X-Prototype-Version', Prototype.Version]; |
|---|
| | 686 | |
|---|
| | 687 | if (this.options.method == 'post') { |
|---|
| | 688 | requestHeaders.push('Content-type', |
|---|
| | 689 | 'application/x-www-form-urlencoded'); |
|---|
| | 690 | |
|---|
| | 691 | /* Force "Connection: close" for Mozilla browsers to work around |
|---|
| | 692 | * a bug where XMLHttpReqeuest sends an incorrect Content-length |
|---|
| | 693 | * header. See Mozilla Bugzilla #246651. |
|---|
| | 694 | */ |
|---|
| | 695 | if (this.transport.overrideMimeType) |
|---|
| | 696 | requestHeaders.push('Connection', 'close'); |
|---|
| | 697 | } |
|---|
| | 698 | |
|---|
| | 699 | if (this.options.requestHeaders) |
|---|
| | 700 | requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); |
|---|
| | 701 | |
|---|
| | 702 | for (var i = 0; i < requestHeaders.length; i += 2) |
|---|
| | 703 | this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); |
|---|
| | 704 | }, |
|---|
| | 705 | |
|---|
| | 706 | onStateChange: function() { |
|---|
| | 707 | var readyState = this.transport.readyState; |
|---|
| | 708 | if (readyState != 1) |
|---|
| | 709 | this.respondToReadyState(this.transport.readyState); |
|---|
| | 710 | }, |
|---|
| | 711 | |
|---|
| | 712 | header: function(name) { |
|---|
| | 713 | try { |
|---|
| | 714 | return this.transport.getResponseHeader(name); |
|---|
| | 715 | } catch (e) {} |
|---|
| | 716 | }, |
|---|
| | 717 | |
|---|
| | 718 | evalJSON: function() { |
|---|
| | 719 | try { |
|---|
| | 720 | return eval(this.header('X-JSON')); |
|---|
| | 721 | } catch (e) {} |
|---|
| | 722 | }, |
|---|
| | 723 | |
|---|
| | 724 | evalResponse: function() { |
|---|
| | 725 | try { |
|---|
| | 726 | return eval(this.transport.responseText); |
|---|
| | 727 | } catch (e) { |
|---|
| | 728 | this.dispatchException(e); |
|---|
| | 729 | } |
|---|
| | 730 | }, |
|---|
| | 731 | |
|---|
| | 732 | respondToReadyState: function(readyState) { |
|---|
| | 733 | var event = Ajax.Request.Events[readyState]; |
|---|
| | 734 | var transport = this.transport, json = this.evalJSON(); |
|---|
| | 735 | |
|---|
| | 736 | if (event == 'Complete') { |
|---|
| | 737 | try { |
|---|
| | 738 | (this.options['on' + this.transport.status] |
|---|
| | 739 | || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] |
|---|
| | 740 | || Prototype.emptyFunction)(transport, json); |
|---|
| | 741 | } catch (e) { |
|---|
| | 742 | this.dispatchException(e); |
|---|
| | 743 | } |
|---|
| | 744 | |
|---|
| | 745 | if ((this.header('Content-type') || '').match(/^text\/javascript/i)) |
|---|
| | 746 | this.evalResponse(); |
|---|
| | 747 | } |
|---|
| | 748 | |
|---|
| | 749 | try { |
|---|
| | 750 | (this.options['on' + event] || Prototype.emptyFunction)(transport, json); |
|---|
| | 751 | Ajax.Responders.dispatch('on' + event, this, transport, json); |
|---|
| | 752 | } catch (e) { |
|---|
| | 753 | this.dispatchException(e); |
|---|
| | 754 | } |
|---|
| | 755 | |
|---|
| | 756 | /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ |
|---|
| | 757 | if (event == 'Complete') |
|---|
| | 758 | this.transport.onreadystatechange = Prototype.emptyFunction; |
|---|
| | 759 | }, |
|---|
| | 760 | |
|---|
| | 761 | dispatchException: function(exception) { |
|---|
| | 762 | (this.options.onException || Prototype.emptyFunction)(this, exception); |
|---|
| | 763 | Ajax.Responders.dispatch('onException', this, exception); |
|---|
| | 764 | } |
|---|
| | 765 | }); |
|---|
| | 766 | |
|---|
| | 767 | Ajax.Updater = Class.create(); |
|---|
| | 768 | |
|---|
| | 769 | Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { |
|---|
| | 770 | initialize: function(container, url, options) { |
|---|
| | 771 | this.containers = { |
|---|
| | 772 | success: container.success ? $(container.success) : $(container), |
|---|
| | 773 | failure: container.failure ? $(container.failure) : |
|---|
| | 774 | (container.success ? null : $(container)) |
|---|
| | 775 | } |
|---|
| | 776 | |
|---|
| | 777 | this.transport = Ajax.getTransport(); |
|---|
| | 778 | this.setOptions(options); |
|---|
| | 779 | |
|---|
| | 780 | var onComplete = this.options.onComplete || Prototype.emptyFunction; |
|---|
| | 781 | this.options.onComplete = (function(transport, object) { |
|---|
| | 782 | this.updateContent(); |
|---|
| | 783 | onComplete(transport, object); |
|---|
| | 784 | }).bind(this); |
|---|
| | 785 | |
|---|
| | 786 | this.request(url); |
|---|
| | 787 | }, |
|---|
| | 788 | |
|---|
| | 789 | updateContent: function() { |
|---|
| | 790 | var receiver = this.responseIsSuccess() ? |
|---|
| | 791 | this.containers.success : this.containers.failure; |
|---|
| | 792 | var response = this.transport.responseText; |
|---|
| | 793 | |
|---|
| | 794 | if (!this.options.evalScripts) |
|---|
| | 795 | response = response.stripScripts(); |
|---|
| | 796 | |
|---|
| | 797 | if (receiver) { |
|---|
| | 798 | if (this.options.insertion) { |
|---|
| | 799 | new this.options.insertion(receiver, response); |
|---|
| | 800 | } else { |
|---|
| | 801 | Element.update(receiver, response); |
|---|
| | 802 | } |
|---|
| | 803 | } |
|---|
| | 804 | |
|---|
| | 805 | if (this.responseIsSuccess()) { |
|---|
| | 806 | if (this.onComplete) |
|---|
| | 807 | setTimeout(this.onComplete.bind(this), 10); |
|---|
| | 808 | } |
|---|
| | 809 | } |
|---|
| | 810 | }); |
|---|
| | 811 | |
|---|
| | 812 | Ajax.PeriodicalUpdater = Class.create(); |
|---|
| | 813 | Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { |
|---|
| | 814 | initialize: function(container, url, options) { |
|---|
| | 815 | this.setOptions(options); |
|---|
| | 816 | this.onComplete = this.options.onComplete; |
|---|
| | 817 | |
|---|
| | 818 | this.frequency = (this.options.frequency || 2); |
|---|
| | 819 | this.decay = (this.options.decay || 1); |
|---|
| | 820 | |
|---|
| | 821 | this.updater = {}; |
|---|
| | 822 | this.container = container; |
|---|
| | 823 | this.url = url; |
|---|
| | 824 | |
|---|
| | 825 | this.start(); |
|---|
| | 826 | }, |
|---|
| | 827 | |
|---|
| | 828 | start: function() { |
|---|
| | 829 | this.options.onComplete = this.updateComplete.bind(this); |
|---|
| | 830 | this.onTimerEvent(); |
|---|
| | 831 | }, |
|---|
| | 832 | |
|---|
| | 833 | stop: function() { |
|---|
| | 834 | this.updater.onComplete = undefined; |
|---|
| | 835 | clearTimeout(this.timer); |
|---|
| | 836 | (this.onComplete || Prototype.emptyFunction).apply(this, arguments); |
|---|
| | 837 | }, |
|---|
| | 838 | |
|---|
| | 839 | updateComplete: function(request) { |
|---|
| | 840 | if (this.options.decay) { |
|---|
| | 841 | this.decay = (request.responseText == this.lastText ? |
|---|
| | 842 | this.decay * this.options.decay : 1); |
|---|
| | 843 | |
|---|
| | 844 | this.lastText = request.responseText; |
|---|
| | 845 | } |
|---|
| | 846 | this.timer = setTimeout(this.onTimerEvent.bind(this), |
|---|
| | 847 | this.decay * this.frequency * 1000); |
|---|
| | 848 | }, |
|---|
| | 849 | |
|---|
| | 850 | onTimerEvent: function() { |
|---|
| | 851 | this.updater = new Ajax.Updater(this.container, this.url, this.options); |
|---|
| | 852 | } |
|---|
| | 853 | }); |
|---|
| | 854 | document.getElementsByClassName = function(className, parentElement) { |
|---|
| | 855 | var children = ($(parentElement) || document.body).getElementsByTagName('*'); |
|---|
| | 856 | return $A(children).inject([], function(elements, child) { |
|---|
| | 857 | if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) |
|---|
| | 858 | elements.push(child); |
|---|
| | 859 | return elements; |
|---|
| | 860 | }); |
|---|
| | 861 | } |
|---|
| | 862 | |
|---|
| | 863 | /*--------------------------------------------------------------------------*/ |
|---|
| | 864 | |
|---|
| | 865 | if (!window.Element) { |
|---|
| | 866 | var Element = new Object(); |
|---|
| | 867 | } |
|---|
| | 868 | |
|---|
| | 869 | Object.extend(Element, { |
|---|
| | 870 | visible: function(element) { |
|---|
| | 871 | return $(element).style.display != 'none'; |
|---|
| | 872 | }, |
|---|
| | 873 | |
|---|
| | 874 | toggle: function() { |
|---|
| | 875 | for (var i = 0; i < arguments.length; i++) { |
|---|
| | 876 | var element = $(arguments[i]); |
|---|
| | 877 | Element[Element.visible(element) ? 'hide' : 'show'](element); |
|---|
| | 878 | } |
|---|
| | 879 | }, |
|---|
| | 880 | |
|---|
| | 881 | hide: function() { |
|---|
| | 882 | for (var i = 0; i < arguments.length; i++) { |
|---|
| | 883 | var element = $(arguments[i]); |
|---|
| | 884 | element.style.display = 'none'; |
|---|
| | 885 | } |
|---|
| | 886 | }, |
|---|
| | 887 | |
|---|
| | 888 | show: function() { |
|---|
| | 889 | for (var i = 0; i < arguments.length; i++) { |
|---|
| | 890 | var element = $(arguments[i]); |
|---|
| | 891 | element.style.display = ''; |
|---|
| | 892 | } |
|---|
| | 893 | }, |
|---|
| | 894 | |
|---|
| | 895 | remove: function(element) { |
|---|
| | 896 | element = $(element); |
|---|
| | 897 | element.parentNode.removeChild(element); |
|---|
| | 898 | }, |
|---|
| | 899 | |
|---|
| | 900 | update: function(element, html) { |
|---|
| | 901 | $(element).innerHTML = html.stripScripts(); |
|---|
| | 902 | setTimeout(function() {html.evalScripts()}, 10); |
|---|
| | 903 | }, |
|---|
| | 904 | |
|---|
| | 905 | getHeight: function(element) { |
|---|
| | 906 | element = $(element); |
|---|
| | 907 | return element.offsetHeight; |
|---|
| | 908 | }, |
|---|
| | 909 | |
|---|
| | 910 | classNames: function(element) { |
|---|
| | 911 | return new Element.ClassNames(element); |
|---|
| | 912 | }, |
|---|
| | 913 | |
|---|
| | 914 | hasClassName: function(element, className) { |
|---|
| | 915 | if (!(element = $(element))) return; |
|---|
| | 916 | return Element.classNames(element).include(className); |
|---|
| | 917 | }, |
|---|
| | 918 | |
|---|
| | 919 | addClassName: function(element, className) { |
|---|
| | 920 | if (!(element = $(element))) return; |
|---|
| | 921 | return Element.classNames(element).add(className); |
|---|
| | 922 | }, |
|---|
| | 923 | |
|---|
| | 924 | removeClassName: function(element, className) { |
|---|
| | 925 | if (!(element = $(element))) return; |
|---|
| | 926 | return Element.classNames(element).remove(className); |
|---|
| | 927 | }, |
|---|
| | 928 | |
|---|
| | 929 | // removes whitespace-only text node children |
|---|
| | 930 | cleanWhitespace: function(element) { |
|---|
| | 931 | element = $(element); |
|---|
| | 932 | for (var i = 0; i < element.childNodes.length; i++) { |
|---|
| | 933 | var node = element.childNodes[i]; |
|---|
| | 934 | if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) |
|---|
| | 935 | Element.remove(node); |
|---|
| | 936 | } |
|---|
| | 937 | }, |
|---|
| | 938 | |
|---|
| | 939 | empty: function(element) { |
|---|
| | 940 | return $(element).innerHTML.match(/^\s*$/); |
|---|
| | 941 | }, |
|---|
| | 942 | |
|---|
| | 943 | scrollTo: function(element) { |
|---|
| | 944 | element = $(element); |
|---|
| | 945 | var x = element.x ? element.x : element.offsetLeft, |
|---|
| | 946 | y = element.y ? element.y : element.offsetTop; |
|---|
| | 947 | window.scrollTo(x, y); |
|---|
| | 948 | }, |
|---|
| | 949 | |
|---|
| | 950 | getStyle: function(element, style) { |
|---|
| | 951 | element = $(element); |
|---|
| | 952 | var value = element.style[style.camelize()]; |
|---|
| | 953 | if (!value) { |
|---|
| | 954 | if (document.defaultView && document.defaultView.getComputedStyle) { |
|---|
| | 955 | var css = document.defaultView.getComputedStyle(element, null); |
|---|
| | 956 | value = css ? css.getPropertyValue(style) : null; |
|---|
| | 957 | } else if (element.currentStyle) { |
|---|
| | 958 | value = element.currentStyle[style.camelize()]; |
|---|
| | 959 | } |
|---|
| | 960 | } |
|---|
| | 961 | |
|---|
| | 962 | if (window.opera && ['left', 'top', 'right', 'bottom'].include(style)) |
|---|
| | 963 | if (Element.getStyle(element, 'position') == 'static') value = 'auto'; |
|---|
| | 964 | |
|---|
| | 965 | return value == 'auto' ? null : value; |
|---|
| | 966 | }, |
|---|
| | 967 | |
|---|
| | 968 | setStyle: function(element, style) { |
|---|
| | 969 | element = $(element); |
|---|
| | 970 | for (name in style) |
|---|
| | 971 | element.style[name.camelize()] = style[name]; |
|---|
| | 972 | }, |
|---|
| | 973 | |
|---|
| | 974 | getDimensions: function(element) { |
|---|
| | 975 | element = $(element); |
|---|
| | 976 | if (Element.getStyle(element, 'display') != 'none') |
|---|
| | 977 | return {width: element.offsetWidth, height: element.offsetHeight}; |
|---|
| | 978 | |
|---|
| | 979 | // All *Width and *Height properties give 0 on elements with display none, |
|---|
| | 980 | // so enable the element temporarily |
|---|
| | 981 | var els = element.style; |
|---|
| | 982 | var originalVisibility = els.visibility; |
|---|
| | 983 | var originalPosition = els.position; |
|---|
| | 984 | els.visibility = 'hidden'; |
|---|
| | 985 | els.position = 'absolute'; |
|---|
| | 986 | els.display = ''; |
|---|
| | 987 | var originalWidth = element.clientWidth; |
|---|
| | 988 | var originalHeight = element.clientHeight; |
|---|
| | 989 | els.display = 'none'; |
|---|
| | 990 | els.position = originalPosition; |
|---|
| | 991 | els.visibility = originalVisibility; |
|---|
| | 992 | return {width: originalWidth, height: originalHeight}; |
|---|
| | 993 | }, |
|---|
| | 994 | |
|---|
| | 995 | makePositioned: function(element) { |
|---|
| | 996 | element = $(element); |
|---|
| | 997 | var pos = Element.getStyle(element, 'position'); |
|---|
| | 998 | if (pos == 'static' || !pos) { |
|---|
| | 999 | element._madePositioned = true; |
|---|
| | 1000 | element.style.position = 'relative'; |
|---|
| | 1001 | // Opera returns the offset relative to the positioning context, when an |
|---|
| | 1002 | // element is position relative but top and left have not been defined |
|---|
| | 1003 | if (window.opera) { |
|---|
| | 1004 | element.style.top = 0; |
|---|
| | 1005 | element.style.left = 0; |
|---|
| | 1006 | } |
|---|
| | 1007 | } |
|---|
| | 1008 | }, |
|---|
| | 1009 | |
|---|
| | 1010 | undoPositioned: function(element) { |
|---|
| | 1011 | element = $(element); |
|---|
| | 1012 | if (element._madePositioned) { |
|---|
| | 1013 | element._madePositioned = undefined; |
|---|
| | 1014 | element.style.position = |
|---|
| | 1015 | element.style.top = |
|---|
| | 1016 | element.style.left = |
|---|
| | 1017 | element.style.bottom = |
|---|
| | 1018 | element.style.right = ''; |
|---|
| | 1019 | } |
|---|
| | 1020 | }, |
|---|
| | 1021 | |
|---|
| | 1022 | makeClipping: function(element) { |
|---|
| | 1023 | element = $(element); |
|---|
| | 1024 | if (element._overflow) return; |
|---|
| | 1025 | element._overflow = element.style.overflow; |
|---|
| | 1026 | if ((Element.getStyle(element, 'overflow') || 'visible') != 'hidden') |
|---|
| | 1027 | element.style.overflow = 'hidden'; |
|---|
| | 1028 | }, |
|---|
| | 1029 | |
|---|
| | 1030 | undoClipping: function(element) { |
|---|
| | 1031 | element = $(element); |
|---|
| | 1032 | if (element._overflow) return; |
|---|
| | 1033 | element.style.overflow = element._overflow; |
|---|
| | 1034 | element._overflow = undefined; |
|---|
| | 1035 | } |
|---|
| | 1036 | }); |
|---|
| | 1037 | |
|---|
| | 1038 | var Toggle = new Object(); |
|---|
| | 1039 | Toggle.display = Element.toggle; |
|---|
| | 1040 | |
|---|
| | 1041 | /*--------------------------------------------------------------------------*/ |
|---|
| | 1042 | |
|---|
| | 1043 | Abstract.Insertion = function(adjacency) { |
|---|
| | 1044 | this.adjacency = adjacency; |
|---|
| | 1045 | } |
|---|
| | 1046 | |
|---|
| | 1047 | Abstract.Insertion.prototype = { |
|---|
| | 1048 | initialize: function(element, content) { |
|---|
| | 1049 | this.element = $(element); |
|---|
| | 1050 | this.content = content.stripScripts(); |
|---|
| | 1051 | |
|---|
| | 1052 | if (this.adjacency && this.element.insertAdjacentHTML) { |
|---|
| | 1053 | try { |
|---|
| | 1054 | this.element.insertAdjacentHTML(this.adjacency, this.content); |
|---|
| | 1055 | } catch (e) { |
|---|
| | 1056 | if (this.element.tagName.toLowerCase() == 'tbody') { |
|---|
| | 1057 | this.insertContent(this.contentFromAnonymousTable()); |
|---|
| | 1058 | } else { |
|---|
| | 1059 | throw e; |
|---|
| | 1060 | } |
|---|
| | 1061 | } |
|---|
| | 1062 | } else { |
|---|
| | 1063 | this.range = this.element.ownerDocument.createRange(); |
|---|
| | 1064 | if (this.initializeRange) this.initializeRange(); |
|---|
| | 1065 | this.insertContent([this.range.createContextualFragment(this.content)]); |
|---|
| | 1066 | } |
|---|
| | 1067 | |
|---|
| | 1068 | setTimeout(function() {content.evalScripts()}, 10); |
|---|
| | 1069 | }, |
|---|
| | 1070 | |
|---|
| | 1071 | contentFromAnonymousTable: function() { |
|---|
| | 1072 | var div = document.createElement('div'); |
|---|
| | 1073 | div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>'; |
|---|
| | 1074 | return $A(div.childNodes[0].childNodes[0].childNodes); |
|---|
| | 1075 | } |
|---|
| | 1076 | } |
|---|
| | 1077 | |
|---|
| | 1078 | var Insertion = new Object(); |
|---|
| | 1079 | |
|---|
| | 1080 | Insertion.Before = Class.create(); |
|---|
| | 1081 | Insertion.Before.prototype = Object.extend(new Abstract.Insertion('beforeBegin'), { |
|---|
| | 1082 | initializeRange: function() { |
|---|
| | 1083 | this.range.setStartBefore(this.element); |
|---|
| | 1084 | }, |
|---|
| | 1085 | |
|---|
| | 1086 | insertContent: function(fragments) { |
|---|
| | 1087 | fragments.each((function(fragment) { |
|---|
| | 1088 | this.element.parentNode.insertBefore(fragment, this.element); |
|---|
| | 1089 | }).bind(this)); |
|---|
| | 1090 | } |
|---|
| | 1091 | }); |
|---|
| | 1092 | |
|---|
| | 1093 | Insertion.Top = Class.create(); |
|---|
| | 1094 | Insertion.Top.prototype = Object.extend(new Abstract.Insertion('afterBegin'), { |
|---|
| | 1095 | initializeRange: function() { |
|---|
| | 1096 | this.range.selectNodeContents(this.element); |
|---|
| | 1097 | this.range.collapse(true); |
|---|
| | 1098 | }, |
|---|
| | 1099 | |
|---|
| | 1100 | insertContent: function(fragments) { |
|---|
| | 1101 | fragments.reverse(false).each((function(fragment) { |
|---|
| | 1102 | this.element.insertBefore(fragment, this.element.firstChild); |
|---|
| | 1103 | }).bind(this)); |
|---|
| | 1104 | } |
|---|
| | 1105 | }); |
|---|
| | 1106 | |
|---|
| | 1107 | Insertion.Bottom = Class.create(); |
|---|
| | 1108 | Insertion.Bottom.prototype = Object.extend(new Abstract.Insertion('beforeEnd'), { |
|---|
| | 1109 | initializeRange: function() { |
|---|
| | 1110 | this.range.selectNodeContents(this.element); |
|---|
| | 1111 | this.range.collapse(this.element); |
|---|
| | 1112 | }, |
|---|
| | 1113 | |
|---|
| | 1114 | insertContent: function(fragments) { |
|---|
| | 1115 | fragments.each((function(fragment) { |
|---|
| | 1116 | this.element.appendChild(fragment); |
|---|
| | 1117 | }).bind(this)); |
|---|
| | 1118 | } |
|---|
| | 1119 | }); |
|---|
| | 1120 | |
|---|
| | 1121 | Insertion.After = Class.create(); |
|---|
| | 1122 | Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), { |
|---|
| | 1123 | initializeRange: function() { |
|---|
| | 1124 | this.range.setStartAfter(this.element); |
|---|
| | 1125 | }, |
|---|
| | 1126 | |
|---|
| | 1127 | insertContent: function(fragments) { |
|---|
| | 1128 | fragments.each((function(fragment) { |
|---|
| | 1129 | this.element.parentNode.insertBefore(fragment, |
|---|
| | 1130 | this.element.nextSibling); |
|---|
| | 1131 | }).bind(this)); |
|---|
| | 1132 | } |
|---|
| | 1133 | }); |
|---|
| | 1134 | |
|---|
| | 1135 | /*--------------------------------------------------------------------------*/ |
|---|
| | 1136 | |
|---|
| | 1137 | Element.ClassNames = Class.create(); |
|---|
| | 1138 | Element.ClassNames.prototype = { |
|---|
| | 1139 | initialize: function(element) { |
|---|
| | 1140 | this.element = $(element); |
|---|
| | 1141 | }, |
|---|
| | 1142 | |
|---|
| | 1143 | _each: function(iterator) { |
|---|
| | 1144 | this.element.className.split(/\s+/).select(function(name) { |
|---|
| | 1145 | return name.length > 0; |
|---|
| | 1146 | })._each(iterator); |
|---|
| | 1147 | }, |
|---|
| | 1148 | |
|---|
| | 1149 | set: function(className) { |
|---|
| | 1150 | this.element.className = className; |
|---|
| | 1151 | }, |
|---|
| | 1152 | |
|---|
| | 1153 | add: function(classNameToAdd) { |
|---|
| | 1154 | if (this.include(classNameToAdd)) return; |
|---|
| | 1155 | this.set(this.toArray().concat(classNameToAdd).join(' ')); |
|---|
| | 1156 | }, |
|---|
| | 1157 | |
|---|
| | 1158 | remove: function(classNameToRemove) { |
|---|
| | 1159 | if (!this.include(classNameToRemove)) return; |
|---|
| | 1160 | this.set(this.select(function(className) { |
|---|
| | 1161 | return className != classNameToRemove; |
|---|
| | 1162 | }).join(' ')); |
|---|
| | 1163 | }, |
|---|
| | 1164 | |
|---|
| | 1165 | toString: function() { |
|---|
| | 1166 | return this.toArray().join(' '); |
|---|
| | 1167 | } |
|---|
| | 1168 | } |
|---|
| | 1169 | |
|---|
| | 1170 | Object.extend(Element.ClassNames.prototype, Enumerable); |
|---|
| | 1171 | var Field = { |
|---|
| | 1172 | clear: function() { |
|---|
| | 1173 | for (var i = 0; i < arguments.length; i++) |
|---|
| | 1174 | $(arguments[i]).value = ''; |
|---|
| | 1175 | }, |
|---|
| | 1176 | |
|---|
| | 1177 | focus: function(element) { |
|---|
| | 1178 | $(element).focus(); |
|---|
| | 1179 | }, |
|---|
| | 1180 | |
|---|
| | 1181 | |
|---|