Commit 07245d7554745780aa5ba1666489d9083e4e8e56
- Diff rendering mode:
- inline
- side by side
build/css/validator.css
(0 / 17)
|   | |||
| 1 | .error-validate { | ||
| 2 | border-width:1px; | ||
| 3 | border-style:double; | ||
| 4 | border-color:red; | ||
| 5 | } | ||
| 6 | |||
| 7 | .error-validate-message { | ||
| 8 | font-size:10px; | ||
| 9 | background-color:#faaf10; | ||
| 10 | font-weight:bold; | ||
| 11 | } | ||
| 12 | |||
| 13 | .success-validate { | ||
| 14 | border-width:1px; | ||
| 15 | border-style:double; | ||
| 16 | border-color:green; | ||
| 17 | } |
build/prototype.js
(0 / 4320)
|   | |||
| 1 | /* Prototype JavaScript framework, version 1.6.0.3 | ||
| 2 | * (c) 2005-2008 Sam Stephenson | ||
| 3 | * | ||
| 4 | * Prototype is freely distributable under the terms of an MIT-style license. | ||
| 5 | * For details, see the Prototype web site: http://www.prototypejs.org/ | ||
| 6 | * | ||
| 7 | *--------------------------------------------------------------------------*/ | ||
| 8 | |||
| 9 | var Prototype = { | ||
| 10 | Version: '1.6.0.3', | ||
| 11 | |||
| 12 | Browser: { | ||
| 13 | IE: !!(window.attachEvent && | ||
| 14 | navigator.userAgent.indexOf('Opera') === -1), | ||
| 15 | Opera: navigator.userAgent.indexOf('Opera') > -1, | ||
| 16 | WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, | ||
| 17 | Gecko: navigator.userAgent.indexOf('Gecko') > -1 && | ||
| 18 | navigator.userAgent.indexOf('KHTML') === -1, | ||
| 19 | MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) | ||
| 20 | }, | ||
| 21 | |||
| 22 | BrowserFeatures: { | ||
| 23 | XPath: !!document.evaluate, | ||
| 24 | SelectorsAPI: !!document.querySelector, | ||
| 25 | ElementExtensions: !!window.HTMLElement, | ||
| 26 | SpecificElementExtensions: | ||
| 27 | document.createElement('div')['__proto__'] && | ||
| 28 | document.createElement('div')['__proto__'] !== | ||
| 29 | document.createElement('form')['__proto__'] | ||
| 30 | }, | ||
| 31 | |||
| 32 | ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', | ||
| 33 | JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, | ||
| 34 | |||
| 35 | emptyFunction: function() { }, | ||
| 36 | K: function(x) { return x } | ||
| 37 | }; | ||
| 38 | |||
| 39 | if (Prototype.Browser.MobileSafari) | ||
| 40 | Prototype.BrowserFeatures.SpecificElementExtensions = false; | ||
| 41 | |||
| 42 | |||
| 43 | /* Based on Alex Arnell's inheritance implementation. */ | ||
| 44 | var Class = { | ||
| 45 | create: function() { | ||
| 46 | var parent = null, properties = $A(arguments); | ||
| 47 | if (Object.isFunction(properties[0])) | ||
| 48 | parent = properties.shift(); | ||
| 49 | |||
| 50 | function klass() { | ||
| 51 | this.initialize.apply(this, arguments); | ||
| 52 | } | ||
| 53 | |||
| 54 | Object.extend(klass, Class.Methods); | ||
| 55 | klass.superclass = parent; | ||
| 56 | klass.subclasses = []; | ||
| 57 | |||
| 58 | if (parent) { | ||
| 59 | var subclass = function() { }; | ||
| 60 | subclass.prototype = parent.prototype; | ||
| 61 | klass.prototype = new subclass; | ||
| 62 | parent.subclasses.push(klass); | ||
| 63 | } | ||
| 64 | |||
| 65 | for (var i = 0; i < properties.length; i++) | ||
| 66 | klass.addMethods(properties[i]); | ||
| 67 | |||
| 68 | if (!klass.prototype.initialize) | ||
| 69 | klass.prototype.initialize = Prototype.emptyFunction; | ||
| 70 | |||
| 71 | klass.prototype.constructor = klass; | ||
| 72 | |||
| 73 | return klass; | ||
| 74 | } | ||
| 75 | }; | ||
| 76 | |||
| 77 | Class.Methods = { | ||
| 78 | addMethods: function(source) { | ||
| 79 | var ancestor = this.superclass && this.superclass.prototype; | ||
| 80 | var properties = Object.keys(source); | ||
| 81 | |||
| 82 | if (!Object.keys({ toString: true }).length) | ||
| 83 | properties.push("toString", "valueOf"); | ||
| 84 | |||
| 85 | for (var i = 0, length = properties.length; i < length; i++) { | ||
| 86 | var property = properties[i], value = source[property]; | ||
| 87 | if (ancestor && Object.isFunction(value) && | ||
| 88 | value.argumentNames().first() == "$super") { | ||
| 89 | var method = value; | ||
| 90 | value = (function(m) { | ||
| 91 | return function() { return ancestor[m].apply(this, arguments) }; | ||
| 92 | })(property).wrap(method); | ||
| 93 | |||
| 94 | value.valueOf = method.valueOf.bind(method); | ||
| 95 | value.toString = method.toString.bind(method); | ||
| 96 | } | ||
| 97 | this.prototype[property] = value; | ||
| 98 | } | ||
| 99 | |||
| 100 | return this; | ||
| 101 | } | ||
| 102 | }; | ||
| 103 | |||
| 104 | var Abstract = { }; | ||
| 105 | |||
| 106 | Object.extend = function(destination, source) { | ||
| 107 | for (var property in source) | ||
| 108 | destination[property] = source[property]; | ||
| 109 | return destination; | ||
| 110 | }; | ||
| 111 | |||
| 112 | Object.extend(Object, { | ||
| 113 | inspect: function(object) { | ||
| 114 | try { | ||
| 115 | if (Object.isUndefined(object)) return 'undefined'; | ||
| 116 | if (object === null) return 'null'; | ||
| 117 | return object.inspect ? object.inspect() : String(object); | ||
| 118 | } catch (e) { | ||
| 119 | if (e instanceof RangeError) return '...'; | ||
| 120 | throw e; | ||
| 121 | } | ||
| 122 | }, | ||
| 123 | |||
| 124 | toJSON: function(object) { | ||
| 125 | var type = typeof object; | ||
| 126 | switch (type) { | ||
| 127 | case 'undefined': | ||
| 128 | case 'function': | ||
| 129 | case 'unknown': return; | ||
| 130 | case 'boolean': return object.toString(); | ||
| 131 | } | ||
| 132 | |||
| 133 | if (object === null) return 'null'; | ||
| 134 | if (object.toJSON) return object.toJSON(); | ||
| 135 | if (Object.isElement(object)) return; | ||
| 136 | |||
| 137 | var results = []; | ||
| 138 | for (var property in object) { | ||
| 139 | var value = Object.toJSON(object[property]); | ||
| 140 | if (!Object.isUndefined(value)) | ||
| 141 | results.push(property.toJSON() + ': ' + value); | ||
| 142 | } | ||
| 143 | |||
| 144 | return '{' + results.join(', ') + '}'; | ||
| 145 | }, | ||
| 146 | |||
| 147 | toQueryString: function(object) { | ||
| 148 | return $H(object).toQueryString(); | ||
| 149 | }, | ||
| 150 | |||
| 151 | toHTML: function(object) { | ||
| 152 | return object && object.toHTML ? object.toHTML() : String.interpret(object); | ||
| 153 | }, | ||
| 154 | |||
| 155 | keys: function(object) { | ||
| 156 | var keys = []; | ||
| 157 | for (var property in object) | ||
| 158 | keys.push(property); | ||
| 159 | return keys; | ||
| 160 | }, | ||
| 161 | |||
| 162 | values: function(object) { | ||
| 163 | var values = []; | ||
| 164 | for (var property in object) | ||
| 165 | values.push(object[property]); | ||
| 166 | return values; | ||
| 167 | }, | ||
| 168 | |||
| 169 | clone: function(object) { | ||
| 170 | return Object.extend({ }, object); | ||
| 171 | }, | ||
| 172 | |||
| 173 | isElement: function(object) { | ||
| 174 | return !!(object && object.nodeType == 1); | ||
| 175 | }, | ||
| 176 | |||
| 177 | isArray: function(object) { | ||
| 178 | return object != null && typeof object == "object" && | ||
| 179 | 'splice' in object && 'join' in object; | ||
| 180 | }, | ||
| 181 | |||
| 182 | isHash: function(object) { | ||
| 183 | return object instanceof Hash; | ||
| 184 | }, | ||
| 185 | |||
| 186 | isFunction: function(object) { | ||
| 187 | return typeof object == "function"; | ||
| 188 | }, | ||
| 189 | |||
| 190 | isString: function(object) { | ||
| 191 | return typeof object == "string"; | ||
| 192 | }, | ||
| 193 | |||
| 194 | isNumber: function(object) { | ||
| 195 | return typeof object == "number"; | ||
| 196 | }, | ||
| 197 | |||
| 198 | isUndefined: function(object) { | ||
| 199 | return typeof object == "undefined"; | ||
| 200 | } | ||
| 201 | }); | ||
| 202 | |||
| 203 | Object.extend(Function.prototype, { | ||
| 204 | argumentNames: function() { | ||
| 205 | var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1] | ||
| 206 | .replace(/\s+/g, '').split(','); | ||
| 207 | return names.length == 1 && !names[0] ? [] : names; | ||
| 208 | }, | ||
| 209 | |||
| 210 | bind: function() { | ||
| 211 | if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; | ||
| 212 | var __method = this, args = $A(arguments), object = args.shift(); | ||
| 213 | return function() { | ||
| 214 | return __method.apply(object, args.concat($A(arguments))); | ||
| 215 | } | ||
| 216 | }, | ||
| 217 | |||
| 218 | bindAsEventListener: function() { | ||
| 219 | var __method = this, args = $A(arguments), object = args.shift(); | ||
| 220 | return function(event) { | ||
| 221 | return __method.apply(object, [event || window.event].concat(args)); | ||
| 222 | } | ||
| 223 | }, | ||
| 224 | |||
| 225 | curry: function() { | ||
| 226 | if (!arguments.length) return this; | ||
| 227 | var __method = this, args = $A(arguments); | ||
| 228 | return function() { | ||
| 229 | return __method.apply(this, args.concat($A(arguments))); | ||
| 230 | } | ||
| 231 | }, | ||
| 232 | |||
| 233 | delay: function() { | ||
| 234 | var __method = this, args = $A(arguments), timeout = args.shift() * 1000; | ||
| 235 | return window.setTimeout(function() { | ||
| 236 | return __method.apply(__method, args); | ||
| 237 | }, timeout); | ||
| 238 | }, | ||
| 239 | |||
| 240 | defer: function() { | ||
| 241 | var args = [0.01].concat($A(arguments)); | ||
| 242 | return this.delay.apply(this, args); | ||
| 243 | }, | ||
| 244 | |||
| 245 | wrap: function(wrapper) { | ||
| 246 | var __method = this; | ||
| 247 | return function() { | ||
| 248 | return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); | ||
| 249 | } | ||
| 250 | }, | ||
| 251 | |||
| 252 | methodize: function() { | ||
| 253 | if (this._methodized) return this._methodized; | ||
| 254 | var __method = this; | ||
| 255 | return this._methodized = function() { | ||
| 256 | return __method.apply(null, [this].concat($A(arguments))); | ||
| 257 | }; | ||
| 258 | } | ||
| 259 | }); | ||
| 260 | |||
| 261 | Date.prototype.toJSON = function() { | ||
| 262 | return '"' + this.getUTCFullYear() + '-' + | ||
| 263 | (this.getUTCMonth() + 1).toPaddedString(2) + '-' + | ||
| 264 | this.getUTCDate().toPaddedString(2) + 'T' + | ||
| 265 | this.getUTCHours().toPaddedString(2) + ':' + | ||
| 266 | this.getUTCMinutes().toPaddedString(2) + ':' + | ||
| 267 | this.getUTCSeconds().toPaddedString(2) + 'Z"'; | ||
| 268 | }; | ||
| 269 | |||
| 270 | var Try = { | ||
| 271 | these: function() { | ||
| 272 | var returnValue; | ||
| 273 | |||
| 274 | for (var i = 0, length = arguments.length; i < length; i++) { | ||
| 275 | var lambda = arguments[i]; | ||
| 276 | try { | ||
| 277 | returnValue = lambda(); | ||
| 278 | break; | ||
| 279 | } catch (e) { } | ||
| 280 | } | ||
| 281 | |||
| 282 | return returnValue; | ||
| 283 | } | ||
| 284 | }; | ||
| 285 | |||
| 286 | RegExp.prototype.match = RegExp.prototype.test; | ||
| 287 | |||
| 288 | RegExp.escape = function(str) { | ||
| 289 | return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); | ||
| 290 | }; | ||
| 291 | |||
| 292 | /*--------------------------------------------------------------------------*/ | ||
| 293 | |||
| 294 | var PeriodicalExecuter = Class.create({ | ||
| 295 | initialize: function(callback, frequency) { | ||
| 296 | this.callback = callback; | ||
| 297 | this.frequency = frequency; | ||
| 298 | this.currentlyExecuting = false; | ||
| 299 | |||
| 300 | this.registerCallback(); | ||
| 301 | }, | ||
| 302 | |||
| 303 | registerCallback: function() { | ||
| 304 | this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); | ||
| 305 | }, | ||
| 306 | |||
| 307 | execute: function() { | ||
| 308 | this.callback(this); | ||
| 309 | }, | ||
| 310 | |||
| 311 | stop: function() { | ||
| 312 | if (!this.timer) return; | ||
| 313 | clearInterval(this.timer); | ||
| 314 | this.timer = null; | ||
| 315 | }, | ||
| 316 | |||
| 317 | onTimerEvent: function() { | ||
| 318 | if (!this.currentlyExecuting) { | ||
| 319 | try { | ||
| 320 | this.currentlyExecuting = true; | ||
| 321 | this.execute(); | ||
| 322 | } finally { | ||
| 323 | this.currentlyExecuting = false; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | }); | ||
| 328 | Object.extend(String, { | ||
| 329 | interpret: function(value) { | ||
| 330 | return value == null ? '' : String(value); | ||
| 331 | }, | ||
| 332 | specialChar: { | ||
| 333 | '\b': '\\b', | ||
| 334 | '\t': '\\t', | ||
| 335 | '\n': '\\n', | ||
| 336 | '\f': '\\f', | ||
| 337 | '\r': '\\r', | ||
| 338 | '\\': '\\\\' | ||
| 339 | } | ||
| 340 | }); | ||
| 341 | |||
| 342 | Object.extend(String.prototype, { | ||
| 343 | gsub: function(pattern, replacement) { | ||
| 344 | var result = '', source = this, match; | ||
| 345 | replacement = arguments.callee.prepareReplacement(replacement); | ||
| 346 | |||
| 347 | while (source.length > 0) { | ||
| 348 | if (match = source.match(pattern)) { | ||
| 349 | result += source.slice(0, match.index); | ||
| 350 | result += String.interpret(replacement(match)); | ||
| 351 | source = source.slice(match.index + match[0].length); | ||
| 352 | } else { | ||
| 353 | result += source, source = ''; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | return result; | ||
| 357 | }, | ||
| 358 | |||
| 359 | sub: function(pattern, replacement, count) { | ||
| 360 | replacement = this.gsub.prepareReplacement(replacement); | ||
| 361 | count = Object.isUndefined(count) ? 1 : count; | ||
| 362 | |||
| 363 | return this.gsub(pattern, function(match) { | ||
| 364 | if (--count < 0) return match[0]; | ||
| 365 | return replacement(match); | ||
| 366 | }); | ||
| 367 | }, | ||
| 368 | |||
| 369 | scan: function(pattern, iterator) { | ||
| 370 | this.gsub(pattern, iterator); | ||
| 371 | return String(this); | ||
| 372 | }, | ||
| 373 | |||
| 374 | truncate: function(length, truncation) { | ||
| 375 | length = length || 30; | ||
| 376 | truncation = Object.isUndefined(truncation) ? '...' : truncation; | ||
| 377 | return this.length > length ? | ||
| 378 | this.slice(0, length - truncation.length) + truncation : String(this); | ||
| 379 | }, | ||
| 380 | |||
| 381 | strip: function() { | ||
| 382 | return this.replace(/^\s+/, '').replace(/\s+$/, ''); | ||
| 383 | }, | ||
| 384 | |||
| 385 | stripTags: function() { | ||
| 386 | return this.replace(/<\/?[^>]+>/gi, ''); | ||
| 387 | }, | ||
| 388 | |||
| 389 | stripScripts: function() { | ||
| 390 | return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); | ||
| 391 | }, | ||
| 392 | |||
| 393 | extractScripts: function() { | ||
| 394 | var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); | ||
| 395 | var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); | ||
| 396 | return (this.match(matchAll) || []).map(function(scriptTag) { | ||
| 397 | return (scriptTag.match(matchOne) || ['', ''])[1]; | ||
| 398 | }); | ||
| 399 | }, | ||
| 400 | |||
| 401 | evalScripts: function() { | ||
| 402 | return this.extractScripts().map(function(script) { return eval(script) }); | ||
| 403 | }, | ||
| 404 | |||
| 405 | escapeHTML: function() { | ||
| 406 | var self = arguments.callee; | ||
| 407 | self.text.data = this; | ||
| 408 | return self.div.innerHTML; | ||
| 409 | }, | ||
| 410 | |||
| 411 | unescapeHTML: function() { | ||
| 412 | var div = new Element('div'); | ||
| 413 | div.innerHTML = this.stripTags(); | ||
| 414 | return div.childNodes[0] ? (div.childNodes.length > 1 ? | ||
| 415 | $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : | ||
| 416 | div.childNodes[0].nodeValue) : ''; | ||
| 417 | }, | ||
| 418 | |||
| 419 | toQueryParams: function(separator) { | ||
| 420 | var match = this.strip().match(/([^?#]*)(#.*)?$/); | ||
| 421 | if (!match) return { }; | ||
| 422 | |||
| 423 | return match[1].split(separator || '&').inject({ }, function(hash, pair) { | ||
| 424 | if ((pair = pair.split('='))[0]) { | ||
| 425 | var key = decodeURIComponent(pair.shift()); | ||
| 426 | var value = pair.length > 1 ? pair.join('=') : pair[0]; | ||
| 427 | if (value != undefined) value = decodeURIComponent(value); | ||
| 428 | |||
| 429 | if (key in hash) { | ||
| 430 | if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; | ||
| 431 | hash[key].push(value); | ||
| 432 | } | ||
| 433 | else hash[key] = value; | ||
| 434 | } | ||
| 435 | return hash; | ||
| 436 | }); | ||
| 437 | }, | ||
| 438 | |||
| 439 | toArray: function() { | ||
| 440 | return this.split(''); | ||
| 441 | }, | ||
| 442 | |||
| 443 | succ: function() { | ||
| 444 | return this.slice(0, this.length - 1) + | ||
| 445 | String.fromCharCode(this.charCodeAt(this.length - 1) + 1); | ||
| 446 | }, | ||
| 447 | |||
| 448 | times: function(count) { | ||
| 449 | return count < 1 ? '' : new Array(count + 1).join(this); | ||
| 450 | }, | ||
| 451 | |||
| 452 | camelize: function() { | ||
| 453 | var parts = this.split('-'), len = parts.length; | ||
| 454 | if (len == 1) return parts[0]; | ||
| 455 | |||
| 456 | var camelized = this.charAt(0) == '-' | ||
| 457 | ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) | ||
| 458 | : parts[0]; | ||
| 459 | |||
| 460 | for (var i = 1; i < len; i++) | ||
| 461 | camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); | ||
| 462 | |||
| 463 | return camelized; | ||
| 464 | }, | ||
| 465 | |||
| 466 | capitalize: function() { | ||
| 467 | return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); | ||
| 468 | }, | ||
| 469 | |||
| 470 | underscore: function() { | ||
| 471 | return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); | ||
| 472 | }, | ||
| 473 | |||
| 474 | dasherize: function() { | ||
| 475 | return this.gsub(/_/,'-'); | ||
| 476 | }, | ||
| 477 | |||
| 478 | inspect: function(useDoubleQuotes) { | ||
| 479 | var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { | ||
| 480 | var character = String.specialChar[match[0]]; | ||
| 481 | return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); | ||
| 482 | }); | ||
| 483 | if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; | ||
| 484 | return "'" + escapedString.replace(/'/g, '\\\'') + "'"; | ||
| 485 | }, | ||
| 486 | |||
| 487 | toJSON: function() { | ||
| 488 | return this.inspect(true); | ||
| 489 | }, | ||
| 490 | |||
| 491 | unfilterJSON: function(filter) { | ||
| 492 | return this.sub(filter || Prototype.JSONFilter, '#{1}'); | ||
| 493 | }, | ||
| 494 | |||
| 495 | isJSON: function() { | ||
| 496 | var str = this; | ||
| 497 | if (str.blank()) return false; | ||
| 498 | str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); | ||
| 499 | return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); | ||
| 500 | }, | ||
| 501 | |||
| 502 | evalJSON: function(sanitize) { | ||
| 503 | var json = this.unfilterJSON(); | ||
| 504 | try { | ||
| 505 | if (!sanitize || json.isJSON()) return eval('(' + json + ')'); | ||
| 506 | } catch (e) { } | ||
| 507 | throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); | ||
| 508 | }, | ||
| 509 | |||
| 510 | include: function(pattern) { | ||
| 511 | return this.indexOf(pattern) > -1; | ||
| 512 | }, | ||
| 513 | |||
| 514 | startsWith: function(pattern) { | ||
| 515 | return this.indexOf(pattern) === 0; | ||
| 516 | }, | ||
| 517 | |||
| 518 | endsWith: function(pattern) { | ||
| 519 | var d = this.length - pattern.length; | ||
| 520 | return d >= 0 && this.lastIndexOf(pattern) === d; | ||
| 521 | }, | ||
| 522 | |||
| 523 | empty: function() { | ||
| 524 | return this == ''; | ||
| 525 | }, | ||
| 526 | |||
| 527 | blank: function() { | ||
| 528 | return /^\s*$/.test(this); | ||
| 529 | }, | ||
| 530 | |||
| 531 | interpolate: function(object, pattern) { | ||
| 532 | return new Template(this, pattern).evaluate(object); | ||
| 533 | } | ||
| 534 | }); | ||
| 535 | |||
| 536 | if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { | ||
| 537 | escapeHTML: function() { | ||
| 538 | return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | ||
| 539 | }, | ||
| 540 | unescapeHTML: function() { | ||
| 541 | return this.stripTags().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | ||
| 542 | } | ||
| 543 | }); | ||
| 544 | |||
| 545 | String.prototype.gsub.prepareReplacement = function(replacement) { | ||
| 546 | if (Object.isFunction(replacement)) return replacement; | ||
| 547 | var template = new Template(replacement); | ||
| 548 | return function(match) { return template.evaluate(match) }; | ||
| 549 | }; | ||
| 550 | |||
| 551 | String.prototype.parseQuery = String.prototype.toQueryParams; | ||
| 552 | |||
| 553 | Object.extend(String.prototype.escapeHTML, { | ||
| 554 | div: document.createElement('div'), | ||
| 555 | text: document.createTextNode('') | ||
| 556 | }); | ||
| 557 | |||
| 558 | String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text); | ||
| 559 | |||
| 560 | var Template = Class.create({ | ||
| 561 | initialize: function(template, pattern) { | ||
| 562 | this.template = template.toString(); | ||
| 563 | this.pattern = pattern || Template.Pattern; | ||
| 564 | }, | ||
| 565 | |||
| 566 | evaluate: function(object) { | ||
| 567 | if (Object.isFunction(object.toTemplateReplacements)) | ||
| 568 | object = object.toTemplateReplacements(); | ||
| 569 | |||
| 570 | return this.template.gsub(this.pattern, function(match) { | ||
| 571 | if (object == null) return ''; | ||
| 572 | |||
| 573 | var before = match[1] || ''; | ||
| 574 | if (before == '\\') return match[2]; | ||
| 575 | |||
| 576 | var ctx = object, expr = match[3]; | ||
| 577 | var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; | ||
| 578 | match = pattern.exec(expr); | ||
| 579 | if (match == null) return before; | ||
| 580 | |||
| 581 | while (match != null) { | ||
| 582 | var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; | ||
| 583 | ctx = ctx[comp]; | ||
| 584 | if (null == ctx || '' == match[3]) break; | ||
| 585 | expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); | ||
| 586 | match = pattern.exec(expr); | ||
| 587 | } | ||
| 588 | |||
| 589 | return before + String.interpret(ctx); | ||
| 590 | }); | ||
| 591 | } | ||
| 592 | }); | ||
| 593 | Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; | ||
| 594 | |||
| 595 | var $break = { }; | ||
| 596 | |||
| 597 | var Enumerable = { | ||
| 598 | each: function(iterator, context) { | ||
| 599 | var index = 0; | ||
| 600 | try { | ||
| 601 | this._each(function(value) { | ||
| 602 | iterator.call(context, value, index++); | ||
| 603 | }); | ||
| 604 | } catch (e) { | ||
| 605 | if (e != $break) throw e; | ||
| 606 | } | ||
| 607 | return this; | ||
| 608 | }, | ||
| 609 | |||
| 610 | eachSlice: function(number, iterator, context) { | ||
| 611 | var index = -number, slices = [], array = this.toArray(); | ||
| 612 | if (number < 1) return array; | ||
| 613 | while ((index += number) < array.length) | ||
| 614 | slices.push(array.slice(index, index+number)); | ||
| 615 | return slices.collect(iterator, context); | ||
| 616 | }, | ||
| 617 | |||
| 618 | all: function(iterator, context) { | ||
| 619 | iterator = iterator || Prototype.K; | ||
| 620 | var result = true; | ||
| 621 | this.each(function(value, index) { | ||
| 622 | result = result && !!iterator.call(context, value, index); | ||
| 623 | if (!result) throw $break; | ||
| 624 | }); | ||
| 625 | return result; | ||
| 626 | }, | ||
| 627 | |||
| 628 | any: function(iterator, context) { | ||
| 629 | iterator = iterator || Prototype.K; | ||
| 630 | var result = false; | ||
| 631 | this.each(function(value, index) { | ||
| 632 | if (result = !!iterator.call(context, value, index)) | ||
| 633 | throw $break; | ||
| 634 | }); | ||
| 635 | return result; | ||
| 636 | }, | ||
| 637 | |||
| 638 | collect: function(iterator, context) { | ||
| 639 | iterator = iterator || Prototype.K; | ||
| 640 | var results = []; | ||
| 641 | this.each(function(value, index) { | ||
| 642 | results.push(iterator.call(context, value, index)); | ||
| 643 | }); | ||
| 644 | return results; | ||
| 645 | }, | ||
| 646 | |||
| 647 | detect: function(iterator, context) { | ||
| 648 | var result; | ||
| 649 | this.each(function(value, index) { | ||
| 650 | if (iterator.call(context, value, index)) { | ||
| 651 | result = value; | ||
| 652 | throw $break; | ||
| 653 | } | ||
| 654 | }); | ||
| 655 | return result; | ||
| 656 | }, | ||
| 657 | |||
| 658 | findAll: function(iterator, context) { | ||
| 659 | var results = []; | ||
| 660 | this.each(function(value, index) { | ||
| 661 | if (iterator.call(context, value, index)) | ||
| 662 | results.push(value); | ||
| 663 | }); | ||
| 664 | return results; | ||
| 665 | }, | ||
| 666 | |||
| 667 | grep: function(filter, iterator, context) { | ||
| 668 | iterator = iterator || Prototype.K; | ||
| 669 | var results = []; | ||
| 670 | |||
| 671 | if (Object.isString(filter)) | ||
| 672 | filter = new RegExp(filter); | ||
| 673 | |||
| 674 | this.each(function(value, index) { | ||
| 675 | if (filter.match(value)) | ||
| 676 | results.push(iterator.call(context, value, index)); | ||
| 677 | }); | ||
| 678 | return results; | ||
| 679 | }, | ||
| 680 | |||
| 681 | include: function(object) { | ||
| 682 | if (Object.isFunction(this.indexOf)) | ||
| 683 | if (this.indexOf(object) != -1) return true; | ||
| 684 | |||
| 685 | var found = false; | ||
| 686 | this.each(function(value) { | ||
| 687 | if (value == object) { | ||
| 688 | found = true; | ||
| 689 | throw $break; | ||
| 690 | } | ||
| 691 | }); | ||
| 692 | return found; | ||
| 693 | }, | ||
| 694 | |||
| 695 | inGroupsOf: function(number, fillWith) { | ||
| 696 | fillWith = Object.isUndefined(fillWith) ? null : fillWith; | ||
| 697 | return this.eachSlice(number, function(slice) { | ||
| 698 | while(slice.length < number) slice.push(fillWith); | ||
| 699 | return slice; | ||
| 700 | }); | ||
| 701 | }, | ||
| 702 | |||
| 703 | inject: function(memo, iterator, context) { | ||
| 704 | this.each(function(value, index) { | ||
| 705 | memo = iterator.call(context, memo, value, index); | ||
| 706 | }); | ||
| 707 | return memo; | ||
| 708 | }, | ||
| 709 | |||
| 710 | invoke: function(method) { | ||
| 711 | var args = $A(arguments).slice(1); | ||
| 712 | return this.map(function(value) { | ||
| 713 | return value[method].apply(value, args); | ||
| 714 | }); | ||
| 715 | }, | ||
| 716 | |||
| 717 | max: function(iterator, context) { | ||
| 718 | iterator = iterator || Prototype.K; | ||
| 719 | var result; | ||
| 720 | this.each(function(value, index) { | ||
| 721 | value = iterator.call(context, value, index); | ||
| 722 | if (result == null || value >= result) | ||
| 723 | result = value; | ||
| 724 | }); | ||
| 725 | return result; | ||
| 726 | }, | ||
| 727 | |||
| 728 | min: function(iterator, context) { | ||
| 729 | iterator = iterator || Prototype.K; | ||
| 730 | var result; | ||
| 731 | this.each(function(value, index) { | ||
| 732 | value = iterator.call(context, value, index); | ||
| 733 | if (result == null || value < result) | ||
| 734 | result = value; | ||
| 735 | }); | ||
| 736 | return result; | ||
| 737 | }, | ||
| 738 | |||
| 739 | partition: function(iterator, context) { | ||
| 740 | iterator = iterator || Prototype.K; | ||
| 741 | var trues = [], falses = []; | ||
| 742 | this.each(function(value, index) { | ||
| 743 | (iterator.call(context, value, index) ? | ||
| 744 | trues : falses).push(value); | ||
| 745 | }); | ||
| 746 | return [trues, falses]; | ||
| 747 | }, | ||
| 748 | |||
| 749 | pluck: function(property) { | ||
| 750 | var results = []; | ||
| 751 | this.each(function(value) { | ||
| 752 | results.push(value[property]); | ||
| 753 | }); | ||
| 754 | return results; | ||
| 755 | }, | ||
| 756 | |||
| 757 | reject: function(iterator, context) { | ||
| 758 | var results = []; | ||
| 759 | this.each(function(value, index) { | ||
| 760 | if (!iterator.call(context, value, index)) | ||
| 761 | results.push(value); | ||
| 762 | }); | ||
| 763 | return results; | ||
| 764 | }, | ||
| 765 | |||
| 766 | sortBy: function(iterator, context) { | ||
| 767 | return this.map(function(value, index) { | ||
| 768 | return { | ||
| 769 | value: value, | ||
| 770 | criteria: iterator.call(context, value, index) | ||
| 771 | }; | ||
| 772 | }).sort(function(left, right) { | ||
| 773 | var a = left.criteria, b = right.criteria; | ||
| 774 | return a < b ? -1 : a > b ? 1 : 0; | ||
| 775 | }).pluck('value'); | ||
| 776 | }, | ||
| 777 | |||
| 778 | toArray: function() { | ||
| 779 | return this.map(); | ||
| 780 | }, | ||
| 781 | |||
| 782 | zip: function() { | ||
| 783 | var iterator = Prototype.K, args = $A(arguments); | ||
| 784 | if (Object.isFunction(args.last())) | ||
| 785 | iterator = args.pop(); | ||
| 786 | |||
| 787 | var collections = [this].concat(args).map($A); | ||
| 788 | return this.map(function(value, index) { | ||
| 789 | return iterator(collections.pluck(index)); | ||
| 790 | }); | ||
| 791 | }, | ||
| 792 | |||
| 793 | size: function() { | ||
| 794 | return this.toArray().length; | ||
| 795 | }, | ||
| 796 | |||
| 797 | inspect: function() { | ||
| 798 | return '#<Enumerable:' + this.toArray().inspect() + '>'; | ||
| 799 | } | ||
| 800 | }; | ||
| 801 | |||
| 802 | Object.extend(Enumerable, { | ||
| 803 | map: Enumerable.collect, | ||
| 804 | find: Enumerable.detect, | ||
| 805 | select: Enumerable.findAll, | ||
| 806 | filter: Enumerable.findAll, | ||
| 807 | member: Enumerable.include, | ||
| 808 | entries: Enumerable.toArray, | ||
| 809 | every: Enumerable.all, | ||
| 810 | some: Enumerable.any | ||
| 811 | }); | ||
| 812 | function $A(iterable) { | ||
| 813 | if (!iterable) return []; | ||
| 814 | if (iterable.toArray) return iterable.toArray(); | ||
| 815 | var length = iterable.length || 0, results = new Array(length); | ||
| 816 | while (length--) results[length] = iterable[length]; | ||
| 817 | return results; | ||
| 818 | } | ||
| 819 | |||
| 820 | if (Prototype.Browser.WebKit) { | ||
| 821 | $A = function(iterable) { | ||
| 822 | if (!iterable) return []; | ||
| 823 | // In Safari, only use the `toArray` method if it's not a NodeList. | ||
| 824 | // A NodeList is a function, has an function `item` property, and a numeric | ||
| 825 | // `length` property. Adapted from Google Doctype. | ||
| 826 | if (!(typeof iterable === 'function' && typeof iterable.length === | ||
| 827 | 'number' && typeof iterable.item === 'function') && iterable.toArray) | ||
| 828 | return iterable.toArray(); | ||
| 829 | var length = iterable.length || 0, results = new Array(length); | ||
| 830 | while (length--) results[length] = iterable[length]; | ||
| 831 | return results; | ||
| 832 | }; | ||
| 833 | } | ||
| 834 | |||
| 835 | Array.from = $A; | ||
| 836 | |||
| 837 | Object.extend(Array.prototype, Enumerable); | ||
| 838 | |||
| 839 | if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; | ||
| 840 | |||
| 841 | Object.extend(Array.prototype, { | ||
| 842 | _each: function(iterator) { | ||
| 843 | for (var i = 0, length = this.length; i < length; i++) | ||
| 844 | iterator(this[i]); | ||
| 845 | }, | ||
| 846 | |||
| 847 | clear: function() { | ||
| 848 | this.length = 0; | ||
| 849 | return this; | ||
| 850 | }, | ||
| 851 | |||
| 852 | first: function() { | ||
| 853 | return this[0]; | ||
| 854 | }, | ||
| 855 | |||
| 856 | last: function() { | ||
| 857 | return this[this.length - 1]; | ||
| 858 | }, | ||
| 859 | |||
| 860 | compact: function() { | ||
| 861 | return this.select(function(value) { | ||
| 862 | return value != null; | ||
| 863 | }); | ||
| 864 | }, | ||
| 865 | |||
| 866 | flatten: function() { | ||
| 867 | return this.inject([], function(array, value) { | ||
| 868 | return array.concat(Object.isArray(value) ? | ||
| 869 | value.flatten() : [value]); | ||
| 870 | }); | ||
| 871 | }, | ||
| 872 | |||
| 873 | without: function() { | ||
| 874 | var values = $A(arguments); | ||
| 875 | return this.select(function(value) { | ||
| 876 | return !values.include(value); | ||
| 877 | }); | ||
| 878 | }, | ||
| 879 | |||
| 880 | reverse: function(inline) { | ||
| 881 | return (inline !== false ? this : this.toArray())._reverse(); | ||
| 882 | }, | ||
| 883 | |||
| 884 | reduce: function() { | ||
| 885 | return this.length > 1 ? this : this[0]; | ||
| 886 | }, | ||
| 887 | |||
| 888 | uniq: function(sorted) { | ||
| 889 | return this.inject([], function(array, value, index) { | ||
| 890 | if (0 == index || (sorted ? array.last() != value : !array.include(value))) | ||
| 891 | array.push(value); | ||
| 892 | return array; | ||
| 893 | }); | ||
| 894 | }, | ||
| 895 | |||
| 896 | intersect: function(array) { | ||
| 897 | return this.uniq().findAll(function(item) { | ||
| 898 | return array.detect(function(value) { return item === value }); | ||
| 899 | }); | ||
| 900 | }, | ||
| 901 | |||
| 902 | clone: function() { | ||
| 903 | return [].concat(this); | ||
| 904 | }, | ||
| 905 | |||
| 906 | size: function() { | ||
| 907 | return this.length; | ||
| 908 | }, | ||
| 909 | |||
| 910 | inspect: function() { | ||
| 911 | return '[' + this.map(Object.inspect).join(', ') + ']'; | ||
| 912 | }, | ||
| 913 | |||
| 914 | toJSON: function() { | ||
| 915 | var results = []; | ||
| 916 | this.each(function(object) { | ||
| 917 | var value = Object.toJSON(object); | ||
| 918 | if (!Object.isUndefined(value)) results.push(value); | ||
| 919 | }); | ||
| 920 | return '[' + results.join(', ') + ']'; | ||
| 921 | } | ||
| 922 | }); | ||
| 923 | |||
| 924 | // use native browser JS 1.6 implementation if available | ||
| 925 | if (Object.isFunction(Array.prototype.forEach)) | ||
| 926 | Array.prototype._each = Array.prototype.forEach; | ||
| 927 | |||
| 928 | if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { | ||
| 929 | i || (i = 0); | ||
| 930 | var length = this.length; | ||
| 931 | if (i < 0) i = length + i; | ||
| 932 | for (; i < length; i++) | ||
| 933 | if (this[i] === item) return i; | ||
| 934 | return -1; | ||
| 935 | }; | ||
| 936 | |||
| 937 | if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { | ||
| 938 | i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; | ||
| 939 | var n = this.slice(0, i).reverse().indexOf(item); | ||
| 940 | return (n < 0) ? n : i - n - 1; | ||
| 941 | }; | ||
| 942 | |||
| 943 | Array.prototype.toArray = Array.prototype.clone; | ||
| 944 | |||
| 945 | function $w(string) { | ||
| 946 | if (!Object.isString(string)) return []; | ||
| 947 | string = string.strip(); | ||
| 948 | return string ? string.split(/\s+/) : []; | ||
| 949 | } | ||
| 950 | |||
| 951 | if (Prototype.Browser.Opera){ | ||
| 952 | Array.prototype.concat = function() { | ||
| 953 | var array = []; | ||
| 954 | for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); | ||
| 955 | for (var i = 0, length = arguments.length; i < length; i++) { | ||
| 956 | if (Object.isArray(arguments[i])) { | ||
| 957 | for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) | ||
| 958 | array.push(arguments[i][j]); | ||
| 959 | } else { | ||
| 960 | array.push(arguments[i]); | ||
| 961 | } | ||
| 962 | } | ||
| 963 | return array; | ||
| 964 | }; | ||
| 965 | } | ||
| 966 | Object.extend(Number.prototype, { | ||
| 967 | toColorPart: function() { | ||
| 968 | return this.toPaddedString(2, 16); | ||
| 969 | }, | ||
| 970 | |||
| 971 | succ: function() { | ||
| 972 | return this + 1; | ||
| 973 | }, | ||
| 974 | |||
| 975 | times: function(iterator, context) { | ||
| 976 | $R(0, this, true).each(iterator, context); | ||
| 977 | return this; | ||
| 978 | }, | ||
| 979 | |||
| 980 | toPaddedString: function(length, radix) { | ||
| 981 | var string = this.toString(radix || 10); | ||
| 982 | return '0'.times(length - string.length) + string; | ||
| 983 | }, | ||
| 984 | |||
| 985 | toJSON: function() { | ||
| 986 | return isFinite(this) ? this.toString() : 'null'; | ||
| 987 | } | ||
| 988 | }); | ||
| 989 | |||
| 990 | $w('abs round ceil floor').each(function(method){ | ||
| 991 | Number.prototype[method] = Math[method].methodize(); | ||
| 992 | }); | ||
| 993 | function $H(object) { | ||
| 994 | return new Hash(object); | ||
| 995 | }; | ||
| 996 | |||
| 997 | var Hash = Class.create(Enumerable, (function() { | ||
| 998 | |||
| 999 | function toQueryPair(key, value) { | ||
| 1000 | if (Object.isUndefined(value)) return key; | ||
| 1001 | return key + '=' + encodeURIComponent(String.interpret(value)); | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | return { | ||
| 1005 | initialize: function(object) { | ||
| 1006 | this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); | ||
| 1007 | }, | ||
| 1008 | |||
| 1009 | _each: function(iterator) { | ||
| 1010 | for (var key in this._object) { | ||
| 1011 | var value = this._object[key], pair = [key, value]; | ||
| 1012 | pair.key = key; | ||
| 1013 | pair.value = value; | ||
| 1014 | iterator(pair); | ||
| 1015 | } | ||
| 1016 | }, | ||
| 1017 | |||
| 1018 | set: function(key, value) { | ||
| 1019 | return this._object[key] = value; | ||
| 1020 | }, | ||
| 1021 | |||
| 1022 | get: function(key) { | ||
| 1023 | // simulating poorly supported hasOwnProperty | ||
| 1024 | if (this._object[key] !== Object.prototype[key]) | ||
| 1025 | return this._object[key]; | ||
| 1026 | }, | ||
| 1027 | |||
| 1028 | unset: function(key) { | ||
| 1029 | var value = this._object[key]; | ||
| 1030 | delete this._object[key]; | ||
| 1031 | return value; | ||
| 1032 | }, | ||
| 1033 | |||
| 1034 | toObject: function() { | ||
| 1035 | return Object.clone(this._object); | ||
| 1036 | }, | ||
| 1037 | |||
| 1038 | keys: function() { | ||
| 1039 | return this.pluck('key'); | ||
| 1040 | }, | ||
| 1041 | |||
| 1042 | values: function() { | ||
| 1043 | return this.pluck('value'); | ||
| 1044 | }, | ||
| 1045 | |||
| 1046 | index: function(value) { | ||
| 1047 | var match = this.detect(function(pair) { | ||
| 1048 | return pair.value === value; | ||
| 1049 | }); | ||
| 1050 | return match && match.key; | ||
| 1051 | }, | ||
| 1052 | |||
| 1053 | merge: function(object) { | ||
| 1054 | return this.clone().update(object); | ||
| 1055 | }, | ||
| 1056 | |||
| 1057 | update: function(object) { | ||
| 1058 | return new Hash(object).inject(this, function(result, pair) { | ||
| 1059 | result.set(pair.key, pair.value); | ||
| 1060 | return result; | ||
| 1061 | }); | ||
| 1062 | }, | ||
| 1063 | |||
| 1064 | toQueryString: function() { | ||
| 1065 | return this.inject([], function(results, pair) { | ||
| 1066 | var key = encodeURIComponent(pair.key), values = pair.value; | ||
| 1067 | |||
| 1068 | if (values && typeof values == 'object') { | ||
| 1069 | if (Object.isArray(values)) | ||
| 1070 | return results.concat(values.map(toQueryPair.curry(key))); | ||
| 1071 | } else results.push(toQueryPair(key, values)); | ||
| 1072 | return results; | ||
| 1073 | }).join('&'); | ||
| 1074 | }, | ||
| 1075 | |||
| 1076 | inspect: function() { | ||
| 1077 | return '#<Hash:{' + this.map(function(pair) { | ||
| 1078 | return pair.map(Object.inspect).join(': '); | ||
| 1079 | }).join(', ') + '}>'; | ||
| 1080 | }, | ||
| 1081 | |||
| 1082 | toJSON: function() { | ||
| 1083 | return Object.toJSON(this.toObject()); | ||
| 1084 | }, | ||
| 1085 | |||
| 1086 | clone: function() { | ||
| 1087 | return new Hash(this); | ||
| 1088 | } | ||
| 1089 | } | ||
| 1090 | })()); | ||
| 1091 | |||
| 1092 | Hash.prototype.toTemplateReplacements = Hash.prototype.toObject; | ||
| 1093 | Hash.from = $H; | ||
| 1094 | var ObjectRange = Class.create(Enumerable, { | ||
| 1095 | initialize: function(start, end, exclusive) { | ||
| 1096 | this.start = start; | ||
| 1097 | this.end = end; | ||
| 1098 | this.exclusive = exclusive; | ||
| 1099 | }, | ||
| 1100 | |||
| 1101 | _each: function(iterator) { | ||
| 1102 | var value = this.start; | ||
| 1103 | while (this.include(value)) { | ||
| 1104 | iterator(value); | ||
| 1105 | value = value.succ(); | ||
| 1106 | } | ||
| 1107 | }, | ||
| 1108 | |||
| 1109 | include: function(value) { | ||
| 1110 | if (value < this.start) | ||
| 1111 | return false; | ||
| 1112 | if (this.exclusive) | ||
| 1113 | return value < this.end; | ||
| 1114 | return value <= this.end; | ||
| 1115 | } | ||
| 1116 | }); | ||
| 1117 | |||
| 1118 | var $R = function(start, end, exclusive) { | ||
| 1119 | return new ObjectRange(start, end, exclusive); | ||
| 1120 | }; | ||
| 1121 | |||
| 1122 | var Ajax = { | ||
| 1123 | getTransport: function() { | ||
| 1124 | return Try.these( | ||
| 1125 | function() {return new XMLHttpRequest()}, | ||
| 1126 | function() {return new ActiveXObject('Msxml2.XMLHTTP')}, | ||
| 1127 | function() {return new ActiveXObject('Microsoft.XMLHTTP')} | ||
| 1128 | ) || false; | ||
| 1129 | }, | ||
| 1130 | |||
| 1131 | activeRequestCount: 0 | ||
| 1132 | }; | ||
| 1133 | |||
| 1134 | Ajax.Responders = { | ||
| 1135 | responders: [], | ||
| 1136 | |||
| 1137 | _each: function(iterator) { | ||
| 1138 | this.responders._each(iterator); | ||
| 1139 | }, | ||
| 1140 | |||
| 1141 | register: function(responder) { | ||
| 1142 | if (!this.include(responder)) | ||
| 1143 | this.responders.push(responder); | ||
| 1144 | }, | ||
| 1145 | |||
| 1146 | unregister: function(responder) { | ||
| 1147 | this.responders = this.responders.without(responder); | ||
| 1148 | }, | ||
| 1149 | |||
| 1150 | dispatch: function(callback, request, transport, json) { | ||
| 1151 | this.each(function(responder) { | ||
| 1152 | if (Object.isFunction(responder[callback])) { | ||
| 1153 | try { | ||
| 1154 | responder[callback].apply(responder, [request, transport, json]); | ||
| 1155 | } catch (e) { } | ||
| 1156 | } | ||
| 1157 | }); | ||
| 1158 | } | ||
| 1159 | }; | ||
| 1160 | |||
| 1161 | Object.extend(Ajax.Responders, Enumerable); | ||
| 1162 | |||
| 1163 | Ajax.Responders.register({ | ||
| 1164 | onCreate: function() { Ajax.activeRequestCount++ }, | ||
| 1165 | onComplete: function() { Ajax.activeRequestCount-- } | ||
| 1166 | }); | ||
| 1167 | |||
| 1168 | Ajax.Base = Class.create({ | ||
| 1169 | initialize: function(options) { | ||
| 1170 | this.options = { | ||
| 1171 | method: 'post', | ||
| 1172 | asynchronous: true, | ||
| 1173 | contentType: 'application/x-www-form-urlencoded', | ||
| 1174 | encoding: 'UTF-8', | ||
| 1175 | parameters: '', | ||
| 1176 | evalJSON: true, | ||
| 1177 | evalJS: true | ||
| 1178 | }; | ||
| 1179 | Object.extend(this.options, options || { }); | ||
| 1180 | |||
| 1181 | this.options.method = this.options.method.toLowerCase(); | ||
| 1182 | |||
| 1183 | if (Object.isString(this.options.parameters)) | ||
| 1184 | this.options.parameters = this.options.parameters.toQueryParams(); | ||
| 1185 | else if (Object.isHash(this.options.parameters)) | ||
| 1186 | this.options.parameters = this.options.parameters.toObject(); | ||
| 1187 | } | ||
| 1188 | }); | ||
| 1189 | |||
| 1190 | Ajax.Request = Class.create(Ajax.Base, { | ||
| 1191 | _complete: false, | ||
| 1192 | |||
| 1193 | initialize: function($super, url, options) { | ||
| 1194 | $super(options); | ||
| 1195 | this.transport = Ajax.getTransport(); | ||
| 1196 | this.request(url); | ||
| 1197 | }, | ||
| 1198 | |||
| 1199 | request: function(url) { | ||
| 1200 | this.url = url; | ||
| 1201 | this.method = this.options.method; | ||
| 1202 | var params = Object.clone(this.options.parameters); | ||
| 1203 | |||
| 1204 | if (!['get', 'post'].include(this.method)) { | ||
| 1205 | // simulate other verbs over post | ||
| 1206 | params['_method'] = this.method; | ||
| 1207 | this.method = 'post'; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | this.parameters = params; | ||
| 1211 | |||
| 1212 | if (params = Object.toQueryString(params)) { | ||
| 1213 | // when GET, append parameters to URL | ||
| 1214 | if (this.method == 'get') | ||
| 1215 | this.url += (this.url.include('?') ? '&' : '?') + params; | ||
| 1216 | else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) | ||
| 1217 | params += '&_='; | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | try { | ||
| 1221 | var response = new Ajax.Response(this); | ||
| 1222 | if (this.options.onCreate) this.options.onCreate(response); | ||
| 1223 | Ajax.Responders.dispatch('onCreate', this, response); | ||
| 1224 | |||
| 1225 | this.transport.open(this.method.toUpperCase(), this.url, | ||
| 1226 | this.options.asynchronous); | ||
| 1227 | |||
| 1228 | if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); | ||
| 1229 | |||
| 1230 | this.transport.onreadystatechange = this.onStateChange.bind(this); | ||
| 1231 | this.setRequestHeaders(); | ||
| 1232 | |||
| 1233 | this.body = this.method == 'post' ? (this.options.postBody || params) : null; | ||
| 1234 | this.transport.send(this.body); | ||
| 1235 | |||
| 1236 | /* Force Firefox to handle ready state 4 for synchronous requests */ | ||
| 1237 | if (!this.options.asynchronous && this.transport.overrideMimeType) | ||
| 1238 | this.onStateChange(); | ||
| 1239 | |||
| 1240 | } | ||
| 1241 | catch (e) { | ||
| 1242 | this.dispatchException(e); | ||
| 1243 | } | ||
| 1244 | }, | ||
| 1245 | |||
| 1246 | onStateChange: function() { | ||
| 1247 | var readyState = this.transport.readyState; | ||
| 1248 | if (readyState > 1 && !((readyState == 4) && this._complete)) | ||
| 1249 | this.respondToReadyState(this.transport.readyState); | ||
| 1250 | }, | ||
| 1251 | |||
| 1252 | setRequestHeaders: function() { | ||
| 1253 | var headers = { | ||
| 1254 | 'X-Requested-With': 'XMLHttpRequest', | ||
| 1255 | 'X-Prototype-Version': Prototype.Version, | ||
| 1256 | 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' | ||
| 1257 | }; | ||
| 1258 | |||
| 1259 | if (this.method == 'post') { | ||
| 1260 | headers['Content-type'] = this.options.contentType + | ||
| 1261 | (this.options.encoding ? '; charset=' + this.options.encoding : ''); | ||
| 1262 | |||
| 1263 | /* Force "Connection: close" for older Mozilla browsers to work | ||
| 1264 | * around a bug where XMLHttpRequest sends an incorrect | ||
| 1265 | * Content-length header. See Mozilla Bugzilla #246651. | ||
| 1266 | */ | ||
| 1267 | if (this.transport.overrideMimeType && | ||
| 1268 | (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) | ||
| 1269 | headers['Connection'] = 'close'; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | // user-defined headers | ||
| 1273 | if (typeof this.options.requestHeaders == 'object') { | ||
| 1274 | var extras = this.options.requestHeaders; | ||
| 1275 | |||
| 1276 | if (Object.isFunction(extras.push)) | ||
| 1277 | for (var i = 0, length = extras.length; i < length; i += 2) | ||
| 1278 | headers[extras[i]] = extras[i+1]; | ||
| 1279 | else | ||
| 1280 | $H(extras).each(function(pair) { headers[pair.key] = pair.value }); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | for (var name in headers) | ||
| 1284 | this.transport.setRequestHeader(name, headers[name]); | ||
| 1285 | }, | ||
| 1286 | |||
| 1287 | success: function() { | ||
| 1288 | var status = this.getStatus(); | ||
| 1289 | return !status || (status >= 200 && status < 300); | ||
| 1290 | }, | ||
| 1291 | |||
| 1292 | getStatus: function() { | ||
| 1293 | try { | ||
| 1294 | return this.transport.status || 0; | ||
| 1295 | } catch (e) { return 0 } | ||
| 1296 | }, | ||
| 1297 | |||
| 1298 | respondToReadyState: function(readyState) { | ||
| 1299 | var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); | ||
| 1300 | |||
| 1301 | if (state == 'Complete') { | ||
| 1302 | try { | ||
| 1303 | this._complete = true; | ||
| 1304 | (this.options['on' + response.status] | ||
| 1305 | || this.options['on' + (this.success() ? 'Success' : 'Failure')] | ||
| 1306 | || Prototype.emptyFunction)(response, response.headerJSON); | ||
| 1307 | } catch (e) { | ||
| 1308 | this.dispatchException(e); | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | var contentType = response.getHeader('Content-type'); | ||
| 1312 | if (this.options.evalJS == 'force' | ||
| 1313 | || (this.options.evalJS && this.isSameOrigin() && contentType | ||
| 1314 | && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) | ||
| 1315 | this.evalResponse(); | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | try { | ||
| 1319 | (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); | ||
| 1320 | Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); | ||
| 1321 | } catch (e) { | ||
| 1322 | this.dispatchException(e); | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | if (state == 'Complete') { | ||
| 1326 | // avoid memory leak in MSIE: clean up | ||
| 1327 | this.transport.onreadystatechange = Prototype.emptyFunction; | ||
| 1328 | } | ||
| 1329 | }, | ||
| 1330 | |||
| 1331 | isSameOrigin: function() { | ||
| 1332 | var m = this.url.match(/^\s*https?:\/\/[^\/]*/); | ||
| 1333 | return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({ | ||
| 1334 | protocol: location.protocol, | ||
| 1335 | domain: document.domain, | ||
| 1336 | port: location.port ? ':' + location.port : '' | ||
| 1337 | })); | ||
| 1338 | }, | ||
| 1339 | |||
| 1340 | getHeader: function(name) { | ||
| 1341 | try { | ||
| 1342 | return this.transport.getResponseHeader(name) || null; | ||
| 1343 | } catch (e) { return null } | ||
| 1344 | }, | ||
| 1345 | |||
| 1346 | evalResponse: function() { | ||
| 1347 | try { | ||
| 1348 | return eval((this.transport.responseText || '').unfilterJSON()); | ||
| 1349 | } catch (e) { | ||
| 1350 | this.dispatchException(e); | ||
| 1351 | } | ||
| 1352 | }, | ||
| 1353 | |||
| 1354 | dispatchException: function(exception) { | ||
| 1355 | (this.options.onException || Prototype.emptyFunction)(this, exception); | ||
| 1356 | Ajax.Responders.dispatch('onException', this, exception); | ||
| 1357 | } | ||
| 1358 | }); | ||
| 1359 | |||
| 1360 | Ajax.Request.Events = | ||
| 1361 | ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; | ||
| 1362 | |||
| 1363 | Ajax.Response = Class.create({ | ||
| 1364 | initialize: function(request){ | ||
| 1365 | this.request = request; | ||
| 1366 | var transport = this.transport = request.transport, | ||
| 1367 | readyState = this.readyState = transport.readyState; | ||
| 1368 | |||
| 1369 | if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) { | ||
| 1370 | this.status = this.getStatus(); | ||
| 1371 | this.statusText = this.getStatusText(); | ||
| 1372 | this.responseText = String.interpret(transport.responseText); | ||
| 1373 | this.headerJSON = this._getHeaderJSON(); | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | if(readyState == 4) { | ||
| 1377 | var xml = transport.responseXML; | ||
| 1378 | this.responseXML = Object.isUndefined(xml) ? null : xml; | ||
| 1379 | this.responseJSON = this._getResponseJSON(); | ||
| 1380 | } | ||
| 1381 | }, | ||
| 1382 | |||
| 1383 | status: 0, | ||
| 1384 | statusText: '', | ||
| 1385 | |||
| 1386 | getStatus: Ajax.Request.prototype.getStatus, | ||
| 1387 | |||
| 1388 | getStatusText: function() { | ||
| 1389 | try { | ||
| 1390 | return this.transport.statusText || ''; | ||
| 1391 | } catch (e) { return '' } | ||
| 1392 | }, | ||
| 1393 | |||
| 1394 | getHeader: Ajax.Request.prototype.getHeader, | ||
| 1395 | |||
| 1396 | getAllHeaders: function() { | ||
| 1397 | try { | ||
| 1398 | return this.getAllResponseHeaders(); | ||
| 1399 | } catch (e) { return null } | ||
| 1400 | }, | ||
| 1401 | |||
| 1402 | getResponseHeader: function(name) { | ||
| 1403 | return this.transport.getResponseHeader(name); | ||
| 1404 | }, | ||
| 1405 | |||
| 1406 | getAllResponseHeaders: function() { | ||
| 1407 | return this.transport.getAllResponseHeaders(); | ||
| 1408 | }, | ||
| 1409 | |||
| 1410 | _getHeaderJSON: function() { | ||
| 1411 | var json = this.getHeader('X-JSON'); | ||
| 1412 | if (!json) return null; | ||
| 1413 | json = decodeURIComponent(escape(json)); | ||
| 1414 | try { | ||
| 1415 | return json.evalJSON(this.request.options.sanitizeJSON || | ||
| 1416 | !this.request.isSameOrigin()); | ||
| 1417 | } catch (e) { | ||
| 1418 | this.request.dispatchException(e); | ||
| 1419 | } | ||
| 1420 | }, | ||
| 1421 | |||
| 1422 | _getResponseJSON: function() { | ||
| 1423 | var options = this.request.options; | ||
| 1424 | if (!options.evalJSON || (options.evalJSON != 'force' && | ||
| 1425 | !(this.getHeader('Content-type') || '').include('application/json')) || | ||
| 1426 | this.responseText.blank()) | ||
| 1427 | return null; | ||
| 1428 | try { | ||
| 1429 | return this.responseText.evalJSON(options.sanitizeJSON || | ||
| 1430 | !this.request.isSameOrigin()); | ||
| 1431 | } catch (e) { | ||
| 1432 | this.request.dispatchException(e); | ||
| 1433 | } | ||
| 1434 | } | ||
| 1435 | }); | ||
| 1436 | |||
| 1437 | Ajax.Updater = Class.create(Ajax.Request, { | ||
| 1438 | initialize: function($super, container, url, options) { | ||
| 1439 | this.container = { | ||
| 1440 | success: (container.success || container), | ||
| 1441 | failure: (container.failure || (container.success ? null : container)) | ||
| 1442 | }; | ||
| 1443 | |||
| 1444 | options = Object.clone(options); | ||
| 1445 | var onComplete = options.onComplete; | ||
| 1446 | options.onComplete = (function(response, json) { | ||
| 1447 | this.updateContent(response.responseText); | ||
| 1448 | if (Object.isFunction(onComplete)) onComplete(response, json); | ||
| 1449 | }).bind(this); | ||
| 1450 | |||
| 1451 | $super(url, options); | ||
| 1452 | }, | ||
| 1453 | |||
| 1454 | updateContent: function(responseText) { | ||
| 1455 | var receiver = this.container[this.success() ? 'success' : 'failure'], | ||
| 1456 | options = this.options; | ||
| 1457 | |||
| 1458 | if (!options.evalScripts) responseText = responseText.stripScripts(); | ||
| 1459 | |||
| 1460 | if (receiver = $(receiver)) { | ||
| 1461 | if (options.insertion) { | ||
| 1462 | if (Object.isString(options.insertion)) { | ||
| 1463 | var insertion = { }; insertion[options.insertion] = responseText; | ||
| 1464 | receiver.insert(insertion); | ||
| 1465 | } | ||
| 1466 | else options.insertion(receiver, responseText); | ||
| 1467 | } | ||
| 1468 | else receiver.update(responseText); | ||
| 1469 | } | ||
| 1470 | } | ||
| 1471 | }); | ||
| 1472 | |||
| 1473 | Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { | ||
| 1474 | initialize: function($super, container, url, options) { | ||
| 1475 | $super(options); | ||
| 1476 | this.onComplete = this.options.onComplete; | ||
| 1477 | |||
| 1478 | this.frequency = (this.options.frequency || 2); | ||
| 1479 | this.decay = (this.options.decay || 1); | ||
| 1480 | |||
| 1481 | this.updater = { }; | ||
| 1482 | this.container = container; | ||
| 1483 | this.url = url; | ||
| 1484 | |||
| 1485 | this.start(); | ||
| 1486 | }, | ||
| 1487 | |||
| 1488 | start: function() { | ||
| 1489 | this.options.onComplete = this.updateComplete.bind(this); | ||
| 1490 | this.onTimerEvent(); | ||
| 1491 | }, | ||
| 1492 | |||
| 1493 | stop: function() { | ||
| 1494 | this.updater.options.onComplete = undefined; | ||
| 1495 | clearTimeout(this.timer); | ||
| 1496 | (this.onComplete || Prototype.emptyFunction).apply(this, arguments); | ||
| 1497 | }, | ||
| 1498 | |||
| 1499 | updateComplete: function(response) { | ||
| 1500 | if (this.options.decay) { | ||
| 1501 | this.decay = (response.responseText == this.lastText ? | ||
| 1502 | this.decay * this.options.decay : 1); | ||
| 1503 | |||
| 1504 | this.lastText = response.responseText; | ||
| 1505 | } | ||
| 1506 | this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); | ||
| 1507 | }, | ||
| 1508 | |||
| 1509 | onTimerEvent: function() { | ||
| 1510 | this.updater = new Ajax.Updater(this.container, this.url, this.options); | ||
| 1511 | } | ||
| 1512 | }); | ||
| 1513 | function $(element) { | ||
| 1514 | if (arguments.length > 1) { | ||
| 1515 | for (var i = 0, elements = [], length = arguments.length; i < length; i++) | ||
| 1516 | elements.push($(arguments[i])); | ||
| 1517 | return elements; | ||
| 1518 | } | ||
| 1519 | if (Object.isString(element)) | ||
| 1520 | element = document.getElementById(element); | ||
| 1521 | return Element.extend(element); | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | if (Prototype.BrowserFeatures.XPath) { | ||
| 1525 | document._getElementsByXPath = function(expression, parentElement) { | ||
| 1526 | var results = []; | ||
| 1527 | var query = document.evaluate(expression, $(parentElement) || document, | ||
| 1528 | null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | ||
| 1529 | for (var i = 0, length = query.snapshotLength; i < length; i++) | ||
| 1530 | results.push(Element.extend(query.snapshotItem(i))); | ||
| 1531 | return results; | ||
| 1532 | }; | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | /*--------------------------------------------------------------------------*/ | ||
| 1536 | |||
| 1537 | if (!window.Node) var Node = { }; | ||
| 1538 | |||
| 1539 | if (!Node.ELEMENT_NODE) { | ||
| 1540 | // DOM level 2 ECMAScript Language Binding | ||
| 1541 | Object.extend(Node, { | ||
| 1542 | ELEMENT_NODE: 1, | ||
| 1543 | ATTRIBUTE_NODE: 2, | ||
| 1544 | TEXT_NODE: 3, | ||
| 1545 | CDATA_SECTION_NODE: 4, | ||
| 1546 | ENTITY_REFERENCE_NODE: 5, | ||
| 1547 | ENTITY_NODE: 6, | ||
| 1548 | PROCESSING_INSTRUCTION_NODE: 7, | ||
| 1549 | COMMENT_NODE: 8, | ||
| 1550 | DOCUMENT_NODE: 9, | ||
| 1551 | DOCUMENT_TYPE_NODE: 10, | ||
| 1552 | DOCUMENT_FRAGMENT_NODE: 11, | ||
| 1553 | NOTATION_NODE: 12 | ||
| 1554 | }); | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | (function() { | ||
| 1558 | var element = this.Element; | ||
| 1559 | this.Element = function(tagName, attributes) { | ||
| 1560 | attributes = attributes || { }; | ||
| 1561 | tagName = tagName.toLowerCase(); | ||
| 1562 | var cache = Element.cache; | ||
| 1563 | if (Prototype.Browser.IE && attributes.name) { | ||
| 1564 | tagName = '<' + tagName + ' name="' + attributes.name + '">'; | ||
| 1565 | delete attributes.name; | ||
| 1566 | return Element.writeAttribute(document.createElement(tagName), attributes); | ||
| 1567 | } | ||
| 1568 | if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName)); | ||
| 1569 | return Element.writeAttribute(cache[tagName].cloneNode(false), attributes); | ||
| 1570 | }; | ||
| 1571 | Object.extend(this.Element, element || { }); | ||
| 1572 | if (element) this.Element.prototype = element.prototype; | ||
| 1573 | }).call(window); | ||
| 1574 | |||
| 1575 | Element.cache = { }; | ||
| 1576 | |||
| 1577 | Element.Methods = { | ||
| 1578 | visible: function(element) { | ||
| 1579 | return $(element).style.display != 'none'; | ||
| 1580 | }, | ||
| 1581 | |||
| 1582 | toggle: function(element) { | ||
| 1583 | element = $(element); | ||
| 1584 | Element[Element.visible(element) ? 'hide' : 'show'](element); | ||
| 1585 | return element; | ||
| 1586 | }, | ||
| 1587 | |||
| 1588 | hide: function(element) { | ||
| 1589 | element = $(element); | ||
| 1590 | element.style.display = 'none'; | ||
| 1591 | return element; | ||
| 1592 | }, | ||
| 1593 | |||
| 1594 | show: function(element) { | ||
| 1595 | element = $(element); | ||
| 1596 | element.style.display = ''; | ||
| 1597 | return element; | ||
| 1598 | }, | ||
| 1599 | |||
| 1600 | remove: function(element) { | ||
| 1601 | element = $(element); | ||
| 1602 | element.parentNode.removeChild(element); | ||
| 1603 | return element; | ||
| 1604 | }, | ||
| 1605 | |||
| 1606 | update: function(element, content) { | ||
| 1607 | element = $(element); | ||
| 1608 | if (content && content.toElement) content = content.toElement(); | ||
| 1609 | if (Object.isElement(content)) return element.update().insert(content); | ||
| 1610 | content = Object.toHTML(content); | ||
| 1611 | element.innerHTML = content.stripScripts(); | ||
| 1612 | content.evalScripts.bind(content).defer(); | ||
| 1613 | return element; | ||
| 1614 | }, | ||
| 1615 | |||
| 1616 | replace: function(element, content) { | ||
| 1617 | element = $(element); | ||
| 1618 | if (content && content.toElement) content = content.toElement(); | ||
| 1619 | else if (!Object.isElement(content)) { | ||
| 1620 | content = Object.toHTML(content); | ||
| 1621 | var range = element.ownerDocument.createRange(); | ||
| 1622 | range.selectNode(element); | ||
| 1623 | content.evalScripts.bind(content).defer(); | ||
| 1624 | content = range.createContextualFragment(content.stripScripts()); | ||
| 1625 | } | ||
| 1626 | element.parentNode.replaceChild(content, element); | ||
| 1627 | return element; | ||
| 1628 | }, | ||
| 1629 | |||
| 1630 | insert: function(element, insertions) { | ||
| 1631 | element = $(element); | ||
| 1632 | |||
| 1633 | if (Object.isString(insertions) || Object.isNumber(insertions) || | ||
| 1634 | Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) | ||
| 1635 | insertions = {bottom:insertions}; | ||
| 1636 | |||
| 1637 | var content, insert, tagName, childNodes; | ||
| 1638 | |||
| 1639 | for (var position in insertions) { | ||
| 1640 | content = insertions[position]; | ||
| 1641 | position = position.toLowerCase(); | ||
| 1642 | insert = Element._insertionTranslations[position]; | ||
| 1643 | |||
| 1644 | if (content && content.toElement) content = content.toElement(); | ||
| 1645 | if (Object.isElement(content)) { | ||
| 1646 | insert(element, content); | ||
| 1647 | continue; | ||
| 1648 | } | ||
| 1649 | |||
| 1650 | content = Object.toHTML(content); | ||
| 1651 | |||
| 1652 | tagName = ((position == 'before' || position == 'after') | ||
| 1653 | ? element.parentNode : element).tagName.toUpperCase(); | ||
| 1654 | |||
| 1655 | childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); | ||
| 1656 | |||
| 1657 | if (position == 'top' || position == 'after') childNodes.reverse(); | ||
| 1658 | childNodes.each(insert.curry(element)); | ||
| 1659 | |||
| 1660 | content.evalScripts.bind(content).defer(); | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | return element; | ||
| 1664 | }, | ||
| 1665 | |||
| 1666 | wrap: function(element, wrapper, attributes) { | ||
| 1667 | element = $(element); | ||
| 1668 | if (Object.isElement(wrapper)) | ||
| 1669 | $(wrapper).writeAttribute(attributes || { }); | ||
| 1670 | else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes); | ||
| 1671 | else wrapper = new Element('div', wrapper); | ||
| 1672 | if (element.parentNode) | ||
| 1673 | element.parentNode.replaceChild(wrapper, element); | ||
| 1674 | wrapper.appendChild(element); | ||
| 1675 | return wrapper; | ||
| 1676 | }, | ||
| 1677 | |||
| 1678 | inspect: function(element) { | ||
| 1679 | element = $(element); | ||
| 1680 | var result = '<' + element.tagName.toLowerCase(); | ||
| 1681 | $H({'id': 'id', 'className': 'class'}).each(function(pair) { | ||
| 1682 | var property = pair.first(), attribute = pair.last(); | ||
| 1683 | var value = (element[property] || '').toString(); | ||
| 1684 | if (value) result += ' ' + attribute + '=' + value.inspect(true); | ||
| 1685 | }); | ||
| 1686 | return result + '>'; | ||
| 1687 | }, | ||
| 1688 | |||
| 1689 | recursivelyCollect: function(element, property) { | ||
| 1690 | element = $(element); | ||
| 1691 | var elements = []; | ||
| 1692 | while (element = element[property]) | ||
| 1693 | if (element.nodeType == 1) | ||
| 1694 | elements.push(Element.extend(element)); | ||
| 1695 | return elements; | ||
| 1696 | }, | ||
| 1697 | |||
| 1698 | ancestors: function(element) { | ||
| 1699 | return $(element).recursivelyCollect('parentNode'); | ||
| 1700 | }, | ||
| 1701 | |||
| 1702 | descendants: function(element) { | ||
| 1703 | return $(element).select("*"); | ||
| 1704 | }, | ||
| 1705 | |||
| 1706 | firstDescendant: function(element) { | ||
| 1707 | element = $(element).firstChild; | ||
| 1708 | while (element && element.nodeType != 1) element = element.nextSibling; | ||
| 1709 | return $(element); | ||
| 1710 | }, | ||
| 1711 | |||
| 1712 | immediateDescendants: function(element) { | ||
| 1713 | if (!(element = $(element).firstChild)) return []; | ||
| 1714 | while (element && element.nodeType != 1) element = element.nextSibling; | ||
| 1715 | if (element) return [element].concat($(element).nextSiblings()); | ||
| 1716 | return []; | ||
| 1717 | }, | ||
| 1718 | |||
| 1719 | previousSiblings: function(element) { | ||
| 1720 | return $(element).recursivelyCollect('previousSibling'); | ||
| 1721 | }, | ||
| 1722 | |||
| 1723 | nextSiblings: function(element) { | ||
| 1724 | return $(element).recursivelyCollect('nextSibling'); | ||
| 1725 | }, | ||
| 1726 | |||
| 1727 | siblings: function(element) { | ||
| 1728 | element = $(element); | ||
| 1729 | return element.previousSiblings().reverse().concat(element.nextSiblings()); | ||
| 1730 | }, | ||
| 1731 | |||
| 1732 | match: function(element, selector) { | ||
| 1733 | if (Object.isString(selector)) | ||
| 1734 | selector = new Selector(selector); | ||
| 1735 | return selector.match($(element)); | ||
| 1736 | }, | ||
| 1737 | |||
| 1738 | up: function(element, expression, index) { | ||
| 1739 | element = $(element); | ||
| 1740 | if (arguments.length == 1) return $(element.parentNode); | ||
| 1741 | var ancestors = element.ancestors(); | ||
| 1742 | return Object.isNumber(expression) ? ancestors[expression] : | ||
| 1743 | Selector.findElement(ancestors, expression, index); | ||
| 1744 | }, | ||
| 1745 | |||
| 1746 | down: function(element, expression, index) { | ||
| 1747 | element = $(element); | ||
| 1748 | if (arguments.length == 1) return element.firstDescendant(); | ||
| 1749 | return Object.isNumber(expression) ? element.descendants()[expression] : | ||
| 1750 | Element.select(element, expression)[index || 0]; | ||
| 1751 | }, | ||
| 1752 | |||
| 1753 | previous: function(element, expression, index) { | ||
| 1754 | element = $(element); | ||
| 1755 | if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element)); | ||
| 1756 | var previousSiblings = element.previousSiblings(); | ||
| 1757 | return Object.isNumber(expression) ? previousSiblings[expression] : | ||
| 1758 | Selector.findElement(previousSiblings, expression, index); | ||
| 1759 | }, | ||
| 1760 | |||
| 1761 | next: function(element, expression, index) { | ||
| 1762 | element = $(element); | ||
| 1763 | if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element)); | ||
| 1764 | var nextSiblings = element.nextSiblings(); | ||
| 1765 | return Object.isNumber(expression) ? nextSiblings[expression] : | ||
| 1766 | Selector.findElement(nextSiblings, expression, index); | ||
| 1767 | }, | ||
| 1768 | |||
| 1769 | select: function() { | ||
| 1770 | var args = $A(arguments), element = $(args.shift()); | ||
| 1771 | return Selector.findChildElements(element, args); | ||
| 1772 | }, | ||
| 1773 | |||
| 1774 | adjacent: function() { | ||
| 1775 | var args = $A(arguments), element = $(args.shift()); | ||
| 1776 | return Selector.findChildElements(element.parentNode, args).without(element); | ||
| 1777 | }, | ||
| 1778 | |||
| 1779 | identify: function(element) { | ||
| 1780 | element = $(element); | ||
| 1781 | var id = element.readAttribute('id'), self = arguments.callee; | ||
| 1782 | if (id) return id; | ||
| 1783 | do { id = 'anonymous_element_' + self.counter++ } while ($(id)); | ||
| 1784 | element.writeAttribute('id', id); | ||
| 1785 | return id; | ||
| 1786 | }, | ||
| 1787 | |||
| 1788 | readAttribute: function(element, name) { | ||
| 1789 | element = $(element); | ||
| 1790 | if (Prototype.Browser.IE) { | ||
| 1791 | var t = Element._attributeTranslations.read; | ||
| 1792 | if (t.values[name]) return t.values[name](element, name); | ||
| 1793 | if (t.names[name]) name = t.names[name]; | ||
| 1794 | if (name.include(':')) { | ||
| 1795 | return (!element.attributes || !element.attributes[name]) ? null : | ||
| 1796 | element.attributes[name].value; | ||
| 1797 | } | ||
| 1798 | } | ||
| 1799 | return element.getAttribute(name); | ||
| 1800 | }, | ||
| 1801 | |||
| 1802 | writeAttribute: function(element, name, value) { | ||
| 1803 | element = $(element); | ||
| 1804 | var attributes = { }, t = Element._attributeTranslations.write; | ||
| 1805 | |||
| 1806 | if (typeof name == 'object') attributes = name; | ||
| 1807 | else attributes[name] = Object.isUndefined(value) ? true : value; | ||
| 1808 | |||
| 1809 | for (var attr in attributes) { | ||
| 1810 | name = t.names[attr] || attr; | ||
| 1811 | value = attributes[attr]; | ||
| 1812 | if (t.values[attr]) name = t.values[attr](element, value); | ||
| 1813 | if (value === false || value === null) | ||
| 1814 | element.removeAttribute(name); | ||
| 1815 | else if (value === true) | ||
| 1816 | element.setAttribute(name, name); | ||
| 1817 | else element.setAttribute(name, value); | ||
| 1818 | } | ||
| 1819 | return element; | ||
| 1820 | }, | ||
| 1821 | |||
| 1822 | getHeight: function(element) { | ||
| 1823 | return $(element).getDimensions().height; | ||
| 1824 | }, | ||
| 1825 | |||
| 1826 | getWidth: function(element) { | ||
| 1827 | return $(element).getDimensions().width; | ||
| 1828 | }, | ||
| 1829 | |||
| 1830 | classNames: function(element) { | ||
| 1831 | return new Element.ClassNames(element); | ||
| 1832 | }, | ||
| 1833 | |||
| 1834 | hasClassName: function(element, className) { | ||
| 1835 | if (!(element = $(element))) return; | ||
| 1836 | var elementClassName = element.className; | ||
| 1837 | return (elementClassName.length > 0 && (elementClassName == className || | ||
| 1838 | new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName))); | ||
| 1839 | }, | ||
| 1840 | |||
| 1841 | addClassName: function(element, className) { | ||
| 1842 | if (!(element = $(element))) return; | ||
| 1843 | if (!element.hasClassName(className)) | ||
| 1844 | element.className += (element.className ? ' ' : '') + className; | ||
| 1845 | return element; | ||
| 1846 | }, | ||
| 1847 | |||
| 1848 | removeClassName: function(element, className) { | ||
| 1849 | if (!(element = $(element))) return; | ||
| 1850 | element.className = element.className.replace( | ||
| 1851 | new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip(); | ||
| 1852 | return element; | ||
| 1853 | }, | ||
| 1854 | |||
| 1855 | toggleClassName: function(element, className) { | ||
| 1856 | if (!(element = $(element))) return; | ||
| 1857 | return element[element.hasClassName(className) ? | ||
| 1858 | 'removeClassName' : 'addClassName'](className); | ||
| 1859 | }, | ||
| 1860 | |||
| 1861 | // removes whitespace-only text node children | ||
| 1862 | cleanWhitespace: function(element) { | ||
| 1863 | element = $(element); | ||
| 1864 | var node = element.firstChild; | ||
| 1865 | while (node) { | ||
| 1866 | var nextNode = node.nextSibling; | ||
| 1867 | if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) | ||
| 1868 | element.removeChild(node); | ||
| 1869 | node = nextNode; | ||
| 1870 | } | ||
| 1871 | return element; | ||
| 1872 | }, | ||
| 1873 | |||
| 1874 | empty: function(element) { | ||
| 1875 | return $(element).innerHTML.blank(); | ||
| 1876 | }, | ||
| 1877 | |||
| 1878 | descendantOf: function(element, ancestor) { | ||
| 1879 | element = $(element), ancestor = $(ancestor); | ||
| 1880 | |||
| 1881 | if (element.compareDocumentPosition) | ||
| 1882 | return (element.compareDocumentPosition(ancestor) & 8) === 8; | ||
| 1883 | |||
| 1884 | if (ancestor.contains) | ||
| 1885 | return ancestor.contains(element) && ancestor !== element; | ||
| 1886 | |||
| 1887 | while (element = element.parentNode) | ||
| 1888 | if (element == ancestor) return true; | ||
| 1889 | |||
| 1890 | return false; | ||
| 1891 | }, | ||
| 1892 | |||
| 1893 | scrollTo: function(element) { | ||
| 1894 | element = $(element); | ||
| 1895 | var pos = element.cumulativeOffset(); | ||
| 1896 | window.scrollTo(pos[0], pos[1]); | ||
| 1897 | return element; | ||
| 1898 | }, | ||
| 1899 | |||
| 1900 | getStyle: function(element, style) { | ||
| 1901 | element = $(element); | ||
| 1902 | style = style == 'float' ? 'cssFloat' : style.camelize(); | ||
| 1903 | var value = element.style[style]; | ||
| 1904 | if (!value || value == 'auto') { | ||
| 1905 | var css = document.defaultView.getComputedStyle(element, null); | ||
| 1906 | value = css ? css[style] : null; | ||
| 1907 | } | ||
| 1908 | if (style == 'opacity') return value ? parseFloat(value) : 1.0; | ||
| 1909 | return value == 'auto' ? null : value; | ||
| 1910 | }, | ||
| 1911 | |||
| 1912 | getOpacity: function(element) { | ||
| 1913 | return $(element).getStyle('opacity'); | ||
| 1914 | }, | ||
| 1915 | |||
| 1916 | setStyle: function(element, styles) { | ||
| 1917 | element = $(element); | ||
| 1918 | var elementStyle = element.style, match; | ||
| 1919 | if (Object.isString(styles)) { | ||
| 1920 | element.style.cssText += ';' + styles; | ||
| 1921 | return styles.include('opacity') ? | ||
| 1922 | element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element; | ||
| 1923 | } | ||
| 1924 | for (var property in styles) | ||
| 1925 | if (property == 'opacity') element.setOpacity(styles[property]); | ||
| 1926 | else | ||
| 1927 | elementStyle[(property == 'float' || property == 'cssFloat') ? | ||
| 1928 | (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : | ||
| 1929 | property] = styles[property]; | ||
| 1930 | |||
| 1931 | return element; | ||
| 1932 | }, | ||
| 1933 | |||
| 1934 | setOpacity: function(element, value) { | ||
| 1935 | element = $(element); | ||
| 1936 | element.style.opacity = (value == 1 || value === '') ? '' : | ||
| 1937 | (value < 0.00001) ? 0 : value; | ||
| 1938 | return element; | ||
| 1939 | }, | ||
| 1940 | |||
| 1941 | getDimensions: function(element) { | ||
| 1942 | element = $(element); | ||
| 1943 | var display = element.getStyle('display'); | ||
| 1944 | if (display != 'none' && display != null) // Safari bug | ||
| 1945 | return {width: element.offsetWidth, height: element.offsetHeight}; | ||
| 1946 | |||
| 1947 | // All *Width and *Height properties give 0 on elements with display none, | ||
| 1948 | // so enable the element temporarily | ||
| 1949 | var els = element.style; | ||
| 1950 | var originalVisibility = els.visibility; | ||
| 1951 | var originalPosition = els.position; | ||
| 1952 | var originalDisplay = els.display; | ||
| 1953 | els.visibility = 'hidden'; | ||
| 1954 | els.position = 'absolute'; | ||
| 1955 | els.display = 'block'; | ||
| 1956 | var originalWidth = element.clientWidth; | ||
| 1957 | var originalHeight = element.clientHeight; | ||
| 1958 | els.display = originalDisplay; | ||
| 1959 | els.position = originalPosition; | ||
| 1960 | els.visibility = originalVisibility; | ||
| 1961 | return {width: originalWidth, height: originalHeight}; | ||
| 1962 | }, | ||
| 1963 | |||
| 1964 | makePositioned: function(element) { | ||
| 1965 | element = $(element); | ||
| 1966 | var pos = Element.getStyle(element, 'position'); | ||
| 1967 | if (pos == 'static' || !pos) { | ||
| 1968 | element._madePositioned = true; | ||
| 1969 | element.style.position = 'relative'; | ||
| 1970 | // Opera returns the offset relative to the positioning context, when an | ||
| 1971 | // element is position relative but top and left have not been defined | ||
| 1972 | if (Prototype.Browser.Opera) { | ||
| 1973 | element.style.top = 0; | ||
| 1974 | element.style.left = 0; | ||
| 1975 | } | ||
| 1976 | } | ||
| 1977 | return element; | ||
| 1978 | }, | ||
| 1979 | |||
| 1980 | undoPositioned: function(element) { | ||
| 1981 | element = $(element); | ||
| 1982 | if (element._madePositioned) { | ||
| 1983 | element._madePositioned = undefined; | ||
| 1984 | element.style.position = | ||
| 1985 | element.style.top = | ||
| 1986 | element.style.left = | ||
| 1987 | element.style.bottom = | ||
| 1988 | element.style.right = ''; | ||
| 1989 | } | ||
| 1990 | return element; | ||
| 1991 | }, | ||
| 1992 | |||
| 1993 | makeClipping: function(element) { | ||
| 1994 | element = $(element); | ||
| 1995 | if (element._overflow) return element; | ||
| 1996 | element._overflow = Element.getStyle(element, 'overflow') || 'auto'; | ||
| 1997 | if (element._overflow !== 'hidden') | ||
| 1998 | element.style.overflow = 'hidden'; | ||
| 1999 | return element; | ||
| 2000 | }, | ||
| 2001 | |||
| 2002 | undoClipping: function(element) { | ||
| 2003 | element = $(element); | ||
| 2004 | if (!element._overflow) return element; | ||
| 2005 | element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; | ||
| 2006 | element._overflow = null; | ||
| 2007 | return element; | ||
| 2008 | }, | ||
| 2009 | |||
| 2010 | cumulativeOffset: function(element) { | ||
| 2011 | var valueT = 0, valueL = 0; | ||
| 2012 | do { | ||
| 2013 | valueT += element.offsetTop || 0; | ||
| 2014 | valueL += element.offsetLeft || 0; | ||
| 2015 | element = element.offsetParent; | ||
| 2016 | } while (element); | ||
| 2017 | return Element._returnOffset(valueL, valueT); | ||
| 2018 | }, | ||
| 2019 | |||
| 2020 | positionedOffset: function(element) { | ||
| 2021 | var valueT = 0, valueL = 0; | ||
| 2022 | do { | ||
| 2023 | valueT += element.offsetTop || 0; | ||
| 2024 | valueL += element.offsetLeft || 0; | ||
| 2025 | element = element.offsetParent; | ||
| 2026 | if (element) { | ||
| 2027 | if (element.tagName.toUpperCase() == 'BODY') break; | ||
| 2028 | var p = Element.getStyle(element, 'position'); | ||
| 2029 | if (p !== 'static') break; | ||
| 2030 | } | ||
| 2031 | } while (element); | ||
| 2032 | return Element._returnOffset(valueL, valueT); | ||
| 2033 | }, | ||
| 2034 | |||
| 2035 | absolutize: function(element) { | ||
| 2036 | element = $(element); | ||
| 2037 | if (element.getStyle('position') == 'absolute') return element; | ||
| 2038 | // Position.prepare(); // To be done manually by Scripty when it needs it. | ||
| 2039 | |||
| 2040 | var offsets = element.positionedOffset(); | ||
| 2041 | var top = offsets[1]; | ||
| 2042 | var left = offsets[0]; | ||
| 2043 | var width = element.clientWidth; | ||
| 2044 | var height = element.clientHeight; | ||
| 2045 | |||
| 2046 | element._originalLeft = left - parseFloat(element.style.left || 0); | ||
| 2047 | element._originalTop = top - parseFloat(element.style.top || 0); | ||
| 2048 | element._originalWidth = element.style.width; | ||
| 2049 | element._originalHeight = element.style.height; | ||
| 2050 | |||
| 2051 | element.style.position = 'absolute'; | ||
| 2052 | element.style.top = top + 'px'; | ||
| 2053 | element.style.left = left + 'px'; | ||
| 2054 | element.style.width = width + 'px'; | ||
| 2055 | element.style.height = height + 'px'; | ||
| 2056 | return element; | ||
| 2057 | }, | ||
| 2058 | |||
| 2059 | relativize: function(element) { | ||
| 2060 | element = $(element); | ||
| 2061 | if (element.getStyle('position') == 'relative') return element; | ||
| 2062 | // Position.prepare(); // To be done manually by Scripty when it needs it. | ||
| 2063 | |||
| 2064 | element.style.position = 'relative'; | ||
| 2065 | var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); | ||
| 2066 | var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); | ||
| 2067 | |||
| 2068 | element.style.top = top + 'px'; | ||
| 2069 | element.style.left = left + 'px'; | ||
| 2070 | element.style.height = element._originalHeight; | ||
| 2071 | element.style.width = element._originalWidth; | ||
| 2072 | return element; | ||
| 2073 | }, | ||
| 2074 | |||
| 2075 | cumulativeScrollOffset: function(element) { | ||
| 2076 | var valueT = 0, valueL = 0; | ||
| 2077 | do { | ||
| 2078 | valueT += element.scrollTop || 0; | ||
| 2079 | valueL += element.scrollLeft || 0; | ||
| 2080 | element = element.parentNode; | ||
| 2081 | } while (element); | ||
| 2082 | return Element._returnOffset(valueL, valueT); | ||
| 2083 | }, | ||
| 2084 | |||
| 2085 | getOffsetParent: function(element) { | ||
| 2086 | if (element.offsetParent) return $(element.offsetParent); | ||
| 2087 | if (element == document.body) return $(element); | ||
| 2088 | |||
| 2089 | while ((element = element.parentNode) && element != document.body) | ||
| 2090 | if (Element.getStyle(element, 'position') != 'static') | ||
| 2091 | return $(element); | ||
| 2092 | |||
| 2093 | return $(document.body); | ||
| 2094 | }, | ||
| 2095 | |||
| 2096 | viewportOffset: function(forElement) { | ||
| 2097 | var valueT = 0, valueL = 0; | ||
| 2098 | |||
| 2099 | var element = forElement; | ||
| 2100 | do { | ||
| 2101 | valueT += element.offsetTop || 0; | ||
| 2102 | valueL += element.offsetLeft || 0; | ||
| 2103 | |||
| 2104 | // Safari fix | ||
| 2105 | if (element.offsetParent == document.body && | ||
| 2106 | Element.getStyle(element, 'position') == 'absolute') break; | ||
| 2107 | |||
| 2108 | } while (element = element.offsetParent); | ||
| 2109 | |||
| 2110 | element = forElement; | ||
| 2111 | do { | ||
| 2112 | if (!Prototype.Browser.Opera || (element.tagName && (element.tagName.toUpperCase() == 'BODY'))) { | ||
| 2113 | valueT -= element.scrollTop || 0; | ||
| 2114 | valueL -= element.scrollLeft || 0; | ||
| 2115 | } | ||
| 2116 | } while (element = element.parentNode); | ||
| 2117 | |||
| 2118 | return Element._returnOffset(valueL, valueT); | ||
| 2119 | }, | ||
| 2120 | |||
| 2121 | clonePosition: function(element, source) { | ||
| 2122 | var options = Object.extend({ | ||
| 2123 | setLeft: true, | ||
| 2124 | setTop: true, | ||
| 2125 | setWidth: true, | ||
| 2126 | setHeight: true, | ||
| 2127 | offsetTop: 0, | ||
| 2128 | offsetLeft: 0 | ||
| 2129 | }, arguments[2] || { }); | ||
| 2130 | |||
| 2131 | // find page position of source | ||
| 2132 | source = $(source); | ||
| 2133 | var p = source.viewportOffset(); | ||
| 2134 | |||
| 2135 | // find coordinate system to use | ||
| 2136 | element = $(element); | ||
| 2137 | var delta = [0, 0]; | ||
| 2138 | var parent = null; | ||
| 2139 | // delta [0,0] will do fine with position: fixed elements, | ||
| 2140 | // position:absolute needs offsetParent deltas | ||
| 2141 | if (Element.getStyle(element, 'position') == 'absolute') { | ||
| 2142 | parent = element.getOffsetParent(); | ||
| 2143 | delta = parent.viewportOffset(); | ||
| 2144 | } | ||
| 2145 | |||
| 2146 | // correct by body offsets (fixes Safari) | ||
| 2147 | if (parent == document.body) { | ||
| 2148 | delta[0] -= document.body.offsetLeft; | ||
| 2149 | delta[1] -= document.body.offsetTop; | ||
| 2150 | } | ||
| 2151 | |||
| 2152 | // set position | ||
| 2153 | if (options.setLeft) element.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px'; | ||
| 2154 | if (options.setTop) element.style.top = (p[1] - delta[1] + options.offsetTop) + 'px'; | ||
| 2155 | if (options.setWidth) element.style.width = source.offsetWidth + 'px'; | ||
| 2156 | if (options.setHeight) element.style.height = source.offsetHeight + 'px'; | ||
| 2157 | return element; | ||
| 2158 | } | ||
| 2159 | }; | ||
| 2160 | |||
| 2161 | Element.Methods.identify.counter = 1; | ||
| 2162 | |||
| 2163 | Object.extend(Element.Methods, { | ||
| 2164 | getElementsBySelector: Element.Methods.select, | ||
| 2165 | childElements: Element.Methods.immediateDescendants | ||
| 2166 | }); | ||
| 2167 | |||
| 2168 | Element._attributeTranslations = { | ||
| 2169 | write: { | ||
| 2170 | names: { | ||
| 2171 | className: 'class', | ||
| 2172 | htmlFor: 'for' | ||
| 2173 | }, | ||
| 2174 | values: { } | ||
| 2175 | } | ||
| 2176 | }; | ||
| 2177 | |||
| 2178 | if (Prototype.Browser.Opera) { | ||
| 2179 | Element.Methods.getStyle = Element.Methods.getStyle.wrap( | ||
| 2180 | function(proceed, element, style) { | ||
| 2181 | switch (style) { | ||
| 2182 | case 'left': case 'top': case 'right': case 'bottom': | ||
| 2183 | if (proceed(element, 'position') === 'static') return null; | ||
| 2184 | case 'height': case 'width': | ||
| 2185 | // returns '0px' for hidden elements; we want it to return null | ||
| 2186 | if (!Element.visible(element)) return null; | ||
| 2187 | |||
| 2188 | // returns the border-box dimensions rather than the content-box | ||
| 2189 | // dimensions, so we subtract padding and borders from the value | ||
| 2190 | var dim = parseInt(proceed(element, style), 10); | ||
| 2191 | |||
| 2192 | if (dim !== element['offset' + style.capitalize()]) | ||
| 2193 | return dim + 'px'; | ||
| 2194 | |||
| 2195 | var properties; | ||
| 2196 | if (style === 'height') { | ||
| 2197 | properties = ['border-top-width', 'padding-top', | ||
| 2198 | 'padding-bottom', 'border-bottom-width']; | ||
| 2199 | } | ||
| 2200 | else { | ||
| 2201 | properties = ['border-left-width', 'padding-left', | ||
| 2202 | 'padding-right', 'border-right-width']; | ||
| 2203 | } | ||
| 2204 | return properties.inject(dim, function(memo, property) { | ||
| 2205 | var val = proceed(element, property); | ||
| 2206 | return val === null ? memo : memo - parseInt(val, 10); | ||
| 2207 | }) + 'px'; | ||
| 2208 | default: return proceed(element, style); | ||
| 2209 | } | ||
| 2210 | } | ||
| 2211 | ); | ||
| 2212 | |||
| 2213 | Element.Methods.readAttribute = Element.Methods.readAttribute.wrap( | ||
| 2214 | function(proceed, element, attribute) { | ||
| 2215 | if (attribute === 'title') return element.title; | ||
| 2216 | return proceed(element, attribute); | ||
| 2217 | } | ||
| 2218 | ); | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | else if (Prototype.Browser.IE) { | ||
| 2222 | // IE doesn't report offsets correctly for static elements, so we change them | ||
| 2223 | // to "relative" to get the values, then change them back. | ||
| 2224 | Element.Methods.getOffsetParent = Element.Methods.getOffsetParent.wrap( | ||
| 2225 | function(proceed, element) { | ||
| 2226 | element = $(element); | ||
| 2227 | // IE throws an error if element is not in document | ||
| 2228 | try { element.offsetParent } | ||
| 2229 | catch(e) { return $(document.body) } | ||
| 2230 | var position = element.getStyle('position'); | ||
| 2231 | if (position !== 'static') return proceed(element); | ||
| 2232 | element.setStyle({ position: 'relative' }); | ||
| 2233 | var value = proceed(element); | ||
| 2234 | element.setStyle({ position: position }); | ||
| 2235 | return value; | ||
| 2236 | } | ||
| 2237 | ); | ||
| 2238 | |||
| 2239 | $w('positionedOffset viewportOffset').each(function(method) { | ||
| 2240 | Element.Methods[method] = Element.Methods[method].wrap( | ||
| 2241 | function(proceed, element) { | ||
| 2242 | element = $(element); | ||
| 2243 | try { element.offsetParent } | ||
| 2244 | catch(e) { return Element._returnOffset(0,0) } | ||
| 2245 | var position = element.getStyle('position'); | ||
| 2246 | if (position !== 'static') return proceed(element); | ||
| 2247 | // Trigger hasLayout on the offset parent so that IE6 reports | ||
| 2248 | // accurate offsetTop and offsetLeft values for position: fixed. | ||
| 2249 | var offsetParent = element.getOffsetParent(); | ||
| 2250 | if (offsetParent && offsetParent.getStyle('position') === 'fixed') | ||
| 2251 | offsetParent.setStyle({ zoom: 1 }); | ||
| 2252 | element.setStyle({ position: 'relative' }); | ||
| 2253 | var value = proceed(element); | ||
| 2254 | element.setStyle({ position: position }); | ||
| 2255 | return value; | ||
| 2256 | } | ||
| 2257 | ); | ||
| 2258 | }); | ||
| 2259 | |||
| 2260 | Element.Methods.cumulativeOffset = Element.Methods.cumulativeOffset.wrap( | ||
| 2261 | function(proceed, element) { | ||
| 2262 | try { element.offsetParent } | ||
| 2263 | catch(e) { return Element._returnOffset(0,0) } | ||
| 2264 | return proceed(element); | ||
| 2265 | } | ||
| 2266 | ); | ||
| 2267 | |||
| 2268 | Element.Methods.getStyle = function(element, style) { | ||
| 2269 | element = $(element); | ||
| 2270 | style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); | ||
| 2271 | var value = element.style[style]; | ||
| 2272 | if (!value && element.currentStyle) value = element.currentStyle[style]; | ||
| 2273 | |||
| 2274 | if (style == 'opacity') { | ||
| 2275 | if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/)) | ||
| 2276 | if (value[1]) return parseFloat(value[1]) / 100; | ||
| 2277 | return 1.0; | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | if (value == 'auto') { | ||
| 2281 | if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none')) | ||
| 2282 | return element['offset' + style.capitalize()] + 'px'; | ||
| 2283 | return null; | ||
| 2284 | } | ||
| 2285 | return value; | ||
| 2286 | }; | ||
| 2287 | |||
| 2288 | Element.Methods.setOpacity = function(element, value) { | ||
| 2289 | function stripAlpha(filter){ | ||
| 2290 | return filter.replace(/alpha\([^\)]*\)/gi,''); | ||
| 2291 | } | ||
| 2292 | element = $(element); | ||
| 2293 | var currentStyle = element.currentStyle; | ||
| 2294 | if ((currentStyle && !currentStyle.hasLayout) || | ||
| 2295 | (!currentStyle && element.style.zoom == 'normal')) | ||
| 2296 | element.style.zoom = 1; | ||
| 2297 | |||
| 2298 | var filter = element.getStyle('filter'), style = element.style; | ||
| 2299 | if (value == 1 || value === '') { | ||
| 2300 | (filter = stripAlpha(filter)) ? | ||
| 2301 | style.filter = filter : style.removeAttribute('filter'); | ||
| 2302 | return element; | ||
| 2303 | } else if (value < 0.00001) value = 0; | ||
| 2304 | style.filter = stripAlpha(filter) + | ||
| 2305 | 'alpha(opacity=' + (value * 100) + ')'; | ||
| 2306 | return element; | ||
| 2307 | }; | ||
| 2308 | |||
| 2309 | Element._attributeTranslations = { | ||
| 2310 | read: { | ||
| 2311 | names: { | ||
| 2312 | 'class': 'className', | ||
| 2313 | 'for': 'htmlFor' | ||
| 2314 | }, | ||
| 2315 | values: { | ||
| 2316 | _getAttr: function(element, attribute) { | ||
| 2317 | return element.getAttribute(attribute, 2); | ||
| 2318 | }, | ||
| 2319 | _getAttrNode: function(element, attribute) { | ||
| 2320 | var node = element.getAttributeNode(attribute); | ||
| 2321 | return node ? node.value : ""; | ||
| 2322 | }, | ||
| 2323 | _getEv: function(element, attribute) { | ||
| 2324 | attribute = element.getAttribute(attribute); | ||
| 2325 | return attribute ? attribute.toString().slice(23, -2) : null; | ||
| 2326 | }, | ||
| 2327 | _flag: function(element, attribute) { | ||
| 2328 | return $(element).hasAttribute(attribute) ? attribute : null; | ||
| 2329 | }, | ||
| 2330 | style: function(element) { | ||
| 2331 | return element.style.cssText.toLowerCase(); | ||
| 2332 | }, | ||
| 2333 | title: function(element) { | ||
| 2334 | return element.title; | ||
| 2335 | } | ||
| 2336 | } | ||
| 2337 | } | ||
| 2338 | }; | ||
| 2339 | |||
| 2340 | Element._attributeTranslations.write = { | ||
| 2341 | names: Object.extend({ | ||
| 2342 | cellpadding: 'cellPadding', | ||
| 2343 | cellspacing: 'cellSpacing' | ||
| 2344 | }, Element._attributeTranslations.read.names), | ||
| 2345 | values: { | ||
| 2346 | checked: function(element, value) { | ||
| 2347 | element.checked = !!value; | ||
| 2348 | }, | ||
| 2349 | |||
| 2350 | style: function(element, value) { | ||
| 2351 | element.style.cssText = value ? value : ''; | ||
| 2352 | } | ||
| 2353 | } | ||
| 2354 | }; | ||
| 2355 | |||
| 2356 | Element._attributeTranslations.has = {}; | ||
| 2357 | |||
| 2358 | $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' + | ||
| 2359 | 'encType maxLength readOnly longDesc frameBorder').each(function(attr) { | ||
| 2360 | Element._attributeTranslations.write.names[attr.toLowerCase()] = attr; | ||
| 2361 | Element._attributeTranslations.has[attr.toLowerCase()] = attr; | ||
| 2362 | }); | ||
| 2363 | |||
| 2364 | (function(v) { | ||
| 2365 | Object.extend(v, { | ||
| 2366 | href: v._getAttr, | ||
| 2367 | src: v._getAttr, | ||
| 2368 | type: v._getAttr, | ||
| 2369 | action: v._getAttrNode, | ||
| 2370 | disabled: v._flag, | ||
| 2371 | checked: v._flag, | ||
| 2372 | readonly: v._flag, | ||
| 2373 | multiple: v._flag, | ||
| 2374 | onload: v._getEv, | ||
| 2375 | onunload: v._getEv, | ||
| 2376 | onclick: v._getEv, | ||
| 2377 | ondblclick: v._getEv, | ||
| 2378 | onmousedown: v._getEv, | ||
| 2379 | onmouseup: v._getEv, | ||
| 2380 | onmouseover: v._getEv, | ||
| 2381 | onmousemove: v._getEv, | ||
| 2382 | onmouseout: v._getEv, | ||
| 2383 | onfocus: v._getEv, | ||
| 2384 | onblur: v._getEv, | ||
| 2385 | onkeypress: v._getEv, | ||
| 2386 | onkeydown: v._getEv, | ||
| 2387 | onkeyup: v._getEv, | ||
| 2388 | onsubmit: v._getEv, | ||
| 2389 | onreset: v._getEv, | ||
| 2390 | onselect: v._getEv, | ||
| 2391 | onchange: v._getEv | ||
| 2392 | }); | ||
| 2393 | })(Element._attributeTranslations.read.values); | ||
| 2394 | } | ||
| 2395 | |||
| 2396 | else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) { | ||
| 2397 | Element.Methods.setOpacity = function(element, value) { | ||
| 2398 | element = $(element); | ||
| 2399 | element.style.opacity = (value == 1) ? 0.999999 : | ||
| 2400 | (value === '') ? '' : (value < 0.00001) ? 0 : value; | ||
| 2401 | return element; | ||
| 2402 | }; | ||
| 2403 | } | ||
| 2404 | |||
| 2405 | else if (Prototype.Browser.WebKit) { | ||
| 2406 | Element.Methods.setOpacity = function(element, value) { | ||
| 2407 | element = $(element); | ||
| 2408 | element.style.opacity = (value == 1 || value === '') ? '' : | ||
| 2409 | (value < 0.00001) ? 0 : value; | ||
| 2410 | |||
| 2411 | if (value == 1) | ||
| 2412 | if(element.tagName.toUpperCase() == 'IMG' && element.width) { | ||
| 2413 | element.width++; element.width--; | ||
| 2414 | } else try { | ||
| 2415 | var n = document.createTextNode(' '); | ||
| 2416 | element.appendChild(n); | ||
| 2417 | element.removeChild(n); | ||
| 2418 | } catch (e) { } | ||
| 2419 | |||
| 2420 | return element; | ||
| 2421 | }; | ||
| 2422 | |||
| 2423 | // Safari returns margins on body which is incorrect if the child is absolutely | ||
| 2424 | // positioned. For performance reasons, redefine Element#cumulativeOffset for | ||
| 2425 | // KHTML/WebKit only. | ||
| 2426 | Element.Methods.cumulativeOffset = function(element) { | ||
| 2427 | var valueT = 0, valueL = 0; | ||
| 2428 | do { | ||
| 2429 | valueT += element.offsetTop || 0; | ||
| 2430 | valueL += element.offsetLeft || 0; | ||
| 2431 | if (element.offsetParent == document.body) | ||
| 2432 | if (Element.getStyle(element, 'position') == 'absolute') break; | ||
| 2433 | |||
| 2434 | element = element.offsetParent; | ||
| 2435 | } while (element); | ||
| 2436 | |||
| 2437 | return Element._returnOffset(valueL, valueT); | ||
| 2438 | }; | ||
| 2439 | } | ||
| 2440 | |||
| 2441 | if (Prototype.Browser.IE || Prototype.Browser.Opera) { | ||
| 2442 | // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements | ||
| 2443 | Element.Methods.update = function(element, content) { | ||
| 2444 | element = $(element); | ||
| 2445 | |||
| 2446 | if (content && content.toElement) content = content.toElement(); | ||
| 2447 | if (Object.isElement(content)) return element.update().insert(content); | ||
| 2448 | |||
| 2449 | content = Object.toHTML(content); | ||
| 2450 | var tagName = element.tagName.toUpperCase(); | ||
| 2451 | |||
| 2452 | if (tagName in Element._insertionTranslations.tags) { | ||
| 2453 | $A(element.childNodes).each(function(node) { element.removeChild(node) }); | ||
| 2454 | Element._getContentFromAnonymousElement(tagName, content.stripScripts()) | ||
| 2455 | .each(function(node) { element.appendChild(node) }); | ||
| 2456 | } | ||
| 2457 | else element.innerHTML = content.stripScripts(); | ||
| 2458 | |||
| 2459 | content.evalScripts.bind(content).defer(); | ||
| 2460 | return element; | ||
| 2461 | }; | ||
| 2462 | } | ||
| 2463 | |||
| 2464 | if ('outerHTML' in document.createElement('div')) { | ||
| 2465 | Element.Methods.replace = function(element, content) { | ||
| 2466 | element = $(element); | ||
| 2467 | |||
| 2468 | if (content && content.toElement) content = content.toElement(); | ||
| 2469 | if (Object.isElement(content)) { | ||
| 2470 | element.parentNode.replaceChild(content, element); | ||
| 2471 | return element; | ||
| 2472 | } | ||
| 2473 | |||
| 2474 | content = Object.toHTML(content); | ||
| 2475 | var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); | ||
| 2476 | |||
| 2477 | if (Element._insertionTranslations.tags[tagName]) { | ||
| 2478 | var nextSibling = element.next(); | ||
| 2479 | var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); | ||
| 2480 | parent.removeChild(element); | ||
| 2481 | if (nextSibling) | ||
| 2482 | fragments.each(function(node) { parent.insertBefore(node, nextSibling) }); | ||
| 2483 | else | ||
| 2484 | fragments.each(function(node) { parent.appendChild(node) }); | ||
| 2485 | } | ||
| 2486 | else element.outerHTML = content.stripScripts(); | ||
| 2487 | |||
| 2488 | content.evalScripts.bind(content).defer(); | ||
| 2489 | return element; | ||
| 2490 | }; | ||
| 2491 | } | ||
| 2492 | |||
| 2493 | Element._returnOffset = function(l, t) { | ||
| 2494 | var result = [l, t]; | ||
| 2495 | result.left = l; | ||
| 2496 | result.top = t; | ||
| 2497 | return result; | ||
| 2498 | }; | ||
| 2499 | |||
| 2500 | Element._getContentFromAnonymousElement = function(tagName, html) { | ||
| 2501 | var div = new Element('div'), t = Element._insertionTranslations.tags[tagName]; | ||
| 2502 | if (t) { | ||
| 2503 | div.innerHTML = t[0] + html + t[1]; | ||
| 2504 | t[2].times(function() { div = div.firstChild }); | ||
| 2505 | } else div.innerHTML = html; | ||
| 2506 | return $A(div.childNodes); | ||
| 2507 | }; | ||
| 2508 | |||
| 2509 | Element._insertionTranslations = { | ||
| 2510 | before: function(element, node) { | ||
| 2511 | element.parentNode.insertBefore(node, element); | ||
| 2512 | }, | ||
| 2513 | top: function(element, node) { | ||
| 2514 | element.insertBefore(node, element.firstChild); | ||
| 2515 | }, | ||
| 2516 | bottom: function(element, node) { | ||
| 2517 | element.appendChild(node); | ||
| 2518 | }, | ||
| 2519 | after: function(element, node) { | ||
| 2520 | element.parentNode.insertBefore(node, element.nextSibling); | ||
| 2521 | }, | ||
| 2522 | tags: { | ||
| 2523 | TABLE: ['<table>', '</table>', 1], | ||
| 2524 | TBODY: ['<table><tbody>', '</tbody></table>', 2], | ||
| 2525 | TR: ['<table><tbody><tr>', '</tr></tbody></table>', 3], | ||
| 2526 | TD: ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4], | ||
| 2527 | SELECT: ['<select>', '</select>', 1] | ||
| 2528 | } | ||
| 2529 | }; | ||
| 2530 | |||
| 2531 | (function() { | ||
| 2532 | Object.extend(this.tags, { | ||
| 2533 | THEAD: this.tags.TBODY, | ||
| 2534 | TFOOT: this.tags.TBODY, | ||
| 2535 | TH: this.tags.TD | ||
| 2536 | }); | ||
| 2537 | }).call(Element._insertionTranslations); | ||
| 2538 | |||
| 2539 | Element.Methods.Simulated = { | ||
| 2540 | hasAttribute: function(element, attribute) { | ||
| 2541 | attribute = Element._attributeTranslations.has[attribute] || attribute; | ||
| 2542 | var node = $(element).getAttributeNode(attribute); | ||
| 2543 | return !!(node && node.specified); | ||
| 2544 | } | ||
| 2545 | }; | ||
| 2546 | |||
| 2547 | Element.Methods.ByTag = { }; | ||
| 2548 | |||
| 2549 | Object.extend(Element, Element.Methods); | ||
| 2550 | |||
| 2551 | if (!Prototype.BrowserFeatures.ElementExtensions && | ||
| 2552 | document.createElement('div')['__proto__']) { | ||
| 2553 | window.HTMLElement = { }; | ||
| 2554 | window.HTMLElement.prototype = document.createElement('div')['__proto__']; | ||
| 2555 | Prototype.BrowserFeatures.ElementExtensions = true; | ||
| 2556 | } | ||
| 2557 | |||
| 2558 | Element.extend = (function() { | ||
| 2559 | if (Prototype.BrowserFeatures.SpecificElementExtensions) | ||
| 2560 | return Prototype.K; | ||
| 2561 | |||
| 2562 | var Methods = { }, ByTag = Element.Methods.ByTag; | ||
| 2563 | |||
| 2564 | var extend = Object.extend(function(element) { | ||
| 2565 | if (!element || element._extendedByPrototype || | ||
| 2566 | element.nodeType != 1 || element == window) return element; | ||
| 2567 | |||
| 2568 | var methods = Object.clone(Methods), | ||
| 2569 | tagName = element.tagName.toUpperCase(), property, value; | ||
| 2570 | |||
| 2571 | // extend methods for specific tags | ||
| 2572 | if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]); | ||
| 2573 | |||
| 2574 | for (property in methods) { | ||
| 2575 | value = methods[property]; | ||
| 2576 | if (Object.isFunction(value) && !(property in element)) | ||
| 2577 | element[property] = value.methodize(); | ||
| 2578 | } | ||
| 2579 | |||
| 2580 | element._extendedByPrototype = Prototype.emptyFunction; | ||
| 2581 | return element; | ||
| 2582 | |||
| 2583 | }, { | ||
| 2584 | refresh: function() { | ||
| 2585 | // extend methods for all tags (Safari doesn't need this) | ||
| 2586 | if (!Prototype.BrowserFeatures.ElementExtensions) { | ||
| 2587 | Object.extend(Methods, Element.Methods); | ||
| 2588 | Object.extend(Methods, Element.Methods.Simulated); | ||
| 2589 | } | ||
| 2590 | } | ||
| 2591 | }); | ||
| 2592 | |||
| 2593 | extend.refresh(); | ||
| 2594 | return extend; | ||
| 2595 | })(); | ||
| 2596 | |||
| 2597 | Element.hasAttribute = function(element, attribute) { | ||
| 2598 | if (element.hasAttribute) return element.hasAttribute(attribute); | ||
| 2599 | return Element.Methods.Simulated.hasAttribute(element, attribute); | ||
| 2600 | }; | ||
| 2601 | |||
| 2602 | Element.addMethods = function(methods) { | ||
| 2603 | var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag; | ||
| 2604 | |||
| 2605 | if (!methods) { | ||
| 2606 | Object.extend(Form, Form.Methods); | ||
| 2607 | Object.extend(Form.Element, Form.Element.Methods); | ||
| 2608 | Object.extend(Element.Methods.ByTag, { | ||
| 2609 | "FORM": Object.clone(Form.Methods), | ||
| 2610 | "INPUT": Object.clone(Form.Element.Methods), | ||
| 2611 | "SELECT": Object.clone(Form.Element.Methods), | ||
| 2612 | "TEXTAREA": Object.clone(Form.Element.Methods) | ||
| 2613 | }); | ||
| 2614 | } | ||
| 2615 | |||
| 2616 | if (arguments.length == 2) { | ||
| 2617 | var tagName = methods; | ||
| 2618 | methods = arguments[1]; | ||
| 2619 | } | ||
| 2620 | |||
| 2621 | if (!tagName) Object.extend(Element.Methods, methods || { }); | ||
| 2622 | else { | ||
| 2623 | if (Object.isArray(tagName)) tagName.each(extend); | ||
| 2624 | else extend(tagName); | ||
| 2625 | } | ||
| 2626 | |||
| 2627 | function extend(tagName) { | ||
| 2628 | tagName = tagName.toUpperCase(); | ||
| 2629 | if (!Element.Methods.ByTag[tagName]) | ||
| 2630 | Element.Methods.ByTag[tagName] = { }; | ||
| 2631 | Object.extend(Element.Methods.ByTag[tagName], methods); | ||
| 2632 | } | ||
| 2633 | |||
| 2634 | function copy(methods, destination, onlyIfAbsent) { | ||
| 2635 | onlyIfAbsent = onlyIfAbsent || false; | ||
| 2636 | for (var property in methods) { | ||
| 2637 | var value = methods[property]; | ||
| 2638 | if (!Object.isFunction(value)) continue; | ||
| 2639 | if (!onlyIfAbsent || !(property in destination)) | ||
| 2640 | destination[property] = value.methodize(); | ||
| 2641 | } | ||
| 2642 | } | ||
| 2643 | |||
| 2644 | function findDOMClass(tagName) { | ||
| 2645 | var klass; | ||
| 2646 | var trans = { | ||
| 2647 | "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", | ||
| 2648 | "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", | ||
| 2649 | "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", | ||
| 2650 | "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", | ||
| 2651 | "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": | ||
| 2652 | "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": | ||
| 2653 | "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": | ||
| 2654 | "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": | ||
| 2655 | "FrameSet", "IFRAME": "IFrame" | ||
| 2656 | }; | ||
| 2657 | if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; | ||
| 2658 | if (window[klass]) return window[klass]; | ||
| 2659 | klass = 'HTML' + tagName + 'Element'; | ||
| 2660 | if (window[klass]) return window[klass]; | ||
| 2661 | klass = 'HTML' + tagName.capitalize() + 'Element'; | ||
| 2662 | if (window[klass]) return window[klass]; | ||
| 2663 | |||
| 2664 | window[klass] = { }; | ||
| 2665 | window[klass].prototype = document.createElement(tagName)['__proto__']; | ||
| 2666 | return window[klass]; | ||
| 2667 | } | ||
| 2668 | |||
| 2669 | if (F.ElementExtensions) { | ||
| 2670 | copy(Element.Methods, HTMLElement.prototype); | ||
| 2671 | copy(Element.Methods.Simulated, HTMLElement.prototype, true); | ||
| 2672 | } | ||
| 2673 | |||
| 2674 | if (F.SpecificElementExtensions) { | ||
| 2675 | for (var tag in Element.Methods.ByTag) { | ||
| 2676 | var klass = findDOMClass(tag); | ||
| 2677 | if (Object.isUndefined(klass)) continue; | ||
| 2678 | copy(T[tag], klass.prototype); | ||
| 2679 | } | ||
| 2680 | } | ||
| 2681 | |||
| 2682 | Object.extend(Element, Element.Methods); | ||
| 2683 | delete Element.ByTag; | ||
| 2684 | |||
| 2685 | if (Element.extend.refresh) Element.extend.refresh(); | ||
| 2686 | Element.cache = { }; | ||
| 2687 | }; | ||
| 2688 | |||
| 2689 | document.viewport = { | ||
| 2690 | getDimensions: function() { | ||
| 2691 | var dimensions = { }, B = Prototype.Browser; | ||
| 2692 | $w('width height').each(function(d) { | ||
| 2693 | var D = d.capitalize(); | ||
| 2694 | if (B.WebKit && !document.evaluate) { | ||
| 2695 | // Safari <3.0 needs self.innerWidth/Height | ||
| 2696 | dimensions[d] = self['inner' + D]; | ||
| 2697 | } else if (B.Opera && parseFloat(window.opera.version()) < 9.5) { | ||
| 2698 | // Opera <9.5 needs document.body.clientWidth/Height | ||
| 2699 | dimensions[d] = document.body['client' + D] | ||
| 2700 | } else { | ||
| 2701 | dimensions[d] = document.documentElement['client' + D]; | ||
| 2702 | } | ||
| 2703 | }); | ||
| 2704 | return dimensions; | ||
| 2705 | }, | ||
| 2706 | |||
| 2707 | getWidth: function() { | ||
| 2708 | return this.getDimensions().width; | ||
| 2709 | }, | ||
| 2710 | |||
| 2711 | getHeight: function() { | ||
| 2712 | return this.getDimensions().height; | ||
| 2713 | }, | ||
| 2714 | |||
| 2715 | getScrollOffsets: function() { | ||
| 2716 | return Element._returnOffset( | ||
| 2717 | window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, | ||
| 2718 | window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); | ||
| 2719 | } | ||
| 2720 | }; | ||
| 2721 | /* Portions of the Selector class are derived from Jack Slocum's DomQuery, | ||
| 2722 | * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style | ||
| 2723 | * license. Please see http://www.yui-ext.com/ for more information. */ | ||
| 2724 | |||
| 2725 | var Selector = Class.create({ | ||
| 2726 | initialize: function(expression) { | ||
| 2727 | this.expression = expression.strip(); | ||
| 2728 | |||
| 2729 | if (this.shouldUseSelectorsAPI()) { | ||
| 2730 | this.mode = 'selectorsAPI'; | ||
| 2731 | } else if (this.shouldUseXPath()) { | ||
| 2732 | this.mode = 'xpath'; | ||
| 2733 | this.compileXPathMatcher(); | ||
| 2734 | } else { | ||
| 2735 | this.mode = "normal"; | ||
| 2736 | this.compileMatcher(); | ||
| 2737 | } | ||
| 2738 | |||
| 2739 | }, | ||
| 2740 | |||
| 2741 | shouldUseXPath: function() { | ||
| 2742 | if (!Prototype.BrowserFeatures.XPath) return false; | ||
| 2743 | |||
| 2744 | var e = this.expression; | ||
| 2745 | |||
| 2746 | // Safari 3 chokes on :*-of-type and :empty | ||
| 2747 | if (Prototype.Browser.WebKit && | ||
| 2748 | (e.include("-of-type") || e.include(":empty"))) | ||
| 2749 | return false; | ||
| 2750 | |||
| 2751 | // XPath can't do namespaced attributes, nor can it read | ||
| 2752 | // the "checked" property from DOM nodes | ||
| 2753 | if ((/(\[[\w-]*?:|:checked)/).test(e)) | ||
| 2754 | return false; | ||
| 2755 | |||
| 2756 | return true; | ||
| 2757 | }, | ||
| 2758 | |||
| 2759 | shouldUseSelectorsAPI: function() { | ||
| 2760 | if (!Prototype.BrowserFeatures.SelectorsAPI) return false; | ||
| 2761 | |||
| 2762 | if (!Selector._div) Selector._div = new Element('div'); | ||
| 2763 | |||
| 2764 | // Make sure the browser treats the selector as valid. Test on an | ||
| 2765 | // isolated element to minimize cost of this check. | ||
| 2766 | try { | ||
| 2767 | Selector._div.querySelector(this.expression); | ||
| 2768 | } catch(e) { | ||
| 2769 | return false; | ||
| 2770 | } | ||
| 2771 | |||
| 2772 | return true; | ||
| 2773 | }, | ||
| 2774 | |||
| 2775 | compileMatcher: function() { | ||
| 2776 | var e = this.expression, ps = Selector.patterns, h = Selector.handlers, | ||
| 2777 | c = Selector.criteria, le, p, m; | ||
| 2778 | |||
| 2779 | if (Selector._cache[e]) { | ||
| 2780 | this.matcher = Selector._cache[e]; | ||
| 2781 | return; | ||
| 2782 | } | ||
| 2783 | |||
| 2784 | this.matcher = ["this.matcher = function(root) {", | ||
| 2785 | "var r = root, h = Selector.handlers, c = false, n;"]; | ||
| 2786 | |||
| 2787 | while (e && le != e && (/\S/).test(e)) { | ||
| 2788 | le = e; | ||
| 2789 | for (var i in ps) { | ||
| 2790 | p = ps[i]; | ||
| 2791 | if (m = e.match(p)) { | ||
| 2792 | this.matcher.push(Object.isFunction(c[i]) ? c[i](m) : | ||
| 2793 | new Template(c[i]).evaluate(m)); | ||
| 2794 | e = e.replace(m[0], ''); | ||
| 2795 | break; | ||
| 2796 | } | ||
| 2797 | } | ||
| 2798 | } | ||
| 2799 | |||
| 2800 | this.matcher.push("return h.unique(n);\n}"); | ||
| 2801 | eval(this.matcher.join('\n')); | ||
| 2802 | Selector._cache[this.expression] = this.matcher; | ||
| 2803 | }, | ||
| 2804 | |||
| 2805 | compileXPathMatcher: function() { | ||
| 2806 | var e = this.expression, ps = Selector.patterns, | ||
| 2807 | x = Selector.xpath, le, m; | ||
| 2808 | |||
| 2809 | if (Selector._cache[e]) { | ||
| 2810 | this.xpath = Selector._cache[e]; return; | ||
| 2811 | } | ||
| 2812 | |||
| 2813 | this.matcher = ['.//*']; | ||
| 2814 | while (e && le != e && (/\S/).test(e)) { | ||
| 2815 | le = e; | ||
| 2816 | for (var i in ps) { | ||
| 2817 | if (m = e.match(ps[i])) { | ||
| 2818 | this.matcher.push(Object.isFunction(x[i]) ? x[i](m) : | ||
| 2819 | new Template(x[i]).evaluate(m)); | ||
| 2820 | e = e.replace(m[0], ''); | ||
| 2821 | break; | ||
| 2822 | } | ||
| 2823 | } | ||
| 2824 | } | ||
| 2825 | |||
| 2826 | this.xpath = this.matcher.join(''); | ||
| 2827 | Selector._cache[this.expression] = this.xpath; | ||
| 2828 | }, | ||
| 2829 | |||
| 2830 | findElements: function(root) { | ||
| 2831 | root = root || document; | ||
| 2832 | var e = this.expression, results; | ||
| 2833 | |||
| 2834 | switch (this.mode) { | ||
| 2835 | case 'selectorsAPI': | ||
| 2836 | // querySelectorAll queries document-wide, then filters to descendants | ||
| 2837 | // of the context element. That's not what we want. | ||
| 2838 | // Add an explicit context to the selector if necessary. | ||
| 2839 | if (root !== document) { | ||
| 2840 | var oldId = root.id, id = $(root).identify(); | ||
| 2841 | e = "#" + id + " " + e; | ||
| 2842 | } | ||
| 2843 | |||
| 2844 | results = $A(root.querySelectorAll(e)).map(Element.extend); | ||
| 2845 | root.id = oldId; | ||
| 2846 | |||
| 2847 | return results; | ||
| 2848 | case 'xpath': | ||
| 2849 | return document._getElementsByXPath(this.xpath, root); | ||
| 2850 | default: | ||
| 2851 | return this.matcher(root); | ||
| 2852 | } | ||
| 2853 | }, | ||
| 2854 | |||
| 2855 | match: function(element) { | ||
| 2856 | this.tokens = []; | ||
| 2857 | |||
| 2858 | var e = this.expression, ps = Selector.patterns, as = Selector.assertions; | ||
| 2859 | var le, p, m; | ||
| 2860 | |||
| 2861 | while (e && le !== e && (/\S/).test(e)) { | ||
| 2862 | le = e; | ||
| 2863 | for (var i in ps) { | ||
| 2864 | p = ps[i]; | ||
| 2865 | if (m = e.match(p)) { | ||
| 2866 | // use the Selector.assertions methods unless the selector | ||
| 2867 | // is too complex. | ||
| 2868 | if (as[i]) { | ||
| 2869 | this.tokens.push([i, Object.clone(m)]); | ||
| 2870 | e = e.replace(m[0], ''); | ||
| 2871 | } else { | ||
| 2872 | // reluctantly do a document-wide search | ||
| 2873 | // and look for a match in the array | ||
| 2874 | return this.findElements(document).include(element); | ||
| 2875 | } | ||
| 2876 | } | ||
| 2877 | } | ||
| 2878 | } | ||
| 2879 | |||
| 2880 | var match = true, name, matches; | ||
| 2881 | for (var i = 0, token; token = this.tokens[i]; i++) { | ||
| 2882 | name = token[0], matches = token[1]; | ||
| 2883 | if (!Selector.assertions[name](element, matches)) { | ||
| 2884 | match = false; break; | ||
| 2885 | } | ||
| 2886 | } | ||
| 2887 | |||
| 2888 | return match; | ||
| 2889 | }, | ||
| 2890 | |||
| 2891 | toString: function() { | ||
| 2892 | return this.expression; | ||
| 2893 | }, | ||
| 2894 | |||
| 2895 | inspect: function() { | ||
| 2896 | return "#<Selector:" + this.expression.inspect() + ">"; | ||
| 2897 | } | ||
| 2898 | }); | ||
| 2899 | |||
| 2900 | Object.extend(Selector, { | ||
| 2901 | _cache: { }, | ||
| 2902 | |||
| 2903 | xpath: { | ||
| 2904 | descendant: "//*", | ||
| 2905 | child: "/*", | ||
| 2906 | adjacent: "/following-sibling::*[1]", | ||
| 2907 | laterSibling: '/following-sibling::*', | ||
| 2908 | tagName: function(m) { | ||
| 2909 | if (m[1] == '*') return ''; | ||
| 2910 | return "[local-name()='" + m[1].toLowerCase() + | ||
| 2911 | "' or local-name()='" + m[1].toUpperCase() + "']"; | ||
| 2912 | }, | ||
| 2913 | className: "[contains(concat(' ', @class, ' '), ' #{1} ')]", | ||
| 2914 | id: "[@id='#{1}']", | ||
| 2915 | attrPresence: function(m) { | ||
| 2916 | m[1] = m[1].toLowerCase(); | ||
| 2917 | return new Template("[@#{1}]").evaluate(m); | ||
| 2918 | }, | ||
| 2919 | attr: function(m) { | ||
| 2920 | m[1] = m[1].toLowerCase(); | ||
| 2921 | m[3] = m[5] || m[6]; | ||
| 2922 | return new Template(Selector.xpath.operators[m[2]]).evaluate(m); | ||
| 2923 | }, | ||
| 2924 | pseudo: function(m) { | ||
| 2925 | var h = Selector.xpath.pseudos[m[1]]; | ||
| 2926 | if (!h) return ''; | ||
| 2927 | if (Object.isFunction(h)) return h(m); | ||
| 2928 | return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); | ||
| 2929 | }, | ||
| 2930 | operators: { | ||
| 2931 | '=': "[@#{1}='#{3}']", | ||
| 2932 | '!=': "[@#{1}!='#{3}']", | ||
| 2933 | '^=': "[starts-with(@#{1}, '#{3}')]", | ||
| 2934 | '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']", | ||
| 2935 | '*=': "[contains(@#{1}, '#{3}')]", | ||
| 2936 | '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]", | ||
| 2937 | '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]" | ||
| 2938 | }, | ||
| 2939 | pseudos: { | ||
| 2940 | 'first-child': '[not(preceding-sibling::*)]', | ||
| 2941 | 'last-child': '[not(following-sibling::*)]', | ||
| 2942 | 'only-child': '[not(preceding-sibling::* or following-sibling::*)]', | ||
| 2943 | 'empty': "[count(*) = 0 and (count(text()) = 0)]", | ||
| 2944 | 'checked': "[@checked]", | ||
| 2945 | 'disabled': "[(@disabled) and (@type!='hidden')]", | ||
| 2946 | 'enabled': "[not(@disabled) and (@type!='hidden')]", | ||
| 2947 | 'not': function(m) { | ||
| 2948 | var e = m[6], p = Selector.patterns, | ||
| 2949 | x = Selector.xpath, le, v; | ||
| 2950 | |||
| 2951 | var exclusion = []; | ||
| 2952 | while (e && le != e && (/\S/).test(e)) { | ||
| 2953 | le = e; | ||
| 2954 | for (var i in p) { | ||
| 2955 | if (m = e.match(p[i])) { | ||
| 2956 | v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m); | ||
| 2957 | exclusion.push("(" + v.substring(1, v.length - 1) + ")"); | ||
| 2958 | e = e.replace(m[0], ''); | ||
| 2959 | break; | ||
| 2960 | } | ||
| 2961 | } | ||
| 2962 | } | ||
| 2963 | return "[not(" + exclusion.join(" and ") + ")]"; | ||
| 2964 | }, | ||
| 2965 | 'nth-child': function(m) { | ||
| 2966 | return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m); | ||
| 2967 | }, | ||
| 2968 | 'nth-last-child': function(m) { | ||
| 2969 | return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m); | ||
| 2970 | }, | ||
| 2971 | 'nth-of-type': function(m) { | ||
| 2972 | return Selector.xpath.pseudos.nth("position() ", m); | ||
| 2973 | }, | ||
| 2974 | 'nth-last-of-type': function(m) { | ||
| 2975 | return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m); | ||
| 2976 | }, | ||
| 2977 | 'first-of-type': function(m) { | ||
| 2978 | m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m); | ||
| 2979 | }, | ||
| 2980 | 'last-of-type': function(m) { | ||
| 2981 | m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m); | ||
| 2982 | }, | ||
| 2983 | 'only-of-type': function(m) { | ||
| 2984 | var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); | ||
| 2985 | }, | ||
| 2986 | nth: function(fragment, m) { | ||
| 2987 | var mm, formula = m[6], predicate; | ||
| 2988 | if (formula == 'even') formula = '2n+0'; | ||
| 2989 | if (formula == 'odd') formula = '2n+1'; | ||
| 2990 | if (mm = formula.match(/^(\d+)$/)) // digit only | ||
| 2991 | return '[' + fragment + "= " + mm[1] + ']'; | ||
| 2992 | if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b | ||
| 2993 | if (mm[1] == "-") mm[1] = -1; | ||
| 2994 | var a = mm[1] ? Number(mm[1]) : 1; | ||
| 2995 | var b = mm[2] ? Number(mm[2]) : 0; | ||
| 2996 | predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + | ||
| 2997 | "((#{fragment} - #{b}) div #{a} >= 0)]"; | ||
| 2998 | return new Template(predicate).evaluate({ | ||
| 2999 | fragment: fragment, a: a, b: b }); | ||
| 3000 | } | ||
| 3001 | } | ||
| 3002 | } | ||
| 3003 | }, | ||
| 3004 | |||
| 3005 | criteria: { | ||
| 3006 | tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;', | ||
| 3007 | className: 'n = h.className(n, r, "#{1}", c); c = false;', | ||
| 3008 | id: 'n = h.id(n, r, "#{1}", c); c = false;', | ||
| 3009 | attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;', | ||
| 3010 | attr: function(m) { | ||
| 3011 | m[3] = (m[5] || m[6]); | ||
| 3012 | return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m); | ||
| 3013 | }, | ||
| 3014 | pseudo: function(m) { | ||
| 3015 | if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); | ||
| 3016 | return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); | ||
| 3017 | }, | ||
| 3018 | descendant: 'c = "descendant";', | ||
| 3019 | child: 'c = "child";', | ||
| 3020 | adjacent: 'c = "adjacent";', | ||
| 3021 | laterSibling: 'c = "laterSibling";' | ||
| 3022 | }, | ||
| 3023 | |||
| 3024 | patterns: { | ||
| 3025 | // combinators must be listed first | ||
| 3026 | // (and descendant needs to be last combinator) | ||
| 3027 | laterSibling: /^\s*~\s*/, | ||
| 3028 | child: /^\s*>\s*/, | ||
| 3029 | adjacent: /^\s*\+\s*/, | ||
| 3030 | descendant: /^\s/, | ||
| 3031 | |||
| 3032 | // selectors follow | ||
| 3033 | tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, | ||
| 3034 | id: /^#([\w\-\*]+)(\b|$)/, | ||
| 3035 | className: /^\.([\w\-\*]+)(\b|$)/, | ||
| 3036 | pseudo: | ||
| 3037 | /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/, | ||
| 3038 | attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/, | ||
| 3039 | attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ | ||
| 3040 | }, | ||
| 3041 | |||
| 3042 | // for Selector.match and Element#match | ||
| 3043 | assertions: { | ||
| 3044 | tagName: function(element, matches) { | ||
| 3045 | return matches[1].toUpperCase() == element.tagName.toUpperCase(); | ||
| 3046 | }, | ||
| 3047 | |||
| 3048 | className: function(element, matches) { | ||
| 3049 | return Element.hasClassName(element, matches[1]); | ||
| 3050 | }, | ||
| 3051 | |||
| 3052 | id: function(element, matches) { | ||
| 3053 | return element.id === matches[1]; | ||
| 3054 | }, | ||
| 3055 | |||
| 3056 | attrPresence: function(element, matches) { | ||
| 3057 | return Element.hasAttribute(element, matches[1]); | ||
| 3058 | }, | ||
| 3059 | |||
| 3060 | attr: function(element, matches) { | ||
| 3061 | var nodeValue = Element.readAttribute(element, matches[1]); | ||
| 3062 | return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]); | ||
| 3063 | } | ||
| 3064 | }, | ||
| 3065 | |||
| 3066 | handlers: { | ||
| 3067 | // UTILITY FUNCTIONS | ||
| 3068 | // joins two collections | ||
| 3069 | concat: function(a, b) { | ||
| 3070 | for (var i = 0, node; node = b[i]; i++) | ||
| 3071 | a.push(node); | ||
| 3072 | return a; | ||
| 3073 | }, | ||
| 3074 | |||
| 3075 | // marks an array of nodes for counting | ||
| 3076 | mark: function(nodes) { | ||
| 3077 | var _true = Prototype.emptyFunction; | ||
| 3078 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3079 | node._countedByPrototype = _true; | ||
| 3080 | return nodes; | ||
| 3081 | }, | ||
| 3082 | |||
| 3083 | unmark: function(nodes) { | ||
| 3084 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3085 | node._countedByPrototype = undefined; | ||
| 3086 | return nodes; | ||
| 3087 | }, | ||
| 3088 | |||
| 3089 | // mark each child node with its position (for nth calls) | ||
| 3090 | // "ofType" flag indicates whether we're indexing for nth-of-type | ||
| 3091 | // rather than nth-child | ||
| 3092 | index: function(parentNode, reverse, ofType) { | ||
| 3093 | parentNode._countedByPrototype = Prototype.emptyFunction; | ||
| 3094 | if (reverse) { | ||
| 3095 | for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) { | ||
| 3096 | var node = nodes[i]; | ||
| 3097 | if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; | ||
| 3098 | } | ||
| 3099 | } else { | ||
| 3100 | for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++) | ||
| 3101 | if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; | ||
| 3102 | } | ||
| 3103 | }, | ||
| 3104 | |||
| 3105 | // filters out duplicates and extends all nodes | ||
| 3106 | unique: function(nodes) { | ||
| 3107 | if (nodes.length == 0) return nodes; | ||
| 3108 | var results = [], n; | ||
| 3109 | for (var i = 0, l = nodes.length; i < l; i++) | ||
| 3110 | if (!(n = nodes[i])._countedByPrototype) { | ||
| 3111 | n._countedByPrototype = Prototype.emptyFunction; | ||
| 3112 | results.push(Element.extend(n)); | ||
| 3113 | } | ||
| 3114 | return Selector.handlers.unmark(results); | ||
| 3115 | }, | ||
| 3116 | |||
| 3117 | // COMBINATOR FUNCTIONS | ||
| 3118 | descendant: function(nodes) { | ||
| 3119 | var h = Selector.handlers; | ||
| 3120 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3121 | h.concat(results, node.getElementsByTagName('*')); | ||
| 3122 | return results; | ||
| 3123 | }, | ||
| 3124 | |||
| 3125 | child: function(nodes) { | ||
| 3126 | var h = Selector.handlers; | ||
| 3127 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | ||
| 3128 | for (var j = 0, child; child = node.childNodes[j]; j++) | ||
| 3129 | if (child.nodeType == 1 && child.tagName != '!') results.push(child); | ||
| 3130 | } | ||
| 3131 | return results; | ||
| 3132 | }, | ||
| 3133 | |||
| 3134 | adjacent: function(nodes) { | ||
| 3135 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | ||
| 3136 | var next = this.nextElementSibling(node); | ||
| 3137 | if (next) results.push(next); | ||
| 3138 | } | ||
| 3139 | return results; | ||
| 3140 | }, | ||
| 3141 | |||
| 3142 | laterSibling: function(nodes) { | ||
| 3143 | var h = Selector.handlers; | ||
| 3144 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3145 | h.concat(results, Element.nextSiblings(node)); | ||
| 3146 | return results; | ||
| 3147 | }, | ||
| 3148 | |||
| 3149 | nextElementSibling: function(node) { | ||
| 3150 | while (node = node.nextSibling) | ||
| 3151 | if (node.nodeType == 1) return node; | ||
| 3152 | return null; | ||
| 3153 | }, | ||
| 3154 | |||
| 3155 | previousElementSibling: function(node) { | ||
| 3156 | while (node = node.previousSibling) | ||
| 3157 | if (node.nodeType == 1) return node; | ||
| 3158 | return null; | ||
| 3159 | }, | ||
| 3160 | |||
| 3161 | // TOKEN FUNCTIONS | ||
| 3162 | tagName: function(nodes, root, tagName, combinator) { | ||
| 3163 | var uTagName = tagName.toUpperCase(); | ||
| 3164 | var results = [], h = Selector.handlers; | ||
| 3165 | if (nodes) { | ||
| 3166 | if (combinator) { | ||
| 3167 | // fastlane for ordinary descendant combinators | ||
| 3168 | if (combinator == "descendant") { | ||
| 3169 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3170 | h.concat(results, node.getElementsByTagName(tagName)); | ||
| 3171 | return results; | ||
| 3172 | } else nodes = this[combinator](nodes); | ||
| 3173 | if (tagName == "*") return nodes; | ||
| 3174 | } | ||
| 3175 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3176 | if (node.tagName.toUpperCase() === uTagName) results.push(node); | ||
| 3177 | return results; | ||
| 3178 | } else return root.getElementsByTagName(tagName); | ||
| 3179 | }, | ||
| 3180 | |||
| 3181 | id: function(nodes, root, id, combinator) { | ||
| 3182 | var targetNode = $(id), h = Selector.handlers; | ||
| 3183 | if (!targetNode) return []; | ||
| 3184 | if (!nodes && root == document) return [targetNode]; | ||
| 3185 | if (nodes) { | ||
| 3186 | if (combinator) { | ||
| 3187 | if (combinator == 'child') { | ||
| 3188 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3189 | if (targetNode.parentNode == node) return [targetNode]; | ||
| 3190 | } else if (combinator == 'descendant') { | ||
| 3191 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3192 | if (Element.descendantOf(targetNode, node)) return [targetNode]; | ||
| 3193 | } else if (combinator == 'adjacent') { | ||
| 3194 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3195 | if (Selector.handlers.previousElementSibling(targetNode) == node) | ||
| 3196 | return [targetNode]; | ||
| 3197 | } else nodes = h[combinator](nodes); | ||
| 3198 | } | ||
| 3199 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3200 | if (node == targetNode) return [targetNode]; | ||
| 3201 | return []; | ||
| 3202 | } | ||
| 3203 | return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : []; | ||
| 3204 | }, | ||
| 3205 | |||
| 3206 | className: function(nodes, root, className, combinator) { | ||
| 3207 | if (nodes && combinator) nodes = this[combinator](nodes); | ||
| 3208 | return Selector.handlers.byClassName(nodes, root, className); | ||
| 3209 | }, | ||
| 3210 | |||
| 3211 | byClassName: function(nodes, root, className) { | ||
| 3212 | if (!nodes) nodes = Selector.handlers.descendant([root]); | ||
| 3213 | var needle = ' ' + className + ' '; | ||
| 3214 | for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) { | ||
| 3215 | nodeClassName = node.className; | ||
| 3216 | if (nodeClassName.length == 0) continue; | ||
| 3217 | if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle)) | ||
| 3218 | results.push(node); | ||
| 3219 | } | ||
| 3220 | return results; | ||
| 3221 | }, | ||
| 3222 | |||
| 3223 | attrPresence: function(nodes, root, attr, combinator) { | ||
| 3224 | if (!nodes) nodes = root.getElementsByTagName("*"); | ||
| 3225 | if (nodes && combinator) nodes = this[combinator](nodes); | ||
| 3226 | var results = []; | ||
| 3227 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3228 | if (Element.hasAttribute(node, attr)) results.push(node); | ||
| 3229 | return results; | ||
| 3230 | }, | ||
| 3231 | |||
| 3232 | attr: function(nodes, root, attr, value, operator, combinator) { | ||
| 3233 | if (!nodes) nodes = root.getElementsByTagName("*"); | ||
| 3234 | if (nodes && combinator) nodes = this[combinator](nodes); | ||
| 3235 | var handler = Selector.operators[operator], results = []; | ||
| 3236 | for (var i = 0, node; node = nodes[i]; i++) { | ||
| 3237 | var nodeValue = Element.readAttribute(node, attr); | ||
| 3238 | if (nodeValue === null) continue; | ||
| 3239 | if (handler(nodeValue, value)) results.push(node); | ||
| 3240 | } | ||
| 3241 | return results; | ||
| 3242 | }, | ||
| 3243 | |||
| 3244 | pseudo: function(nodes, name, value, root, combinator) { | ||
| 3245 | if (nodes && combinator) nodes = this[combinator](nodes); | ||
| 3246 | if (!nodes) nodes = root.getElementsByTagName("*"); | ||
| 3247 | return Selector.pseudos[name](nodes, value, root); | ||
| 3248 | } | ||
| 3249 | }, | ||
| 3250 | |||
| 3251 | pseudos: { | ||
| 3252 | 'first-child': function(nodes, value, root) { | ||
| 3253 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | ||
| 3254 | if (Selector.handlers.previousElementSibling(node)) continue; | ||
| 3255 | results.push(node); | ||
| 3256 | } | ||
| 3257 | return results; | ||
| 3258 | }, | ||
| 3259 | 'last-child': function(nodes, value, root) { | ||
| 3260 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | ||
| 3261 | if (Selector.handlers.nextElementSibling(node)) continue; | ||
| 3262 | results.push(node); | ||
| 3263 | } | ||
| 3264 | return results; | ||
| 3265 | }, | ||
| 3266 | 'only-child': function(nodes, value, root) { | ||
| 3267 | var h = Selector.handlers; | ||
| 3268 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3269 | if (!h.previousElementSibling(node) && !h.nextElementSibling(node)) | ||
| 3270 | results.push(node); | ||
| 3271 | return results; | ||
| 3272 | }, | ||
| 3273 | 'nth-child': function(nodes, formula, root) { | ||
| 3274 | return Selector.pseudos.nth(nodes, formula, root); | ||
| 3275 | }, | ||
| 3276 | 'nth-last-child': function(nodes, formula, root) { | ||
| 3277 | return Selector.pseudos.nth(nodes, formula, root, true); | ||
| 3278 | }, | ||
| 3279 | 'nth-of-type': function(nodes, formula, root) { | ||
| 3280 | return Selector.pseudos.nth(nodes, formula, root, false, true); | ||
| 3281 | }, | ||
| 3282 | 'nth-last-of-type': function(nodes, formula, root) { | ||
| 3283 | return Selector.pseudos.nth(nodes, formula, root, true, true); | ||
| 3284 | }, | ||
| 3285 | 'first-of-type': function(nodes, formula, root) { | ||
| 3286 | return Selector.pseudos.nth(nodes, "1", root, false, true); | ||
| 3287 | }, | ||
| 3288 | 'last-of-type': function(nodes, formula, root) { | ||
| 3289 | return Selector.pseudos.nth(nodes, "1", root, true, true); | ||
| 3290 | }, | ||
| 3291 | 'only-of-type': function(nodes, formula, root) { | ||
| 3292 | var p = Selector.pseudos; | ||
| 3293 | return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); | ||
| 3294 | }, | ||
| 3295 | |||
| 3296 | // handles the an+b logic | ||
| 3297 | getIndices: function(a, b, total) { | ||
| 3298 | if (a == 0) return b > 0 ? [b] : []; | ||
| 3299 | return $R(1, total).inject([], function(memo, i) { | ||
| 3300 | if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); | ||
| 3301 | return memo; | ||
| 3302 | }); | ||
| 3303 | }, | ||
| 3304 | |||
| 3305 | // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type | ||
| 3306 | nth: function(nodes, formula, root, reverse, ofType) { | ||
| 3307 | if (nodes.length == 0) return []; | ||
| 3308 | if (formula == 'even') formula = '2n+0'; | ||
| 3309 | if (formula == 'odd') formula = '2n+1'; | ||
| 3310 | var h = Selector.handlers, results = [], indexed = [], m; | ||
| 3311 | h.mark(nodes); | ||
| 3312 | for (var i = 0, node; node = nodes[i]; i++) { | ||
| 3313 | if (!node.parentNode._countedByPrototype) { | ||
| 3314 | h.index(node.parentNode, reverse, ofType); | ||
| 3315 | indexed.push(node.parentNode); | ||
| 3316 | } | ||
| 3317 | } | ||
| 3318 | if (formula.match(/^\d+$/)) { // just a number | ||
| 3319 | formula = Number(formula); | ||
| 3320 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3321 | if (node.nodeIndex == formula) results.push(node); | ||
| 3322 | } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b | ||
| 3323 | if (m[1] == "-") m[1] = -1; | ||
| 3324 | var a = m[1] ? Number(m[1]) : 1; | ||
| 3325 | var b = m[2] ? Number(m[2]) : 0; | ||
| 3326 | var indices = Selector.pseudos.getIndices(a, b, nodes.length); | ||
| 3327 | for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { | ||
| 3328 | for (var j = 0; j < l; j++) | ||
| 3329 | if (node.nodeIndex == indices[j]) results.push(node); | ||
| 3330 | } | ||
| 3331 | } | ||
| 3332 | h.unmark(nodes); | ||
| 3333 | h.unmark(indexed); | ||
| 3334 | return results; | ||
| 3335 | }, | ||
| 3336 | |||
| 3337 | 'empty': function(nodes, value, root) { | ||
| 3338 | for (var i = 0, results = [], node; node = nodes[i]; i++) { | ||
| 3339 | // IE treats comments as element nodes | ||
| 3340 | if (node.tagName == '!' || node.firstChild) continue; | ||
| 3341 | results.push(node); | ||
| 3342 | } | ||
| 3343 | return results; | ||
| 3344 | }, | ||
| 3345 | |||
| 3346 | 'not': function(nodes, selector, root) { | ||
| 3347 | var h = Selector.handlers, selectorType, m; | ||
| 3348 | var exclusions = new Selector(selector).findElements(root); | ||
| 3349 | h.mark(exclusions); | ||
| 3350 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3351 | if (!node._countedByPrototype) results.push(node); | ||
| 3352 | h.unmark(exclusions); | ||
| 3353 | return results; | ||
| 3354 | }, | ||
| 3355 | |||
| 3356 | 'enabled': function(nodes, value, root) { | ||
| 3357 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3358 | if (!node.disabled && (!node.type || node.type !== 'hidden')) | ||
| 3359 | results.push(node); | ||
| 3360 | return results; | ||
| 3361 | }, | ||
| 3362 | |||
| 3363 | 'disabled': function(nodes, value, root) { | ||
| 3364 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3365 | if (node.disabled) results.push(node); | ||
| 3366 | return results; | ||
| 3367 | }, | ||
| 3368 | |||
| 3369 | 'checked': function(nodes, value, root) { | ||
| 3370 | for (var i = 0, results = [], node; node = nodes[i]; i++) | ||
| 3371 | if (node.checked) results.push(node); | ||
| 3372 | return results; | ||
| 3373 | } | ||
| 3374 | }, | ||
| 3375 | |||
| 3376 | operators: { | ||
| 3377 | '=': function(nv, v) { return nv == v; }, | ||
| 3378 | '!=': function(nv, v) { return nv != v; }, | ||
| 3379 | '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); }, | ||
| 3380 | '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); }, | ||
| 3381 | '*=': function(nv, v) { return nv == v || nv && nv.include(v); }, | ||
| 3382 | '$=': function(nv, v) { return nv.endsWith(v); }, | ||
| 3383 | '*=': function(nv, v) { return nv.include(v); }, | ||
| 3384 | '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); }, | ||
| 3385 | '|=': function(nv, v) { return ('-' + (nv || "").toUpperCase() + | ||
| 3386 | '-').include('-' + (v || "").toUpperCase() + '-'); } | ||
| 3387 | }, | ||
| 3388 | |||
| 3389 | split: function(expression) { | ||
| 3390 | var expressions = []; | ||
| 3391 | expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) { | ||
| 3392 | expressions.push(m[1].strip()); | ||
| 3393 | }); | ||
| 3394 | return expressions; | ||
| 3395 | }, | ||
| 3396 | |||
| 3397 | matchElements: function(elements, expression) { | ||
| 3398 | var matches = $$(expression), h = Selector.handlers; | ||
| 3399 | h.mark(matches); | ||
| 3400 | for (var i = 0, results = [], element; element = elements[i]; i++) | ||
| 3401 | if (element._countedByPrototype) results.push(element); | ||
| 3402 | h.unmark(matches); | ||
| 3403 | return results; | ||
| 3404 | }, | ||
| 3405 | |||
| 3406 | findElement: function(elements, expression, index) { | ||
| 3407 | if (Object.isNumber(expression)) { | ||
| 3408 | index = expression; expression = false; | ||
| 3409 | } | ||
| 3410 | return Selector.matchElements(elements, expression || '*')[index || 0]; | ||
| 3411 | }, | ||
| 3412 | |||
| 3413 | findChildElements: function(element, expressions) { | ||
| 3414 | expressions = Selector.split(expressions.join(',')); | ||
| 3415 | var results = [], h = Selector.handlers; | ||
| 3416 | for (var i = 0, l = expressions.length, selector; i < l; i++) { | ||
| 3417 | selector = new Selector(expressions[i].strip()); | ||
| 3418 | h.concat(results, selector.findElements(element)); | ||
| 3419 | } | ||
| 3420 | return (l > 1) ? h.unique(results) : results; | ||
| 3421 | } | ||
| 3422 | }); | ||
| 3423 | |||
| 3424 | if (Prototype.Browser.IE) { | ||
| 3425 | Object.extend(Selector.handlers, { | ||
| 3426 | // IE returns comment nodes on getElementsByTagName("*"). | ||
| 3427 | // Filter them out. | ||
| 3428 | concat: function(a, b) { | ||
| 3429 | for (var i = 0, node; node = b[i]; i++) | ||
| 3430 | if (node.tagName !== "!") a.push(node); | ||
| 3431 | return a; | ||
| 3432 | }, | ||
| 3433 | |||
| 3434 | // IE improperly serializes _countedByPrototype in (inner|outer)HTML. | ||
| 3435 | unmark: function(nodes) { | ||
| 3436 | for (var i = 0, node; node = nodes[i]; i++) | ||
| 3437 | node.removeAttribute('_countedByPrototype'); | ||
| 3438 | return nodes; | ||
| 3439 | } | ||
| 3440 | }); | ||
| 3441 | } | ||
| 3442 | |||
| 3443 | function $$() { | ||
| 3444 | return Selector.findChildElements(document, $A(arguments)); | ||
| 3445 | } | ||
| 3446 | var Form = { | ||
| 3447 | reset: function(form) { | ||
| 3448 | $(form).reset(); | ||
| 3449 | return form; | ||
| 3450 | }, | ||
| 3451 | |||
| 3452 | serializeElements: function(elements, options) { | ||
| 3453 | if (typeof options != 'object') options = { hash: !!options }; | ||
| 3454 | else if (Object.isUndefined(options.hash)) options.hash = true; | ||
| 3455 | var key, value, submitted = false, submit = options.submit; | ||
| 3456 | |||
| 3457 | var data = elements.inject({ }, function(result, element) { | ||
| 3458 | if (!element.disabled && element.name) { | ||
| 3459 | key = element.name; value = $(element).getValue(); | ||
| 3460 | if (value != null && element.type != 'file' && (element.type != 'submit' || (!submitted && | ||
| 3461 | submit !== false && (!submit || key == submit) && (submitted = true)))) { | ||
| 3462 | if (key in result) { | ||
| 3463 | // a key is already present; construct an array of values | ||
| 3464 | if (!Object.isArray(result[key])) result[key] = [result[key]]; | ||
| 3465 | result[key].push(value); | ||
| 3466 | } | ||
| 3467 | else result[key] = value; | ||
| 3468 | } | ||
| 3469 | } | ||
| 3470 | return result; | ||
| 3471 | }); | ||
| 3472 | |||
| 3473 | return options.hash ? data : Object.toQueryString(data); | ||
| 3474 | } | ||
| 3475 | }; | ||
| 3476 | |||
| 3477 | Form.Methods = { | ||
| 3478 | serialize: function(form, options) { | ||
| 3479 | return Form.serializeElements(Form.getElements(form), options); | ||
| 3480 | }, | ||
| 3481 | |||
| 3482 | getElements: function(form) { | ||
| 3483 | return $A($(form).getElementsByTagName('*')).inject([], | ||
| 3484 | function(elements, child) { | ||
| 3485 | if (Form.Element.Serializers[child.tagName.toLowerCase()]) | ||
| 3486 | elements.push(Element.extend(child)); | ||
| 3487 | return elements; | ||
| 3488 | } | ||
| 3489 | ); | ||
| 3490 | }, | ||
| 3491 | |||
| 3492 | getInputs: function(form, typeName, name) { | ||
| 3493 | form = $(form); | ||
| 3494 | var inputs = form.getElementsByTagName('input'); | ||
| 3495 | |||
| 3496 | if (!typeName && !name) return $A(inputs).map(Element.extend); | ||
| 3497 | |||
| 3498 | for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { | ||
| 3499 | var input = inputs[i]; | ||
| 3500 | if ((typeName && input.type != typeName) || (name && input.name != name)) | ||
| 3501 | continue; | ||
| 3502 | matchingInputs.push(Element.extend(input)); | ||
| 3503 | } | ||
| 3504 | |||
| 3505 | return matchingInputs; | ||
| 3506 | }, | ||
| 3507 | |||
| 3508 | disable: function(form) { | ||
| 3509 | form = $(form); | ||
| 3510 | Form.getElements(form).invoke('disable'); | ||
| 3511 | return form; | ||
| 3512 | }, | ||
| 3513 | |||
| 3514 | enable: function(form) { | ||
| 3515 | form = $(form); | ||
| 3516 | Form.getElements(form).invoke('enable'); | ||
| 3517 | return form; | ||
| 3518 | }, | ||
| 3519 | |||
| 3520 | findFirstElement: function(form) { | ||
| 3521 | var elements = $(form).getElements().findAll(function(element) { | ||
| 3522 | return 'hidden' != element.type && !element.disabled; | ||
| 3523 | }); | ||
| 3524 | var firstByIndex = elements.findAll(function(element) { | ||
| 3525 | return element.hasAttribute('tabIndex') && element.tabIndex >= 0; | ||
| 3526 | }).sortBy(function(element) { return element.tabIndex }).first(); | ||
| 3527 | |||
| 3528 | return firstByIndex ? firstByIndex : elements.find(function(element) { | ||
| 3529 | return ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); | ||
| 3530 | }); | ||
| 3531 | }, | ||
| 3532 | |||
| 3533 | focusFirstElement: function(form) { | ||
| 3534 | form = $(form); | ||
| 3535 | form.findFirstElement().activate(); | ||
| 3536 | return form; | ||
| 3537 | }, | ||
| 3538 | |||
| 3539 | request: function(form, options) { | ||
| 3540 | form = $(form), options = Object.clone(options || { }); | ||
| 3541 | |||
| 3542 | var params = options.parameters, action = form.readAttribute('action') || ''; | ||
| 3543 | if (action.blank()) action = window.location.href; | ||
| 3544 | options.parameters = form.serialize(true); | ||
| 3545 | |||
| 3546 | if (params) { | ||
| 3547 | if (Object.isString(params)) params = params.toQueryParams(); | ||
| 3548 | Object.extend(options.parameters, params); | ||
| 3549 | } | ||
| 3550 | |||
| 3551 | if (form.hasAttribute('method') && !options.method) | ||
| 3552 | options.method = form.method; | ||
| 3553 | |||
| 3554 | return new Ajax.Request(action, options); | ||
| 3555 | } | ||
| 3556 | }; | ||
| 3557 | |||
| 3558 | /*--------------------------------------------------------------------------*/ | ||
| 3559 | |||
| 3560 | Form.Element = { | ||
| 3561 | focus: function(element) { | ||
| 3562 | $(element).focus(); | ||
| 3563 | return element; | ||
| 3564 | }, | ||
| 3565 | |||
| 3566 | select: function(element) { | ||
| 3567 | $(element).select(); | ||
| 3568 | return element; | ||
| 3569 | } | ||
| 3570 | }; | ||
| 3571 | |||
| 3572 | Form.Element.Methods = { | ||
| 3573 | serialize: function(element) { | ||
| 3574 | element = $(element); | ||
| 3575 | if (!element.disabled && element.name) { | ||
| 3576 | var value = element.getValue(); | ||
| 3577 | if (value != undefined) { | ||
| 3578 | var pair = { }; | ||
| 3579 | pair[element.name] = value; | ||
| 3580 | return Object.toQueryString(pair); | ||
| 3581 | } | ||
| 3582 | } | ||
| 3583 | return ''; | ||
| 3584 | }, | ||
| 3585 | |||
| 3586 | getValue: function(element) { | ||
| 3587 | element = $(element); | ||
| 3588 | var method = element.tagName.toLowerCase(); | ||
| 3589 | return Form.Element.Serializers[method](element); | ||
| 3590 | }, | ||
| 3591 | |||
| 3592 | setValue: function(element, value) { | ||
| 3593 | element = $(element); | ||
| 3594 | var method = element.tagName.toLowerCase(); | ||
| 3595 | Form.Element.Serializers[method](element, value); | ||
| 3596 | return element; | ||
| 3597 | }, | ||
| 3598 | |||
| 3599 | clear: function(element) { | ||
| 3600 | $(element).value = ''; | ||
| 3601 | return element; | ||
| 3602 | }, | ||
| 3603 | |||
| 3604 | present: function(element) { | ||
| 3605 | return $(element).value != ''; | ||
| 3606 | }, | ||
| 3607 | |||
| 3608 | activate: function(element) { | ||
| 3609 | element = $(element); | ||
| 3610 | try { | ||
| 3611 | element.focus(); | ||
| 3612 | if (element.select && (element.tagName.toLowerCase() != 'input' || | ||
| 3613 | !['button', 'reset', 'submit'].include(element.type))) | ||
| 3614 | element.select(); | ||
| 3615 | } catch (e) { } | ||
| 3616 | return element; | ||
| 3617 | }, | ||
| 3618 | |||
| 3619 | disable: function(element) { | ||
| 3620 | element = $(element); | ||
| 3621 | element.disabled = true; | ||
| 3622 | return element; | ||
| 3623 | }, | ||
| 3624 | |||
| 3625 | enable: function(element) { | ||
| 3626 | element = $(element); | ||
| 3627 | element.disabled = false; | ||
| 3628 | return element; | ||
| 3629 | } | ||
| 3630 | }; | ||
| 3631 | |||
| 3632 | /*--------------------------------------------------------------------------*/ | ||
| 3633 | |||
| 3634 | var Field = Form.Element; | ||
| 3635 | var $F = Form.Element.Methods.getValue; | ||
| 3636 | |||
| 3637 | /*--------------------------------------------------------------------------*/ | ||
| 3638 | |||
| 3639 | Form.Element.Serializers = { | ||
| 3640 | input: function(element, value) { | ||
| 3641 | switch (element.type.toLowerCase()) { | ||
| 3642 | case 'checkbox': | ||
| 3643 | case 'radio': | ||
| 3644 | return Form.Element.Serializers.inputSelector(element, value); | ||
| 3645 | default: | ||
| 3646 | return Form.Element.Serializers.textarea(element, value); | ||
| 3647 | } | ||
| 3648 | }, | ||
| 3649 | |||
| 3650 | inputSelector: function(element, value) { | ||
| 3651 | if (Object.isUndefined(value)) return element.checked ? element.value : null; | ||
| 3652 | else element.checked = !!value; | ||
| 3653 | }, | ||
| 3654 | |||
| 3655 | textarea: function(element, value) { | ||
| 3656 | if (Object.isUndefined(value)) return element.value; | ||
| 3657 | else element.value = value; | ||
| 3658 | }, | ||
| 3659 | |||
| 3660 | select: function(element, value) { | ||
| 3661 | if (Object.isUndefined(value)) | ||
| 3662 | return this[element.type == 'select-one' ? | ||
| 3663 | 'selectOne' : 'selectMany'](element); | ||
| 3664 | else { | ||
| 3665 | var opt, currentValue, single = !Object.isArray(value); | ||
| 3666 | for (var i = 0, length = element.length; i < length; i++) { | ||
| 3667 | opt = element.options[i]; | ||
| 3668 | currentValue = this.optionValue(opt); | ||
| 3669 | if (single) { | ||
| 3670 | if (currentValue == value) { | ||
| 3671 | opt.selected = true; | ||
| 3672 | return; | ||
| 3673 | } | ||
| 3674 | } | ||
| 3675 | else opt.selected = value.include(currentValue); | ||
| 3676 | } | ||
| 3677 | } | ||
| 3678 | }, | ||
| 3679 | |||
| 3680 | selectOne: function(element) { | ||
| 3681 | var index = element.selectedIndex; | ||
| 3682 | return index >= 0 ? this.optionValue(element.options[index]) : null; | ||
| 3683 | }, | ||
| 3684 | |||
| 3685 | selectMany: function(element) { | ||
| 3686 | var values, length = element.length; | ||
| 3687 | if (!length) return null; | ||
| 3688 | |||
| 3689 | for (var i = 0, values = []; i < length; i++) { | ||
| 3690 | var opt = element.options[i]; | ||
| 3691 | if (opt.selected) values.push(this.optionValue(opt)); | ||
| 3692 | } | ||
| 3693 | return values; | ||
| 3694 | }, | ||
| 3695 | |||
| 3696 | optionValue: function(opt) { | ||
| 3697 | // extend element because hasAttribute may not be native | ||
| 3698 | return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text; | ||
| 3699 | } | ||
| 3700 | }; | ||
| 3701 | |||
| 3702 | /*--------------------------------------------------------------------------*/ | ||
| 3703 | |||
| 3704 | Abstract.TimedObserver = Class.create(PeriodicalExecuter, { | ||
| 3705 | initialize: function($super, element, frequency, callback) { | ||
| 3706 | $super(callback, frequency); | ||
| 3707 | this.element = $(element); | ||
| 3708 | this.lastValue = this.getValue(); | ||
| 3709 | }, | ||
| 3710 | |||
| 3711 | execute: function() { | ||
| 3712 | var value = this.getValue(); | ||
| 3713 | if (Object.isString(this.lastValue) && Object.isString(value) ? | ||
| 3714 | this.lastValue != value : String(this.lastValue) != String(value)) { | ||
| 3715 | this.callback(this.element, value); | ||
| 3716 | this.lastValue = value; | ||
| 3717 | } | ||
| 3718 | } | ||
| 3719 | }); | ||
| 3720 | |||
| 3721 | Form.Element.Observer = Class.create(Abstract.TimedObserver, { | ||
| 3722 | getValue: function() { | ||
| 3723 | return Form.Element.getValue(this.element); | ||
| 3724 | } | ||
| 3725 | }); | ||
| 3726 | |||
| 3727 | Form.Observer = Class.create(Abstract.TimedObserver, { | ||
| 3728 | getValue: function() { | ||
| 3729 | return Form.serialize(this.element); | ||
| 3730 | } | ||
| 3731 | }); | ||
| 3732 | |||
| 3733 | /*--------------------------------------------------------------------------*/ | ||
| 3734 | |||
| 3735 | Abstract.EventObserver = Class.create({ | ||
| 3736 | initialize: function(element, callback) { | ||
| 3737 | this.element = $(element); | ||
| 3738 | this.callback = callback; | ||
| 3739 | |||
| 3740 | this.lastValue = this.getValue(); | ||
| 3741 | if (this.element.tagName.toLowerCase() == 'form') | ||
| 3742 | this.registerFormCallbacks(); | ||
| 3743 | else | ||
| 3744 | this.registerCallback(this.element); | ||
| 3745 | }, | ||
| 3746 | |||
| 3747 | onElementEvent: function() { | ||
| 3748 | var value = this.getValue(); | ||
| 3749 | if (this.lastValue != value) { | ||
| 3750 | this.callback(this.element, value); | ||
| 3751 | this.lastValue = value; | ||
| 3752 | } | ||
| 3753 | }, | ||
| 3754 | |||
| 3755 | registerFormCallbacks: function() { | ||
| 3756 | Form.getElements(this.element).each(this.registerCallback, this); | ||
| 3757 | }, | ||
| 3758 | |||
| 3759 | registerCallback: function(element) { | ||
| 3760 | if (element.type) { | ||
| 3761 | switch (element.type.toLowerCase()) { | ||
| 3762 | case 'checkbox': | ||
| 3763 | case 'radio': | ||
| 3764 | Event.observe(element, 'click', this.onElementEvent.bind(this)); | ||
| 3765 | break; | ||
| 3766 | default: | ||
| 3767 | Event.observe(element, 'change', this.onElementEvent.bind(this)); | ||
| 3768 | break; | ||
| 3769 | } | ||
| 3770 | } | ||
| 3771 | } | ||
| 3772 | }); | ||
| 3773 | |||
| 3774 | Form.Element.EventObserver = Class.create(Abstract.EventObserver, { | ||
| 3775 | getValue: function() { | ||
| 3776 | return Form.Element.getValue(this.element); | ||
| 3777 | } | ||
| 3778 | }); | ||
| 3779 | |||
| 3780 | Form.EventObserver = Class.create(Abstract.EventObserver, { | ||
| 3781 | getValue: function() { | ||
| 3782 | return Form.serialize(this.element); | ||
| 3783 | } | ||
| 3784 | }); | ||
| 3785 | if (!window.Event) var Event = { }; | ||
| 3786 | |||
| 3787 | Object.extend(Event, { | ||
| 3788 | KEY_BACKSPACE: 8, | ||
| 3789 | KEY_TAB: 9, | ||
| 3790 | KEY_RETURN: 13, | ||
| 3791 | KEY_ESC: 27, | ||
| 3792 | KEY_LEFT: 37, | ||
| 3793 | KEY_UP: 38, | ||
| 3794 | KEY_RIGHT: 39, | ||
| 3795 | KEY_DOWN: 40, | ||
| 3796 | KEY_DELETE: 46, | ||
| 3797 | KEY_HOME: 36, | ||
| 3798 | KEY_END: 35, | ||
| 3799 | KEY_PAGEUP: 33, | ||
| 3800 | KEY_PAGEDOWN: 34, | ||
| 3801 | KEY_INSERT: 45, | ||
| 3802 | |||
| 3803 | cache: { }, | ||
| 3804 | |||
| 3805 | relatedTarget: function(event) { | ||
| 3806 | var element; | ||
| 3807 | switch(event.type) { | ||
| 3808 | case 'mouseover': element = event.fromElement; break; | ||
| 3809 | case 'mouseout': element = event.toElement; break; | ||
| 3810 | default: return null; | ||
| 3811 | } | ||
| 3812 | return Element.extend(element); | ||
| 3813 | } | ||
| 3814 | }); | ||
| 3815 | |||
| 3816 | Event.Methods = (function() { | ||
| 3817 | var isButton; | ||
| 3818 | |||
| 3819 | if (Prototype.Browser.IE) { | ||
| 3820 | var buttonMap = { 0: 1, 1: 4, 2: 2 }; | ||
| 3821 | isButton = function(event, code) { | ||
| 3822 | return event.button == buttonMap[code]; | ||
| 3823 | }; | ||
| 3824 | |||
| 3825 | } else if (Prototype.Browser.WebKit) { | ||
| 3826 | isButton = function(event, code) { | ||
| 3827 | switch (code) { | ||
| 3828 | case 0: return event.which == 1 && !event.metaKey; | ||
| 3829 | case 1: return event.which == 1 && event.metaKey; | ||
| 3830 | default: return false; | ||
| 3831 | } | ||
| 3832 | }; | ||
| 3833 | |||
| 3834 | } else { | ||
| 3835 | isButton = function(event, code) { | ||
| 3836 | return event.which ? (event.which === code + 1) : (event.button === code); | ||
| 3837 | }; | ||
| 3838 | } | ||
| 3839 | |||
| 3840 | return { | ||
| 3841 | isLeftClick: function(event) { return isButton(event, 0) }, | ||
| 3842 | isMiddleClick: function(event) { return isButton(event, 1) }, | ||
| 3843 | isRightClick: function(event) { return isButton(event, 2) }, | ||
| 3844 | |||
| 3845 | element: function(event) { | ||
| 3846 | event = Event.extend(event); | ||
| 3847 | |||
| 3848 | var node = event.target, | ||
| 3849 | type = event.type, | ||
| 3850 | currentTarget = event.currentTarget; | ||
| 3851 | |||
| 3852 | if (currentTarget && currentTarget.tagName) { | ||
| 3853 | // Firefox screws up the "click" event when moving between radio buttons | ||
| 3854 | // via arrow keys. It also screws up the "load" and "error" events on images, | ||
| 3855 | // reporting the document as the target instead of the original image. | ||
| 3856 | if (type === 'load' || type === 'error' || | ||
| 3857 | (type === 'click' && currentTarget.tagName.toLowerCase() === 'input' | ||
| 3858 | && currentTarget.type === 'radio')) | ||
| 3859 | node = currentTarget; | ||
| 3860 | } | ||
| 3861 | if (node.nodeType == Node.TEXT_NODE) node = node.parentNode; | ||
| 3862 | return Element.extend(node); | ||
| 3863 | }, | ||
| 3864 | |||
| 3865 | findElement: function(event, expression) { | ||
| 3866 | var element = Event.element(event); | ||
| 3867 | if (!expression) return element; | ||
| 3868 | var elements = [element].concat(element.ancestors()); | ||
| 3869 | return Selector.findElement(elements, expression, 0); | ||
| 3870 | }, | ||
| 3871 | |||
| 3872 | pointer: function(event) { | ||
| 3873 | var docElement = document.documentElement, | ||
| 3874 | body = document.body || { scrollLeft: 0, scrollTop: 0 }; | ||
| 3875 | return { | ||
| 3876 | x: event.pageX || (event.clientX + | ||
| 3877 | (docElement.scrollLeft || body.scrollLeft) - | ||
| 3878 | (docElement.clientLeft || 0)), | ||
| 3879 | y: event.pageY || (event.clientY + | ||
| 3880 | (docElement.scrollTop || body.scrollTop) - | ||
| 3881 | (docElement.clientTop || 0)) | ||
| 3882 | }; | ||
| 3883 | }, | ||
| 3884 | |||
| 3885 | pointerX: function(event) { return Event.pointer(event).x }, | ||
| 3886 | pointerY: function(event) { return Event.pointer(event).y }, | ||
| 3887 | |||
| 3888 | stop: function(event) { | ||
| 3889 | Event.extend(event); | ||
| 3890 | event.preventDefault(); | ||
| 3891 | event.stopPropagation(); | ||
| 3892 | event.stopped = true; | ||
| 3893 | } | ||
| 3894 | }; | ||
| 3895 | })(); | ||
| 3896 | |||
| 3897 | Event.extend = (function() { | ||
| 3898 | var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { | ||
| 3899 | m[name] = Event.Methods[name].methodize(); | ||
| 3900 | return m; | ||
| 3901 | }); | ||
| 3902 | |||
| 3903 | if (Prototype.Browser.IE) { | ||
| 3904 | Object.extend(methods, { | ||
| 3905 | stopPropagation: function() { this.cancelBubble = true }, | ||
| 3906 | preventDefault: function() { this.returnValue = false }, | ||
| 3907 | inspect: function() { return "[object Event]" } | ||
| 3908 | }); | ||
| 3909 | |||
| 3910 | return function(event) { | ||
| 3911 | if (!event) return false; | ||
| 3912 | if (event._extendedByPrototype) return event; | ||
| 3913 | |||
| 3914 | event._extendedByPrototype = Prototype.emptyFunction; | ||
| 3915 | var pointer = Event.pointer(event); | ||
| 3916 | Object.extend(event, { | ||
| 3917 | target: event.srcElement, | ||
| 3918 | relatedTarget: Event.relatedTarget(event), | ||
| 3919 | pageX: pointer.x, | ||
| 3920 | pageY: pointer.y | ||
| 3921 | }); | ||
| 3922 | return Object.extend(event, methods); | ||
| 3923 | }; | ||
| 3924 | |||
| 3925 | } else { | ||
| 3926 | Event.prototype = Event.prototype || document.createEvent("HTMLEvents")['__proto__']; | ||
| 3927 | Object.extend(Event.prototype, methods); | ||
| 3928 | return Prototype.K; | ||
| 3929 | } | ||
| 3930 | })(); | ||
| 3931 | |||
| 3932 | Object.extend(Event, (function() { | ||
| 3933 | var cache = Event.cache; | ||
| 3934 | |||
| 3935 | function getEventID(element) { | ||
| 3936 | if (element._prototypeEventID) return element._prototypeEventID[0]; | ||
| 3937 | arguments.callee.id = arguments.callee.id || 1; | ||
| 3938 | return element._prototypeEventID = [++arguments.callee.id]; | ||
| 3939 | } | ||
| 3940 | |||
| 3941 | function getDOMEventName(eventName) { | ||
| 3942 | if (eventName && eventName.include(':')) return "dataavailable"; | ||
| 3943 | return eventName; | ||
| 3944 | } | ||
| 3945 | |||
| 3946 | function getCacheForID(id) { | ||
| 3947 | return cache[id] = cache[id] || { }; | ||
| 3948 | } | ||
| 3949 | |||
| 3950 | function getWrappersForEventName(id, eventName) { | ||
| 3951 | var c = getCacheForID(id); | ||
| 3952 | return c[eventName] = c[eventName] || []; | ||
| 3953 | } | ||
| 3954 | |||
| 3955 | function createWrapper(element, eventName, handler) { | ||
| 3956 | var id = getEventID(element); | ||
| 3957 | var c = getWrappersForEventName(id, eventName); | ||
| 3958 | if (c.pluck("handler").include(handler)) return false; | ||
| 3959 | |||
| 3960 | var wrapper = function(event) { | ||
| 3961 | if (!Event || !Event.extend || | ||
| 3962 | (event.eventName && event.eventName != eventName)) | ||
| 3963 | return false; | ||
| 3964 | |||
| 3965 | Event.extend(event); | ||
| 3966 | handler.call(element, event); | ||
| 3967 | }; | ||
| 3968 | |||
| 3969 | wrapper.handler = handler; | ||
| 3970 | c.push(wrapper); | ||
| 3971 | return wrapper; | ||
| 3972 | } | ||
| 3973 | |||
| 3974 | function findWrapper(id, eventName, handler) { | ||
| 3975 | var c = getWrappersForEventName(id, eventName); | ||
| 3976 | return c.find(function(wrapper) { return wrapper.handler == handler }); | ||
| 3977 | } | ||
| 3978 | |||
| 3979 | function destroyWrapper(id, eventName, handler) { | ||
| 3980 | var c = getCacheForID(id); | ||
| 3981 | if (!c[eventName]) return false; | ||
| 3982 | c[eventName] = c[eventName].without(findWrapper(id, eventName, handler)); | ||
| 3983 | } | ||
| 3984 | |||
| 3985 | function destroyCache() { | ||
| 3986 | for (var id in cache) | ||
| 3987 | for (var eventName in cache[id]) | ||
| 3988 | cache[id][eventName] = null; | ||
| 3989 | } | ||
| 3990 | |||
| 3991 | |||
| 3992 | // Internet Explorer needs to remove event handlers on page unload | ||
| 3993 | // in order to avoid memory leaks. | ||
| 3994 | if (window.attachEvent) { | ||
| 3995 | window.attachEvent("onunload", destroyCache); | ||
| 3996 | } | ||
| 3997 | |||
| 3998 | // Safari has a dummy event handler on page unload so that it won't | ||
| 3999 | // use its bfcache. Safari <= 3.1 has an issue with restoring the "document" | ||
| 4000 | // object when page is returned to via the back button using its bfcache. | ||
| 4001 | if (Prototype.Browser.WebKit) { | ||
| 4002 | window.addEventListener('unload', Prototype.emptyFunction, false); | ||
| 4003 | } | ||
| 4004 | |||
| 4005 | return { | ||
| 4006 | observe: function(element, eventName, handler) { | ||
| 4007 | element = $(element); | ||
| 4008 | var name = getDOMEventName(eventName); | ||
| 4009 | |||
| 4010 | var wrapper = createWrapper(element, eventName, handler); | ||
| 4011 | if (!wrapper) return element; | ||
| 4012 | |||
| 4013 | if (element.addEventListener) { | ||
| 4014 | element.addEventListener(name, wrapper, false); | ||
| 4015 | } else { | ||
| 4016 | element.attachEvent("on" + name, wrapper); | ||
| 4017 | } | ||
| 4018 | |||
| 4019 | return element; | ||
| 4020 | }, | ||
| 4021 | |||
| 4022 | stopObserving: function(element, eventName, handler) { | ||
| 4023 | element = $(element); | ||
| 4024 | var id = getEventID(element), name = getDOMEventName(eventName); | ||
| 4025 | |||
| 4026 | if (!handler && eventName) { | ||
| 4027 | getWrappersForEventName(id, eventName).each(function(wrapper) { | ||
| 4028 | element.stopObserving(eventName, wrapper.handler); | ||
| 4029 | }); | ||
| 4030 | return element; | ||
| 4031 | |||
| 4032 | } else if (!eventName) { | ||
| 4033 | Object.keys(getCacheForID(id)).each(function(eventName) { | ||
| 4034 | element.stopObserving(eventName); | ||
| 4035 | }); | ||
| 4036 | return element; | ||
| 4037 | } | ||
| 4038 | |||
| 4039 | var wrapper = findWrapper(id, eventName, handler); | ||
| 4040 | if (!wrapper) return element; | ||
| 4041 | |||
| 4042 | if (element.removeEventListener) { | ||
| 4043 | element.removeEventListener(name, wrapper, false); | ||
| 4044 | } else { | ||
| 4045 | element.detachEvent("on" + name, wrapper); | ||
| 4046 | } | ||
| 4047 | |||
| 4048 | destroyWrapper(id, eventName, handler); | ||
| 4049 | |||
| 4050 | return element; | ||
| 4051 | }, | ||
| 4052 | |||
| 4053 | fire: function(element, eventName, memo) { | ||
| 4054 | element = $(element); | ||
| 4055 | if (element == document && document.createEvent && !element.dispatchEvent) | ||
| 4056 | element = document.documentElement; | ||
| 4057 | |||
| 4058 | var event; | ||
| 4059 | if (document.createEvent) { | ||
| 4060 | event = document.createEvent("HTMLEvents"); | ||
| 4061 | event.initEvent("dataavailable", true, true); | ||
| 4062 | } else { | ||
| 4063 | event = document.createEventObject(); | ||
| 4064 | event.eventType = "ondataavailable"; | ||
| 4065 | } | ||
| 4066 | |||
| 4067 | event.eventName = eventName; | ||
| 4068 | event.memo = memo || { }; | ||
| 4069 | |||
| 4070 | if (document.createEvent) { | ||
| 4071 | element.dispatchEvent(event); | ||
| 4072 | } else { | ||
| 4073 | element.fireEvent(event.eventType, event); | ||
| 4074 | } | ||
| 4075 | |||
| 4076 | return Event.extend(event); | ||
| 4077 | } | ||
| 4078 | }; | ||
| 4079 | })()); | ||
| 4080 | |||
| 4081 | Object.extend(Event, Event.Methods); | ||
| 4082 | |||
| 4083 | Element.addMethods({ | ||
| 4084 | fire: Event.fire, | ||
| 4085 | observe: Event.observe, | ||
| 4086 | stopObserving: Event.stopObserving | ||
| 4087 | }); | ||
| 4088 | |||
| 4089 | Object.extend(document, { | ||
| 4090 | fire: Element.Methods.fire.methodize(), | ||
| 4091 | observe: Element.Methods.observe.methodize(), | ||
| 4092 | stopObserving: Element.Methods.stopObserving.methodize(), | ||
| 4093 | loaded: false | ||
| 4094 | }); | ||
| 4095 | |||
| 4096 | (function() { | ||
| 4097 | /* Support for the DOMContentLoaded event is based on work by Dan Webb, | ||
| 4098 | Matthias Miller, Dean Edwards and John Resig. */ | ||
| 4099 | |||
| 4100 | var timer; | ||
| 4101 | |||
| 4102 | function fireContentLoadedEvent() { | ||
| 4103 | if (document.loaded) return; | ||
| 4104 | if (timer) window.clearInterval(timer); | ||
| 4105 | document.fire("dom:loaded"); | ||
| 4106 | document.loaded = true; | ||
| 4107 | } | ||
| 4108 | |||
| 4109 | if (document.addEventListener) { | ||
| 4110 | if (Prototype.Browser.WebKit) { | ||
| 4111 | timer = window.setInterval(function() { | ||
| 4112 | if (/loaded|complete/.test(document.readyState)) | ||
| 4113 | fireContentLoadedEvent(); | ||
| 4114 | }, 0); | ||
| 4115 | |||
| 4116 | Event.observe(window, "load", fireContentLoadedEvent); | ||
| 4117 | |||
| 4118 | } else { | ||
| 4119 | document.addEventListener("DOMContentLoaded", | ||
| 4120 | fireContentLoadedEvent, false); | ||
| 4121 | } | ||
| 4122 | |||
| 4123 | } else { | ||
| 4124 | document.write("<script id=__onDOMContentLoaded defer src=//:><\/script>"); | ||
| 4125 | $("__onDOMContentLoaded").onreadystatechange = function() { | ||
| 4126 | if (this.readyState == "complete") { | ||
| 4127 | this.onreadystatechange = null; | ||
| 4128 | fireContentLoadedEvent(); | ||
| 4129 | } | ||
| 4130 | }; | ||
| 4131 | } | ||
| 4132 | })(); | ||
| 4133 | /*------------------------------- DEPRECATED -------------------------------*/ | ||
| 4134 | |||
| 4135 | Hash.toQueryString = Object.toQueryString; | ||
| 4136 | |||
| 4137 | var Toggle = { display: Element.toggle }; | ||
| 4138 | |||
| 4139 | Element.Methods.childOf = Element.Methods.descendantOf; | ||
| 4140 | |||
| 4141 | var Insertion = { | ||
| 4142 | Before: function(element, content) { | ||
| 4143 | return Element.insert(element, {before:content}); | ||
| 4144 | }, | ||
| 4145 | |||
| 4146 | Top: function(element, content) { | ||
| 4147 | return Element.insert(element, {top:content}); | ||
| 4148 | }, | ||
| 4149 | |||
| 4150 | Bottom: function(element, content) { | ||
| 4151 | return Element.insert(element, {bottom:content}); | ||
| 4152 | }, | ||
| 4153 | |||
| 4154 | After: function(element, content) { | ||
| 4155 | return Element.insert(element, {after:content}); | ||
| 4156 | } | ||
| 4157 | }; | ||
| 4158 | |||
| 4159 | var $continue = new Error('"throw $continue" is deprecated, use "return" instead'); | ||
| 4160 | |||
| 4161 | // This should be moved to script.aculo.us; notice the deprecated methods | ||
| 4162 | // further below, that map to the newer Element methods. | ||
| 4163 | var Position = { | ||
| 4164 | // set to true if needed, warning: firefox performance problems | ||
| 4165 | // NOT neeeded for page scrolling, only if draggable contained in | ||
| 4166 | // scrollable elements | ||
| 4167 | includeScrollOffsets: false, | ||
| 4168 | |||
| 4169 | // must be called before calling withinIncludingScrolloffset, every time the | ||
| 4170 | // page is scrolled | ||
| 4171 | prepare: function() { | ||
| 4172 | this.deltaX = window.pageXOffset | ||
| 4173 | || document.documentElement.scrollLeft | ||
| 4174 | || document.body.scrollLeft | ||
| 4175 | || 0; | ||
| 4176 | this.deltaY = window.pageYOffset | ||
| 4177 | || document.documentElement.scrollTop | ||
| 4178 | || document.body.scrollTop | ||
| 4179 | || 0; | ||
| 4180 | }, | ||
| 4181 | |||
| 4182 | // caches x/y coordinate pair to use with overlap | ||
| 4183 | within: function(element, x, y) { | ||
| 4184 | if (this.includeScrollOffsets) | ||
| 4185 | return this.withinIncludingScrolloffsets(element, x, y); | ||
| 4186 | this.xcomp = x; | ||
| 4187 | this.ycomp = y; | ||
| 4188 | this.offset = Element.cumulativeOffset(element); | ||
| 4189 | |||
| 4190 | return (y >= this.offset[1] && | ||
| 4191 | y < this.offset[1] + element.offsetHeight && | ||
| 4192 | x >= this.offset[0] && | ||
| 4193 | x < this.offset[0] + element.offsetWidth); | ||
| 4194 | }, | ||
| 4195 | |||
| 4196 | withinIncludingScrolloffsets: function(element, x, y) { | ||
| 4197 | var offsetcache = Element.cumulativeScrollOffset(element); | ||
| 4198 | |||
| 4199 | this.xcomp = x + offsetcache[0] - this.deltaX; | ||
| 4200 | this.ycomp = y + offsetcache[1] - this.deltaY; | ||
| 4201 | this.offset = Element.cumulativeOffset(element); | ||
| 4202 | |||
| 4203 | return (this.ycomp >= this.offset[1] && | ||
| 4204 | this.ycomp < this.offset[1] + element.offsetHeight && | ||
| 4205 | this.xcomp >= this.offset[0] && | ||
| 4206 | this.xcomp < this.offset[0] + element.offsetWidth); | ||
| 4207 | }, | ||
| 4208 | |||
| 4209 | // within must be called directly before | ||
| 4210 | overlap: function(mode, element) { | ||
| 4211 | if (!mode) return 0; | ||
| 4212 | if (mode == 'vertical') | ||
| 4213 | return ((this.offset[1] + element.offsetHeight) - this.ycomp) / | ||
| 4214 | element.offsetHeight; | ||
| 4215 | if (mode == 'horizontal') | ||
| 4216 | return ((this.offset[0] + element.offsetWidth) - this.xcomp) / | ||
| 4217 | element.offsetWidth; | ||
| 4218 | }, | ||
| 4219 | |||
| 4220 | // Deprecation layer -- use newer Element methods now (1.5.2). | ||
| 4221 | |||
| 4222 | cumulativeOffset: Element.Methods.cumulativeOffset, | ||
| 4223 | |||
| 4224 | positionedOffset: Element.Methods.positionedOffset, | ||
| 4225 | |||
| 4226 | absolutize: function(element) { | ||
| 4227 | Position.prepare(); | ||
| 4228 | return Element.absolutize(element); | ||
| 4229 | }, | ||
| 4230 | |||
| 4231 | relativize: function(element) { | ||
| 4232 | Position.prepare(); | ||
| 4233 | return Element.relativize(element); | ||
| 4234 | }, | ||
| 4235 | |||
| 4236 | realOffset: Element.Methods.cumulativeScrollOffset, | ||
| 4237 | |||
| 4238 | offsetParent: Element.Methods.getOffsetParent, | ||
| 4239 | |||
| 4240 | page: Element.Methods.viewportOffset, | ||
| 4241 | |||
| 4242 | clone: function(source, target, options) { | ||
| 4243 | options = options || { }; | ||
| 4244 | return Element.clonePosition(target, source, options); | ||
| 4245 | } | ||
| 4246 | }; | ||
| 4247 | |||
| 4248 | /*--------------------------------------------------------------------------*/ | ||
| 4249 | |||
| 4250 | if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ | ||
| 4251 | function iter(name) { | ||
| 4252 | return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; | ||
| 4253 | } | ||
| 4254 | |||
| 4255 | instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ? | ||
| 4256 | function(element, className) { | ||
| 4257 | className = className.toString().strip(); | ||
| 4258 | var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className); | ||
| 4259 | return cond ? document._getElementsByXPath('.//*' + cond, element) : []; | ||
| 4260 | } : function(element, className) { | ||
| 4261 | className = className.toString().strip(); | ||
| 4262 | var elements = [], classNames = (/\s/.test(className) ? $w(className) : null); | ||
| 4263 | if (!classNames && !className) return elements; | ||
| 4264 | |||
| 4265 | var nodes = $(element).getElementsByTagName('*'); | ||
| 4266 | className = ' ' + className + ' '; | ||
| 4267 | |||
| 4268 | for (var i = 0, child, cn; child = nodes[i]; i++) { | ||
| 4269 | if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) || | ||
| 4270 | (classNames && classNames.all(function(name) { | ||
| 4271 | return !name.toString().blank() && cn.include(' ' + name + ' '); | ||
| 4272 | })))) | ||
| 4273 | elements.push(Element.extend(child)); | ||
| 4274 | } | ||
| 4275 | return elements; | ||
| 4276 | }; | ||
| 4277 | |||
| 4278 | return function(className, parentElement) { | ||
| 4279 | return $(parentElement || document.body).getElementsByClassName(className); | ||
| 4280 | }; | ||
| 4281 | }(Element.Methods); | ||
| 4282 | |||
| 4283 | /*--------------------------------------------------------------------------*/ | ||
| 4284 | |||
| 4285 | Element.ClassNames = Class.create(); | ||
| 4286 | Element.ClassNames.prototype = { | ||
| 4287 | initialize: function(element) { | ||
| 4288 | this.element = $(element); | ||
| 4289 | }, | ||
| 4290 | |||
| 4291 | _each: function(iterator) { | ||
| 4292 | this.element.className.split(/\s+/).select(function(name) { | ||
| 4293 | return name.length > 0; | ||
| 4294 | })._each(iterator); | ||
| 4295 | }, | ||
| 4296 | |||
| 4297 | set: function(className) { | ||
| 4298 | this.element.className = className; | ||
| 4299 | }, | ||
| 4300 | |||
| 4301 | add: function(classNameToAdd) { | ||
| 4302 | if (this.include(classNameToAdd)) return; | ||
| 4303 | this.set($A(this).concat(classNameToAdd).join(' ')); | ||
| 4304 | }, | ||
| 4305 | |||
| 4306 | remove: function(classNameToRemove) { | ||
| 4307 | if (!this.include(classNameToRemove)) return; | ||
| 4308 | this.set($A(this).without(classNameToRemove).join(' ')); | ||
| 4309 | }, | ||
| 4310 | |||
| 4311 | toString: function() { | ||
| 4312 | return $A(this).join(' '); | ||
| 4313 | } | ||
| 4314 | }; | ||
| 4315 | |||
| 4316 | Object.extend(Element.ClassNames.prototype, Enumerable); | ||
| 4317 | |||
| 4318 | /*--------------------------------------------------------------------------*/ | ||
| 4319 | |||
| 4320 | Element.addMethods(); |
build/scripty/src/builder.js
(0 / 136)
|   | |||
| 1 | // script.aculo.us builder.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // | ||
| 5 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 6 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 7 | |||
| 8 | var Builder = { | ||
| 9 | NODEMAP: { | ||
| 10 | AREA: 'map', | ||
| 11 | CAPTION: 'table', | ||
| 12 | COL: 'table', | ||
| 13 | COLGROUP: 'table', | ||
| 14 | LEGEND: 'fieldset', | ||
| 15 | OPTGROUP: 'select', | ||
| 16 | OPTION: 'select', | ||
| 17 | PARAM: 'object', | ||
| 18 | TBODY: 'table', | ||
| 19 | TD: 'table', | ||
| 20 | TFOOT: 'table', | ||
| 21 | TH: 'table', | ||
| 22 | THEAD: 'table', | ||
| 23 | TR: 'table' | ||
| 24 | }, | ||
| 25 | // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken, | ||
| 26 | // due to a Firefox bug | ||
| 27 | node: function(elementName) { | ||
| 28 | elementName = elementName.toUpperCase(); | ||
| 29 | |||
| 30 | // try innerHTML approach | ||
| 31 | var parentTag = this.NODEMAP[elementName] || 'div'; | ||
| 32 | var parentElement = document.createElement(parentTag); | ||
| 33 | try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 | ||
| 34 | parentElement.innerHTML = "<" + elementName + "></" + elementName + ">"; | ||
| 35 | } catch(e) {} | ||
| 36 | var element = parentElement.firstChild || null; | ||
| 37 | |||
| 38 | // see if browser added wrapping tags | ||
| 39 | if(element && (element.tagName.toUpperCase() != elementName)) | ||
| 40 | element = element.getElementsByTagName(elementName)[0]; | ||
| 41 | |||
| 42 | // fallback to createElement approach | ||
| 43 | if(!element) element = document.createElement(elementName); | ||
| 44 | |||
| 45 | // abort if nothing could be created | ||
| 46 | if(!element) return; | ||
| 47 | |||
| 48 | // attributes (or text) | ||
| 49 | if(arguments[1]) | ||
| 50 | if(this._isStringOrNumber(arguments[1]) || | ||
| 51 | (arguments[1] instanceof Array) || | ||
| 52 | arguments[1].tagName) { | ||
| 53 | this._children(element, arguments[1]); | ||
| 54 | } else { | ||
| 55 | var attrs = this._attributes(arguments[1]); | ||
| 56 | if(attrs.length) { | ||
| 57 | try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 | ||
| 58 | parentElement.innerHTML = "<" +elementName + " " + | ||
| 59 | attrs + "></" + elementName + ">"; | ||
| 60 | } catch(e) {} | ||
| 61 | element = parentElement.firstChild || null; | ||
| 62 | // workaround firefox 1.0.X bug | ||
| 63 | if(!element) { | ||
| 64 | element = document.createElement(elementName); | ||
| 65 | for(attr in arguments[1]) | ||
| 66 | element[attr == 'class' ? 'className' : attr] = arguments[1][attr]; | ||
| 67 | } | ||
| 68 | if(element.tagName.toUpperCase() != elementName) | ||
| 69 | element = parentElement.getElementsByTagName(elementName)[0]; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | // text, or array of children | ||
| 74 | if(arguments[2]) | ||
| 75 | this._children(element, arguments[2]); | ||
| 76 | |||
| 77 | return $(element); | ||
| 78 | }, | ||
| 79 | _text: function(text) { | ||
| 80 | return document.createTextNode(text); | ||
| 81 | }, | ||
| 82 | |||
| 83 | ATTR_MAP: { | ||
| 84 | 'className': 'class', | ||
| 85 | 'htmlFor': 'for' | ||
| 86 | }, | ||
| 87 | |||
| 88 | _attributes: function(attributes) { | ||
| 89 | var attrs = []; | ||
| 90 | for(attribute in attributes) | ||
| 91 | attrs.push((attribute in this.ATTR_MAP ? this.ATTR_MAP[attribute] : attribute) + | ||
| 92 | '="' + attributes[attribute].toString().escapeHTML().gsub(/"/,'"') + '"'); | ||
| 93 | return attrs.join(" "); | ||
| 94 | }, | ||
| 95 | _children: function(element, children) { | ||
| 96 | if(children.tagName) { | ||
| 97 | element.appendChild(children); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | if(typeof children=='object') { // array can hold nodes and text | ||
| 101 | children.flatten().each( function(e) { | ||
| 102 | if(typeof e=='object') | ||
| 103 | element.appendChild(e); | ||
| 104 | else | ||
| 105 | if(Builder._isStringOrNumber(e)) | ||
| 106 | element.appendChild(Builder._text(e)); | ||
| 107 | }); | ||
| 108 | } else | ||
| 109 | if(Builder._isStringOrNumber(children)) | ||
| 110 | element.appendChild(Builder._text(children)); | ||
| 111 | }, | ||
| 112 | _isStringOrNumber: function(param) { | ||
| 113 | return(typeof param=='string' || typeof param=='number'); | ||
| 114 | }, | ||
| 115 | build: function(html) { | ||
| 116 | var element = this.node('div'); | ||
| 117 | $(element).update(html.strip()); | ||
| 118 | return element.down(); | ||
| 119 | }, | ||
| 120 | dump: function(scope) { | ||
| 121 | if(typeof scope != 'object' && typeof scope != 'function') scope = window; //global scope | ||
| 122 | |||
| 123 | var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " + | ||
| 124 | "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " + | ||
| 125 | "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+ | ||
| 126 | "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+ | ||
| 127 | "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+ | ||
| 128 | "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/); | ||
| 129 | |||
| 130 | tags.each( function(tag){ | ||
| 131 | scope[tag] = function() { | ||
| 132 | return Builder.node.apply(Builder, [tag].concat($A(arguments))); | ||
| 133 | }; | ||
| 134 | }); | ||
| 135 | } | ||
| 136 | }; |
build/scripty/src/controls.js
(0 / 965)
|   | |||
| 1 | // script.aculo.us controls.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // (c) 2005-2008 Ivan Krstic (http://blogs.law.harvard.edu/ivan) | ||
| 5 | // (c) 2005-2008 Jon Tirsen (http://www.tirsen.com) | ||
| 6 | // Contributors: | ||
| 7 | // Richard Livsey | ||
| 8 | // Rahul Bhargava | ||
| 9 | // Rob Wills | ||
| 10 | // | ||
| 11 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 12 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 13 | |||
| 14 | // Autocompleter.Base handles all the autocompletion functionality | ||
| 15 | // that's independent of the data source for autocompletion. This | ||
| 16 | // includes drawing the autocompletion menu, observing keyboard | ||
| 17 | // and mouse events, and similar. | ||
| 18 | // | ||
| 19 | // Specific autocompleters need to provide, at the very least, | ||
| 20 | // a getUpdatedChoices function that will be invoked every time | ||
| 21 | // the text inside the monitored textbox changes. This method | ||
| 22 | // should get the text for which to provide autocompletion by | ||
| 23 | // invoking this.getToken(), NOT by directly accessing | ||
| 24 | // this.element.value. This is to allow incremental tokenized | ||
| 25 | // autocompletion. Specific auto-completion logic (AJAX, etc) | ||
| 26 | // belongs in getUpdatedChoices. | ||
| 27 | // | ||
| 28 | // Tokenized incremental autocompletion is enabled automatically | ||
| 29 | // when an autocompleter is instantiated with the 'tokens' option | ||
| 30 | // in the options parameter, e.g.: | ||
| 31 | // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' }); | ||
| 32 | // will incrementally autocomplete with a comma as the token. | ||
| 33 | // Additionally, ',' in the above example can be replaced with | ||
| 34 | // a token array, e.g. { tokens: [',', '\n'] } which | ||
| 35 | // enables autocompletion on multiple tokens. This is most | ||
| 36 | // useful when one of the tokens is \n (a newline), as it | ||
| 37 | // allows smart autocompletion after linebreaks. | ||
| 38 | |||
| 39 | if(typeof Effect == 'undefined') | ||
| 40 | throw("controls.js requires including script.aculo.us' effects.js library"); | ||
| 41 | |||
| 42 | var Autocompleter = { }; | ||
| 43 | Autocompleter.Base = Class.create({ | ||
| 44 | baseInitialize: function(element, update, options) { | ||
| 45 | element = $(element); | ||
| 46 | this.element = element; | ||
| 47 | this.update = $(update); | ||
| 48 | this.hasFocus = false; | ||
| 49 | this.changed = false; | ||
| 50 | this.active = false; | ||
| 51 | this.index = 0; | ||
| 52 | this.entryCount = 0; | ||
| 53 | this.oldElementValue = this.element.value; | ||
| 54 | |||
| 55 | if(this.setOptions) | ||
| 56 | this.setOptions(options); | ||
| 57 | else | ||
| 58 | this.options = options || { }; | ||
| 59 | |||
| 60 | this.options.paramName = this.options.paramName || this.element.name; | ||
| 61 | this.options.tokens = this.options.tokens || []; | ||
| 62 | this.options.frequency = this.options.frequency || 0.4; | ||
| 63 | this.options.minChars = this.options.minChars || 1; | ||
| 64 | this.options.onShow = this.options.onShow || | ||
| 65 | function(element, update){ | ||
| 66 | if(!update.style.position || update.style.position=='absolute') { | ||
| 67 | update.style.position = 'absolute'; | ||
| 68 | Position.clone(element, update, { | ||
| 69 | setHeight: false, | ||
| 70 | offsetTop: element.offsetHeight | ||
| 71 | }); | ||
| 72 | } | ||
| 73 | Effect.Appear(update,{duration:0.15}); | ||
| 74 | }; | ||
| 75 | this.options.onHide = this.options.onHide || | ||
| 76 | function(element, update){ new Effect.Fade(update,{duration:0.15}) }; | ||
| 77 | |||
| 78 | if(typeof(this.options.tokens) == 'string') | ||
| 79 | this.options.tokens = new Array(this.options.tokens); | ||
| 80 | // Force carriage returns as token delimiters anyway | ||
| 81 | if (!this.options.tokens.include('\n')) | ||
| 82 | this.options.tokens.push('\n'); | ||
| 83 | |||
| 84 | this.observer = null; | ||
| 85 | |||
| 86 | this.element.setAttribute('autocomplete','off'); | ||
| 87 | |||
| 88 | Element.hide(this.update); | ||
| 89 | |||
| 90 | Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); | ||
| 91 | Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this)); | ||
| 92 | }, | ||
| 93 | |||
| 94 | show: function() { | ||
| 95 | if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); | ||
| 96 | if(!this.iefix && | ||
| 97 | (Prototype.Browser.IE) && | ||
| 98 | (Element.getStyle(this.update, 'position')=='absolute')) { | ||
| 99 | new Insertion.After(this.update, | ||
| 100 | '<iframe id="' + this.update.id + '_iefix" '+ | ||
| 101 | 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + | ||
| 102 | 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'); | ||
| 103 | this.iefix = $(this.update.id+'_iefix'); | ||
| 104 | } | ||
| 105 | if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50); | ||
| 106 | }, | ||
| 107 | |||
| 108 | fixIEOverlapping: function() { | ||
| 109 | Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)}); | ||
| 110 | this.iefix.style.zIndex = 1; | ||
| 111 | this.update.style.zIndex = 2; | ||
| 112 | Element.show(this.iefix); | ||
| 113 | }, | ||
| 114 | |||
| 115 | hide: function() { | ||
| 116 | this.stopIndicator(); | ||
| 117 | if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update); | ||
| 118 | if(this.iefix) Element.hide(this.iefix); | ||
| 119 | }, | ||
| 120 | |||
| 121 | startIndicator: function() { | ||
| 122 | if(this.options.indicator) Element.show(this.options.indicator); | ||
| 123 | }, | ||
| 124 | |||
| 125 | stopIndicator: function() { | ||
| 126 | if(this.options.indicator) Element.hide(this.options.indicator); | ||
| 127 | }, | ||
| 128 | |||
| 129 | onKeyPress: function(event) { | ||
| 130 | if(this.active) | ||
| 131 | switch(event.keyCode) { | ||
| 132 | case Event.KEY_TAB: | ||
| 133 | case Event.KEY_RETURN: | ||
| 134 | this.selectEntry(); | ||
| 135 | Event.stop(event); | ||
| 136 | case Event.KEY_ESC: | ||
| 137 | this.hide(); | ||
| 138 | this.active = false; | ||
| 139 | Event.stop(event); | ||
| 140 | return; | ||
| 141 | case Event.KEY_LEFT: | ||
| 142 | case Event.KEY_RIGHT: | ||
| 143 | return; | ||
| 144 | case Event.KEY_UP: | ||
| 145 | this.markPrevious(); | ||
| 146 | this.render(); | ||
| 147 | Event.stop(event); | ||
| 148 | return; | ||
| 149 | case Event.KEY_DOWN: | ||
| 150 | this.markNext(); | ||
| 151 | this.render(); | ||
| 152 | Event.stop(event); | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | else | ||
| 156 | if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || | ||
| 157 | (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return; | ||
| 158 | |||
| 159 | this.changed = true; | ||
| 160 | this.hasFocus = true; | ||
| 161 | |||
| 162 | if(this.observer) clearTimeout(this.observer); | ||
| 163 | this.observer = | ||
| 164 | setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000); | ||
| 165 | }, | ||
| 166 | |||
| 167 | activate: function() { | ||
| 168 | this.changed = false; | ||
| 169 | this.hasFocus = true; | ||
| 170 | this.getUpdatedChoices(); | ||
| 171 | }, | ||
| 172 | |||
| 173 | onHover: function(event) { | ||
| 174 | var element = Event.findElement(event, 'LI'); | ||
| 175 | if(this.index != element.autocompleteIndex) | ||
| 176 | { | ||
| 177 | this.index = element.autocompleteIndex; | ||
| 178 | this.render(); | ||
| 179 | } | ||
| 180 | Event.stop(event); | ||
| 181 | }, | ||
| 182 | |||
| 183 | onClick: function(event) { | ||
| 184 | var element = Event.findElement(event, 'LI'); | ||
| 185 | this.index = element.autocompleteIndex; | ||
| 186 | this.selectEntry(); | ||
| 187 | this.hide(); | ||
| 188 | }, | ||
| 189 | |||
| 190 | onBlur: function(event) { | ||
| 191 | // needed to make click events working | ||
| 192 | setTimeout(this.hide.bind(this), 250); | ||
| 193 | this.hasFocus = false; | ||
| 194 | this.active = false; | ||
| 195 | }, | ||
| 196 | |||
| 197 | render: function() { | ||
| 198 | if(this.entryCount > 0) { | ||
| 199 | for (var i = 0; i < this.entryCount; i++) | ||
| 200 | this.index==i ? | ||
| 201 | Element.addClassName(this.getEntry(i),"selected") : | ||
| 202 | Element.removeClassName(this.getEntry(i),"selected"); | ||
| 203 | if(this.hasFocus) { | ||
| 204 | this.show(); | ||
| 205 | this.active = true; | ||
| 206 | } | ||
| 207 | } else { | ||
| 208 | this.active = false; | ||
| 209 | this.hide(); | ||
| 210 | } | ||
| 211 | }, | ||
| 212 | |||
| 213 | markPrevious: function() { | ||
| 214 | if(this.index > 0) this.index--; | ||
| 215 | else this.index = this.entryCount-1; | ||
| 216 | this.getEntry(this.index).scrollIntoView(true); | ||
| 217 | }, | ||
| 218 | |||
| 219 | markNext: function() { | ||
| 220 | if(this.index < this.entryCount-1) this.index++; | ||
| 221 | else this.index = 0; | ||
| 222 | this.getEntry(this.index).scrollIntoView(false); | ||
| 223 | }, | ||
| 224 | |||
| 225 | getEntry: function(index) { | ||
| 226 | return this.update.firstChild.childNodes[index]; | ||
| 227 | }, | ||
| 228 | |||
| 229 | getCurrentEntry: function() { | ||
| 230 | return this.getEntry(this.index); | ||
| 231 | }, | ||
| 232 | |||
| 233 | selectEntry: function() { | ||
| 234 | this.active = false; | ||
| 235 | this.updateElement(this.getCurrentEntry()); | ||
| 236 | }, | ||
| 237 | |||
| 238 | updateElement: function(selectedElement) { | ||
| 239 | if (this.options.updateElement) { | ||
| 240 | this.options.updateElement(selectedElement); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | var value = ''; | ||
| 244 | if (this.options.select) { | ||
| 245 | var nodes = $(selectedElement).select('.' + this.options.select) || []; | ||
| 246 | if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select); | ||
| 247 | } else | ||
| 248 | value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal'); | ||
| 249 | |||
| 250 | var bounds = this.getTokenBounds(); | ||
| 251 | if (bounds[0] != -1) { | ||
| 252 | var newValue = this.element.value.substr(0, bounds[0]); | ||
| 253 | var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/); | ||
| 254 | if (whitespace) | ||
| 255 | newValue += whitespace[0]; | ||
| 256 | this.element.value = newValue + value + this.element.value.substr(bounds[1]); | ||
| 257 | } else { | ||
| 258 | this.element.value = value; | ||
| 259 | } | ||
| 260 | this.oldElementValue = this.element.value; | ||
| 261 | this.element.focus(); | ||
| 262 | |||
| 263 | if (this.options.afterUpdateElement) | ||
| 264 | this.options.afterUpdateElement(this.element, selectedElement); | ||
| 265 | }, | ||
| 266 | |||
| 267 | updateChoices: function(choices) { | ||
| 268 | if(!this.changed && this.hasFocus) { | ||
| 269 | this.update.innerHTML = choices; | ||
| 270 | Element.cleanWhitespace(this.update); | ||
| 271 | Element.cleanWhitespace(this.update.down()); | ||
| 272 | |||
| 273 | if(this.update.firstChild && this.update.down().childNodes) { | ||
| 274 | this.entryCount = | ||
| 275 | this.update.down().childNodes.length; | ||
| 276 | for (var i = 0; i < this.entryCount; i++) { | ||
| 277 | var entry = this.getEntry(i); | ||
| 278 | entry.autocompleteIndex = i; | ||
| 279 | this.addObservers(entry); | ||
| 280 | } | ||
| 281 | } else { | ||
| 282 | this.entryCount = 0; | ||
| 283 | } | ||
| 284 | |||
| 285 | this.stopIndicator(); | ||
| 286 | this.index = 0; | ||
| 287 | |||
| 288 | if(this.entryCount==1 && this.options.autoSelect) { | ||
| 289 | this.selectEntry(); | ||
| 290 | this.hide(); | ||
| 291 | } else { | ||
| 292 | this.render(); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | }, | ||
| 296 | |||
| 297 | addObservers: function(element) { | ||
| 298 | Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this)); | ||
| 299 | Event.observe(element, "click", this.onClick.bindAsEventListener(this)); | ||
| 300 | }, | ||
| 301 | |||
| 302 | onObserverEvent: function() { | ||
| 303 | this.changed = false; | ||
| 304 | this.tokenBounds = null; | ||
| 305 | if(this.getToken().length>=this.options.minChars) { | ||
| 306 | this.getUpdatedChoices(); | ||
| 307 | } else { | ||
| 308 | this.active = false; | ||
| 309 | this.hide(); | ||
| 310 | } | ||
| 311 | this.oldElementValue = this.element.value; | ||
| 312 | }, | ||
| 313 | |||
| 314 | getToken: function() { | ||
| 315 | var bounds = this.getTokenBounds(); | ||
| 316 | return this.element.value.substring(bounds[0], bounds[1]).strip(); | ||
| 317 | }, | ||
| 318 | |||
| 319 | getTokenBounds: function() { | ||
| 320 | if (null != this.tokenBounds) return this.tokenBounds; | ||
| 321 | var value = this.element.value; | ||
| 322 | if (value.strip().empty()) return [-1, 0]; | ||
| 323 | var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue); | ||
| 324 | var offset = (diff == this.oldElementValue.length ? 1 : 0); | ||
| 325 | var prevTokenPos = -1, nextTokenPos = value.length; | ||
| 326 | var tp; | ||
| 327 | for (var index = 0, l = this.options.tokens.length; index < l; ++index) { | ||
| 328 | tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1); | ||
| 329 | if (tp > prevTokenPos) prevTokenPos = tp; | ||
| 330 | tp = value.indexOf(this.options.tokens[index], diff + offset); | ||
| 331 | if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp; | ||
| 332 | } | ||
| 333 | return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]); | ||
| 334 | } | ||
| 335 | }); | ||
| 336 | |||
| 337 | Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) { | ||
| 338 | var boundary = Math.min(newS.length, oldS.length); | ||
| 339 | for (var index = 0; index < boundary; ++index) | ||
| 340 | if (newS[index] != oldS[index]) | ||
| 341 | return index; | ||
| 342 | return boundary; | ||
| 343 | }; | ||
| 344 | |||
| 345 | Ajax.Autocompleter = Class.create(Autocompleter.Base, { | ||
| 346 | initialize: function(element, update, url, options) { | ||
| 347 | this.baseInitialize(element, update, options); | ||
| 348 | this.options.asynchronous = true; | ||
| 349 | this.options.onComplete = this.onComplete.bind(this); | ||
| 350 | this.options.defaultParams = this.options.parameters || null; | ||
| 351 | this.url = url; | ||
| 352 | }, | ||
| 353 | |||
| 354 | getUpdatedChoices: function() { | ||
| 355 | this.startIndicator(); | ||
| 356 | |||
| 357 | var entry = encodeURIComponent(this.options.paramName) + '=' + | ||
| 358 | encodeURIComponent(this.getToken()); | ||
| 359 | |||
| 360 | this.options.parameters = this.options.callback ? | ||
| 361 | this.options.callback(this.element, entry) : entry; | ||
| 362 | |||
| 363 | if(this.options.defaultParams) | ||
| 364 | this.options.parameters += '&' + this.options.defaultParams; | ||
| 365 | |||
| 366 | new Ajax.Request(this.url, this.options); | ||
| 367 | }, | ||
| 368 | |||
| 369 | onComplete: function(request) { | ||
| 370 | this.updateChoices(request.responseText); | ||
| 371 | } | ||
| 372 | }); | ||
| 373 | |||
| 374 | // The local array autocompleter. Used when you'd prefer to | ||
| 375 | // inject an array of autocompletion options into the page, rather | ||
| 376 | // than sending out Ajax queries, which can be quite slow sometimes. | ||
| 377 | // | ||
| 378 | // The constructor takes four parameters. The first two are, as usual, | ||
| 379 | // the id of the monitored textbox, and id of the autocompletion menu. | ||
| 380 | // The third is the array you want to autocomplete from, and the fourth | ||
| 381 | // is the options block. | ||
| 382 | // | ||
| 383 | // Extra local autocompletion options: | ||
| 384 | // - choices - How many autocompletion choices to offer | ||
| 385 | // | ||
| 386 | // - partialSearch - If false, the autocompleter will match entered | ||
| 387 | // text only at the beginning of strings in the | ||
| 388 | // autocomplete array. Defaults to true, which will | ||
| 389 | // match text at the beginning of any *word* in the | ||
| 390 | // strings in the autocomplete array. If you want to | ||
| 391 | // search anywhere in the string, additionally set | ||
| 392 | // the option fullSearch to true (default: off). | ||
| 393 | // | ||
| 394 | // - fullSsearch - Search anywhere in autocomplete array strings. | ||
| 395 | // | ||
| 396 | // - partialChars - How many characters to enter before triggering | ||
| 397 | // a partial match (unlike minChars, which defines | ||
| 398 | // how many characters are required to do any match | ||
| 399 | // at all). Defaults to 2. | ||
| 400 | // | ||
| 401 | // - ignoreCase - Whether to ignore case when autocompleting. | ||
| 402 | // Defaults to true. | ||
| 403 | // | ||
| 404 | // It's possible to pass in a custom function as the 'selector' | ||
| 405 | // option, if you prefer to write your own autocompletion logic. | ||
| 406 | // In that case, the other options above will not apply unless | ||
| 407 | // you support them. | ||
| 408 | |||
| 409 | Autocompleter.Local = Class.create(Autocompleter.Base, { | ||
| 410 | initialize: function(element, update, array, options) { | ||
| 411 | this.baseInitialize(element, update, options); | ||
| 412 | this.options.array = array; | ||
| 413 | }, | ||
| 414 | |||
| 415 | getUpdatedChoices: function() { | ||
| 416 | this.updateChoices(this.options.selector(this)); | ||
| 417 | }, | ||
| 418 | |||
| 419 | setOptions: function(options) { | ||
| 420 | this.options = Object.extend({ | ||
| 421 | choices: 10, | ||
| 422 | partialSearch: true, | ||
| 423 | partialChars: 2, | ||
| 424 | ignoreCase: true, | ||
| 425 | fullSearch: false, | ||
| 426 | selector: function(instance) { | ||
| 427 | var ret = []; // Beginning matches | ||
| 428 | var partial = []; // Inside matches | ||
| 429 | var entry = instance.getToken(); | ||
| 430 | var count = 0; | ||
| 431 | |||
| 432 | for (var i = 0; i < instance.options.array.length && | ||
| 433 | ret.length < instance.options.choices ; i++) { | ||
| 434 | |||
| 435 | var elem = instance.options.array[i]; | ||
| 436 | var foundPos = instance.options.ignoreCase ? | ||
| 437 | elem.toLowerCase().indexOf(entry.toLowerCase()) : | ||
| 438 | elem.indexOf(entry); | ||
| 439 | |||
| 440 | while (foundPos != -1) { | ||
| 441 | if (foundPos == 0 && elem.length != entry.length) { | ||
| 442 | ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" + | ||
| 443 | elem.substr(entry.length) + "</li>"); | ||
| 444 | break; | ||
| 445 | } else if (entry.length >= instance.options.partialChars && | ||
| 446 | instance.options.partialSearch && foundPos != -1) { | ||
| 447 | if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) { | ||
| 448 | partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" + | ||
| 449 | elem.substr(foundPos, entry.length) + "</strong>" + elem.substr( | ||
| 450 | foundPos + entry.length) + "</li>"); | ||
| 451 | break; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | foundPos = instance.options.ignoreCase ? | ||
| 456 | elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : | ||
| 457 | elem.indexOf(entry, foundPos + 1); | ||
| 458 | |||
| 459 | } | ||
| 460 | } | ||
| 461 | if (partial.length) | ||
| 462 | ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)); | ||
| 463 | return "<ul>" + ret.join('') + "</ul>"; | ||
| 464 | } | ||
| 465 | }, options || { }); | ||
| 466 | } | ||
| 467 | }); | ||
| 468 | |||
| 469 | // AJAX in-place editor and collection editor | ||
| 470 | // Full rewrite by Christophe Porteneuve <tdd@tddsworld.com> (April 2007). | ||
| 471 | |||
| 472 | // Use this if you notice weird scrolling problems on some browsers, | ||
| 473 | // the DOM might be a bit confused when this gets called so do this | ||
| 474 | // waits 1 ms (with setTimeout) until it does the activation | ||
| 475 | Field.scrollFreeActivate = function(field) { | ||
| 476 | setTimeout(function() { | ||
| 477 | Field.activate(field); | ||
| 478 | }, 1); | ||
| 479 | }; | ||
| 480 | |||
| 481 | Ajax.InPlaceEditor = Class.create({ | ||
| 482 | initialize: function(element, url, options) { | ||
| 483 | this.url = url; | ||
| 484 | this.element = element = $(element); | ||
| 485 | this.prepareOptions(); | ||
| 486 | this._controls = { }; | ||
| 487 | arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!! | ||
| 488 | Object.extend(this.options, options || { }); | ||
| 489 | if (!this.options.formId && this.element.id) { | ||
| 490 | this.options.formId = this.element.id + '-inplaceeditor'; | ||
| 491 | if ($(this.options.formId)) | ||
| 492 | this.options.formId = ''; | ||
| 493 | } | ||
| 494 | if (this.options.externalControl) | ||
| 495 | this.options.externalControl = $(this.options.externalControl); | ||
| 496 | if (!this.options.externalControl) | ||
| 497 | this.options.externalControlOnly = false; | ||
| 498 | this._originalBackground = this.element.getStyle('background-color') || 'transparent'; | ||
| 499 | this.element.title = this.options.clickToEditText; | ||
| 500 | this._boundCancelHandler = this.handleFormCancellation.bind(this); | ||
| 501 | this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this); | ||
| 502 | this._boundFailureHandler = this.handleAJAXFailure.bind(this); | ||
| 503 | this._boundSubmitHandler = this.handleFormSubmission.bind(this); | ||
| 504 | this._boundWrapperHandler = this.wrapUp.bind(this); | ||
| 505 | this.registerListeners(); | ||
| 506 | }, | ||
| 507 | checkForEscapeOrReturn: function(e) { | ||
| 508 | if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return; | ||
| 509 | if (Event.KEY_ESC == e.keyCode) | ||
| 510 | this.handleFormCancellation(e); | ||
| 511 | else if (Event.KEY_RETURN == e.keyCode) | ||
| 512 | this.handleFormSubmission(e); | ||
| 513 | }, | ||
| 514 | createControl: function(mode, handler, extraClasses) { | ||
| 515 | var control = this.options[mode + 'Control']; | ||
| 516 | var text = this.options[mode + 'Text']; | ||
| 517 | if ('button' == control) { | ||
| 518 | var btn = document.createElement('input'); | ||
| 519 | btn.type = 'submit'; | ||
| 520 | btn.value = text; | ||
| 521 | btn.className = 'editor_' + mode + '_button'; | ||
| 522 | if ('cancel' == mode) | ||
| 523 | btn.onclick = this._boundCancelHandler; | ||
| 524 | this._form.appendChild(btn); | ||
| 525 | this._controls[mode] = btn; | ||
| 526 | } else if ('link' == control) { | ||
| 527 | var link = document.createElement('a'); | ||
| 528 | link.href = '#'; | ||
| 529 | link.appendChild(document.createTextNode(text)); | ||
| 530 | link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler; | ||
| 531 | link.className = 'editor_' + mode + '_link'; | ||
| 532 | if (extraClasses) | ||
| 533 | link.className += ' ' + extraClasses; | ||
| 534 | this._form.appendChild(link); | ||
| 535 | this._controls[mode] = link; | ||
| 536 | } | ||
| 537 | }, | ||
| 538 | createEditField: function() { | ||
| 539 | var text = (this.options.loadTextURL ? this.options.loadingText : this.getText()); | ||
| 540 | var fld; | ||
| 541 | if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) { | ||
| 542 | fld = document.createElement('input'); | ||
| 543 | fld.type = 'text'; | ||
| 544 | var size = this.options.size || this.options.cols || 0; | ||
| 545 | if (0 < size) fld.size = size; | ||
| 546 | } else { | ||
| 547 | fld = document.createElement('textarea'); | ||
| 548 | fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows); | ||
| 549 | fld.cols = this.options.cols || 40; | ||
| 550 | } | ||
| 551 | fld.name = this.options.paramName; | ||
| 552 | fld.value = text; // No HTML breaks conversion anymore | ||
| 553 | fld.className = 'editor_field'; | ||
| 554 | if (this.options.submitOnBlur) | ||
| 555 | fld.onblur = this._boundSubmitHandler; | ||
| 556 | this._controls.editor = fld; | ||
| 557 | if (this.options.loadTextURL) | ||
| 558 | this.loadExternalText(); | ||
| 559 | this._form.appendChild(this._controls.editor); | ||
| 560 | }, | ||
| 561 | createForm: function() { | ||
| 562 | var ipe = this; | ||
| 563 | function addText(mode, condition) { | ||
| 564 | var text = ipe.options['text' + mode + 'Controls']; | ||
| 565 | if (!text || condition === false) return; | ||
| 566 | ipe._form.appendChild(document.createTextNode(text)); | ||
| 567 | }; | ||
| 568 | this._form = $(document.createElement('form')); | ||
| 569 | this._form.id = this.options.formId; | ||
| 570 | this._form.addClassName(this.options.formClassName); | ||
| 571 | this._form.onsubmit = this._boundSubmitHandler; | ||
| 572 | this.createEditField(); | ||
| 573 | if ('textarea' == this._controls.editor.tagName.toLowerCase()) | ||
| 574 | this._form.appendChild(document.createElement('br')); | ||
| 575 | if (this.options.onFormCustomization) | ||
| 576 | this.options.onFormCustomization(this, this._form); | ||
| 577 | addText('Before', this.options.okControl || this.options.cancelControl); | ||
| 578 | this.createControl('ok', this._boundSubmitHandler); | ||
| 579 | addText('Between', this.options.okControl && this.options.cancelControl); | ||
| 580 | this.createControl('cancel', this._boundCancelHandler, 'editor_cancel'); | ||
| 581 | addText('After', this.options.okControl || this.options.cancelControl); | ||
| 582 | }, | ||
| 583 | destroy: function() { | ||
| 584 | if (this._oldInnerHTML) | ||
| 585 | this.element.innerHTML = this._oldInnerHTML; | ||
| 586 | this.leaveEditMode(); | ||
| 587 | this.unregisterListeners(); | ||
| 588 | }, | ||
| 589 | enterEditMode: function(e) { | ||
| 590 | if (this._saving || this._editing) return; | ||
| 591 | this._editing = true; | ||
| 592 | this.triggerCallback('onEnterEditMode'); | ||
| 593 | if (this.options.externalControl) | ||
| 594 | this.options.externalControl.hide(); | ||
| 595 | this.element.hide(); | ||
| 596 | this.createForm(); | ||
| 597 | this.element.parentNode.insertBefore(this._form, this.element); | ||
| 598 | if (!this.options.loadTextURL) | ||
| 599 | this.postProcessEditField(); | ||
| 600 | if (e) Event.stop(e); | ||
| 601 | }, | ||
| 602 | enterHover: function(e) { | ||
| 603 | if (this.options.hoverClassName) | ||
| 604 | this.element.addClassName(this.options.hoverClassName); | ||
| 605 | if (this._saving) return; | ||
| 606 | this.triggerCallback('onEnterHover'); | ||
| 607 | }, | ||
| 608 | getText: function() { | ||
| 609 | return this.element.innerHTML.unescapeHTML(); | ||
| 610 | }, | ||
| 611 | handleAJAXFailure: function(transport) { | ||
| 612 | this.triggerCallback('onFailure', transport); | ||
| 613 | if (this._oldInnerHTML) { | ||
| 614 | this.element.innerHTML = this._oldInnerHTML; | ||
| 615 | this._oldInnerHTML = null; | ||
| 616 | } | ||
| 617 | }, | ||
| 618 | handleFormCancellation: function(e) { | ||
| 619 | this.wrapUp(); | ||
| 620 | if (e) Event.stop(e); | ||
| 621 | }, | ||
| 622 | handleFormSubmission: function(e) { | ||
| 623 | var form = this._form; | ||
| 624 | var value = $F(this._controls.editor); | ||
| 625 | this.prepareSubmission(); | ||
| 626 | var params = this.options.callback(form, value) || ''; | ||
| 627 | if (Object.isString(params)) | ||
| 628 | params = params.toQueryParams(); | ||
| 629 | params.editorId = this.element.id; | ||
| 630 | if (this.options.htmlResponse) { | ||
| 631 | var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions); | ||
| 632 | Object.extend(options, { | ||
| 633 | parameters: params, | ||
| 634 | onComplete: this._boundWrapperHandler, | ||
| 635 | onFailure: this._boundFailureHandler | ||
| 636 | }); | ||
| 637 | new Ajax.Updater({ success: this.element }, this.url, options); | ||
| 638 | } else { | ||
| 639 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | ||
| 640 | Object.extend(options, { | ||
| 641 | parameters: params, | ||
| 642 | onComplete: this._boundWrapperHandler, | ||
| 643 | onFailure: this._boundFailureHandler | ||
| 644 | }); | ||
| 645 | new Ajax.Request(this.url, options); | ||
| 646 | } | ||
| 647 | if (e) Event.stop(e); | ||
| 648 | }, | ||
| 649 | leaveEditMode: function() { | ||
| 650 | this.element.removeClassName(this.options.savingClassName); | ||
| 651 | this.removeForm(); | ||
| 652 | this.leaveHover(); | ||
| 653 | this.element.style.backgroundColor = this._originalBackground; | ||
| 654 | this.element.show(); | ||
| 655 | if (this.options.externalControl) | ||
| 656 | this.options.externalControl.show(); | ||
| 657 | this._saving = false; | ||
| 658 | this._editing = false; | ||
| 659 | this._oldInnerHTML = null; | ||
| 660 | this.triggerCallback('onLeaveEditMode'); | ||
| 661 | }, | ||
| 662 | leaveHover: function(e) { | ||
| 663 | if (this.options.hoverClassName) | ||
| 664 | this.element.removeClassName(this.options.hoverClassName); | ||
| 665 | if (this._saving) return; | ||
| 666 | this.triggerCallback('onLeaveHover'); | ||
| 667 | }, | ||
| 668 | loadExternalText: function() { | ||
| 669 | this._form.addClassName(this.options.loadingClassName); | ||
| 670 | this._controls.editor.disabled = true; | ||
| 671 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | ||
| 672 | Object.extend(options, { | ||
| 673 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | ||
| 674 | onComplete: Prototype.emptyFunction, | ||
| 675 | onSuccess: function(transport) { | ||
| 676 | this._form.removeClassName(this.options.loadingClassName); | ||
| 677 | var text = transport.responseText; | ||
| 678 | if (this.options.stripLoadedTextTags) | ||
| 679 | text = text.stripTags(); | ||
| 680 | this._controls.editor.value = text; | ||
| 681 | this._controls.editor.disabled = false; | ||
| 682 | this.postProcessEditField(); | ||
| 683 | }.bind(this), | ||
| 684 | onFailure: this._boundFailureHandler | ||
| 685 | }); | ||
| 686 | new Ajax.Request(this.options.loadTextURL, options); | ||
| 687 | }, | ||
| 688 | postProcessEditField: function() { | ||
| 689 | var fpc = this.options.fieldPostCreation; | ||
| 690 | if (fpc) | ||
| 691 | $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate'](); | ||
| 692 | }, | ||
| 693 | prepareOptions: function() { | ||
| 694 | this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions); | ||
| 695 | Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks); | ||
| 696 | [this._extraDefaultOptions].flatten().compact().each(function(defs) { | ||
| 697 | Object.extend(this.options, defs); | ||
| 698 | }.bind(this)); | ||
| 699 | }, | ||
| 700 | prepareSubmission: function() { | ||
| 701 | this._saving = true; | ||
| 702 | this.removeForm(); | ||
| 703 | this.leaveHover(); | ||
| 704 | this.showSaving(); | ||
| 705 | }, | ||
| 706 | registerListeners: function() { | ||
| 707 | this._listeners = { }; | ||
| 708 | var listener; | ||
| 709 | $H(Ajax.InPlaceEditor.Listeners).each(function(pair) { | ||
| 710 | listener = this[pair.value].bind(this); | ||
| 711 | this._listeners[pair.key] = listener; | ||
| 712 | if (!this.options.externalControlOnly) | ||
| 713 | this.element.observe(pair.key, listener); | ||
| 714 | if (this.options.externalControl) | ||
| 715 | this.options.externalControl.observe(pair.key, listener); | ||
| 716 | }.bind(this)); | ||
| 717 | }, | ||
| 718 | removeForm: function() { | ||
| 719 | if (!this._form) return; | ||
| 720 | this._form.remove(); | ||
| 721 | this._form = null; | ||
| 722 | this._controls = { }; | ||
| 723 | }, | ||
| 724 | showSaving: function() { | ||
| 725 | this._oldInnerHTML = this.element.innerHTML; | ||
| 726 | this.element.innerHTML = this.options.savingText; | ||
| 727 | this.element.addClassName(this.options.savingClassName); | ||
| 728 | this.element.style.backgroundColor = this._originalBackground; | ||
| 729 | this.element.show(); | ||
| 730 | }, | ||
| 731 | triggerCallback: function(cbName, arg) { | ||
| 732 | if ('function' == typeof this.options[cbName]) { | ||
| 733 | this.options[cbName](this, arg); | ||
| 734 | } | ||
| 735 | }, | ||
| 736 | unregisterListeners: function() { | ||
| 737 | $H(this._listeners).each(function(pair) { | ||
| 738 | if (!this.options.externalControlOnly) | ||
| 739 | this.element.stopObserving(pair.key, pair.value); | ||
| 740 | if (this.options.externalControl) | ||
| 741 | this.options.externalControl.stopObserving(pair.key, pair.value); | ||
| 742 | }.bind(this)); | ||
| 743 | }, | ||
| 744 | wrapUp: function(transport) { | ||
| 745 | this.leaveEditMode(); | ||
| 746 | // Can't use triggerCallback due to backward compatibility: requires | ||
| 747 | // binding + direct element | ||
| 748 | this._boundComplete(transport, this.element); | ||
| 749 | } | ||
| 750 | }); | ||
| 751 | |||
| 752 | Object.extend(Ajax.InPlaceEditor.prototype, { | ||
| 753 | dispose: Ajax.InPlaceEditor.prototype.destroy | ||
| 754 | }); | ||
| 755 | |||
| 756 | Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, { | ||
| 757 | initialize: function($super, element, url, options) { | ||
| 758 | this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions; | ||
| 759 | $super(element, url, options); | ||
| 760 | }, | ||
| 761 | |||
| 762 | createEditField: function() { | ||
| 763 | var list = document.createElement('select'); | ||
| 764 | list.name = this.options.paramName; | ||
| 765 | list.size = 1; | ||
| 766 | this._controls.editor = list; | ||
| 767 | this._collection = this.options.collection || []; | ||
| 768 | if (this.options.loadCollectionURL) | ||
| 769 | this.loadCollection(); | ||
| 770 | else | ||
| 771 | this.checkForExternalText(); | ||
| 772 | this._form.appendChild(this._controls.editor); | ||
| 773 | }, | ||
| 774 | |||
| 775 | loadCollection: function() { | ||
| 776 | this._form.addClassName(this.options.loadingClassName); | ||
| 777 | this.showLoadingText(this.options.loadingCollectionText); | ||
| 778 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | ||
| 779 | Object.extend(options, { | ||
| 780 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | ||
| 781 | onComplete: Prototype.emptyFunction, | ||
| 782 | onSuccess: function(transport) { | ||
| 783 | var js = transport.responseText.strip(); | ||
| 784 | if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check | ||
| 785 | throw('Server returned an invalid collection representation.'); | ||
| 786 | this._collection = eval(js); | ||
| 787 | this.checkForExternalText(); | ||
| 788 | }.bind(this), | ||
| 789 | onFailure: this.onFailure | ||
| 790 | }); | ||
| 791 | new Ajax.Request(this.options.loadCollectionURL, options); | ||
| 792 | }, | ||
| 793 | |||
| 794 | showLoadingText: function(text) { | ||
| 795 | this._controls.editor.disabled = true; | ||
| 796 | var tempOption = this._controls.editor.firstChild; | ||
| 797 | if (!tempOption) { | ||
| 798 | tempOption = document.createElement('option'); | ||
| 799 | tempOption.value = ''; | ||
| 800 | this._controls.editor.appendChild(tempOption); | ||
| 801 | tempOption.selected = true; | ||
| 802 | } | ||
| 803 | tempOption.update((text || '').stripScripts().stripTags()); | ||
| 804 | }, | ||
| 805 | |||
| 806 | checkForExternalText: function() { | ||
| 807 | this._text = this.getText(); | ||
| 808 | if (this.options.loadTextURL) | ||
| 809 | this.loadExternalText(); | ||
| 810 | else | ||
| 811 | this.buildOptionList(); | ||
| 812 | }, | ||
| 813 | |||
| 814 | loadExternalText: function() { | ||
| 815 | this.showLoadingText(this.options.loadingText); | ||
| 816 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); | ||
| 817 | Object.extend(options, { | ||
| 818 | parameters: 'editorId=' + encodeURIComponent(this.element.id), | ||
| 819 | onComplete: Prototype.emptyFunction, | ||
| 820 | onSuccess: function(transport) { | ||
| 821 | this._text = transport.responseText.strip(); | ||
| 822 | this.buildOptionList(); | ||
| 823 | }.bind(this), | ||
| 824 | onFailure: this.onFailure | ||
| 825 | }); | ||
| 826 | new Ajax.Request(this.options.loadTextURL, options); | ||
| 827 | }, | ||
| 828 | |||
| 829 | buildOptionList: function() { | ||
| 830 | this._form.removeClassName(this.options.loadingClassName); | ||
| 831 | this._collection = this._collection.map(function(entry) { | ||
| 832 | return 2 === entry.length ? entry : [entry, entry].flatten(); | ||
| 833 | }); | ||
| 834 | var marker = ('value' in this.options) ? this.options.value : this._text; | ||
| 835 | var textFound = this._collection.any(function(entry) { | ||
| 836 | return entry[0] == marker; | ||
| 837 | }.bind(this)); | ||
| 838 | this._controls.editor.update(''); | ||
| 839 | var option; | ||
| 840 | this._collection.each(function(entry, index) { | ||
| 841 | option = document.createElement('option'); | ||
| 842 | option.value = entry[0]; | ||
| 843 | option.selected = textFound ? entry[0] == marker : 0 == index; | ||
| 844 | option.appendChild(document.createTextNode(entry[1])); | ||
| 845 | this._controls.editor.appendChild(option); | ||
| 846 | }.bind(this)); | ||
| 847 | this._controls.editor.disabled = false; | ||
| 848 | Field.scrollFreeActivate(this._controls.editor); | ||
| 849 | } | ||
| 850 | }); | ||
| 851 | |||
| 852 | //**** DEPRECATION LAYER FOR InPlace[Collection]Editor! **** | ||
| 853 | //**** This only exists for a while, in order to let **** | ||
| 854 | //**** users adapt to the new API. Read up on the new **** | ||
| 855 | //**** API and convert your code to it ASAP! **** | ||
| 856 | |||
| 857 | Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) { | ||
| 858 | if (!options) return; | ||
| 859 | function fallback(name, expr) { | ||
| 860 | if (name in options || expr === undefined) return; | ||
| 861 | options[name] = expr; | ||
| 862 | }; | ||
| 863 | fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' : | ||
| 864 | options.cancelLink == options.cancelButton == false ? false : undefined))); | ||
| 865 | fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' : | ||
| 866 | options.okLink == options.okButton == false ? false : undefined))); | ||
| 867 | fallback('highlightColor', options.highlightcolor); | ||
| 868 | fallback('highlightEndColor', options.highlightendcolor); | ||
| 869 | }; | ||
| 870 | |||
| 871 | Object.extend(Ajax.InPlaceEditor, { | ||
| 872 | DefaultOptions: { | ||
| 873 | ajaxOptions: { }, | ||
| 874 | autoRows: 3, // Use when multi-line w/ rows == 1 | ||
| 875 | cancelControl: 'link', // 'link'|'button'|false | ||
| 876 | cancelText: 'cancel', | ||
| 877 | clickToEditText: 'Click to edit', | ||
| 878 | externalControl: null, // id|elt | ||
| 879 | externalControlOnly: false, | ||
| 880 | fieldPostCreation: 'activate', // 'activate'|'focus'|false | ||
| 881 | formClassName: 'inplaceeditor-form', | ||
| 882 | formId: null, // id|elt | ||
| 883 | highlightColor: '#ffff99', | ||
| 884 | highlightEndColor: '#ffffff', | ||
| 885 | hoverClassName: '', | ||
| 886 | htmlResponse: true, | ||
| 887 | loadingClassName: 'inplaceeditor-loading', | ||
| 888 | loadingText: 'Loading...', | ||
| 889 | okControl: 'button', // 'link'|'button'|false | ||
| 890 | okText: 'ok', | ||
| 891 | paramName: 'value', | ||
| 892 | rows: 1, // If 1 and multi-line, uses autoRows | ||
| 893 | savingClassName: 'inplaceeditor-saving', | ||
| 894 | savingText: 'Saving...', | ||
| 895 | size: 0, | ||
| 896 | stripLoadedTextTags: false, | ||
| 897 | submitOnBlur: false, | ||
| 898 | textAfterControls: '', | ||
| 899 | textBeforeControls: '', | ||
| 900 | textBetweenControls: '' | ||
| 901 | }, | ||
| 902 | DefaultCallbacks: { | ||
| 903 | callback: function(form) { | ||
| 904 | return Form.serialize(form); | ||
| 905 | }, | ||
| 906 | onComplete: function(transport, element) { | ||
| 907 | // For backward compatibility, this one is bound to the IPE, and passes | ||
| 908 | // the element directly. It was too often customized, so we don't break it. | ||
| 909 | new Effect.Highlight(element, { | ||
| 910 | startcolor: this.options.highlightColor, keepBackgroundImage: true }); | ||
| 911 | }, | ||
| 912 | onEnterEditMode: null, | ||
| 913 | onEnterHover: function(ipe) { | ||
| 914 | ipe.element.style.backgroundColor = ipe.options.highlightColor; | ||
| 915 | if (ipe._effect) | ||
| 916 | ipe._effect.cancel(); | ||
| 917 | }, | ||
| 918 | onFailure: function(transport, ipe) { | ||
| 919 | alert('Error communication with the server: ' + transport.responseText.stripTags()); | ||
| 920 | }, | ||
| 921 | onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls. | ||
| 922 | onLeaveEditMode: null, | ||
| 923 | onLeaveHover: function(ipe) { | ||
| 924 | ipe._effect = new Effect.Highlight(ipe.element, { | ||
| 925 | startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor, | ||
| 926 | restorecolor: ipe._originalBackground, keepBackgroundImage: true | ||
| 927 | }); | ||
| 928 | } | ||
| 929 | }, | ||
| 930 | Listeners: { | ||
| 931 | click: 'enterEditMode', | ||
| 932 | keydown: 'checkForEscapeOrReturn', | ||
| 933 | mouseover: 'enterHover', | ||
| 934 | mouseout: 'leaveHover' | ||
| 935 | } | ||
| 936 | }); | ||
| 937 | |||
| 938 | Ajax.InPlaceCollectionEditor.DefaultOptions = { | ||
| 939 | loadingCollectionText: 'Loading options...' | ||
| 940 | }; | ||
| 941 | |||
| 942 | // Delayed observer, like Form.Element.Observer, | ||
| 943 | // but waits for delay after last key input | ||
| 944 | // Ideal for live-search fields | ||
| 945 | |||
| 946 | Form.Element.DelayedObserver = Class.create({ | ||
| 947 | initialize: function(element, delay, callback) { | ||
| 948 | this.delay = delay || 0.5; | ||
| 949 | this.element = $(element); | ||
| 950 | this.callback = callback; | ||
| 951 | this.timer = null; | ||
| 952 | this.lastValue = $F(this.element); | ||
| 953 | Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this)); | ||
| 954 | }, | ||
| 955 | delayedListener: function(event) { | ||
| 956 | if(this.lastValue == $F(this.element)) return; | ||
| 957 | if(this.timer) clearTimeout(this.timer); | ||
| 958 | this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000); | ||
| 959 | this.lastValue = $F(this.element); | ||
| 960 | }, | ||
| 961 | onTimerEvent: function() { | ||
| 962 | this.timer = null; | ||
| 963 | this.callback(this.element, $F(this.element)); | ||
| 964 | } | ||
| 965 | }); |
build/scripty/src/dragdrop.js
(0 / 975)
|   | |||
| 1 | // script.aculo.us dragdrop.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // (c) 2005-2008 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) | ||
| 5 | // | ||
| 6 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 7 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 8 | |||
| 9 | if(Object.isUndefined(Effect)) | ||
| 10 | throw("dragdrop.js requires including script.aculo.us' effects.js library"); | ||
| 11 | |||
| 12 | var Droppables = { | ||
| 13 | drops: [], | ||
| 14 | |||
| 15 | remove: function(element) { | ||
| 16 | this.drops = this.drops.reject(function(d) { return d.element==$(element) }); | ||
| 17 | }, | ||
| 18 | |||
| 19 | add: function(element) { | ||
| 20 | element = $(element); | ||
| 21 | var options = Object.extend({ | ||
| 22 | greedy: true, | ||
| 23 | hoverclass: null, | ||
| 24 | tree: false | ||
| 25 | }, arguments[1] || { }); | ||
| 26 | |||
| 27 | // cache containers | ||
| 28 | if(options.containment) { | ||
| 29 | options._containers = []; | ||
| 30 | var containment = options.containment; | ||
| 31 | if(Object.isArray(containment)) { | ||
| 32 | containment.each( function(c) { options._containers.push($(c)) }); | ||
| 33 | } else { | ||
| 34 | options._containers.push($(containment)); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | if(options.accept) options.accept = [options.accept].flatten(); | ||
| 39 | |||
| 40 | Element.makePositioned(element); // fix IE | ||
| 41 | options.element = element; | ||
| 42 | |||
| 43 | this.drops.push(options); | ||
| 44 | }, | ||
| 45 | |||
| 46 | findDeepestChild: function(drops) { | ||
| 47 | deepest = drops[0]; | ||
| 48 | |||
| 49 | for (i = 1; i < drops.length; ++i) | ||
| 50 | if (Element.isParent(drops[i].element, deepest.element)) | ||
| 51 | deepest = drops[i]; | ||
| 52 | |||
| 53 | return deepest; | ||
| 54 | }, | ||
| 55 | |||
| 56 | isContained: function(element, drop) { | ||
| 57 | var containmentNode; | ||
| 58 | if(drop.tree) { | ||
| 59 | containmentNode = element.treeNode; | ||
| 60 | } else { | ||
| 61 | containmentNode = element.parentNode; | ||
| 62 | } | ||
| 63 | return drop._containers.detect(function(c) { return containmentNode == c }); | ||
| 64 | }, | ||
| 65 | |||
| 66 | isAffected: function(point, element, drop) { | ||
| 67 | return ( | ||
| 68 | (drop.element!=element) && | ||
| 69 | ((!drop._containers) || | ||
| 70 | this.isContained(element, drop)) && | ||
| 71 | ((!drop.accept) || | ||
| 72 | (Element.classNames(element).detect( | ||
| 73 | function(v) { return drop.accept.include(v) } ) )) && | ||
| 74 | Position.within(drop.element, point[0], point[1]) ); | ||
| 75 | }, | ||
| 76 | |||
| 77 | deactivate: function(drop) { | ||
| 78 | if(drop.hoverclass) | ||
| 79 | Element.removeClassName(drop.element, drop.hoverclass); | ||
| 80 | this.last_active = null; | ||
| 81 | }, | ||
| 82 | |||
| 83 | activate: function(drop) { | ||
| 84 | if(drop.hoverclass) | ||
| 85 | Element.addClassName(drop.element, drop.hoverclass); | ||
| 86 | this.last_active = drop; | ||
| 87 | }, | ||
| 88 | |||
| 89 | show: function(point, element) { | ||
| 90 | if(!this.drops.length) return; | ||
| 91 | var drop, affected = []; | ||
| 92 | |||
| 93 | this.drops.each( function(drop) { | ||
| 94 | if(Droppables.isAffected(point, element, drop)) | ||
| 95 | affected.push(drop); | ||
| 96 | }); | ||
| 97 | |||
| 98 | if(affected.length>0) | ||
| 99 | drop = Droppables.findDeepestChild(affected); | ||
| 100 | |||
| 101 | if(this.last_active && this.last_active != drop) this.deactivate(this.last_active); | ||
| 102 | if (drop) { | ||
| 103 | Position.within(drop.element, point[0], point[1]); | ||
| 104 | if(drop.onHover) | ||
| 105 | drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); | ||
| 106 | |||
| 107 | if (drop != this.last_active) Droppables.activate(drop); | ||
| 108 | } | ||
| 109 | }, | ||
| 110 | |||
| 111 | fire: function(event, element) { | ||
| 112 | if(!this.last_active) return; | ||
| 113 | Position.prepare(); | ||
| 114 | |||
| 115 | if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) | ||
| 116 | if (this.last_active.onDrop) { | ||
| 117 | this.last_active.onDrop(element, this.last_active.element, event); | ||
| 118 | return true; | ||
| 119 | } | ||
| 120 | }, | ||
| 121 | |||
| 122 | reset: function() { | ||
| 123 | if(this.last_active) | ||
| 124 | this.deactivate(this.last_active); | ||
| 125 | } | ||
| 126 | }; | ||
| 127 | |||
| 128 | var Draggables = { | ||
| 129 | drags: [], | ||
| 130 | observers: [], | ||
| 131 | |||
| 132 | register: function(draggable) { | ||
| 133 | if(this.drags.length == 0) { | ||
| 134 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); | ||
| 135 | this.eventMouseMove = this.updateDrag.bindAsEventListener(this); | ||
| 136 | this.eventKeypress = this.keyPress.bindAsEventListener(this); | ||
| 137 | |||
| 138 | Event.observe(document, "mouseup", this.eventMouseUp); | ||
| 139 | Event.observe(document, "mousemove", this.eventMouseMove); | ||
| 140 | Event.observe(document, "keypress", this.eventKeypress); | ||
| 141 | } | ||
| 142 | this.drags.push(draggable); | ||
| 143 | }, | ||
| 144 | |||
| 145 | unregister: function(draggable) { | ||
| 146 | this.drags = this.drags.reject(function(d) { return d==draggable }); | ||
| 147 | if(this.drags.length == 0) { | ||
| 148 | Event.stopObserving(document, "mouseup", this.eventMouseUp); | ||
| 149 | Event.stopObserving(document, "mousemove", this.eventMouseMove); | ||
| 150 | Event.stopObserving(document, "keypress", this.eventKeypress); | ||
| 151 | } | ||
| 152 | }, | ||
| 153 | |||
| 154 | activate: function(draggable) { | ||
| 155 | if(draggable.options.delay) { | ||
| 156 | this._timeout = setTimeout(function() { | ||
| 157 | Draggables._timeout = null; | ||
| 158 | window.focus(); | ||
| 159 | Draggables.activeDraggable = draggable; | ||
| 160 | }.bind(this), draggable.options.delay); | ||
| 161 | } else { | ||
| 162 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari | ||
| 163 | this.activeDraggable = draggable; | ||
| 164 | } | ||
| 165 | }, | ||
| 166 | |||
| 167 | deactivate: function() { | ||
| 168 | this.activeDraggable = null; | ||
| 169 | }, | ||
| 170 | |||
| 171 | updateDrag: function(event) { | ||
| 172 | if(!this.activeDraggable) return; | ||
| 173 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
| 174 | // Mozilla-based browsers fire successive mousemove events with | ||
| 175 | // the same coordinates, prevent needless redrawing (moz bug?) | ||
| 176 | if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; | ||
| 177 | this._lastPointer = pointer; | ||
| 178 | |||
| 179 | this.activeDraggable.updateDrag(event, pointer); | ||
| 180 | }, | ||
| 181 | |||
| 182 | endDrag: function(event) { | ||
| 183 | if(this._timeout) { | ||
| 184 | clearTimeout(this._timeout); | ||
| 185 | this._timeout = null; | ||
| 186 | } | ||
| 187 | if(!this.activeDraggable) return; | ||
| 188 | this._lastPointer = null; | ||
| 189 | this.activeDraggable.endDrag(event); | ||
| 190 | this.activeDraggable = null; | ||
| 191 | }, | ||
| 192 | |||
| 193 | keyPress: function(event) { | ||
| 194 | if(this.activeDraggable) | ||
| 195 | this.activeDraggable.keyPress(event); | ||
| 196 | }, | ||
| 197 | |||
| 198 | addObserver: function(observer) { | ||
| 199 | this.observers.push(observer); | ||
| 200 | this._cacheObserverCallbacks(); | ||
| 201 | }, | ||
| 202 | |||
| 203 | removeObserver: function(element) { // element instead of observer fixes mem leaks | ||
| 204 | this.observers = this.observers.reject( function(o) { return o.element==element }); | ||
| 205 | this._cacheObserverCallbacks(); | ||
| 206 | }, | ||
| 207 | |||
| 208 | notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' | ||
| 209 | if(this[eventName+'Count'] > 0) | ||
| 210 | this.observers.each( function(o) { | ||
| 211 | if(o[eventName]) o[eventName](eventName, draggable, event); | ||
| 212 | }); | ||
| 213 | if(draggable.options[eventName]) draggable.options[eventName](draggable, event); | ||
| 214 | }, | ||
| 215 | |||
| 216 | _cacheObserverCallbacks: function() { | ||
| 217 | ['onStart','onEnd','onDrag'].each( function(eventName) { | ||
| 218 | Draggables[eventName+'Count'] = Draggables.observers.select( | ||
| 219 | function(o) { return o[eventName]; } | ||
| 220 | ).length; | ||
| 221 | }); | ||
| 222 | } | ||
| 223 | }; | ||
| 224 | |||
| 225 | /*--------------------------------------------------------------------------*/ | ||
| 226 | |||
| 227 | var Draggable = Class.create({ | ||
| 228 | initialize: function(element) { | ||
| 229 | var defaults = { | ||
| 230 | handle: false, | ||
| 231 | reverteffect: function(element, top_offset, left_offset) { | ||
| 232 | var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; | ||
| 233 | new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, | ||
| 234 | queue: {scope:'_draggable', position:'end'} | ||
| 235 | }); | ||
| 236 | }, | ||
| 237 | endeffect: function(element) { | ||
| 238 | var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0; | ||
| 239 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, | ||
| 240 | queue: {scope:'_draggable', position:'end'}, | ||
| 241 | afterFinish: function(){ | ||
| 242 | Draggable._dragging[element] = false | ||
| 243 | } | ||
| 244 | }); | ||
| 245 | }, | ||
| 246 | zindex: 1000, | ||
| 247 | revert: false, | ||
| 248 | quiet: false, | ||
| 249 | scroll: false, | ||
| 250 | scrollSensitivity: 20, | ||
| 251 | scrollSpeed: 15, | ||
| 252 | snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } | ||
| 253 | delay: 0 | ||
| 254 | }; | ||
| 255 | |||
| 256 | if(!arguments[1] || Object.isUndefined(arguments[1].endeffect)) | ||
| 257 | Object.extend(defaults, { | ||
| 258 | starteffect: function(element) { | ||
| 259 | element._opacity = Element.getOpacity(element); | ||
| 260 | Draggable._dragging[element] = true; | ||
| 261 | new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); | ||
| 262 | } | ||
| 263 | }); | ||
| 264 | |||
| 265 | var options = Object.extend(defaults, arguments[1] || { }); | ||
| 266 | |||
| 267 | this.element = $(element); | ||
| 268 | |||
| 269 | if(options.handle && Object.isString(options.handle)) | ||
| 270 | this.handle = this.element.down('.'+options.handle, 0); | ||
| 271 | |||
| 272 | if(!this.handle) this.handle = $(options.handle); | ||
| 273 | if(!this.handle) this.handle = this.element; | ||
| 274 | |||
| 275 | if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { | ||
| 276 | options.scroll = $(options.scroll); | ||
| 277 | this._isScrollChild = Element.childOf(this.element, options.scroll); | ||
| 278 | } | ||
| 279 | |||
| 280 | Element.makePositioned(this.element); // fix IE | ||
| 281 | |||
| 282 | this.options = options; | ||
| 283 | this.dragging = false; | ||
| 284 | |||
| 285 | this.eventMouseDown = this.initDrag.bindAsEventListener(this); | ||
| 286 | Event.observe(this.handle, "mousedown", this.eventMouseDown); | ||
| 287 | |||
| 288 | Draggables.register(this); | ||
| 289 | }, | ||
| 290 | |||
| 291 | destroy: function() { | ||
| 292 | Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); | ||
| 293 | Draggables.unregister(this); | ||
| 294 | }, | ||
| 295 | |||
| 296 | currentDelta: function() { | ||
| 297 | return([ | ||
| 298 | parseInt(Element.getStyle(this.element,'left') || '0'), | ||
| 299 | parseInt(Element.getStyle(this.element,'top') || '0')]); | ||
| 300 | }, | ||
| 301 | |||
| 302 | initDrag: function(event) { | ||
| 303 | if(!Object.isUndefined(Draggable._dragging[this.element]) && | ||
| 304 | Draggable._dragging[this.element]) return; | ||
| 305 | if(Event.isLeftClick(event)) { | ||
| 306 | // abort on form elements, fixes a Firefox issue | ||
| 307 | var src = Event.element(event); | ||
| 308 | if((tag_name = src.tagName.toUpperCase()) && ( | ||
| 309 | tag_name=='INPUT' || | ||
| 310 | tag_name=='SELECT' || | ||
| 311 | tag_name=='OPTION' || | ||
| 312 | tag_name=='BUTTON' || | ||
| 313 | tag_name=='TEXTAREA')) return; | ||
| 314 | |||
| 315 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
| 316 | var pos = Position.cumulativeOffset(this.element); | ||
| 317 | this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); | ||
| 318 | |||
| 319 | Draggables.activate(this); | ||
| 320 | Event.stop(event); | ||
| 321 | } | ||
| 322 | }, | ||
| 323 | |||
| 324 | startDrag: function(event) { | ||
| 325 | this.dragging = true; | ||
| 326 | if(!this.delta) | ||
| 327 | this.delta = this.currentDelta(); | ||
| 328 | |||
| 329 | if(this.options.zindex) { | ||
| 330 | this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); | ||
| 331 | this.element.style.zIndex = this.options.zindex; | ||
| 332 | } | ||
| 333 | |||
| 334 | if(this.options.ghosting) { | ||
| 335 | this._clone = this.element.cloneNode(true); | ||
| 336 | this._originallyAbsolute = (this.element.getStyle('position') == 'absolute'); | ||
| 337 | if (!this._originallyAbsolute) | ||
| 338 | Position.absolutize(this.element); | ||
| 339 | this.element.parentNode.insertBefore(this._clone, this.element); | ||
| 340 | } | ||
| 341 | |||
| 342 | if(this.options.scroll) { | ||
| 343 | if (this.options.scroll == window) { | ||
| 344 | var where = this._getWindowScroll(this.options.scroll); | ||
| 345 | this.originalScrollLeft = where.left; | ||
| 346 | this.originalScrollTop = where.top; | ||
| 347 | } else { | ||
| 348 | this.originalScrollLeft = this.options.scroll.scrollLeft; | ||
| 349 | this.originalScrollTop = this.options.scroll.scrollTop; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | Draggables.notify('onStart', this, event); | ||
| 354 | |||
| 355 | if(this.options.starteffect) this.options.starteffect(this.element); | ||
| 356 | }, | ||
| 357 | |||
| 358 | updateDrag: function(event, pointer) { | ||
| 359 | if(!this.dragging) this.startDrag(event); | ||
| 360 | |||
| 361 | if(!this.options.quiet){ | ||
| 362 | Position.prepare(); | ||
| 363 | Droppables.show(pointer, this.element); | ||
| 364 | } | ||
| 365 | |||
| 366 | Draggables.notify('onDrag', this, event); | ||
| 367 | |||
| 368 | this.draw(pointer); | ||
| 369 | if(this.options.change) this.options.change(this); | ||
| 370 | |||
| 371 | if(this.options.scroll) { | ||
| 372 | this.stopScrolling(); | ||
| 373 | |||
| 374 | var p; | ||
| 375 | if (this.options.scroll == window) { | ||
| 376 | with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } | ||
| 377 | } else { | ||
| 378 | p = Position.page(this.options.scroll); | ||
| 379 | p[0] += this.options.scroll.scrollLeft + Position.deltaX; | ||
| 380 | p[1] += this.options.scroll.scrollTop + Position.deltaY; | ||
| 381 | p.push(p[0]+this.options.scroll.offsetWidth); | ||
| 382 | p.push(p[1]+this.options.scroll.offsetHeight); | ||
| 383 | } | ||
| 384 | var speed = [0,0]; | ||
| 385 | if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); | ||
| 386 | if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); | ||
| 387 | if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); | ||
| 388 | if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); | ||
| 389 | this.startScrolling(speed); | ||
| 390 | } | ||
| 391 | |||
| 392 | // fix AppleWebKit rendering | ||
| 393 | if(Prototype.Browser.WebKit) window.scrollBy(0,0); | ||
| 394 | |||
| 395 | Event.stop(event); | ||
| 396 | }, | ||
| 397 | |||
| 398 | finishDrag: function(event, success) { | ||
| 399 | this.dragging = false; | ||
| 400 | |||
| 401 | if(this.options.quiet){ | ||
| 402 | Position.prepare(); | ||
| 403 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
| 404 | Droppables.show(pointer, this.element); | ||
| 405 | } | ||
| 406 | |||
| 407 | if(this.options.ghosting) { | ||
| 408 | if (!this._originallyAbsolute) | ||
| 409 | Position.relativize(this.element); | ||
| 410 | delete this._originallyAbsolute; | ||
| 411 | Element.remove(this._clone); | ||
| 412 | this._clone = null; | ||
| 413 | } | ||
| 414 | |||
| 415 | var dropped = false; | ||
| 416 | if(success) { | ||
| 417 | dropped = Droppables.fire(event, this.element); | ||
| 418 | if (!dropped) dropped = false; | ||
| 419 | } | ||
| 420 | if(dropped && this.options.onDropped) this.options.onDropped(this.element); | ||
| 421 | Draggables.notify('onEnd', this, event); | ||
| 422 | |||
| 423 | var revert = this.options.revert; | ||
| 424 | if(revert && Object.isFunction(revert)) revert = revert(this.element); | ||
| 425 | |||
| 426 | var d = this.currentDelta(); | ||
| 427 | if(revert && this.options.reverteffect) { | ||
| 428 | if (dropped == 0 || revert != 'failure') | ||
| 429 | this.options.reverteffect(this.element, | ||
| 430 | d[1]-this.delta[1], d[0]-this.delta[0]); | ||
| 431 | } else { | ||
| 432 | this.delta = d; | ||
| 433 | } | ||
| 434 | |||
| 435 | if(this.options.zindex) | ||
| 436 | this.element.style.zIndex = this.originalZ; | ||
| 437 | |||
| 438 | if(this.options.endeffect) | ||
| 439 | this.options.endeffect(this.element); | ||
| 440 | |||
| 441 | Draggables.deactivate(this); | ||
| 442 | Droppables.reset(); | ||
| 443 | }, | ||
| 444 | |||
| 445 | keyPress: function(event) { | ||
| 446 | if(event.keyCode!=Event.KEY_ESC) return; | ||
| 447 | this.finishDrag(event, false); | ||
| 448 | Event.stop(event); | ||
| 449 | }, | ||
| 450 | |||
| 451 | endDrag: function(event) { | ||
| 452 | if(!this.dragging) return; | ||
| 453 | this.stopScrolling(); | ||
| 454 | this.finishDrag(event, true); | ||
| 455 | Event.stop(event); | ||
| 456 | }, | ||
| 457 | |||
| 458 | draw: function(point) { | ||
| 459 | var pos = Position.cumulativeOffset(this.element); | ||
| 460 | if(this.options.ghosting) { | ||
| 461 | var r = Position.realOffset(this.element); | ||
| 462 | pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; | ||
| 463 | } | ||
| 464 | |||
| 465 | var d = this.currentDelta(); | ||
| 466 | pos[0] -= d[0]; pos[1] -= d[1]; | ||
| 467 | |||
| 468 | if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { | ||
| 469 | pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; | ||
| 470 | pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; | ||
| 471 | } | ||
| 472 | |||
| 473 | var p = [0,1].map(function(i){ | ||
| 474 | return (point[i]-pos[i]-this.offset[i]) | ||
| 475 | }.bind(this)); | ||
| 476 | |||
| 477 | if(this.options.snap) { | ||
| 478 | if(Object.isFunction(this.options.snap)) { | ||
| 479 | p = this.options.snap(p[0],p[1],this); | ||
| 480 | } else { | ||
| 481 | if(Object.isArray(this.options.snap)) { | ||
| 482 | p = p.map( function(v, i) { | ||
| 483 | return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this)); | ||
| 484 | } else { | ||
| 485 | p = p.map( function(v) { | ||
| 486 | return (v/this.options.snap).round()*this.options.snap }.bind(this)); | ||
| 487 | } | ||
| 488 | }} | ||
| 489 | |||
| 490 | var style = this.element.style; | ||
| 491 | if((!this.options.constraint) || (this.options.constraint=='horizontal')) | ||
| 492 | style.left = p[0] + "px"; | ||
| 493 | if((!this.options.constraint) || (this.options.constraint=='vertical')) | ||
| 494 | style.top = p[1] + "px"; | ||
| 495 | |||
| 496 | if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering | ||
| 497 | }, | ||
| 498 | |||
| 499 | stopScrolling: function() { | ||
| 500 | if(this.scrollInterval) { | ||
| 501 | clearInterval(this.scrollInterval); | ||
| 502 | this.scrollInterval = null; | ||
| 503 | Draggables._lastScrollPointer = null; | ||
| 504 | } | ||
| 505 | }, | ||
| 506 | |||
| 507 | startScrolling: function(speed) { | ||
| 508 | if(!(speed[0] || speed[1])) return; | ||
| 509 | this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; | ||
| 510 | this.lastScrolled = new Date(); | ||
| 511 | this.scrollInterval = setInterval(this.scroll.bind(this), 10); | ||
| 512 | }, | ||
| 513 | |||
| 514 | scroll: function() { | ||
| 515 | var current = new Date(); | ||
| 516 | var delta = current - this.lastScrolled; | ||
| 517 | this.lastScrolled = current; | ||
| 518 | if(this.options.scroll == window) { | ||
| 519 | with (this._getWindowScroll(this.options.scroll)) { | ||
| 520 | if (this.scrollSpeed[0] || this.scrollSpeed[1]) { | ||
| 521 | var d = delta / 1000; | ||
| 522 | this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } else { | ||
| 526 | this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; | ||
| 527 | this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; | ||
| 528 | } | ||
| 529 | |||
| 530 | Position.prepare(); | ||
| 531 | Droppables.show(Draggables._lastPointer, this.element); | ||
| 532 | Draggables.notify('onDrag', this); | ||
| 533 | if (this._isScrollChild) { | ||
| 534 | Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); | ||
| 535 | Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; | ||
| 536 | Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; | ||
| 537 | if (Draggables._lastScrollPointer[0] < 0) | ||
| 538 | Draggables._lastScrollPointer[0] = 0; | ||
| 539 | if (Draggables._lastScrollPointer[1] < 0) | ||
| 540 | Draggables._lastScrollPointer[1] = 0; | ||
| 541 | this.draw(Draggables._lastScrollPointer); | ||
| 542 | } | ||
| 543 | |||
| 544 | if(this.options.change) this.options.change(this); | ||
| 545 | }, | ||
| 546 | |||
| 547 | _getWindowScroll: function(w) { | ||
| 548 | var T, L, W, H; | ||
| 549 | with (w.document) { | ||
| 550 | if (w.document.documentElement && documentElement.scrollTop) { | ||
| 551 | T = documentElement.scrollTop; | ||
| 552 | L = documentElement.scrollLeft; | ||
| 553 | } else if (w.document.body) { | ||
| 554 | T = body.scrollTop; | ||
| 555 | L = body.scrollLeft; | ||
| 556 | } | ||
| 557 | if (w.innerWidth) { | ||
| 558 | W = w.innerWidth; | ||
| 559 | H = w.innerHeight; | ||
| 560 | } else if (w.document.documentElement && documentElement.clientWidth) { | ||
| 561 | W = documentElement.clientWidth; | ||
| 562 | H = documentElement.clientHeight; | ||
| 563 | } else { | ||
| 564 | W = body.offsetWidth; | ||
| 565 | H = body.offsetHeight; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | return { top: T, left: L, width: W, height: H }; | ||
| 569 | } | ||
| 570 | }); | ||
| 571 | |||
| 572 | Draggable._dragging = { }; | ||
| 573 | |||
| 574 | /*--------------------------------------------------------------------------*/ | ||
| 575 | |||
| 576 | var SortableObserver = Class.create({ | ||
| 577 | initialize: function(element, observer) { | ||
| 578 | this.element = $(element); | ||
| 579 | this.observer = observer; | ||
| 580 | this.lastValue = Sortable.serialize(this.element); | ||
| 581 | }, | ||
| 582 | |||
| 583 | onStart: function() { | ||
| 584 | this.lastValue = Sortable.serialize(this.element); | ||
| 585 | }, | ||
| 586 | |||
| 587 | onEnd: function() { | ||
| 588 | Sortable.unmark(); | ||
| 589 | if(this.lastValue != Sortable.serialize(this.element)) | ||
| 590 | this.observer(this.element) | ||
| 591 | } | ||
| 592 | }); | ||
| 593 | |||
| 594 | var Sortable = { | ||
| 595 | SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, | ||
| 596 | |||
| 597 | sortables: { }, | ||
| 598 | |||
| 599 | _findRootElement: function(element) { | ||
| 600 | while (element.tagName.toUpperCase() != "BODY") { | ||
| 601 | if(element.id && Sortable.sortables[element.id]) return element; | ||
| 602 | element = element.parentNode; | ||
| 603 | } | ||
| 604 | }, | ||
| 605 | |||
| 606 | options: function(element) { | ||
| 607 | element = Sortable._findRootElement($(element)); | ||
| 608 | if(!element) return; | ||
| 609 | return Sortable.sortables[element.id]; | ||
| 610 | }, | ||
| 611 | |||
| 612 | destroy: function(element){ | ||
| 613 | element = $(element); | ||
| 614 | var s = Sortable.sortables[element.id]; | ||
| 615 | |||
| 616 | if(s) { | ||
| 617 | Draggables.removeObserver(s.element); | ||
| 618 | s.droppables.each(function(d){ Droppables.remove(d) }); | ||
| 619 | s.draggables.invoke('destroy'); | ||
| 620 | |||
| 621 | delete Sortable.sortables[s.element.id]; | ||
| 622 | } | ||
| 623 | }, | ||
| 624 | |||
| 625 | create: function(element) { | ||
| 626 | element = $(element); | ||
| 627 | var options = Object.extend({ | ||
| 628 | element: element, | ||
| 629 | tag: 'li', // assumes li children, override with tag: 'tagname' | ||
| 630 | dropOnEmpty: false, | ||
| 631 | tree: false, | ||
| 632 | treeTag: 'ul', | ||
| 633 | overlap: 'vertical', // one of 'vertical', 'horizontal' | ||
| 634 | constraint: 'vertical', // one of 'vertical', 'horizontal', false | ||
| 635 | containment: element, // also takes array of elements (or id's); or false | ||
| 636 | handle: false, // or a CSS class | ||
| 637 | only: false, | ||
| 638 | delay: 0, | ||
| 639 | hoverclass: null, | ||
| 640 | ghosting: false, | ||
| 641 | quiet: false, | ||
| 642 | scroll: false, | ||
| 643 | scrollSensitivity: 20, | ||
| 644 | scrollSpeed: 15, | ||
| 645 | format: this.SERIALIZE_RULE, | ||
| 646 | |||
| 647 | // these take arrays of elements or ids and can be | ||
| 648 | // used for better initialization performance | ||
| 649 | elements: false, | ||
| 650 | handles: false, | ||
| 651 | |||
| 652 | onChange: Prototype.emptyFunction, | ||
| 653 | onUpdate: Prototype.emptyFunction | ||
| 654 | }, arguments[1] || { }); | ||
| 655 | |||
| 656 | // clear any old sortable with same element | ||
| 657 | this.destroy(element); | ||
| 658 | |||
| 659 | // build options for the draggables | ||
| 660 | var options_for_draggable = { | ||
| 661 | revert: true, | ||
| 662 | quiet: options.quiet, | ||
| 663 | scroll: options.scroll, | ||
| 664 | scrollSpeed: options.scrollSpeed, | ||
| 665 | scrollSensitivity: options.scrollSensitivity, | ||
| 666 | delay: options.delay, | ||
| 667 | ghosting: options.ghosting, | ||
| 668 | constraint: options.constraint, | ||
| 669 | handle: options.handle }; | ||
| 670 | |||
| 671 | if(options.starteffect) | ||
| 672 | options_for_draggable.starteffect = options.starteffect; | ||
| 673 | |||
| 674 | if(options.reverteffect) | ||
| 675 | options_for_draggable.reverteffect = options.reverteffect; | ||
| 676 | else | ||
| 677 | if(options.ghosting) options_for_draggable.reverteffect = function(element) { | ||
| 678 | element.style.top = 0; | ||
| 679 | element.style.left = 0; | ||
| 680 | }; | ||
| 681 | |||
| 682 | if(options.endeffect) | ||
| 683 | options_for_draggable.endeffect = options.endeffect; | ||
| 684 | |||
| 685 | if(options.zindex) | ||
| 686 | options_for_draggable.zindex = options.zindex; | ||
| 687 | |||
| 688 | // build options for the droppables | ||
| 689 | var options_for_droppable = { | ||
| 690 | overlap: options.overlap, | ||
| 691 | containment: options.containment, | ||
| 692 | tree: options.tree, | ||
| 693 | hoverclass: options.hoverclass, | ||
| 694 | onHover: Sortable.onHover | ||
| 695 | }; | ||
| 696 | |||
| 697 | var options_for_tree = { | ||
| 698 | onHover: Sortable.onEmptyHover, | ||
| 699 | overlap: options.overlap, | ||
| 700 | containment: options.containment, | ||
| 701 | hoverclass: options.hoverclass | ||
| 702 | }; | ||
| 703 | |||
| 704 | // fix for gecko engine | ||
| 705 | Element.cleanWhitespace(element); | ||
| 706 | |||
| 707 | options.draggables = []; | ||
| 708 | options.droppables = []; | ||
| 709 | |||
| 710 | // drop on empty handling | ||
| 711 | if(options.dropOnEmpty || options.tree) { | ||
| 712 | Droppables.add(element, options_for_tree); | ||
| 713 | options.droppables.push(element); | ||
| 714 | } | ||
| 715 | |||
| 716 | (options.elements || this.findElements(element, options) || []).each( function(e,i) { | ||
| 717 | var handle = options.handles ? $(options.handles[i]) : | ||
| 718 | (options.handle ? $(e).select('.' + options.handle)[0] : e); | ||
| 719 | options.draggables.push( | ||
| 720 | new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); | ||
| 721 | Droppables.add(e, options_for_droppable); | ||
| 722 | if(options.tree) e.treeNode = element; | ||
| 723 | options.droppables.push(e); | ||
| 724 | }); | ||
| 725 | |||
| 726 | if(options.tree) { | ||
| 727 | (Sortable.findTreeElements(element, options) || []).each( function(e) { | ||
| 728 | Droppables.add(e, options_for_tree); | ||
| 729 | e.treeNode = element; | ||
| 730 | options.droppables.push(e); | ||
| 731 | }); | ||
| 732 | } | ||
| 733 | |||
| 734 | // keep reference | ||
| 735 | this.sortables[element.id] = options; | ||
| 736 | |||
| 737 | // for onupdate | ||
| 738 | Draggables.addObserver(new SortableObserver(element, options.onUpdate)); | ||
| 739 | |||
| 740 | }, | ||
| 741 | |||
| 742 | // return all suitable-for-sortable elements in a guaranteed order | ||
| 743 | findElements: function(element, options) { | ||
| 744 | return Element.findChildren( | ||
| 745 | element, options.only, options.tree ? true : false, options.tag); | ||
| 746 | }, | ||
| 747 | |||
| 748 | findTreeElements: function(element, options) { | ||
| 749 | return Element.findChildren( | ||
| 750 | element, options.only, options.tree ? true : false, options.treeTag); | ||
| 751 | }, | ||
| 752 | |||
| 753 | onHover: function(element, dropon, overlap) { | ||
| 754 | if(Element.isParent(dropon, element)) return; | ||
| 755 | |||
| 756 | if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { | ||
| 757 | return; | ||
| 758 | } else if(overlap>0.5) { | ||
| 759 | Sortable.mark(dropon, 'before'); | ||
| 760 | if(dropon.previousSibling != element) { | ||
| 761 | var oldParentNode = element.parentNode; | ||
| 762 | element.style.visibility = "hidden"; // fix gecko rendering | ||
| 763 | dropon.parentNode.insertBefore(element, dropon); | ||
| 764 | if(dropon.parentNode!=oldParentNode) | ||
| 765 | Sortable.options(oldParentNode).onChange(element); | ||
| 766 | Sortable.options(dropon.parentNode).onChange(element); | ||
| 767 | } | ||
| 768 | } else { | ||
| 769 | Sortable.mark(dropon, 'after'); | ||
| 770 | var nextElement = dropon.nextSibling || null; | ||
| 771 | if(nextElement != element) { | ||
| 772 | var oldParentNode = element.parentNode; | ||
| 773 | element.style.visibility = "hidden"; // fix gecko rendering | ||
| 774 | dropon.parentNode.insertBefore(element, nextElement); | ||
| 775 | if(dropon.parentNode!=oldParentNode) | ||
| 776 | Sortable.options(oldParentNode).onChange(element); | ||
| 777 | Sortable.options(dropon.parentNode).onChange(element); | ||
| 778 | } | ||
| 779 | } | ||
| 780 | }, | ||
| 781 | |||
| 782 | onEmptyHover: function(element, dropon, overlap) { | ||
| 783 | var oldParentNode = element.parentNode; | ||
| 784 | var droponOptions = Sortable.options(dropon); | ||
| 785 | |||
| 786 | if(!Element.isParent(dropon, element)) { | ||
| 787 | var index; | ||
| 788 | |||
| 789 | var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); | ||
| 790 | var child = null; | ||
| 791 | |||
| 792 | if(children) { | ||
| 793 | var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); | ||
| 794 | |||
| 795 | for (index = 0; index < children.length; index += 1) { | ||
| 796 | if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { | ||
| 797 | offset -= Element.offsetSize (children[index], droponOptions.overlap); | ||
| 798 | } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { | ||
| 799 | child = index + 1 < children.length ? children[index + 1] : null; | ||
| 800 | break; | ||
| 801 | } else { | ||
| 802 | child = children[index]; | ||
| 803 | break; | ||
| 804 | } | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | dropon.insertBefore(element, child); | ||
| 809 | |||
| 810 | Sortable.options(oldParentNode).onChange(element); | ||
| 811 | droponOptions.onChange(element); | ||
| 812 | } | ||
| 813 | }, | ||
| 814 | |||
| 815 | unmark: function() { | ||
| 816 | if(Sortable._marker) Sortable._marker.hide(); | ||
| 817 | }, | ||
| 818 | |||
| 819 | mark: function(dropon, position) { | ||
| 820 | // mark on ghosting only | ||
| 821 | var sortable = Sortable.options(dropon.parentNode); | ||
| 822 | if(sortable && !sortable.ghosting) return; | ||
| 823 | |||
| 824 | if(!Sortable._marker) { | ||
| 825 | Sortable._marker = | ||
| 826 | ($('dropmarker') || Element.extend(document.createElement('DIV'))). | ||
| 827 | hide().addClassName('dropmarker').setStyle({position:'absolute'}); | ||
| 828 | document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); | ||
| 829 | } | ||
| 830 | var offsets = Position.cumulativeOffset(dropon); | ||
| 831 | Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); | ||
| 832 | |||
| 833 | if(position=='after') | ||
| 834 | if(sortable.overlap == 'horizontal') | ||
| 835 | Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); | ||
| 836 | else | ||
| 837 | Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); | ||
| 838 | |||
| 839 | Sortable._marker.show(); | ||
| 840 | }, | ||
| 841 | |||
| 842 | _tree: function(element, options, parent) { | ||
| 843 | var children = Sortable.findElements(element, options) || []; | ||
| 844 | |||
| 845 | for (var i = 0; i < children.length; ++i) { | ||
| 846 | var match = children[i].id.match(options.format); | ||
| 847 | |||
| 848 | if (!match) continue; | ||
| 849 | |||
| 850 | var child = { | ||
| 851 | id: encodeURIComponent(match ? match[1] : null), | ||
| 852 | element: element, | ||
| 853 | parent: parent, | ||
| 854 | children: [], | ||
| 855 | position: parent.children.length, | ||
| 856 | container: $(children[i]).down(options.treeTag) | ||
| 857 | }; | ||
| 858 | |||
| 859 | /* Get the element containing the children and recurse over it */ | ||
| 860 | if (child.container) | ||
| 861 | this._tree(child.container, options, child); | ||
| 862 | |||
| 863 | parent.children.push (child); | ||
| 864 | } | ||
| 865 | |||
| 866 | return parent; | ||
| 867 | }, | ||
| 868 | |||
| 869 | tree: function(element) { | ||
| 870 | element = $(element); | ||
| 871 | var sortableOptions = this.options(element); | ||
| 872 | var options = Object.extend({ | ||
| 873 | tag: sortableOptions.tag, | ||
| 874 | treeTag: sortableOptions.treeTag, | ||
| 875 | only: sortableOptions.only, | ||
| 876 | name: element.id, | ||
| 877 | format: sortableOptions.format | ||
| 878 | }, arguments[1] || { }); | ||
| 879 | |||
| 880 | var root = { | ||
| 881 | id: null, | ||
| 882 | parent: null, | ||
| 883 | children: [], | ||
| 884 | container: element, | ||
| 885 | position: 0 | ||
| 886 | }; | ||
| 887 | |||
| 888 | return Sortable._tree(element, options, root); | ||
| 889 | }, | ||
| 890 | |||
| 891 | /* Construct a [i] index for a particular node */ | ||
| 892 | _constructIndex: function(node) { | ||
| 893 | var index = ''; | ||
| 894 | do { | ||
| 895 | if (node.id) index = '[' + node.position + ']' + index; | ||
| 896 | } while ((node = node.parent) != null); | ||
| 897 | return index; | ||
| 898 | }, | ||
| 899 | |||
| 900 | sequence: function(element) { | ||
| 901 | element = $(element); | ||
| 902 | var options = Object.extend(this.options(element), arguments[1] || { }); | ||
| 903 | |||
| 904 | return $(this.findElements(element, options) || []).map( function(item) { | ||
| 905 | return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; | ||
| 906 | }); | ||
| 907 | }, | ||
| 908 | |||
| 909 | setSequence: function(element, new_sequence) { | ||
| 910 | element = $(element); | ||
| 911 | var options = Object.extend(this.options(element), arguments[2] || { }); | ||
| 912 | |||
| 913 | var nodeMap = { }; | ||
| 914 | this.findElements(element, options).each( function(n) { | ||
| 915 | if (n.id.match(options.format)) | ||
| 916 | nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; | ||
| 917 | n.parentNode.removeChild(n); | ||
| 918 | }); | ||
| 919 | |||
| 920 | new_sequence.each(function(ident) { | ||
| 921 | var n = nodeMap[ident]; | ||
| 922 | if (n) { | ||
| 923 | n[1].appendChild(n[0]); | ||
| 924 | delete nodeMap[ident]; | ||
| 925 | } | ||
| 926 | }); | ||
| 927 | }, | ||
| 928 | |||
| 929 | serialize: function(element) { | ||
| 930 | element = $(element); | ||
| 931 | var options = Object.extend(Sortable.options(element), arguments[1] || { }); | ||
| 932 | var name = encodeURIComponent( | ||
| 933 | (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); | ||
| 934 | |||
| 935 | if (options.tree) { | ||
| 936 | return Sortable.tree(element, arguments[1]).children.map( function (item) { | ||
| 937 | return [name + Sortable._constructIndex(item) + "[id]=" + | ||
| 938 | encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); | ||
| 939 | }).flatten().join('&'); | ||
| 940 | } else { | ||
| 941 | return Sortable.sequence(element, arguments[1]).map( function(item) { | ||
| 942 | return name + "[]=" + encodeURIComponent(item); | ||
| 943 | }).join('&'); | ||
| 944 | } | ||
| 945 | } | ||
| 946 | }; | ||
| 947 | |||
| 948 | // Returns true if child is contained within element | ||
| 949 | Element.isParent = function(child, element) { | ||
| 950 | if (!child.parentNode || child == element) return false; | ||
| 951 | if (child.parentNode == element) return true; | ||
| 952 | return Element.isParent(child.parentNode, element); | ||
| 953 | }; | ||
| 954 | |||
| 955 | Element.findChildren = function(element, only, recursive, tagName) { | ||
| 956 | if(!element.hasChildNodes()) return null; | ||
| 957 | tagName = tagName.toUpperCase(); | ||
| 958 | if(only) only = [only].flatten(); | ||
| 959 | var elements = []; | ||
| 960 | $A(element.childNodes).each( function(e) { | ||
| 961 | if(e.tagName && e.tagName.toUpperCase()==tagName && | ||
| 962 | (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) | ||
| 963 | elements.push(e); | ||
| 964 | if(recursive) { | ||
| 965 | var grandchildren = Element.findChildren(e, only, recursive, tagName); | ||
| 966 | if(grandchildren) elements.push(grandchildren); | ||
| 967 | } | ||
| 968 | }); | ||
| 969 | |||
| 970 | return (elements.length>0 ? elements.flatten() : []); | ||
| 971 | }; | ||
| 972 | |||
| 973 | Element.offsetSize = function (element, type) { | ||
| 974 | return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; | ||
| 975 | }; |
build/scripty/src/effects.js
(0 / 1130)
|   | |||
| 1 | // script.aculo.us effects.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // Contributors: | ||
| 5 | // Justin Palmer (http://encytemedia.com/) | ||
| 6 | // Mark Pilgrim (http://diveintomark.org/) | ||
| 7 | // Martin Bialasinki | ||
| 8 | // | ||
| 9 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 10 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 11 | |||
| 12 | // converts rgb() and #xxx to #xxxxxx format, | ||
| 13 | // returns self (or first argument) if not convertable | ||
| 14 | String.prototype.parseColor = function() { | ||
| 15 | var color = '#'; | ||
| 16 | if (this.slice(0,4) == 'rgb(') { | ||
| 17 | var cols = this.slice(4,this.length-1).split(','); | ||
| 18 | var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3); | ||
| 19 | } else { | ||
| 20 | if (this.slice(0,1) == '#') { | ||
| 21 | if (this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase(); | ||
| 22 | if (this.length==7) color = this.toLowerCase(); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | return (color.length==7 ? color : (arguments[0] || this)); | ||
| 26 | }; | ||
| 27 | |||
| 28 | /*--------------------------------------------------------------------------*/ | ||
| 29 | |||
| 30 | Element.collectTextNodes = function(element) { | ||
| 31 | return $A($(element).childNodes).collect( function(node) { | ||
| 32 | return (node.nodeType==3 ? node.nodeValue : | ||
| 33 | (node.hasChildNodes() ? Element.collectTextNodes(node) : '')); | ||
| 34 | }).flatten().join(''); | ||
| 35 | }; | ||
| 36 | |||
| 37 | Element.collectTextNodesIgnoreClass = function(element, className) { | ||
| 38 | return $A($(element).childNodes).collect( function(node) { | ||
| 39 | return (node.nodeType==3 ? node.nodeValue : | ||
| 40 | ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? | ||
| 41 | Element.collectTextNodesIgnoreClass(node, className) : '')); | ||
| 42 | }).flatten().join(''); | ||
| 43 | }; | ||
| 44 | |||
| 45 | Element.setContentZoom = function(element, percent) { | ||
| 46 | element = $(element); | ||
| 47 | element.setStyle({fontSize: (percent/100) + 'em'}); | ||
| 48 | if (Prototype.Browser.WebKit) window.scrollBy(0,0); | ||
| 49 | return element; | ||
| 50 | }; | ||
| 51 | |||
| 52 | Element.getInlineOpacity = function(element){ | ||
| 53 | return $(element).style.opacity || ''; | ||
| 54 | }; | ||
| 55 | |||
| 56 | Element.forceRerendering = function(element) { | ||
| 57 | try { | ||
| 58 | element = $(element); | ||
| 59 | var n = document.createTextNode(' '); | ||
| 60 | element.appendChild(n); | ||
| 61 | element.removeChild(n); | ||
| 62 | } catch(e) { } | ||
| 63 | }; | ||
| 64 | |||
| 65 | /*--------------------------------------------------------------------------*/ | ||
| 66 | |||
| 67 | var Effect = { | ||
| 68 | _elementDoesNotExistError: { | ||
| 69 | name: 'ElementDoesNotExistError', | ||
| 70 | message: 'The specified DOM element does not exist, but is required for this effect to operate' | ||
| 71 | }, | ||
| 72 | Transitions: { | ||
| 73 | linear: Prototype.K, | ||
| 74 | sinoidal: function(pos) { | ||
| 75 | return (-Math.cos(pos*Math.PI)/2) + .5; | ||
| 76 | }, | ||
| 77 | reverse: function(pos) { | ||
| 78 | return 1-pos; | ||
| 79 | }, | ||
| 80 | flicker: function(pos) { | ||
| 81 | var pos = ((-Math.cos(pos*Math.PI)/4) + .75) + Math.random()/4; | ||
| 82 | return pos > 1 ? 1 : pos; | ||
| 83 | }, | ||
| 84 | wobble: function(pos) { | ||
| 85 | return (-Math.cos(pos*Math.PI*(9*pos))/2) + .5; | ||
| 86 | }, | ||
| 87 | pulse: function(pos, pulses) { | ||
| 88 | return (-Math.cos((pos*((pulses||5)-.5)*2)*Math.PI)/2) + .5; | ||
| 89 | }, | ||
| 90 | spring: function(pos) { | ||
| 91 | return 1 - (Math.cos(pos * 4.5 * Math.PI) * Math.exp(-pos * 6)); | ||
| 92 | }, | ||
| 93 | none: function(pos) { | ||
| 94 | return 0; | ||
| 95 | }, | ||
| 96 | full: function(pos) { | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | }, | ||
| 100 | DefaultOptions: { | ||
| 101 | duration: 1.0, // seconds | ||
| 102 | fps: 100, // 100= assume 66fps max. | ||
| 103 | sync: false, // true for combining | ||
| 104 | from: 0.0, | ||
| 105 | to: 1.0, | ||
| 106 | delay: 0.0, | ||
| 107 | queue: 'parallel' | ||
| 108 | }, | ||
| 109 | tagifyText: function(element) { | ||
| 110 | var tagifyStyle = 'position:relative'; | ||
| 111 | if (Prototype.Browser.IE) tagifyStyle += ';zoom:1'; | ||
| 112 | |||
| 113 | element = $(element); | ||
| 114 | $A(element.childNodes).each( function(child) { | ||
| 115 | if (child.nodeType==3) { | ||
| 116 | child.nodeValue.toArray().each( function(character) { | ||
| 117 | element.insertBefore( | ||
| 118 | new Element('span', {style: tagifyStyle}).update( | ||
| 119 | character == ' ' ? String.fromCharCode(160) : character), | ||
| 120 | child); | ||
| 121 | }); | ||
| 122 | Element.remove(child); | ||
| 123 | } | ||
| 124 | }); | ||
| 125 | }, | ||
| 126 | multiple: function(element, effect) { | ||
| 127 | var elements; | ||
| 128 | if (((typeof element == 'object') || | ||
| 129 | Object.isFunction(element)) && | ||
| 130 | (element.length)) | ||
| 131 | elements = element; | ||
| 132 | else | ||
| 133 | elements = $(element).childNodes; | ||
| 134 | |||
| 135 | var options = Object.extend({ | ||
| 136 | speed: 0.1, | ||
| 137 | delay: 0.0 | ||
| 138 | }, arguments[2] || { }); | ||
| 139 | var masterDelay = options.delay; | ||
| 140 | |||
| 141 | $A(elements).each( function(element, index) { | ||
| 142 | new effect(element, Object.extend(options, { delay: index * options.speed + masterDelay })); | ||
| 143 | }); | ||
| 144 | }, | ||
| 145 | PAIRS: { | ||
| 146 | 'slide': ['SlideDown','SlideUp'], | ||
| 147 | 'blind': ['BlindDown','BlindUp'], | ||
| 148 | 'appear': ['Appear','Fade'] | ||
| 149 | }, | ||
| 150 | toggle: function(element, effect) { | ||
| 151 | element = $(element); | ||
| 152 | effect = (effect || 'appear').toLowerCase(); | ||
| 153 | var options = Object.extend({ | ||
| 154 | queue: { position:'end', scope:(element.id || 'global'), limit: 1 } | ||
| 155 | }, arguments[2] || { }); | ||
| 156 | Effect[element.visible() ? | ||
| 157 | Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options); | ||
| 158 | } | ||
| 159 | }; | ||
| 160 | |||
| 161 | Effect.DefaultOptions.transition = Effect.Transitions.sinoidal; | ||
| 162 | |||
| 163 | /* ------------- core effects ------------- */ | ||
| 164 | |||
| 165 | Effect.ScopedQueue = Class.create(Enumerable, { | ||
| 166 | initialize: function() { | ||
| 167 | this.effects = []; | ||
| 168 | this.interval = null; | ||
| 169 | }, | ||
| 170 | _each: function(iterator) { | ||
| 171 | this.effects._each(iterator); | ||
| 172 | }, | ||
| 173 | add: function(effect) { | ||
| 174 | var timestamp = new Date().getTime(); | ||
| 175 | |||
| 176 | var position = Object.isString(effect.options.queue) ? | ||
| 177 | effect.options.queue : effect.options.queue.position; | ||
| 178 | |||
| 179 | switch(position) { | ||
| 180 | case 'front': | ||
| 181 | // move unstarted effects after this effect | ||
| 182 | this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) { | ||
| 183 | e.startOn += effect.finishOn; | ||
| 184 | e.finishOn += effect.finishOn; | ||
| 185 | }); | ||
| 186 | break; | ||
| 187 | case 'with-last': | ||
| 188 | timestamp = this.effects.pluck('startOn').max() || timestamp; | ||
| 189 | break; | ||
| 190 | case 'end': | ||
| 191 | // start effect after last queued effect has finished | ||
| 192 | timestamp = this.effects.pluck('finishOn').max() || timestamp; | ||
| 193 | break; | ||
| 194 | } | ||
| 195 | |||
| 196 | effect.startOn += timestamp; | ||
| 197 | effect.finishOn += timestamp; | ||
| 198 | |||
| 199 | if (!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit)) | ||
| 200 | this.effects.push(effect); | ||
| 201 | |||
| 202 | if (!this.interval) | ||
| 203 | this.interval = setInterval(this.loop.bind(this), 15); | ||
| 204 | }, | ||
| 205 | remove: function(effect) { | ||
| 206 | this.effects = this.effects.reject(function(e) { return e==effect }); | ||
| 207 | if (this.effects.length == 0) { | ||
| 208 | clearInterval(this.interval); | ||
| 209 | this.interval = null; | ||
| 210 | } | ||
| 211 | }, | ||
| 212 | loop: function() { | ||
| 213 | var timePos = new Date().getTime(); | ||
| 214 | for(var i=0, len=this.effects.length;i<len;i++) | ||
| 215 | this.effects[i] && this.effects[i].loop(timePos); | ||
| 216 | } | ||
| 217 | }); | ||
| 218 | |||
| 219 | Effect.Queues = { | ||
| 220 | instances: $H(), | ||
| 221 | get: function(queueName) { | ||
| 222 | if (!Object.isString(queueName)) return queueName; | ||
| 223 | |||
| 224 | return this.instances.get(queueName) || | ||
| 225 | this.instances.set(queueName, new Effect.ScopedQueue()); | ||
| 226 | } | ||
| 227 | }; | ||
| 228 | Effect.Queue = Effect.Queues.get('global'); | ||
| 229 | |||
| 230 | Effect.Base = Class.create({ | ||
| 231 | position: null, | ||
| 232 | start: function(options) { | ||
| 233 | function codeForEvent(options,eventName){ | ||
| 234 | return ( | ||
| 235 | (options[eventName+'Internal'] ? 'this.options.'+eventName+'Internal(this);' : '') + | ||
| 236 | (options[eventName] ? 'this.options.'+eventName+'(this);' : '') | ||
| 237 | ); | ||
| 238 | } | ||
| 239 | if (options && options.transition === false) options.transition = Effect.Transitions.linear; | ||
| 240 | this.options = Object.extend(Object.extend({ },Effect.DefaultOptions), options || { }); | ||
| 241 | this.currentFrame = 0; | ||
| 242 | this.state = 'idle'; | ||
| 243 | this.startOn = this.options.delay*1000; | ||
| 244 | this.finishOn = this.startOn+(this.options.duration*1000); | ||
| 245 | this.fromToDelta = this.options.to-this.options.from; | ||
| 246 | this.totalTime = this.finishOn-this.startOn; | ||
| 247 | this.totalFrames = this.options.fps*this.options.duration; | ||
| 248 | |||
| 249 | this.render = (function() { | ||
| 250 | function dispatch(effect, eventName) { | ||
| 251 | if (effect.options[eventName + 'Internal']) | ||
| 252 | effect.options[eventName + 'Internal'](effect); | ||
| 253 | if (effect.options[eventName]) | ||
| 254 | effect.options[eventName](effect); | ||
| 255 | } | ||
| 256 | |||
| 257 | return function(pos) { | ||
| 258 | if (this.state === "idle") { | ||
| 259 | this.state = "running"; | ||
| 260 | dispatch(this, 'beforeSetup'); | ||
| 261 | if (this.setup) this.setup(); | ||
| 262 | dispatch(this, 'afterSetup'); | ||
| 263 | } | ||
| 264 | if (this.state === "running") { | ||
| 265 | pos = (this.options.transition(pos) * this.fromToDelta) + this.options.from; | ||
| 266 | this.position = pos; | ||
| 267 | dispatch(this, 'beforeUpdate'); | ||
| 268 | if (this.update) this.update(pos); | ||
| 269 | dispatch(this, 'afterUpdate'); | ||
| 270 | } | ||
| 271 | }; | ||
| 272 | })(); | ||
| 273 | |||
| 274 | this.event('beforeStart'); | ||
| 275 | if (!this.options.sync) | ||
| 276 | Effect.Queues.get(Object.isString(this.options.queue) ? | ||
| 277 | 'global' : this.options.queue.scope).add(this); | ||
| 278 | }, | ||
| 279 | loop: function(timePos) { | ||
| 280 | if (timePos >= this.startOn) { | ||
| 281 | if (timePos >= this.finishOn) { | ||
| 282 | this.render(1.0); | ||
| 283 | this.cancel(); | ||
| 284 | this.event('beforeFinish'); | ||
| 285 | if (this.finish) this.finish(); | ||
| 286 | this.event('afterFinish'); | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | var pos = (timePos - this.startOn) / this.totalTime, | ||
| 290 | frame = (pos * this.totalFrames).round(); | ||
| 291 | if (frame > this.currentFrame) { | ||
| 292 | this.render(pos); | ||
| 293 | this.currentFrame = frame; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | }, | ||
| 297 | cancel: function() { | ||
| 298 | if (!this.options.sync) | ||
| 299 | Effect.Queues.get(Object.isString(this.options.queue) ? | ||
| 300 | 'global' : this.options.queue.scope).remove(this); | ||
| 301 | this.state = 'finished'; | ||
| 302 | }, | ||
| 303 | event: function(eventName) { | ||
| 304 | if (this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this); | ||
| 305 | if (this.options[eventName]) this.options[eventName](this); | ||
| 306 | }, | ||
| 307 | inspect: function() { | ||
| 308 | var data = $H(); | ||
| 309 | for(property in this) | ||
| 310 | if (!Object.isFunction(this[property])) data.set(property, this[property]); | ||
| 311 | return '#<Effect:' + data.inspect() + ',options:' + $H(this.options).inspect() + '>'; | ||
| 312 | } | ||
| 313 | }); | ||
| 314 | |||
| 315 | Effect.Parallel = Class.create(Effect.Base, { | ||
| 316 | initialize: function(effects) { | ||
| 317 | this.effects = effects || []; | ||
| 318 | this.start(arguments[1]); | ||
| 319 | }, | ||
| 320 | update: function(position) { | ||
| 321 | this.effects.invoke('render', position); | ||
| 322 | }, | ||
| 323 | finish: function(position) { | ||
| 324 | this.effects.each( function(effect) { | ||
| 325 | effect.render(1.0); | ||
| 326 | effect.cancel(); | ||
| 327 | effect.event('beforeFinish'); | ||
| 328 | if (effect.finish) effect.finish(position); | ||
| 329 | effect.event('afterFinish'); | ||
| 330 | }); | ||
| 331 | } | ||
| 332 | }); | ||
| 333 | |||
| 334 | Effect.Tween = Class.create(Effect.Base, { | ||
| 335 | initialize: function(object, from, to) { | ||
| 336 | object = Object.isString(object) ? $(object) : object; | ||
| 337 | var args = $A(arguments), method = args.last(), | ||
| 338 | options = args.length == 5 ? args[3] : null; | ||
| 339 | this.method = Object.isFunction(method) ? method.bind(object) : | ||
| 340 | Object.isFunction(object[method]) ? object[method].bind(object) : | ||
| 341 | function(value) { object[method] = value }; | ||
| 342 | this.start(Object.extend({ from: from, to: to }, options || { })); | ||
| 343 | }, | ||
| 344 | update: function(position) { | ||
| 345 | this.method(position); | ||
| 346 | } | ||
| 347 | }); | ||
| 348 | |||
| 349 | Effect.Event = Class.create(Effect.Base, { | ||
| 350 | initialize: function() { | ||
| 351 | this.start(Object.extend({ duration: 0 }, arguments[0] || { })); | ||
| 352 | }, | ||
| 353 | update: Prototype.emptyFunction | ||
| 354 | }); | ||
| 355 | |||
| 356 | Effect.Opacity = Class.create(Effect.Base, { | ||
| 357 | initialize: function(element) { | ||
| 358 | this.element = $(element); | ||
| 359 | if (!this.element) throw(Effect._elementDoesNotExistError); | ||
| 360 | // make this work on IE on elements without 'layout' | ||
| 361 | if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) | ||
| 362 | this.element.setStyle({zoom: 1}); | ||
| 363 | var options = Object.extend({ | ||
| 364 | from: this.element.getOpacity() || 0.0, | ||
| 365 | to: 1.0 | ||
| 366 | }, arguments[1] || { }); | ||
| 367 | this.start(options); | ||
| 368 | }, | ||
| 369 | update: function(position) { | ||
| 370 | this.element.setOpacity(position); | ||
| 371 | } | ||
| 372 | }); | ||
| 373 | |||
| 374 | Effect.Move = Class.create(Effect.Base, { | ||
| 375 | initialize: function(element) { | ||
| 376 | this.element = $(element); | ||
| 377 | if (!this.element) throw(Effect._elementDoesNotExistError); | ||
| 378 | var options = Object.extend({ | ||
| 379 | x: 0, | ||
| 380 | y: 0, | ||
| 381 | mode: 'relative' | ||
| 382 | }, arguments[1] || { }); | ||
| 383 | this.start(options); | ||
| 384 | }, | ||
| 385 | setup: function() { | ||
| 386 | this.element.makePositioned(); | ||
| 387 | this.originalLeft = parseFloat(this.element.getStyle('left') || '0'); | ||
| 388 | this.originalTop = parseFloat(this.element.getStyle('top') || '0'); | ||
| 389 | if (this.options.mode == 'absolute') { | ||
| 390 | this.options.x = this.options.x - this.originalLeft; | ||
| 391 | this.options.y = this.options.y - this.originalTop; | ||
| 392 | } | ||
| 393 | }, | ||
| 394 | update: function(position) { | ||
| 395 | this.element.setStyle({ | ||
| 396 | left: (this.options.x * position + this.originalLeft).round() + 'px', | ||
| 397 | top: (this.options.y * position + this.originalTop).round() + 'px' | ||
| 398 | }); | ||
| 399 | } | ||
| 400 | }); | ||
| 401 | |||
| 402 | // for backwards compatibility | ||
| 403 | Effect.MoveBy = function(element, toTop, toLeft) { | ||
| 404 | return new Effect.Move(element, | ||
| 405 | Object.extend({ x: toLeft, y: toTop }, arguments[3] || { })); | ||
| 406 | }; | ||
| 407 | |||
| 408 | Effect.Scale = Class.create(Effect.Base, { | ||
| 409 | initialize: function(element, percent) { | ||
| 410 | this.element = $(element); | ||
| 411 | if (!this.element) throw(Effect._elementDoesNotExistError); | ||
| 412 | var options = Object.extend({ | ||
| 413 | scaleX: true, | ||
| 414 | scaleY: true, | ||
| 415 | scaleContent: true, | ||
| 416 | scaleFromCenter: false, | ||
| 417 | scaleMode: 'box', // 'box' or 'contents' or { } with provided values | ||
| 418 | scaleFrom: 100.0, | ||
| 419 | scaleTo: percent | ||
| 420 | }, arguments[2] || { }); | ||
| 421 | this.start(options); | ||
| 422 | }, | ||
| 423 | setup: function() { | ||
| 424 | this.restoreAfterFinish = this.options.restoreAfterFinish || false; | ||
| 425 | this.elementPositioning = this.element.getStyle('position'); | ||
| 426 | |||
| 427 | this.originalStyle = { }; | ||
| 428 | ['top','left','width','height','fontSize'].each( function(k) { | ||
| 429 | this.originalStyle[k] = this.element.style[k]; | ||
| 430 | }.bind(this)); | ||
| 431 | |||
| 432 | this.originalTop = this.element.offsetTop; | ||
| 433 | this.originalLeft = this.element.offsetLeft; | ||
| 434 | |||
| 435 | var fontSize = this.element.getStyle('font-size') || '100%'; | ||
| 436 | ['em','px','%','pt'].each( function(fontSizeType) { | ||
| 437 | if (fontSize.indexOf(fontSizeType)>0) { | ||
| 438 | this.fontSize = parseFloat(fontSize); | ||
| 439 | this.fontSizeType = fontSizeType; | ||
| 440 | } | ||
| 441 | }.bind(this)); | ||
| 442 | |||
| 443 | this.factor = (this.options.scaleTo - this.options.scaleFrom)/100; | ||
| 444 | |||
| 445 | this.dims = null; | ||
| 446 | if (this.options.scaleMode=='box') | ||
| 447 | this.dims = [this.element.offsetHeight, this.element.offsetWidth]; | ||
| 448 | if (/^content/.test(this.options.scaleMode)) | ||
| 449 | this.dims = [this.element.scrollHeight, this.element.scrollWidth]; | ||
| 450 | if (!this.dims) | ||
| 451 | this.dims = [this.options.scaleMode.originalHeight, | ||
| 452 | this.options.scaleMode.originalWidth]; | ||
| 453 | }, | ||
| 454 | update: function(position) { | ||
| 455 | var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position); | ||
| 456 | if (this.options.scaleContent && this.fontSize) | ||
| 457 | this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType }); | ||
| 458 | this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale); | ||
| 459 | }, | ||
| 460 | finish: function(position) { | ||
| 461 | if (this.restoreAfterFinish) this.element.setStyle(this.originalStyle); | ||
| 462 | }, | ||
| 463 | setDimensions: function(height, width) { | ||
| 464 | var d = { }; | ||
| 465 | if (this.options.scaleX) d.width = width.round() + 'px'; | ||
| 466 | if (this.options.scaleY) d.height = height.round() + 'px'; | ||
| 467 | if (this.options.scaleFromCenter) { | ||
| 468 | var topd = (height - this.dims[0])/2; | ||
| 469 | var leftd = (width - this.dims[1])/2; | ||
| 470 | if (this.elementPositioning == 'absolute') { | ||
| 471 | if (this.options.scaleY) d.top = this.originalTop-topd + 'px'; | ||
| 472 | if (this.options.scaleX) d.left = this.originalLeft-leftd + 'px'; | ||
| 473 | } else { | ||
| 474 | if (this.options.scaleY) d.top = -topd + 'px'; | ||
| 475 | if (this.options.scaleX) d.left = -leftd + 'px'; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | this.element.setStyle(d); | ||
| 479 | } | ||
| 480 | }); | ||
| 481 | |||
| 482 | Effect.Highlight = Class.create(Effect.Base, { | ||
| 483 | initialize: function(element) { | ||
| 484 | this.element = $(element); | ||
| 485 | if (!this.element) throw(Effect._elementDoesNotExistError); | ||
| 486 | var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || { }); | ||
| 487 | this.start(options); | ||
| 488 | }, | ||
| 489 | setup: function() { | ||
| 490 | // Prevent executing on elements not in the layout flow | ||
| 491 | if (this.element.getStyle('display')=='none') { this.cancel(); return; } | ||
| 492 | // Disable background image during the effect | ||
| 493 | this.oldStyle = { }; | ||
| 494 | if (!this.options.keepBackgroundImage) { | ||
| 495 | this.oldStyle.backgroundImage = this.element.getStyle('background-image'); | ||
| 496 | this.element.setStyle({backgroundImage: 'none'}); | ||
| 497 | } | ||
| 498 | if (!this.options.endcolor) | ||
| 499 | this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff'); | ||
| 500 | if (!this.options.restorecolor) | ||
| 501 | this.options.restorecolor = this.element.getStyle('background-color'); | ||
| 502 | // init color calculations | ||
| 503 | this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this)); | ||
| 504 | this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this)); | ||
| 505 | }, | ||
| 506 | update: function(position) { | ||
| 507 | this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){ | ||
| 508 | return m+((this._base[i]+(this._delta[i]*position)).round().toColorPart()); }.bind(this)) }); | ||
| 509 | }, | ||
| 510 | finish: function() { | ||
| 511 | this.element.setStyle(Object.extend(this.oldStyle, { | ||
| 512 | backgroundColor: this.options.restorecolor | ||
| 513 | })); | ||
| 514 | } | ||
| 515 | }); | ||
| 516 | |||
| 517 | Effect.ScrollTo = function(element) { | ||
| 518 | var options = arguments[1] || { }, | ||
| 519 | scrollOffsets = document.viewport.getScrollOffsets(), | ||
| 520 | elementOffsets = $(element).cumulativeOffset(); | ||
| 521 | |||
| 522 | if (options.offset) elementOffsets[1] += options.offset; | ||
| 523 | |||
| 524 | return new Effect.Tween(null, | ||
| 525 | scrollOffsets.top, | ||
| 526 | elementOffsets[1], | ||
| 527 | options, | ||
| 528 | function(p){ scrollTo(scrollOffsets.left, p.round()); } | ||
| 529 | ); | ||
| 530 | }; | ||
| 531 | |||
| 532 | /* ------------- combination effects ------------- */ | ||
| 533 | |||
| 534 | Effect.Fade = function(element) { | ||
| 535 | element = $(element); | ||
| 536 | var oldOpacity = element.getInlineOpacity(); | ||
| 537 | var options = Object.extend({ | ||
| 538 | from: element.getOpacity() || 1.0, | ||
| 539 | to: 0.0, | ||
| 540 | afterFinishInternal: function(effect) { | ||
| 541 | if (effect.options.to!=0) return; | ||
| 542 | effect.element.hide().setStyle({opacity: oldOpacity}); | ||
| 543 | } | ||
| 544 | }, arguments[1] || { }); | ||
| 545 | return new Effect.Opacity(element,options); | ||
| 546 | }; | ||
| 547 | |||
| 548 | Effect.Appear = function(element) { | ||
| 549 | element = $(element); | ||
| 550 | var options = Object.extend({ | ||
| 551 | from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0), | ||
| 552 | to: 1.0, | ||
| 553 | // force Safari to render floated elements properly | ||
| 554 | afterFinishInternal: function(effect) { | ||
| 555 | effect.element.forceRerendering(); | ||
| 556 | }, | ||
| 557 | beforeSetup: function(effect) { | ||
| 558 | effect.element.setOpacity(effect.options.from).show(); | ||
| 559 | }}, arguments[1] || { }); | ||
| 560 | return new Effect.Opacity(element,options); | ||
| 561 | }; | ||
| 562 | |||
| 563 | Effect.Puff = function(element) { | ||
| 564 | element = $(element); | ||
| 565 | var oldStyle = { | ||
| 566 | opacity: element.getInlineOpacity(), | ||
| 567 | position: element.getStyle('position'), | ||
| 568 | top: element.style.top, | ||
| 569 | left: element.style.left, | ||
| 570 | width: element.style.width, | ||
| 571 | height: element.style.height | ||
| 572 | }; | ||
| 573 | return new Effect.Parallel( | ||
| 574 | [ new Effect.Scale(element, 200, | ||
| 575 | { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }), | ||
| 576 | new Effect.Opacity(element, { sync: true, to: 0.0 } ) ], | ||
| 577 | Object.extend({ duration: 1.0, | ||
| 578 | beforeSetupInternal: function(effect) { | ||
| 579 | Position.absolutize(effect.effects[0].element); | ||
| 580 | }, | ||
| 581 | afterFinishInternal: function(effect) { | ||
| 582 | effect.effects[0].element.hide().setStyle(oldStyle); } | ||
| 583 | }, arguments[1] || { }) | ||
| 584 | ); | ||
| 585 | }; | ||
| 586 | |||
| 587 | Effect.BlindUp = function(element) { | ||
| 588 | element = $(element); | ||
| 589 | element.makeClipping(); | ||
| 590 | return new Effect.Scale(element, 0, | ||
| 591 | Object.extend({ scaleContent: false, | ||
| 592 | scaleX: false, | ||
| 593 | restoreAfterFinish: true, | ||
| 594 | afterFinishInternal: function(effect) { | ||
| 595 | effect.element.hide().undoClipping(); | ||
| 596 | } | ||
| 597 | }, arguments[1] || { }) | ||
| 598 | ); | ||
| 599 | }; | ||
| 600 | |||
| 601 | Effect.BlindDown = function(element) { | ||
| 602 | element = $(element); | ||
| 603 | var elementDimensions = element.getDimensions(); | ||
| 604 | return new Effect.Scale(element, 100, Object.extend({ | ||
| 605 | scaleContent: false, | ||
| 606 | scaleX: false, | ||
| 607 | scaleFrom: 0, | ||
| 608 | scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | ||
| 609 | restoreAfterFinish: true, | ||
| 610 | afterSetup: function(effect) { | ||
| 611 | effect.element.makeClipping().setStyle({height: '0px'}).show(); | ||
| 612 | }, | ||
| 613 | afterFinishInternal: function(effect) { | ||
| 614 | effect.element.undoClipping(); | ||
| 615 | } | ||
| 616 | }, arguments[1] || { })); | ||
| 617 | }; | ||
| 618 | |||
| 619 | Effect.SwitchOff = function(element) { | ||
| 620 | element = $(element); | ||
| 621 | var oldOpacity = element.getInlineOpacity(); | ||
| 622 | return new Effect.Appear(element, Object.extend({ | ||
| 623 | duration: 0.4, | ||
| 624 | from: 0, | ||
| 625 | transition: Effect.Transitions.flicker, | ||
| 626 | afterFinishInternal: function(effect) { | ||
| 627 | new Effect.Scale(effect.element, 1, { | ||
| 628 | duration: 0.3, scaleFromCenter: true, | ||
| 629 | scaleX: false, scaleContent: false, restoreAfterFinish: true, | ||
| 630 | beforeSetup: function(effect) { | ||
| 631 | effect.element.makePositioned().makeClipping(); | ||
| 632 | }, | ||
| 633 | afterFinishInternal: function(effect) { | ||
| 634 | effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity}); | ||
| 635 | } | ||
| 636 | }); | ||
| 637 | } | ||
| 638 | }, arguments[1] || { })); | ||
| 639 | }; | ||
| 640 | |||
| 641 | Effect.DropOut = function(element) { | ||
| 642 | element = $(element); | ||
| 643 | var oldStyle = { | ||
| 644 | top: element.getStyle('top'), | ||
| 645 | left: element.getStyle('left'), | ||
| 646 | opacity: element.getInlineOpacity() }; | ||
| 647 | return new Effect.Parallel( | ||
| 648 | [ new Effect.Move(element, {x: 0, y: 100, sync: true }), | ||
| 649 | new Effect.Opacity(element, { sync: true, to: 0.0 }) ], | ||
| 650 | Object.extend( | ||
| 651 | { duration: 0.5, | ||
| 652 | beforeSetup: function(effect) { | ||
| 653 | effect.effects[0].element.makePositioned(); | ||
| 654 | }, | ||
| 655 | afterFinishInternal: function(effect) { | ||
| 656 | effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle); | ||
| 657 | } | ||
| 658 | }, arguments[1] || { })); | ||
| 659 | }; | ||
| 660 | |||
| 661 | Effect.Shake = function(element) { | ||
| 662 | element = $(element); | ||
| 663 | var options = Object.extend({ | ||
| 664 | distance: 20, | ||
| 665 | duration: 0.5 | ||
| 666 | }, arguments[1] || {}); | ||
| 667 | var distance = parseFloat(options.distance); | ||
| 668 | var split = parseFloat(options.duration) / 10.0; | ||
| 669 | var oldStyle = { | ||
| 670 | top: element.getStyle('top'), | ||
| 671 | left: element.getStyle('left') }; | ||
| 672 | return new Effect.Move(element, | ||
| 673 | { x: distance, y: 0, duration: split, afterFinishInternal: function(effect) { | ||
| 674 | new Effect.Move(effect.element, | ||
| 675 | { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | ||
| 676 | new Effect.Move(effect.element, | ||
| 677 | { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | ||
| 678 | new Effect.Move(effect.element, | ||
| 679 | { x: -distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | ||
| 680 | new Effect.Move(effect.element, | ||
| 681 | { x: distance*2, y: 0, duration: split*2, afterFinishInternal: function(effect) { | ||
| 682 | new Effect.Move(effect.element, | ||
| 683 | { x: -distance, y: 0, duration: split, afterFinishInternal: function(effect) { | ||
| 684 | effect.element.undoPositioned().setStyle(oldStyle); | ||
| 685 | }}); }}); }}); }}); }}); }}); | ||
| 686 | }; | ||
| 687 | |||
| 688 | Effect.SlideDown = function(element) { | ||
| 689 | element = $(element).cleanWhitespace(); | ||
| 690 | // SlideDown need to have the content of the element wrapped in a container element with fixed height! | ||
| 691 | var oldInnerBottom = element.down().getStyle('bottom'); | ||
| 692 | var elementDimensions = element.getDimensions(); | ||
| 693 | return new Effect.Scale(element, 100, Object.extend({ | ||
| 694 | scaleContent: false, | ||
| 695 | scaleX: false, | ||
| 696 | scaleFrom: window.opera ? 0 : 1, | ||
| 697 | scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | ||
| 698 | restoreAfterFinish: true, | ||
| 699 | afterSetup: function(effect) { | ||
| 700 | effect.element.makePositioned(); | ||
| 701 | effect.element.down().makePositioned(); | ||
| 702 | if (window.opera) effect.element.setStyle({top: ''}); | ||
| 703 | effect.element.makeClipping().setStyle({height: '0px'}).show(); | ||
| 704 | }, | ||
| 705 | afterUpdateInternal: function(effect) { | ||
| 706 | effect.element.down().setStyle({bottom: | ||
| 707 | (effect.dims[0] - effect.element.clientHeight) + 'px' }); | ||
| 708 | }, | ||
| 709 | afterFinishInternal: function(effect) { | ||
| 710 | effect.element.undoClipping().undoPositioned(); | ||
| 711 | effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); } | ||
| 712 | }, arguments[1] || { }) | ||
| 713 | ); | ||
| 714 | }; | ||
| 715 | |||
| 716 | Effect.SlideUp = function(element) { | ||
| 717 | element = $(element).cleanWhitespace(); | ||
| 718 | var oldInnerBottom = element.down().getStyle('bottom'); | ||
| 719 | var elementDimensions = element.getDimensions(); | ||
| 720 | return new Effect.Scale(element, window.opera ? 0 : 1, | ||
| 721 | Object.extend({ scaleContent: false, | ||
| 722 | scaleX: false, | ||
| 723 | scaleMode: 'box', | ||
| 724 | scaleFrom: 100, | ||
| 725 | scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, | ||
| 726 | restoreAfterFinish: true, | ||
| 727 | afterSetup: function(effect) { | ||
| 728 | effect.element.makePositioned(); | ||
| 729 | effect.element.down().makePositioned(); | ||
| 730 | if (window.opera) effect.element.setStyle({top: ''}); | ||
| 731 | effect.element.makeClipping().show(); | ||
| 732 | }, | ||
| 733 | afterUpdateInternal: function(effect) { | ||
| 734 | effect.element.down().setStyle({bottom: | ||
| 735 | (effect.dims[0] - effect.element.clientHeight) + 'px' }); | ||
| 736 | }, | ||
| 737 | afterFinishInternal: function(effect) { | ||
| 738 | effect.element.hide().undoClipping().undoPositioned(); | ||
| 739 | effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); | ||
| 740 | } | ||
| 741 | }, arguments[1] || { }) | ||
| 742 | ); | ||
| 743 | }; | ||
| 744 | |||
| 745 | // Bug in opera makes the TD containing this element expand for a instance after finish | ||
| 746 | Effect.Squish = function(element) { | ||
| 747 | return new Effect.Scale(element, window.opera ? 1 : 0, { | ||
| 748 | restoreAfterFinish: true, | ||
| 749 | beforeSetup: function(effect) { | ||
| 750 | effect.element.makeClipping(); | ||
| 751 | }, | ||
| 752 | afterFinishInternal: function(effect) { | ||
| 753 | effect.element.hide().undoClipping(); | ||
| 754 | } | ||
| 755 | }); | ||
| 756 | }; | ||
| 757 | |||
| 758 | Effect.Grow = function(element) { | ||
| 759 | element = $(element); | ||
| 760 | var options = Object.extend({ | ||
| 761 | direction: 'center', | ||
| 762 | moveTransition: Effect.Transitions.sinoidal, | ||
| 763 | scaleTransition: Effect.Transitions.sinoidal, | ||
| 764 | opacityTransition: Effect.Transitions.full | ||
| 765 | }, arguments[1] || { }); | ||
| 766 | var oldStyle = { | ||
| 767 | top: element.style.top, | ||
| 768 | left: element.style.left, | ||
| 769 | height: element.style.height, | ||
| 770 | width: element.style.width, | ||
| 771 | opacity: element.getInlineOpacity() }; | ||
| 772 | |||
| 773 | var dims = element.getDimensions(); | ||
| 774 | var initialMoveX, initialMoveY; | ||
| 775 | var moveX, moveY; | ||
| 776 | |||
| 777 | switch (options.direction) { | ||
| 778 | case 'top-left': | ||
| 779 | initialMoveX = initialMoveY = moveX = moveY = 0; | ||
| 780 | break; | ||
| 781 | case 'top-right': | ||
| 782 | initialMoveX = dims.width; | ||
| 783 | initialMoveY = moveY = 0; | ||
| 784 | moveX = -dims.width; | ||
| 785 | break; | ||
| 786 | case 'bottom-left': | ||
| 787 | initialMoveX = moveX = 0; | ||
| 788 | initialMoveY = dims.height; | ||
| 789 | moveY = -dims.height; | ||
| 790 | break; | ||
| 791 | case 'bottom-right': | ||
| 792 | initialMoveX = dims.width; | ||
| 793 | initialMoveY = dims.height; | ||
| 794 | moveX = -dims.width; | ||
| 795 | moveY = -dims.height; | ||
| 796 | break; | ||
| 797 | case 'center': | ||
| 798 | initialMoveX = dims.width / 2; | ||
| 799 | initialMoveY = dims.height / 2; | ||
| 800 | moveX = -dims.width / 2; | ||
| 801 | moveY = -dims.height / 2; | ||
| 802 | break; | ||
| 803 | } | ||
| 804 | |||
| 805 | return new Effect.Move(element, { | ||
| 806 | x: initialMoveX, | ||
| 807 | y: initialMoveY, | ||
| 808 | duration: 0.01, | ||
| 809 | beforeSetup: function(effect) { | ||
| 810 | effect.element.hide().makeClipping().makePositioned(); | ||
| 811 | }, | ||
| 812 | afterFinishInternal: function(effect) { | ||
| 813 | new Effect.Parallel( | ||
| 814 | [ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }), | ||
| 815 | new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }), | ||
| 816 | new Effect.Scale(effect.element, 100, { | ||
| 817 | scaleMode: { originalHeight: dims.height, originalWidth: dims.width }, | ||
| 818 | sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true}) | ||
| 819 | ], Object.extend({ | ||
| 820 | beforeSetup: function(effect) { | ||
| 821 | effect.effects[0].element.setStyle({height: '0px'}).show(); | ||
| 822 | }, | ||
| 823 | afterFinishInternal: function(effect) { | ||
| 824 | effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle); | ||
| 825 | } | ||
| 826 | }, options) | ||
| 827 | ); | ||
| 828 | } | ||
| 829 | }); | ||
| 830 | }; | ||
| 831 | |||
| 832 | Effect.Shrink = function(element) { | ||
| 833 | element = $(element); | ||
| 834 | var options = Object.extend({ | ||
| 835 | direction: 'center', | ||
| 836 | moveTransition: Effect.Transitions.sinoidal, | ||
| 837 | scaleTransition: Effect.Transitions.sinoidal, | ||
| 838 | opacityTransition: Effect.Transitions.none | ||
| 839 | }, arguments[1] || { }); | ||
| 840 | var oldStyle = { | ||
| 841 | top: element.style.top, | ||
| 842 | left: element.style.left, | ||
| 843 | height: element.style.height, | ||
| 844 | width: element.style.width, | ||
| 845 | opacity: element.getInlineOpacity() }; | ||
| 846 | |||
| 847 | var dims = element.getDimensions(); | ||
| 848 | var moveX, moveY; | ||
| 849 | |||
| 850 | switch (options.direction) { | ||
| 851 | case 'top-left': | ||
| 852 | moveX = moveY = 0; | ||
| 853 | break; | ||
| 854 | case 'top-right': | ||
| 855 | moveX = dims.width; | ||
| 856 | moveY = 0; | ||
| 857 | break; | ||
| 858 | case 'bottom-left': | ||
| 859 | moveX = 0; | ||
| 860 | moveY = dims.height; | ||
| 861 | break; | ||
| 862 | case 'bottom-right': | ||
| 863 | moveX = dims.width; | ||
| 864 | moveY = dims.height; | ||
| 865 | break; | ||
| 866 | case 'center': | ||
| 867 | moveX = dims.width / 2; | ||
| 868 | moveY = dims.height / 2; | ||
| 869 | break; | ||
| 870 | } | ||
| 871 | |||
| 872 | return new Effect.Parallel( | ||
| 873 | [ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }), | ||
| 874 | new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}), | ||
| 875 | new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }) | ||
| 876 | ], Object.extend({ | ||
| 877 | beforeStartInternal: function(effect) { | ||
| 878 | effect.effects[0].element.makePositioned().makeClipping(); | ||
| 879 | }, | ||
| 880 | afterFinishInternal: function(effect) { | ||
| 881 | effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); } | ||
| 882 | }, options) | ||
| 883 | ); | ||
| 884 | }; | ||
| 885 | |||
| 886 | Effect.Pulsate = function(element) { | ||
| 887 | element = $(element); | ||
| 888 | var options = arguments[1] || { }, | ||
| 889 | oldOpacity = element.getInlineOpacity(), | ||
| 890 | transition = options.transition || Effect.Transitions.linear, | ||
| 891 | reverser = function(pos){ | ||
| 892 | return 1 - transition((-Math.cos((pos*(options.pulses||5)*2)*Math.PI)/2) + .5); | ||
| 893 | }; | ||
| 894 | |||
| 895 | return new Effect.Opacity(element, | ||
| 896 | Object.extend(Object.extend({ duration: 2.0, from: 0, | ||
| 897 | afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); } | ||
| 898 | }, options), {transition: reverser})); | ||
| 899 | }; | ||
| 900 | |||
| 901 | Effect.Fold = function(element) { | ||
| 902 | element = $(element); | ||
| 903 | var oldStyle = { | ||
| 904 | top: element.style.top, | ||
| 905 | left: element.style.left, | ||
| 906 | width: element.style.width, | ||
| 907 | height: element.style.height }; | ||
| 908 | element.makeClipping(); | ||
| 909 | return new Effect.Scale(element, 5, Object.extend({ | ||
| 910 | scaleContent: false, | ||
| 911 | scaleX: false, | ||
| 912 | afterFinishInternal: function(effect) { | ||
| 913 | new Effect.Scale(element, 1, { | ||
| 914 | scaleContent: false, | ||
| 915 | scaleY: false, | ||
| 916 | afterFinishInternal: function(effect) { | ||
| 917 | effect.element.hide().undoClipping().setStyle(oldStyle); | ||
| 918 | } }); | ||
| 919 | }}, arguments[1] || { })); | ||
| 920 | }; | ||
| 921 | |||
| 922 | Effect.Morph = Class.create(Effect.Base, { | ||
| 923 | initialize: function(element) { | ||
| 924 | this.element = $(element); | ||
| 925 | if (!this.element) throw(Effect._elementDoesNotExistError); | ||
| 926 | var options = Object.extend({ | ||
| 927 | style: { } | ||
| 928 | }, arguments[1] || { }); | ||
| 929 | |||
| 930 | if (!Object.isString(options.style)) this.style = $H(options.style); | ||
| 931 | else { | ||
| 932 | if (options.style.include(':')) | ||
| 933 | this.style = options.style.parseStyle(); | ||
| 934 | else { | ||
| 935 | this.element.addClassName(options.style); | ||
| 936 | this.style = $H(this.element.getStyles()); | ||
| 937 | this.element.removeClassName(options.style); | ||
| 938 | var css = this.element.getStyles(); | ||
| 939 | this.style = this.style.reject(function(style) { | ||
| 940 | return style.value == css[style.key]; | ||
| 941 | }); | ||
| 942 | options.afterFinishInternal = function(effect) { | ||
| 943 | effect.element.addClassName(effect.options.style); | ||
| 944 | effect.transforms.each(function(transform) { | ||
| 945 | effect.element.style[transform.style] = ''; | ||
| 946 | }); | ||
| 947 | }; | ||
| 948 | } | ||
| 949 | } | ||
| 950 | this.start(options); | ||
| 951 | }, | ||
| 952 | |||
| 953 | setup: function(){ | ||
| 954 | function parseColor(color){ | ||
| 955 | if (!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff'; | ||
| 956 | color = color.parseColor(); | ||
| 957 | return $R(0,2).map(function(i){ | ||
| 958 | return parseInt( color.slice(i*2+1,i*2+3), 16 ); | ||
| 959 | }); | ||
| 960 | } | ||
| 961 | this.transforms = this.style.map(function(pair){ | ||
| 962 | var property = pair[0], value = pair[1], unit = null; | ||
| 963 | |||
| 964 | if (value.parseColor('#zzzzzz') != '#zzzzzz') { | ||
| 965 | value = value.parseColor(); | ||
| 966 | unit = 'color'; | ||
| 967 | } else if (property == 'opacity') { | ||
| 968 | value = parseFloat(value); | ||
| 969 | if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) | ||
| 970 | this.element.setStyle({zoom: 1}); | ||
| 971 | } else if (Element.CSS_LENGTH.test(value)) { | ||
| 972 | var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); | ||
| 973 | value = parseFloat(components[1]); | ||
| 974 | unit = (components.length == 3) ? components[2] : null; | ||
| 975 | } | ||
| 976 | |||
| 977 | var originalValue = this.element.getStyle(property); | ||
| 978 | return { | ||
| 979 | style: property.camelize(), | ||
| 980 | originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0), | ||
| 981 | targetValue: unit=='color' ? parseColor(value) : value, | ||
| 982 | unit: unit | ||
| 983 | }; | ||
| 984 | }.bind(this)).reject(function(transform){ | ||
| 985 | return ( | ||
| 986 | (transform.originalValue == transform.targetValue) || | ||
| 987 | ( | ||
| 988 | transform.unit != 'color' && | ||
| 989 | (isNaN(transform.originalValue) || isNaN(transform.targetValue)) | ||
| 990 | ) | ||
| 991 | ); | ||
| 992 | }); | ||
| 993 | }, | ||
| 994 | update: function(position) { | ||
| 995 | var style = { }, transform, i = this.transforms.length; | ||
| 996 | while(i--) | ||
| 997 | style[(transform = this.transforms[i]).style] = | ||
| 998 | transform.unit=='color' ? '#'+ | ||
| 999 | (Math.round(transform.originalValue[0]+ | ||
| 1000 | (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() + | ||
| 1001 | (Math.round(transform.originalValue[1]+ | ||
| 1002 | (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() + | ||
| 1003 | (Math.round(transform.originalValue[2]+ | ||
| 1004 | (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() : | ||
| 1005 | (transform.originalValue + | ||
| 1006 | (transform.targetValue - transform.originalValue) * position).toFixed(3) + | ||
| 1007 | (transform.unit === null ? '' : transform.unit); | ||
| 1008 | this.element.setStyle(style, true); | ||
| 1009 | } | ||
| 1010 | }); | ||
| 1011 | |||
| 1012 | Effect.Transform = Class.create({ | ||
| 1013 | initialize: function(tracks){ | ||
| 1014 | this.tracks = []; | ||
| 1015 | this.options = arguments[1] || { }; | ||
| 1016 | this.addTracks(tracks); | ||
| 1017 | }, | ||
| 1018 | addTracks: function(tracks){ | ||
| 1019 | tracks.each(function(track){ | ||
| 1020 | track = $H(track); | ||
| 1021 | var data = track.values().first(); | ||
| 1022 | this.tracks.push($H({ | ||
| 1023 | ids: track.keys().first(), | ||
| 1024 | effect: Effect.Morph, | ||
| 1025 | options: { style: data } | ||
| 1026 | })); | ||
| 1027 | }.bind(this)); | ||
| 1028 | return this; | ||
| 1029 | }, | ||
| 1030 | play: function(){ | ||
| 1031 | return new Effect.Parallel( | ||
| 1032 | this.tracks.map(function(track){ | ||
| 1033 | var ids = track.get('ids'), effect = track.get('effect'), options = track.get('options'); | ||
| 1034 | var elements = [$(ids) || $$(ids)].flatten(); | ||
| 1035 | return elements.map(function(e){ return new effect(e, Object.extend({ sync:true }, options)) }); | ||
| 1036 | }).flatten(), | ||
| 1037 | this.options | ||
| 1038 | ); | ||
| 1039 | } | ||
| 1040 | }); | ||
| 1041 | |||
| 1042 | Element.CSS_PROPERTIES = $w( | ||
| 1043 | 'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' + | ||
| 1044 | 'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' + | ||
| 1045 | 'borderRightColor borderRightStyle borderRightWidth borderSpacing ' + | ||
| 1046 | 'borderTopColor borderTopStyle borderTopWidth bottom clip color ' + | ||
| 1047 | 'fontSize fontWeight height left letterSpacing lineHeight ' + | ||
| 1048 | 'marginBottom marginLeft marginRight marginTop markerOffset maxHeight '+ | ||
| 1049 | 'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' + | ||
| 1050 | 'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' + | ||
| 1051 | 'right textIndent top width wordSpacing zIndex'); | ||
| 1052 | |||
| 1053 | Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/; | ||
| 1054 | |||
| 1055 | String.__parseStyleElement = document.createElement('div'); | ||
| 1056 | String.prototype.parseStyle = function(){ | ||
| 1057 | var style, styleRules = $H(); | ||
| 1058 | if (Prototype.Browser.WebKit) | ||
| 1059 | style = new Element('div',{style:this}).style; | ||
| 1060 | else { | ||
| 1061 | String.__parseStyleElement.innerHTML = '<div style="' + this + '"></div>'; | ||
| 1062 | style = String.__parseStyleElement.childNodes[0].style; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | Element.CSS_PROPERTIES.each(function(property){ | ||
| 1066 | if (style[property]) styleRules.set(property, style[property]); | ||
| 1067 | }); | ||
| 1068 | |||
| 1069 | if (Prototype.Browser.IE && this.include('opacity')) | ||
| 1070 | styleRules.set('opacity', this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]); | ||
| 1071 | |||
| 1072 | return styleRules; | ||
| 1073 | }; | ||
| 1074 | |||
| 1075 | if (document.defaultView && document.defaultView.getComputedStyle) { | ||
| 1076 | Element.getStyles = function(element) { | ||
| 1077 | var css = document.defaultView.getComputedStyle($(element), null); | ||
| 1078 | return Element.CSS_PROPERTIES.inject({ }, function(styles, property) { | ||
| 1079 | styles[property] = css[property]; | ||
| 1080 | return styles; | ||
| 1081 | }); | ||
| 1082 | }; | ||
| 1083 | } else { | ||
| 1084 | Element.getStyles = function(element) { | ||
| 1085 | element = $(element); | ||
| 1086 | var css = element.currentStyle, styles; | ||
| 1087 | styles = Element.CSS_PROPERTIES.inject({ }, function(results, property) { | ||
| 1088 | results[property] = css[property]; | ||
| 1089 | return results; | ||
| 1090 | }); | ||
| 1091 | if (!styles.opacity) styles.opacity = element.getOpacity(); | ||
| 1092 | return styles; | ||
| 1093 | }; | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | Effect.Methods = { | ||
| 1097 | morph: function(element, style) { | ||
| 1098 | element = $(element); | ||
| 1099 | new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || { })); | ||
| 1100 | return element; | ||
| 1101 | }, | ||
| 1102 | visualEffect: function(element, effect, options) { | ||
| 1103 | element = $(element); | ||
| 1104 | var s = effect.dasherize().camelize(), klass = s.charAt(0).toUpperCase() + s.substring(1); | ||
| 1105 | new Effect[klass](element, options); | ||
| 1106 | return element; | ||
| 1107 | }, | ||
| 1108 | highlight: function(element, options) { | ||
| 1109 | element = $(element); | ||
| 1110 | new Effect.Highlight(element, options); | ||
| 1111 | return element; | ||
| 1112 | } | ||
| 1113 | }; | ||
| 1114 | |||
| 1115 | $w('fade appear grow shrink fold blindUp blindDown slideUp slideDown '+ | ||
| 1116 | 'pulsate shake puff squish switchOff dropOut').each( | ||
| 1117 | function(effect) { | ||
| 1118 | Effect.Methods[effect] = function(element, options){ | ||
| 1119 | element = $(element); | ||
| 1120 | Effect[effect.charAt(0).toUpperCase() + effect.substring(1)](element, options); | ||
| 1121 | return element; | ||
| 1122 | }; | ||
| 1123 | } | ||
| 1124 | ); | ||
| 1125 | |||
| 1126 | $w('getInlineOpacity forceRerendering setContentZoom collectTextNodes collectTextNodesIgnoreClass getStyles').each( | ||
| 1127 | function(f) { Effect.Methods[f] = Element[f]; } | ||
| 1128 | ); | ||
| 1129 | |||
| 1130 | Element.addMethods(Effect.Methods); |
build/scripty/src/scripty.js
(0 / 60)
|   | |||
| 1 | // script.aculo.us scriptaculous.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // | ||
| 5 | // Permission is hereby granted, free of charge, to any person obtaining | ||
| 6 | // a copy of this software and associated documentation files (the | ||
| 7 | // "Software"), to deal in the Software without restriction, including | ||
| 8 | // without limitation the rights to use, copy, modify, merge, publish, | ||
| 9 | // distribute, sublicense, and/or sell copies of the Software, and to | ||
| 10 | // permit persons to whom the Software is furnished to do so, subject to | ||
| 11 | // the following conditions: | ||
| 12 | // | ||
| 13 | // The above copyright notice and this permission notice shall be | ||
| 14 | // included in all copies or substantial portions of the Software. | ||
| 15 | // | ||
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | // | ||
| 24 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 25 | |||
| 26 | var Scriptaculous = { | ||
| 27 | Version: '1.8.2', | ||
| 28 | require: function(libraryName) { | ||
| 29 | // inserting via DOM fails in Safari 2.0, so brute force approach | ||
| 30 | document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>'); | ||
| 31 | }, | ||
| 32 | REQUIRED_PROTOTYPE: '1.6.0.3', | ||
| 33 | load: function() { | ||
| 34 | function convertVersionString(versionString) { | ||
| 35 | var v = versionString.replace(/_.*|\./g, ''); | ||
| 36 | v = parseInt(v + '0'.times(4-v.length)); | ||
| 37 | return versionString.indexOf('_') > -1 ? v-1 : v; | ||
| 38 | } | ||
| 39 | |||
| 40 | if((typeof Prototype=='undefined') || | ||
| 41 | (typeof Element == 'undefined') || | ||
| 42 | (typeof Element.Methods=='undefined') || | ||
| 43 | (convertVersionString(Prototype.Version) < | ||
| 44 | convertVersionString(Scriptaculous.REQUIRED_PROTOTYPE))) | ||
| 45 | throw("script.aculo.us requires the Prototype JavaScript framework >= " + | ||
| 46 | Scriptaculous.REQUIRED_PROTOTYPE); | ||
| 47 | |||
| 48 | var js = /scriptaculous\.js(\?.*)?$/; | ||
| 49 | $$('head script[src]').findAll(function(s) { | ||
| 50 | return s.src.match(js); | ||
| 51 | }).each(function(s) { | ||
| 52 | var path = s.src.replace(js, ''), | ||
| 53 | includes = s.src.match(/\?.*load=([a-z,]*)/); | ||
| 54 | (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each( | ||
| 55 | function(include) { Scriptaculous.require(path+include+'.js') }); | ||
| 56 | }); | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | |||
| 60 | Scriptaculous.load(); |
build/scripty/src/slider.js
(0 / 275)
|   | |||
| 1 | // script.aculo.us slider.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Marty Haught, Thomas Fuchs | ||
| 4 | // | ||
| 5 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 6 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 7 | |||
| 8 | if (!Control) var Control = { }; | ||
| 9 | |||
| 10 | // options: | ||
| 11 | // axis: 'vertical', or 'horizontal' (default) | ||
| 12 | // | ||
| 13 | // callbacks: | ||
| 14 | // onChange(value) | ||
| 15 | // onSlide(value) | ||
| 16 | Control.Slider = Class.create({ | ||
| 17 | initialize: function(handle, track, options) { | ||
| 18 | var slider = this; | ||
| 19 | |||
| 20 | if (Object.isArray(handle)) { | ||
| 21 | this.handles = handle.collect( function(e) { return $(e) }); | ||
| 22 | } else { | ||
| 23 | this.handles = [$(handle)]; | ||
| 24 | } | ||
| 25 | |||
| 26 | this.track = $(track); | ||
| 27 | this.options = options || { }; | ||
| 28 | |||
| 29 | this.axis = this.options.axis || 'horizontal'; | ||
| 30 | this.increment = this.options.increment || 1; | ||
| 31 | this.step = parseInt(this.options.step || '1'); | ||
| 32 | this.range = this.options.range || $R(0,1); | ||
| 33 | |||
| 34 | this.value = 0; // assure backwards compat | ||
| 35 | this.values = this.handles.map( function() { return 0 }); | ||
| 36 | this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false; | ||
| 37 | this.options.startSpan = $(this.options.startSpan || null); | ||
| 38 | this.options.endSpan = $(this.options.endSpan || null); | ||
| 39 | |||
| 40 | this.restricted = this.options.restricted || false; | ||
| 41 | |||
| 42 | this.maximum = this.options.maximum || this.range.end; | ||
| 43 | this.minimum = this.options.minimum || this.range.start; | ||
| 44 | |||
| 45 | // Will be used to align the handle onto the track, if necessary | ||
| 46 | this.alignX = parseInt(this.options.alignX || '0'); | ||
| 47 | this.alignY = parseInt(this.options.alignY || '0'); | ||
| 48 | |||
| 49 | this.trackLength = this.maximumOffset() - this.minimumOffset(); | ||
| 50 | |||
| 51 | this.handleLength = this.isVertical() ? | ||
| 52 | (this.handles[0].offsetHeight != 0 ? | ||
| 53 | this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) : | ||
| 54 | (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth : | ||
| 55 | this.handles[0].style.width.replace(/px$/,"")); | ||
| 56 | |||
| 57 | this.active = false; | ||
| 58 | this.dragging = false; | ||
| 59 | this.disabled = false; | ||
| 60 | |||
| 61 | if (this.options.disabled) this.setDisabled(); | ||
| 62 | |||
| 63 | // Allowed values array | ||
| 64 | this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false; | ||
| 65 | if (this.allowedValues) { | ||
| 66 | this.minimum = this.allowedValues.min(); | ||
| 67 | this.maximum = this.allowedValues.max(); | ||
| 68 | } | ||
| 69 | |||
| 70 | this.eventMouseDown = this.startDrag.bindAsEventListener(this); | ||
| 71 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); | ||
| 72 | this.eventMouseMove = this.update.bindAsEventListener(this); | ||
| 73 | |||
| 74 | // Initialize handles in reverse (make sure first handle is active) | ||
| 75 | this.handles.each( function(h,i) { | ||
| 76 | i = slider.handles.length-1-i; | ||
| 77 | slider.setValue(parseFloat( | ||
| 78 | (Object.isArray(slider.options.sliderValue) ? | ||
| 79 | slider.options.sliderValue[i] : slider.options.sliderValue) || | ||
| 80 | slider.range.start), i); | ||
| 81 | h.makePositioned().observe("mousedown", slider.eventMouseDown); | ||
| 82 | }); | ||
| 83 | |||
| 84 | this.track.observe("mousedown", this.eventMouseDown); | ||
| 85 | document.observe("mouseup", this.eventMouseUp); | ||
| 86 | document.observe("mousemove", this.eventMouseMove); | ||
| 87 | |||
| 88 | this.initialized = true; | ||
| 89 | }, | ||
| 90 | dispose: function() { | ||
| 91 | var slider = this; | ||
| 92 | Event.stopObserving(this.track, "mousedown", this.eventMouseDown); | ||
| 93 | Event.stopObserving(document, "mouseup", this.eventMouseUp); | ||
| 94 | Event.stopObserving(document, "mousemove", this.eventMouseMove); | ||
| 95 | this.handles.each( function(h) { | ||
| 96 | Event.stopObserving(h, "mousedown", slider.eventMouseDown); | ||
| 97 | }); | ||
| 98 | }, | ||
| 99 | setDisabled: function(){ | ||
| 100 | this.disabled = true; | ||
| 101 | }, | ||
| 102 | setEnabled: function(){ | ||
| 103 | this.disabled = false; | ||
| 104 | }, | ||
| 105 | getNearestValue: function(value){ | ||
| 106 | if (this.allowedValues){ | ||
| 107 | if (value >= this.allowedValues.max()) return(this.allowedValues.max()); | ||
| 108 | if (value <= this.allowedValues.min()) return(this.allowedValues.min()); | ||
| 109 | |||
| 110 | var offset = Math.abs(this.allowedValues[0] - value); | ||
| 111 | var newValue = this.allowedValues[0]; | ||
| 112 | this.allowedValues.each( function(v) { | ||
| 113 | var currentOffset = Math.abs(v - value); | ||
| 114 | if (currentOffset <= offset){ | ||
| 115 | newValue = v; | ||
| 116 | offset = currentOffset; | ||
| 117 | } | ||
| 118 | }); | ||
| 119 | return newValue; | ||
| 120 | } | ||
| 121 | if (value > this.range.end) return this.range.end; | ||
| 122 | if (value < this.range.start) return this.range.start; | ||
| 123 | return value; | ||
| 124 | }, | ||
| 125 | setValue: function(sliderValue, handleIdx){ | ||
| 126 | if (!this.active) { | ||
| 127 | this.activeHandleIdx = handleIdx || 0; | ||
| 128 | this.activeHandle = this.handles[this.activeHandleIdx]; | ||
| 129 | this.updateStyles(); | ||
| 130 | } | ||
| 131 | handleIdx = handleIdx || this.activeHandleIdx || 0; | ||
| 132 | if (this.initialized && this.restricted) { | ||
| 133 | if ((handleIdx>0) && (sliderValue<this.values[handleIdx-1])) | ||
| 134 | sliderValue = this.values[handleIdx-1]; | ||
| 135 | if ((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1])) | ||
| 136 | sliderValue = this.values[handleIdx+1]; | ||
| 137 | } | ||
| 138 | sliderValue = this.getNearestValue(sliderValue); | ||
| 139 | this.values[handleIdx] = sliderValue; | ||
| 140 | this.value = this.values[0]; // assure backwards compat | ||
| 141 | |||
| 142 | this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = | ||
| 143 | this.translateToPx(sliderValue); | ||
| 144 | |||
| 145 | this.drawSpans(); | ||
| 146 | if (!this.dragging || !this.event) this.updateFinished(); | ||
| 147 | }, | ||
| 148 | setValueBy: function(delta, handleIdx) { | ||
| 149 | this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta, | ||
| 150 | handleIdx || this.activeHandleIdx || 0); | ||
| 151 | }, | ||
| 152 | translateToPx: function(value) { | ||
| 153 | return Math.round( | ||
| 154 | ((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) * | ||
| 155 | (value - this.range.start)) + "px"; | ||
| 156 | }, | ||
| 157 | translateToValue: function(offset) { | ||
| 158 | return ((offset/(this.trackLength-this.handleLength) * | ||
| 159 | (this.range.end-this.range.start)) + this.range.start); | ||
| 160 | }, | ||
| 161 | getRange: function(range) { | ||
| 162 | var v = this.values.sortBy(Prototype.K); | ||
| 163 | range = range || 0; | ||
| 164 | return $R(v[range],v[range+1]); | ||
| 165 | }, | ||
| 166 | minimumOffset: function(){ | ||
| 167 | return(this.isVertical() ? this.alignY : this.alignX); | ||
| 168 | }, | ||
| 169 | maximumOffset: function(){ | ||
| 170 | return(this.isVertical() ? | ||
| 171 | (this.track.offsetHeight != 0 ? this.track.offsetHeight : | ||
| 172 | this.track.style.height.replace(/px$/,"")) - this.alignY : | ||
| 173 | (this.track.offsetWidth != 0 ? this.track.offsetWidth : | ||
| 174 | this.track.style.width.replace(/px$/,"")) - this.alignX); | ||
| 175 | }, | ||
| 176 | isVertical: function(){ | ||
| 177 | return (this.axis == 'vertical'); | ||
| 178 | }, | ||
| 179 | drawSpans: function() { | ||
| 180 | var slider = this; | ||
| 181 | if (this.spans) | ||
| 182 | $R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) }); | ||
| 183 | if (this.options.startSpan) | ||
| 184 | this.setSpan(this.options.startSpan, | ||
| 185 | $R(0, this.values.length>1 ? this.getRange(0).min() : this.value )); | ||
| 186 | if (this.options.endSpan) | ||
| 187 | this.setSpan(this.options.endSpan, | ||
| 188 | $R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum)); | ||
| 189 | }, | ||
| 190 | setSpan: function(span, range) { | ||
| 191 | if (this.isVertical()) { | ||
| 192 | span.style.top = this.translateToPx(range.start); | ||
| 193 | span.style.height = this.translateToPx(range.end - range.start + this.range.start); | ||
| 194 | } else { | ||
| 195 | span.style.left = this.translateToPx(range.start); | ||
| 196 | span.style.width = this.translateToPx(range.end - range.start + this.range.start); | ||
| 197 | } | ||
| 198 | }, | ||
| 199 | updateStyles: function() { | ||
| 200 | this.handles.each( function(h){ Element.removeClassName(h, 'selected') }); | ||
| 201 | Element.addClassName(this.activeHandle, 'selected'); | ||
| 202 | }, | ||
| 203 | startDrag: function(event) { | ||
| 204 | if (Event.isLeftClick(event)) { | ||
| 205 | if (!this.disabled){ | ||
| 206 | this.active = true; | ||
| 207 | |||
| 208 | var handle = Event.element(event); | ||
| 209 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
| 210 | var track = handle; | ||
| 211 | if (track==this.track) { | ||
| 212 | var offsets = Position.cumulativeOffset(this.track); | ||
| 213 | this.event = event; | ||
| 214 | this.setValue(this.translateToValue( | ||
| 215 | (this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2) | ||
| 216 | )); | ||
| 217 | var offsets = Position.cumulativeOffset(this.activeHandle); | ||
| 218 | this.offsetX = (pointer[0] - offsets[0]); | ||
| 219 | this.offsetY = (pointer[1] - offsets[1]); | ||
| 220 | } else { | ||
| 221 | // find the handle (prevents issues with Safari) | ||
| 222 | while((this.handles.indexOf(handle) == -1) && handle.parentNode) | ||
| 223 | handle = handle.parentNode; | ||
| 224 | |||
| 225 | if (this.handles.indexOf(handle)!=-1) { | ||
| 226 | this.activeHandle = handle; | ||
| 227 | this.activeHandleIdx = this.handles.indexOf(this.activeHandle); | ||
| 228 | this.updateStyles(); | ||
| 229 | |||
| 230 | var offsets = Position.cumulativeOffset(this.activeHandle); | ||
| 231 | this.offsetX = (pointer[0] - offsets[0]); | ||
| 232 | this.offsetY = (pointer[1] - offsets[1]); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | Event.stop(event); | ||
| 237 | } | ||
| 238 | }, | ||
| 239 | update: function(event) { | ||
| 240 | if (this.active) { | ||
| 241 | if (!this.dragging) this.dragging = true; | ||
| 242 | this.draw(event); | ||
| 243 | if (Prototype.Browser.WebKit) window.scrollBy(0,0); | ||
| 244 | Event.stop(event); | ||
| 245 | } | ||
| 246 | }, | ||
| 247 | draw: function(event) { | ||
| 248 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; | ||
| 249 | var offsets = Position.cumulativeOffset(this.track); | ||
| 250 | pointer[0] -= this.offsetX + offsets[0]; | ||
| 251 | pointer[1] -= this.offsetY + offsets[1]; | ||
| 252 | this.event = event; | ||
| 253 | this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] )); | ||
| 254 | if (this.initialized && this.options.onSlide) | ||
| 255 | this.options.onSlide(this.values.length>1 ? this.values : this.value, this); | ||
| 256 | }, | ||
| 257 | endDrag: function(event) { | ||
| 258 | if (this.active && this.dragging) { | ||
| 259 | this.finishDrag(event, true); | ||
| 260 | Event.stop(event); | ||
| 261 | } | ||
| 262 | this.active = false; | ||
| 263 | this.dragging = false; | ||
| 264 | }, | ||
| 265 | finishDrag: function(event, success) { | ||
| 266 | this.active = false; | ||
| 267 | this.dragging = false; | ||
| 268 | this.updateFinished(); | ||
| 269 | }, | ||
| 270 | updateFinished: function() { | ||
| 271 | if (this.initialized && this.options.onChange) | ||
| 272 | this.options.onChange(this.values.length>1 ? this.values : this.value, this); | ||
| 273 | this.event = null; | ||
| 274 | } | ||
| 275 | }); |
build/scripty/src/sound.js
(0 / 55)
|   | |||
| 1 | // script.aculo.us sound.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // | ||
| 5 | // Based on code created by Jules Gravinese (http://www.webveteran.com/) | ||
| 6 | // | ||
| 7 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 8 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 9 | |||
| 10 | Sound = { | ||
| 11 | tracks: {}, | ||
| 12 | _enabled: true, | ||
| 13 | template: | ||
| 14 | new Template('<embed style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>'), | ||
| 15 | enable: function(){ | ||
| 16 | Sound._enabled = true; | ||
| 17 | }, | ||
| 18 | disable: function(){ | ||
| 19 | Sound._enabled = false; | ||
| 20 | }, | ||
| 21 | play: function(url){ | ||
| 22 | if(!Sound._enabled) return; | ||
| 23 | var options = Object.extend({ | ||
| 24 | track: 'global', url: url, replace: false | ||
| 25 | }, arguments[1] || {}); | ||
| 26 | |||
| 27 | if(options.replace && this.tracks[options.track]) { | ||
| 28 | $R(0, this.tracks[options.track].id).each(function(id){ | ||
| 29 | var sound = $('sound_'+options.track+'_'+id); | ||
| 30 | sound.Stop && sound.Stop(); | ||
| 31 | sound.remove(); | ||
| 32 | }); | ||
| 33 | this.tracks[options.track] = null; | ||
| 34 | } | ||
| 35 | |||
| 36 | if(!this.tracks[options.track]) | ||
| 37 | this.tracks[options.track] = { id: 0 }; | ||
| 38 | else | ||
| 39 | this.tracks[options.track].id++; | ||
| 40 | |||
| 41 | options.id = this.tracks[options.track].id; | ||
| 42 | $$('body')[0].insert( | ||
| 43 | Prototype.Browser.IE ? new Element('bgsound',{ | ||
| 44 | id: 'sound_'+options.track+'_'+options.id, | ||
| 45 | src: options.url, loop: 1, autostart: true | ||
| 46 | }) : Sound.template.evaluate(options)); | ||
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | if(Prototype.Browser.Gecko && navigator.userAgent.indexOf("Win") > 0){ | ||
| 51 | if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 })) | ||
| 52 | Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>'); | ||
| 53 | else | ||
| 54 | Sound.play = function(){}; | ||
| 55 | } |
build/scripty/src/unittest.js
(0 / 568)
|   | |||
| 1 | // script.aculo.us unittest.js v1.8.2, Tue Nov 18 18:30:58 +0100 2008 | ||
| 2 | |||
| 3 | // Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) | ||
| 4 | // (c) 2005-2008 Jon Tirsen (http://www.tirsen.com) | ||
| 5 | // (c) 2005-2008 Michael Schuerig (http://www.schuerig.de/michael/) | ||
| 6 | // | ||
| 7 | // script.aculo.us is freely distributable under the terms of an MIT-style license. | ||
| 8 | // For details, see the script.aculo.us web site: http://script.aculo.us/ | ||
| 9 | |||
| 10 | // experimental, Firefox-only | ||
| 11 | Event.simulateMouse = function(element, eventName) { | ||
| 12 | var options = Object.extend({ | ||
| 13 | pointerX: 0, | ||
| 14 | pointerY: 0, | ||
| 15 | buttons: 0, | ||
| 16 | ctrlKey: false, | ||
| 17 | altKey: false, | ||
| 18 | shiftKey: false, | ||
| 19 | metaKey: false | ||
| 20 | }, arguments[2] || {}); | ||
| 21 | var oEvent = document.createEvent("MouseEvents"); | ||
| 22 | oEvent.initMouseEvent(eventName, true, true, document.defaultView, | ||
| 23 | options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, | ||
| 24 | options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, 0, $(element)); | ||
| 25 | |||
| 26 | if(this.mark) Element.remove(this.mark); | ||
| 27 | this.mark = document.createElement('div'); | ||
| 28 | this.mark.appendChild(document.createTextNode(" ")); | ||
| 29 | document.body.appendChild(this.mark); | ||
| 30 | this.mark.style.position = 'absolute'; | ||
| 31 | this.mark.style.top = options.pointerY + "px"; | ||
| 32 | this.mark.style.left = options.pointerX + "px"; | ||
| 33 | this.mark.style.width = "5px"; | ||
| 34 | this.mark.style.height = "5px;"; | ||
| 35 | this.mark.style.borderTop = "1px solid red;"; | ||
| 36 | this.mark.style.borderLeft = "1px solid red;"; | ||
| 37 | |||
| 38 | if(this.step) | ||
| 39 | alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options)); | ||
| 40 | |||
| 41 | $(element).dispatchEvent(oEvent); | ||
| 42 | }; | ||
| 43 | |||
| 44 | // Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2. | ||
| 45 | // You need to downgrade to 1.0.4 for now to get this working | ||
| 46 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much | ||
| 47 | Event.simulateKey = function(element, eventName) { | ||
| 48 | var options = Object.extend({ | ||
| 49 | ctrlKey: false, | ||
| 50 | altKey: false, | ||
| 51 | shiftKey: false, | ||
| 52 | metaKey: false, | ||
| 53 | keyCode: 0, | ||
| 54 | charCode: 0 | ||
| 55 | }, arguments[2] || {}); | ||
| 56 | |||
| 57 | var oEvent = document.createEvent("KeyEvents"); | ||
| 58 | oEvent.initKeyEvent(eventName, true, true, window, | ||
| 59 | options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, | ||
| 60 | options.keyCode, options.charCode ); | ||
| 61 | $(element).dispatchEvent(oEvent); | ||
| 62 | }; | ||
| 63 | |||
| 64 | Event.simulateKeys = function(element, command) { | ||
| 65 | for(var i=0; i<command.length; i++) { | ||
| 66 | Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)}); | ||
| 67 | } | ||
| 68 | }; | ||
| 69 | |||
| 70 | var Test = {}; | ||
| 71 | Test.Unit = {}; | ||
| 72 | |||
| 73 | // security exception workaround | ||
| 74 | Test.Unit.inspect = Object.inspect; | ||
| 75 | |||
| 76 | Test.Unit.Logger = Class.create(); | ||
| 77 | Test.Unit.Logger.prototype = { | ||
| 78 | initialize: function(log) { | ||
| 79 | this.log = $(log); | ||
| 80 | if (this.log) { | ||
| 81 | this._createLogTable(); | ||
| 82 | } | ||
| 83 | }, | ||
| 84 | start: function(testName) { | ||
| 85 | if (!this.log) return; | ||
| 86 | this.testName = testName; | ||
| 87 | this.lastLogLine = document.createElement('tr'); | ||
| 88 | this.statusCell = document.createElement('td'); | ||
| 89 | this.nameCell = document.createElement('td'); | ||
| 90 | this.nameCell.className = "nameCell"; | ||
| 91 | this.nameCell.appendChild(document.createTextNode(testName)); | ||
| 92 | this.messageCell = document.createElement('td'); | ||
| 93 | this.lastLogLine.appendChild(this.statusCell); | ||
| 94 | this.lastLogLine.appendChild(this.nameCell); | ||
| 95 | this.lastLogLine.appendChild(this.messageCell); | ||
| 96 | this.loglines.appendChild(this.lastLogLine); | ||
| 97 | }, | ||
| 98 | finish: function(status, summary) { | ||
| 99 | if (!this.log) return; | ||
| 100 | this.lastLogLine.className = status; | ||
| 101 | this.statusCell.innerHTML = status; | ||
| 102 | this.messageCell.innerHTML = this._toHTML(summary); | ||
| 103 | this.addLinksToResults(); | ||
| 104 | }, | ||
| 105 | message: function(message) { | ||
| 106 | if (!this.log) return; | ||
| 107 | this.messageCell.innerHTML = this._toHTML(message); | ||
| 108 | }, | ||
| 109 | summary: function(summary) { | ||
| 110 | if (!this.log) return; | ||
| 111 | this.logsummary.innerHTML = this._toHTML(summary); | ||
| 112 | }, | ||
| 113 | _createLogTable: function() { | ||
| 114 | this.log.innerHTML = | ||
| 115 | '<div id="logsummary"></div>' + | ||
| 116 | '<table id="logtable">' + | ||
| 117 | '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' + | ||
| 118 | '<tbody id="loglines"></tbody>' + | ||
| 119 | '</table>'; | ||
| 120 | this.logsummary = $('logsummary'); | ||
| 121 | this.loglines = $('loglines'); | ||
| 122 | }, | ||
| 123 | _toHTML: function(txt) { | ||
| 124 | return txt.escapeHTML().replace(/\n/g,"<br/>"); | ||
| 125 | }, | ||
| 126 | addLinksToResults: function(){ | ||
| 127 | $$("tr.failed .nameCell").each( function(td){ // todo: limit to children of this.log | ||
| 128 | td.title = "Run only this test"; | ||
| 129 | Event.observe(td, 'click', function(){ window.location.search = "?tests=" + td.innerHTML;}); | ||
| 130 | }); | ||
| 131 | $$("tr.passed .nameCell").each( function(td){ // todo: limit to children of this.log | ||
| 132 | td.title = "Run all tests"; | ||
| 133 | Event.observe(td, 'click', function(){ window.location.search = "";}); | ||
| 134 | }); | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | |||
| 138 | Test.Unit.Runner = Class.create(); | ||
| 139 | Test.Unit.Runner.prototype = { | ||
| 140 | initialize: function(testcases) { | ||
| 141 | this.options = Object.extend({ | ||
| 142 | testLog: 'testlog' | ||
| 143 | }, arguments[1] || {}); | ||
| 144 | this.options.resultsURL = this.parseResultsURLQueryParameter(); | ||
| 145 | this.options.tests = this.parseTestsQueryParameter(); | ||
| 146 | if (this.options.testLog) { | ||
| 147 | this.options.testLog = $(this.options.testLog) || null; | ||
| 148 | } | ||
| 149 | if(this.options.tests) { | ||
| 150 | this.tests = []; | ||
| 151 | for(var i = 0; i < this.options.tests.length; i++) { | ||
| 152 | if(/^test/.test(this.options.tests[i])) { | ||
| 153 | this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"])); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } else { | ||
| 157 | if (this.options.test) { | ||
| 158 | this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])]; | ||
| 159 | } else { | ||
| 160 | this.tests = []; | ||
| 161 | for(var testcase in testcases) { | ||
| 162 | if(/^test/.test(testcase)) { | ||
| 163 | this.tests.push( | ||
| 164 | new Test.Unit.Testcase( | ||
| 165 | this.options.context ? ' -> ' + this.options.titles[testcase] : testcase, | ||
| 166 | testcases[testcase], testcases["setup"], testcases["teardown"] | ||
| 167 | )); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | this.currentTest = 0; | ||
| 173 | this.logger = new Test.Unit.Logger(this.options.testLog); | ||
| 174 | setTimeout(this.runTests.bind(this), 1000); | ||
| 175 | }, | ||
| 176 | parseResultsURLQueryParameter: function() { | ||
| 177 | return window.location.search.parseQuery()["resultsURL"]; | ||
| 178 | }, | ||
| 179 | parseTestsQueryParameter: function(){ | ||
| 180 | if (window.location.search.parseQuery()["tests"]){ | ||
| 181 | return window.location.search.parseQuery()["tests"].split(','); | ||
| 182 | }; | ||
| 183 | }, | ||
| 184 | // Returns: | ||
| 185 | // "ERROR" if there was an error, | ||
| 186 | // "FAILURE" if there was a failure, or | ||
| 187 | // "SUCCESS" if there was neither | ||
| 188 | getResult: function() { | ||
| 189 | var hasFailure = false; | ||
| 190 | for(var i=0;i<this.tests.length;i++) { | ||
| 191 | if (this.tests[i].errors > 0) { | ||
| 192 | return "ERROR"; | ||
| 193 | } | ||
| 194 | if (this.tests[i].failures > 0) { | ||
| 195 | hasFailure = true; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | if (hasFailure) { | ||
| 199 | return "FAILURE"; | ||
| 200 | } else { | ||
| 201 | return "SUCCESS"; | ||
| 202 | } | ||
| 203 | }, | ||
| 204 | postResults: function() { | ||
| 205 | if (this.options.resultsURL) { | ||
| 206 | new Ajax.Request(this.options.resultsURL, | ||
| 207 | { method: 'get', parameters: 'result=' + this.getResult(), asynchronous: false }); | ||
| 208 | } | ||
| 209 | }, | ||
| 210 | runTests: function() { | ||
| 211 | var test = this.tests[this.currentTest]; | ||
| 212 | if (!test) { | ||
| 213 | // finished! | ||
| 214 | this.postResults(); | ||
| 215 | this.logger.summary(this.summary()); | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | if(!test.isWaiting) { | ||
| 219 | this.logger.start(test.name); | ||
| 220 | } | ||
| 221 | test.run(); | ||
| 222 | if(test.isWaiting) { | ||
| 223 | this.logger.message("Waiting for " + test.timeToWait + "ms"); | ||
| 224 | setTimeout(this.runTests.bind(this), test.timeToWait || 1000); | ||
| 225 | } else { | ||
| 226 | this.logger.finish(test.status(), test.summary()); | ||
| 227 | this.currentTest++; | ||
| 228 | // tail recursive, hopefully the browser will skip the stackframe | ||
| 229 | this.runTests(); | ||
| 230 | } | ||
| 231 | }, | ||
| 232 | summary: function() { | ||
| 233 | var assertions = 0; | ||
| 234 | var failures = 0; | ||
| 235 | var errors = 0; | ||
| 236 | var messages = []; | ||
| 237 | for(var i=0;i<this.tests.length;i++) { | ||
| 238 | assertions += this.tests[i].assertions; | ||
| 239 | failures += this.tests[i].failures; | ||
| 240 | errors += this.tests[i].errors; | ||
| 241 | } | ||
| 242 | return ( | ||
| 243 | (this.options.context ? this.options.context + ': ': '') + | ||
| 244 | this.tests.length + " tests, " + | ||
| 245 | assertions + " assertions, " + | ||
| 246 | failures + " failures, " + | ||
| 247 | errors + " errors"); | ||
| 248 | } | ||
| 249 | }; | ||
| 250 | |||
| 251 | Test.Unit.Assertions = Class.create(); | ||
| 252 | Test.Unit.Assertions.prototype = { | ||
| 253 | initialize: function() { | ||
| 254 | this.assertions = 0; | ||
| 255 | this.failures = 0; | ||
| 256 | this.errors = 0; | ||
| 257 | this.messages = []; | ||
| 258 | }, | ||
| 259 | summary: function() { | ||
| 260 | return ( | ||
| 261 | this.assertions + " assertions, " + | ||
| 262 | this.failures + " failures, " + | ||
| 263 | this.errors + " errors" + "\n" + | ||
| 264 | this.messages.join("\n")); | ||
| 265 | }, | ||
| 266 | pass: function() { | ||
| 267 | this.assertions++; | ||
| 268 | }, | ||
| 269 | fail: function(message) { | ||
| 270 | this.failures++; | ||
| 271 | this.messages.push("Failure: " + message); | ||
| 272 | }, | ||
| 273 | info: function(message) { | ||
| 274 | this.messages.push("Info: " + message); | ||
| 275 | }, | ||
| 276 | error: function(error) { | ||
| 277 | this.errors++; | ||
| 278 | this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")"); | ||
| 279 | }, | ||
| 280 | status: function() { | ||
| 281 | if (this.failures > 0) return 'failed'; | ||
| 282 | if (this.errors > 0) return 'error'; | ||
| 283 | return 'passed'; | ||
| 284 | }, | ||
| 285 | assert: function(expression) { | ||
| 286 | var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"'; | ||
| 287 | try { expression ? this.pass() : | ||
| 288 | this.fail(message); } | ||
| 289 | catch(e) { this.error(e); } | ||
| 290 | }, | ||
| 291 | assertEqual: function(expected, actual) { | ||
| 292 | var message = arguments[2] || "assertEqual"; | ||
| 293 | try { (expected == actual) ? this.pass() : | ||
| 294 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 295 | '", actual "' + Test.Unit.inspect(actual) + '"'); } | ||
| 296 | catch(e) { this.error(e); } | ||
| 297 | }, | ||
| 298 | assertInspect: function(expected, actual) { | ||
| 299 | var message = arguments[2] || "assertInspect"; | ||
| 300 | try { (expected == actual.inspect()) ? this.pass() : | ||
| 301 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 302 | '", actual "' + Test.Unit.inspect(actual) + '"'); } | ||
| 303 | catch(e) { this.error(e); } | ||
| 304 | }, | ||
| 305 | assertEnumEqual: function(expected, actual) { | ||
| 306 | var message = arguments[2] || "assertEnumEqual"; | ||
| 307 | try { $A(expected).length == $A(actual).length && | ||
| 308 | expected.zip(actual).all(function(pair) { return pair[0] == pair[1] }) ? | ||
| 309 | this.pass() : this.fail(message + ': expected ' + Test.Unit.inspect(expected) + | ||
| 310 | ', actual ' + Test.Unit.inspect(actual)); } | ||
| 311 | catch(e) { this.error(e); } | ||
| 312 | }, | ||
| 313 | assertNotEqual: function(expected, actual) { | ||
| 314 | var message = arguments[2] || "assertNotEqual"; | ||
| 315 | try { (expected != actual) ? this.pass() : | ||
| 316 | this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); } | ||
| 317 | catch(e) { this.error(e); } | ||
| 318 | }, | ||
| 319 | assertIdentical: function(expected, actual) { | ||
| 320 | var message = arguments[2] || "assertIdentical"; | ||
| 321 | try { (expected === actual) ? this.pass() : | ||
| 322 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 323 | '", actual "' + Test.Unit.inspect(actual) + '"'); } | ||
| 324 | catch(e) { this.error(e); } | ||
| 325 | }, | ||
| 326 | assertNotIdentical: function(expected, actual) { | ||
| 327 | var message = arguments[2] || "assertNotIdentical"; | ||
| 328 | try { !(expected === actual) ? this.pass() : | ||
| 329 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 330 | '", actual "' + Test.Unit.inspect(actual) + '"'); } | ||
| 331 | catch(e) { this.error(e); } | ||
| 332 | }, | ||
| 333 | assertNull: function(obj) { | ||
| 334 | var message = arguments[1] || 'assertNull'; | ||
| 335 | try { (obj==null) ? this.pass() : | ||
| 336 | this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); } | ||
| 337 | catch(e) { this.error(e); } | ||
| 338 | }, | ||
| 339 | assertMatch: function(expected, actual) { | ||
| 340 | var message = arguments[2] || 'assertMatch'; | ||
| 341 | var regex = new RegExp(expected); | ||
| 342 | try { (regex.exec(actual)) ? this.pass() : | ||
| 343 | this.fail(message + ' : regex: "' + Test.Unit.inspect(expected) + ' did not match: ' + Test.Unit.inspect(actual) + '"'); } | ||
| 344 | catch(e) { this.error(e); } | ||
| 345 | }, | ||
| 346 | assertHidden: function(element) { | ||
| 347 | var message = arguments[1] || 'assertHidden'; | ||
| 348 | this.assertEqual("none", element.style.display, message); | ||
| 349 | }, | ||
| 350 | assertNotNull: function(object) { | ||
| 351 | var message = arguments[1] || 'assertNotNull'; | ||
| 352 | this.assert(object != null, message); | ||
| 353 | }, | ||
| 354 | assertType: function(expected, actual) { | ||
| 355 | var message = arguments[2] || 'assertType'; | ||
| 356 | try { | ||
| 357 | (actual.constructor == expected) ? this.pass() : | ||
| 358 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 359 | '", actual "' + (actual.constructor) + '"'); } | ||
| 360 | catch(e) { this.error(e); } | ||
| 361 | }, | ||
| 362 | assertNotOfType: function(expected, actual) { | ||
| 363 | var message = arguments[2] || 'assertNotOfType'; | ||
| 364 | try { | ||
| 365 | (actual.constructor != expected) ? this.pass() : | ||
| 366 | this.fail(message + ': expected "' + Test.Unit.inspect(expected) + | ||
| 367 | '", actual "' + (actual.constructor) + '"'); } | ||
| 368 | catch(e) { this.error(e); } | ||
| 369 | }, | ||
| 370 | assertInstanceOf: function(expected, actual) { | ||
| 371 | var message = arguments[2] || 'assertInstanceOf'; | ||
| 372 | try { | ||
| 373 | (actual instanceof expected) ? this.pass() : | ||
| 374 | this.fail(message + ": object was not an instance of the expected type"); } | ||
| 375 | catch(e) { this.error(e); } | ||
| 376 | }, | ||
| 377 | assertNotInstanceOf: function(expected, actual) { | ||
| 378 | var message = arguments[2] || 'assertNotInstanceOf'; | ||
| 379 | try { | ||
| 380 | !(actual instanceof expected) ? this.pass() : | ||
| 381 | this.fail(message + ": object was an instance of the not expected type"); } | ||
| 382 | catch(e) { this.error(e); } | ||
| 383 | }, | ||
| 384 | assertRespondsTo: function(method, obj) { | ||
| 385 | var message = arguments[2] || 'assertRespondsTo'; | ||
| 386 | try { | ||
| 387 | (obj[method] && typeof obj[method] == 'function') ? this.pass() : | ||
| 388 | this.fail(message + ": object doesn't respond to [" + method + "]"); } | ||
| 389 | catch(e) { this.error(e); } | ||
| 390 | }, | ||
| 391 | assertReturnsTrue: function(method, obj) { | ||
| 392 | var message = arguments[2] || 'assertReturnsTrue'; | ||
| 393 | try { | ||
| 394 | var m = obj[method]; | ||
| 395 | if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)]; | ||
| 396 | m() ? this.pass() : | ||
| 397 | this.fail(message + ": method returned false"); } | ||
| 398 | catch(e) { this.error(e); } | ||
| 399 | }, | ||
| 400 | assertReturnsFalse: function(method, obj) { | ||
| 401 | var message = arguments[2] || 'assertReturnsFalse'; | ||
| 402 | try { | ||
| 403 | var m = obj[method]; | ||
| 404 | if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)]; | ||
| 405 | !m() ? this.pass() : | ||
| 406 | this.fail(message + ": method returned true"); } | ||
| 407 | catch(e) { this.error(e); } | ||
| 408 | }, | ||
| 409 | assertRaise: function(exceptionName, method) { | ||
| 410 | var message = arguments[2] || 'assertRaise'; | ||
| 411 | try { | ||
| 412 | method(); | ||
| 413 | this.fail(message + ": exception expected but none was raised"); } | ||
| 414 | catch(e) { | ||
| 415 | ((exceptionName == null) || (e.name==exceptionName)) ? this.pass() : this.error(e); | ||
| 416 | } | ||
| 417 | }, | ||
| 418 | assertElementsMatch: function() { | ||
| 419 | var expressions = $A(arguments), elements = $A(expressions.shift()); | ||
| 420 | if (elements.length != expressions.length) { | ||
| 421 | this.fail('assertElementsMatch: size mismatch: ' + elements.length + ' elements, ' + expressions.length + ' expressions'); | ||
| 422 | return false; | ||
| 423 | } | ||
| 424 | elements.zip(expressions).all(function(pair, index) { | ||
| 425 | var element = $(pair.first()), expression = pair.last(); | ||
| 426 | if (element.match(expression)) return true; | ||
| 427 | this.fail('assertElementsMatch: (in index ' + index + ') expected ' + expression.inspect() + ' but got ' + element.inspect()); | ||
| 428 | }.bind(this)) && this.pass(); | ||
| 429 | }, | ||
| 430 | assertElementMatches: function(element, expression) { | ||
| 431 | this.assertElementsMatch([element], expression); | ||
| 432 | }, | ||
| 433 | benchmark: function(operation, iterations) { | ||
| 434 | var startAt = new Date(); | ||
| 435 | (iterations || 1).times(operation); | ||
| 436 | var timeTaken = ((new Date())-startAt); | ||
| 437 | this.info((arguments[2] || 'Operation') + ' finished ' + | ||
| 438 | iterations + ' iterations in ' + (timeTaken/1000)+'s' ); | ||
| 439 | return timeTaken; | ||
| 440 | }, | ||
| 441 | _isVisible: function(element) { | ||
| 442 | element = $(element); | ||
| 443 | if(!element.parentNode) return true; | ||
| 444 | this.assertNotNull(element); | ||
| 445 | if(element.style && Element.getStyle(element, 'display') == 'none') | ||
| 446 | return false; | ||
| 447 | |||
| 448 | return this._isVisible(element.parentNode); | ||
| 449 | }, | ||
| 450 | assertNotVisible: function(element) { | ||
| 451 | this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either. " + ("" || arguments[1])); | ||
| 452 | }, | ||
| 453 | assertVisible: function(element) { | ||
| 454 | this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible. " + ("" || arguments[1])); | ||
| 455 | }, | ||
| 456 | benchmark: function(operation, iterations) { | ||
| 457 | var startAt = new Date(); | ||
| 458 | (iterations || 1).times(operation); | ||
| 459 | var timeTaken = ((new Date())-startAt); | ||
| 460 | this.info((arguments[2] || 'Operation') + ' finished ' + | ||
| 461 | iterations + ' iterations in ' + (timeTaken/1000)+'s' ); | ||
| 462 | return timeTaken; | ||
| 463 | } | ||
| 464 | }; | ||
| 465 | |||
| 466 | Test.Unit.Testcase = Class.create(); | ||
| 467 | Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), { | ||
| 468 | initialize: function(name, test, setup, teardown) { | ||
| 469 | Test.Unit.Assertions.prototype.initialize.bind(this)(); | ||
| 470 | this.name = name; | ||
| 471 | |||
| 472 | if(typeof test == 'string') { | ||
| 473 | test = test.gsub(/(\.should[^\(]+\()/,'#{0}this,'); | ||
| 474 | test = test.gsub(/(\.should[^\(]+)\(this,\)/,'#{1}(this)'); | ||
| 475 | this.test = function() { | ||
| 476 | eval('with(this){'+test+'}'); | ||
| 477 | } | ||
| 478 | } else { | ||
| 479 | this.test = test || function() {}; | ||
| 480 | } | ||
| 481 | |||
| 482 | this.setup = setup || function() {}; | ||
| 483 | this.teardown = teardown || function() {}; | ||
| 484 | this.isWaiting = false; | ||
| 485 | this.timeToWait = 1000; | ||
| 486 | }, | ||
| 487 | wait: function(time, nextPart) { | ||
| 488 | this.isWaiting = true; | ||
| 489 | this.test = nextPart; | ||
| 490 | this.timeToWait = time; | ||
| 491 | }, | ||
| 492 | run: function() { | ||
| 493 | try { | ||
| 494 | try { | ||
| 495 | if (!this.isWaiting) this.setup.bind(this)(); | ||
| 496 | this.isWaiting = false; | ||
| 497 | this.test.bind(this)(); | ||
| 498 | } finally { | ||
| 499 | if(!this.isWaiting) { | ||
| 500 | this.teardown.bind(this)(); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | } | ||
| 504 | catch(e) { this.error(e); } | ||
| 505 | } | ||
| 506 | }); | ||
| 507 | |||
| 508 | // *EXPERIMENTAL* BDD-style testing to please non-technical folk | ||
| 509 | // This draws many ideas from RSpec http://rspec.rubyforge.org/ | ||
| 510 | |||
| 511 | Test.setupBDDExtensionMethods = function(){ | ||
| 512 | var METHODMAP = { | ||
| 513 | shouldEqual: 'assertEqual', | ||
| 514 | shouldNotEqual: 'assertNotEqual', | ||
| 515 | shouldEqualEnum: 'assertEnumEqual', | ||
| 516 | shouldBeA: 'assertType', | ||
| 517 | shouldNotBeA: 'assertNotOfType', | ||
| 518 | shouldBeAn: 'assertType', | ||
| 519 | shouldNotBeAn: 'assertNotOfType', | ||
| 520 | shouldBeNull: 'assertNull', | ||
| 521 | shouldNotBeNull: 'assertNotNull', | ||
| 522 | |||
| 523 | shouldBe: 'assertReturnsTrue', | ||
| 524 | shouldNotBe: 'assertReturnsFalse', | ||
| 525 | shouldRespondTo: 'assertRespondsTo' | ||
| 526 | }; | ||
| 527 | var makeAssertion = function(assertion, args, object) { | ||
| 528 | this[assertion].apply(this,(args || []).concat([object])); | ||
| 529 | }; | ||
| 530 | |||
| 531 | Test.BDDMethods = {}; | ||
| 532 | $H(METHODMAP).each(function(pair) { | ||
| 533 | Test.BDDMethods[pair.key] = function() { | ||
| 534 | var args = $A(arguments); | ||
| 535 | var scope = args.shift(); | ||
| 536 | makeAssertion.apply(scope, [pair.value, args, this]); }; | ||
| 537 | }); | ||
| 538 | |||
| 539 | [Array.prototype, String.prototype, Number.prototype, Boolean.prototype].each( | ||
| 540 | function(p){ Object.extend(p, Test.BDDMethods) } | ||
| 541 | ); | ||
| 542 | }; | ||
| 543 | |||
| 544 | Test.context = function(name, spec, log){ | ||
| 545 | Test.setupBDDExtensionMethods(); | ||
| 546 | |||
| 547 | var compiledSpec = {}; | ||
| 548 | var titles = {}; | ||
| 549 | for(specName in spec) { | ||
| 550 | switch(specName){ | ||
| 551 | case "setup": | ||
| 552 | case "teardown": | ||
| 553 | compiledSpec[specName] = spec[specName]; | ||
| 554 | break; | ||
| 555 | default: | ||
| 556 | var testName = 'test'+specName.gsub(/\s+/,'-').camelize(); | ||
| 557 | var body = spec[specName].toString().split('\n').slice(1); | ||
| 558 | if(/^\{/.test(body[0])) body = body.slice(1); | ||
| 559 | body.pop(); | ||
| 560 | body = body.map(function(statement){ | ||
| 561 | return statement.strip() | ||
| 562 | }); | ||
| 563 | compiledSpec[testName] = body.join('\n'); | ||
| 564 | titles[testName] = specName; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | new Test.Unit.Runner(compiledSpec, { titles: titles, testLog: log || 'testlog', context: name }); | ||
| 568 | }; |
build/tests/index.html
(0 / 21)
|   | |||
| 1 | <html> | ||
| 2 | <head> | ||
| 3 | <title></title> | ||
| 4 | <meta name="generator" content="segun.akintayo"> | ||
| 5 | <meta name="author" content="Akintayo A. Olusegun"> | ||
| 6 | </head> | ||
| 7 | <script src="../prototype.js" language="javascript"></script> | ||
| 8 | <script src="../validator.js" language="javascript"></script> | ||
| 9 | <script src="../validator_rules.js" language="javascript"></script> | ||
| 10 | <script src="../scripty/src/scripty.js" language="javascript"></script> | ||
| 11 | <script src="../scripty/src/effects.js" language="javascript"></script> | ||
| 12 | <link rel="stylesheet" href="../css/validator.css" /> | ||
| 13 | <body> | ||
| 14 | <form name="test" action="test.php" method="POST" id="test"> | ||
| 15 | Username: <INPUT type="TEXT" name="user_name" id="user_name" title="required" /><br /> | ||
| 16 | Password: <INPUT type="PASSWORD" name="pass" id="pass" title="required" /><br /> | ||
| 17 | User SSN: <INPUT type="TEXT" name="ssn" id="ssn" title="numeric" /><br /> | ||
| 18 | <INPUT type="SUBMIT" value="Submit" /> | ||
| 19 | </form> | ||
| 20 | </body> | ||
| 21 | </html> |
build/tests/test.php
(0 / 3)
|   | |||
| 1 | <?php | ||
| 2 | echo "Submitted"; | ||
| 3 | ?> |
build/validator.js
(0 / 66)
|   | |||
| 1 | var formEvent = ""; | ||
| 2 | var elementEvent = ""; | ||
| 3 | |||
| 4 | var obj = { | ||
| 5 | validate_fm: function(event) { | ||
| 6 | form = $(event.target.id); | ||
| 7 | elements = $A(form.elements); | ||
| 8 | formEvent = event; | ||
| 9 | elements.each(validate_element); | ||
| 10 | }, | ||
| 11 | |||
| 12 | validate_et: function(event) { | ||
| 13 | element = $(event.target.id); | ||
| 14 | elementEvent = event; | ||
| 15 | validate_element(element); | ||
| 16 | } | ||
| 17 | }; | ||
| 18 | |||
| 19 | obj.validate_form = obj.validate_fm.bindAsEventListener(obj); | ||
| 20 | obj.validate_elem = obj.validate_et.bindAsEventListener(obj); | ||
| 21 | |||
| 22 | document.observe("dom:loaded", function() { | ||
| 23 | var forms = document.forms; | ||
| 24 | for(var i=0; i < forms.length; i++) { | ||
| 25 | Event.observe(forms[i].id, "submit", obj.validate_form) | ||
| 26 | } | ||
| 27 | }); | ||
| 28 | |||
| 29 | function validate_regex(validator, element) { | ||
| 30 | if(element.value.search(regex[validator]) == 0) { | ||
| 31 | return true; | ||
| 32 | } else { | ||
| 33 | return false; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | function validate_element(element) { | ||
| 38 | validate_class_array = element.title.split(" "); | ||
| 39 | |||
| 40 | validate_class_array.each(function(one_validate_class) { | ||
| 41 | if(one_validate_class.search(/^\s*$/) == -1) { | ||
| 42 | if(validate_regex(one_validate_class, element)) { | ||
| 43 | span_id = "error-span" + element.id; | ||
| 44 | if($(span_id) != null) | ||
| 45 | $(span_id).remove(); | ||
| 46 | element.removeClassName("error-validate"); | ||
| 47 | element.addClassName("success-validate"); | ||
| 48 | } else { | ||
| 49 | element.removeClassName("success-validate"); | ||
| 50 | element.addClassName("error-validate"); | ||
| 51 | element.focus(); | ||
| 52 | span_id = "error-span" + element.id; | ||
| 53 | if($(span_id) != null) | ||
| 54 | $(span_id).remove(); | ||
| 55 | element.insert({ | ||
| 56 | after: "<div id=" + span_id + " style=display:none>" + | ||
| 57 | "<span class=error-validate-message>" + messages[one_validate_class] + "</span>" + | ||
| 58 | "</div>" | ||
| 59 | }); | ||
| 60 | Effect.Appear(span_id); | ||
| 61 | Event.observe(element, "change", obj.validate_elem); | ||
| 62 | Event.stop(formEvent); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | }); | ||
| 66 | } |
build/validator_rules.js
(0 / 15)
|   | |||
| 1 | var messages = { | ||
| 2 | "required": "This field is required", | ||
| 3 | "numeric": "This field must contain a number", | ||
| 4 | "gsm": "This field must contain a valid nigerian phone number", | ||
| 5 | "year": "This field must contain a 4 digit year", | ||
| 6 | "alpha": "This field must contain only alphabets" | ||
| 7 | } | ||
| 8 | |||
| 9 | var regex = { | ||
| 10 | "required": /\w/, | ||
| 11 | "numeric": /^\d\d*$/, | ||
| 12 | "gsm": /^(080|070)[2-9]\d{7}$/, | ||
| 13 | "year": /^\d{4}$/, | ||
| 14 | "alpha": /^[a-zA-Z][a-zA-Z]*$/ | ||
| 15 | } |
|   | |||
| 1 | copy.src.files=true | ||
| 2 | copy.src.target=/var/www/validator | ||
| 3 | index.file=index.php | ||
| 4 | run.as=LOCAL | ||
| 5 | url=http://localhost/validator/ |
nbproject/project.properties
(6 / 0)
|   | |||
| 1 | include.path=${php.global.include.path} | ||
| 2 | source.encoding=UTF-8 | ||
| 3 | src.dir=. | ||
| 4 | tags.asp=false | ||
| 5 | tags.short=true | ||
| 6 | web.root=. |
nbproject/project.xml
(9 / 0)
|   | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | <project xmlns="http://www.netbeans.org/ns/project/1"> | ||
| 3 | <type>org.netbeans.modules.php.project</type> | ||
| 4 | <configuration> | ||
| 5 | <data xmlns="http://www.netbeans.org/ns/php-project/1"> | ||
| 6 | <name>validator</name> | ||
| 7 | </data> | ||
| 8 | </configuration> | ||
| 9 | </project> |
tests/index.html
(4 / 1)
|   | |||
| 14 | 14 | <form name="test" action="test.php" method="POST" id="test"> | |
| 15 | 15 | Username: <INPUT type="TEXT" name="user_name" id="user_name" title="required" /><br /> | |
| 16 | 16 | Password: <INPUT type="PASSWORD" name="pass" id="pass" title="required" /><br /> | |
| 17 | User SSN: <INPUT type="TEXT" name="ssn" id="ssn" title="numeric length:11" /><br /> | ||
| 17 | User SSN: <INPUT type="TEXT" name="ssn" id="ssn" title="numeric length{11}" /><br /> | ||
| 18 | User CustomF1: <INPUT type="TEXT" name="ct1" id="ct1" title="length{3:6}" /><br /> | ||
| 19 | User CustomF2: <INPUT type="TEXT" name="ct1" id="ct2" title="equals:don" /><br /> | ||
| 20 | User CustomF3: <INPUT type="PASSWORD" name="ct1" id="ct3" title="equals:#pass" /><br /> | ||
| 18 | 21 | <INPUT type="SUBMIT" value="Submit" /> | |
| 19 | 22 | </form> | |
| 20 | 23 | </body> |
validator.js
(62 / 6)
|   | |||
| 22 | 22 | obj.validate_elem = obj.validate_et.bindAsEventListener(obj); | |
| 23 | 23 | ||
| 24 | 24 | document.observe("dom:loaded", function() { | |
| 25 | var forms = document.forms; | ||
| 26 | for(var i=0; i < forms.length; i++) { | ||
| 25 | forms = document.forms; | ||
| 26 | for(i=0; i < forms.length; i++) { | ||
| 27 | 27 | thisForm = forms[i]; | |
| 28 | 28 | Event.observe(forms[i].id, "submit", obj.validate_form) | |
| 29 | 29 | } | |
| 30 | 30 | }); | |
| 31 | 31 | ||
| 32 | function do_min_max_length(v) { | ||
| 33 | pos = v.indexOf(":", 0); | ||
| 34 | front = v.substr(0, pos); | ||
| 35 | back = v.substr(pos, v.length); | ||
| 36 | front = front.replace(/[^\d*]/g, ""); | ||
| 37 | back = back.replace(/[^\d*]/g, ""); | ||
| 38 | pre_def = "length{x,y}"; | ||
| 39 | if(messages[pre_def] == undefined) { | ||
| 40 | messages[v] = "This field must have at least " + front + " char(s) and at most " + back + " char(s)"; | ||
| 41 | } else { | ||
| 42 | messages[v] = messages[pre_def].replace(/\{x\}/, front).replace(/\{y\}/,back); | ||
| 43 | } | ||
| 44 | |||
| 45 | regex[v] = new RegExp("^.{" + front + "," + back + "}$"); | ||
| 46 | } | ||
| 47 | |||
| 48 | function do_min_length(v) { | ||
| 49 | len = v.replace(/[^\d*]/g, ""); | ||
| 50 | pre_def = "length{x}"; | ||
| 51 | if(messages[pre_def] == undefined) { | ||
| 52 | messages[v] = "This field must contain exactly " + len + " charaters"; | ||
| 53 | } else { | ||
| 54 | messages[v] = messages[pre_def].replace(/\{x\}/,len); | ||
| 55 | } | ||
| 56 | regex[v] = new RegExp("^.{" + len + "}$"); | ||
| 57 | } | ||
| 58 | |||
| 59 | function do_equals_word(v) { | ||
| 60 | word = v.substr(v.indexOf(":") + 1, v.length); | ||
| 61 | pre_def = "equals:x"; | ||
| 62 | if(messages[pre_def] == undefined) { | ||
| 63 | messages[v] = "This field must be equal to " + word; | ||
| 64 | } else { | ||
| 65 | messages[v] = messages[pre_def].replace(/\{x\}/,word); | ||
| 66 | } | ||
| 67 | regex[v] = new RegExp("^" + word + "$"); | ||
| 68 | } | ||
| 69 | |||
| 70 | function do_equals_id(v) { | ||
| 71 | id = v.substr(v.indexOf("#") + 1, v.length); | ||
| 72 | pre_def = "equals:#id"; | ||
| 73 | if(messages[pre_def] == undefined) { | ||
| 74 | messages[v] = "This field must match with " + $(id).name; | ||
| 75 | } else { | ||
| 76 | messages[v] = messages[pre_def].replace(/\{n\}/,$(id).name); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 32 | 80 | function validate_regex(validator, element) { | |
| 33 | if(validator.search(/(length\:\d*|length\{\d*\})/) == 0) { | ||
| 34 | len = validator.replace(/[^\d*]/g, ""); | ||
| 35 | messages[validator] = "This field must contain exactly " + len + " charaters"; | ||
| 36 | regex[validator] = new RegExp("^.{" + len + "}$"); | ||
| 81 | if(validator.search(/length\{\d*\}/) == 0) { | ||
| 82 | do_min_length(validator); | ||
| 37 | 83 | } | |
| 84 | else if(validator.search(/length\{\d*:\d*\}/) == 0) { | ||
| 85 | do_min_max_length(validator); | ||
| 86 | } | ||
| 87 | else if(validator.search(/equals:\#\w/) == 0) { | ||
| 88 | do_equals_id(validator); | ||
| 89 | } | ||
| 90 | else if(validator.search(/equals:\W*/) == 0) { | ||
| 91 | do_equals_word(validator); | ||
| 92 | } | ||
| 93 | |||
| 38 | 94 | if(element.value.search(regex[validator]) == 0) { | |
| 39 | 95 | return true; | |
| 40 | 96 | } else { |
validator_rules.js
(12 / 4)
|   | |||
| 7 | 7 | "alphanum": "This field must contain alphabets and digits only", | |
| 8 | 8 | "ddmmyyyy": "This field must contain a valid date in the format dd-mm-yyyy or dd/mm/yyyy", | |
| 9 | 9 | "yyyymmdd": "This field must contain a valid date in the format yyyy-mm-dd or yyyy/mm/dd", | |
| 10 | "email": "This field must contain a valid email address" | ||
| 10 | "email": "This field must contain a valid email address", | ||
| 11 | "length{x}": "This field must have at least {x} characters", | ||
| 12 | "length{x,y}": "This field must have at least {x} characters and at most {y} chars", | ||
| 13 | "equals:x": "This field must be equal to the word '{x}'", | ||
| 14 | "equals:#id": "This field must match the value of another field called {n}" | ||
| 11 | 15 | } | |
| 12 | 16 | ||
| 13 | 17 | var regex = { | |
| … | … | ||
| 21 | 21 | "year": /^\d{4}$/, | |
| 22 | 22 | "alpha": /^[a-zA-Z]+$/, | |
| 23 | 23 | "alphanum": /^([a-zA-Z]+|\d+)+$/, | |
| 24 | "ddmmyyyy": /(0[1-9]|[12][0-9]|3[01])[-/. ](0[1-9]|1[0-2])[-/. ](19|20)\d\d/, | ||
| 25 | "yyyymmdd": /(19|20)\d\d[-/. ](0[1-9]|1[0-2])[-/. ](0[1-9]|[12][0-9]|3[01])/, | ||
| 26 | "email": /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/ | ||
| 24 | "email": /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/, | ||
| 25 | "ddmmyyyy": /(0?[1-9]|[12][0-9]|3[01])([-. ]|\/)(0?[1-9]|1[0-2])([-. ]|\/)(19|20)\d\d/, | ||
| 26 | "yyyymmdd": /(19|20)\d\d([-. ]|\/)(0?[1-9]|1[0-2])([-. ]|\/)(0?[1-9]|[12][0-9]|3[01])/ | ||
| 27 | 27 | } | |
| 28 | |||
| 29 | /* | ||
| 30 | * For equals, {n} will be replaced by document.getElementById(id).name | ||
| 31 | */ |

