Changeset 7296
- Timestamp:
- 08/09/07 09:27:11 (2 years ago)
- Files:
-
- spinoffs/scriptaculous/CHANGELOG (modified) (1 diff)
- spinoffs/scriptaculous/lib/prototype.js (modified) (47 diffs)
- spinoffs/scriptaculous/src/effects.js (modified) (15 diffs)
- spinoffs/scriptaculous/test/unit/effects_test.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/scriptaculous/CHANGELOG
r7292 r7296 1 1 *SVN* 2 3 * Some effect code refactoring to take advantage of new Prototype 1.6 features. 4 5 * Effect.ScrollTo is now no longer a core effect (returns a Effect.Tween instance now). 2 6 3 7 * Add Effect.Tween, a new core effect. This effect tweens between two values and sets a property or calls a method on an object (including DOM elements); or allows for a callback method, which will be automatically bound to the object. spinoffs/scriptaculous/lib/prototype.js
r7287 r7296 39 39 var Class = { 40 40 create: function(parent, methods) { 41 if (arguments.length == 1 && typeof parent !== 'function')41 if (arguments.length == 1 && !Object.isFunction(parent)) 42 42 methods = parent, parent = null; 43 43 … … 175 175 isFunction: function(object) { 176 176 return typeof object == "function"; 177 }, 178 179 isString: function(object) { 180 return typeof object == "string"; 181 }, 182 183 isNumber: function(object) { 184 return typeof object == "number"; 185 }, 186 187 isUndefined: function(object) { 188 return typeof object == "undefined"; 177 189 } 178 190 }); … … 180 192 Object.extend(Function.prototype, { 181 193 argumentNames: function() { 182 var names = this.toString().match(/^ function\s*\((.*?)\)/)[1].split(",").invoke("strip");194 var names = this.toString().match(/^[\s\(]*function\s*\((.*?)\)/)[1].split(",").invoke("strip"); 183 195 return names.length == 1 && !names[0] ? [] : names; 184 196 }, … … 250 262 returnValue = lambda(); 251 263 break; 252 } catch (e) { }264 } catch (e) { } 253 265 } 254 266 … … 388 400 toQueryParams: function(separator) { 389 401 var match = this.strip().match(/([^?#]*)(#.*)?$/); 390 if (!match) return { };391 392 return match[1].split(separator || '&').inject({ }, function(hash, pair) {402 if (!match) return { }; 403 404 return match[1].split(separator || '&').inject({ }, function(hash, pair) { 393 405 if ((pair = pair.split('='))[0]) { 394 406 var key = decodeURIComponent(pair.shift()); … … 513 525 514 526 String.prototype.gsub.prepareReplacement = function(replacement) { 515 if ( typeof replacement == 'function') return replacement;527 if (Object.isFunction(replacement)) return replacement; 516 528 var template = new Template(replacement); 517 529 return function(match) { return template.evaluate(match) }; … … 536 548 537 549 evaluate: function(object) { 538 if ( typeof object.toTemplateReplacements == 'function')550 if (Object.isFunction(object.toTemplateReplacements)) 539 551 object = object.toTemplateReplacements(); 540 552 … … 562 574 }; 563 575 564 var $break = { };576 var $break = { }; 565 577 566 578 var Enumerable = { … … 641 653 var results = []; 642 654 643 if ( typeof filter == "string")655 if (Object.isString(filter)) 644 656 filter = new RegExp(filter); 645 657 … … 652 664 653 665 include: function(object) { 654 if ( typeof this.indexOf == 'function')666 if (Object.isFunction(this.indexOf)) 655 667 return this.indexOf(object) != -1; 656 668 … … 754 766 zip: function() { 755 767 var iterator = Prototype.K, args = $A(arguments); 756 if ( typeof args.last() == 'function')768 if (Object.isFunction(args.last())) 757 769 iterator = args.pop(); 758 770 … … 796 808 function $A(iterable) { 797 809 if (!iterable) return []; 798 if (!( typeof iterable == 'function'&& iterable == '[object NodeList]') &&799 iterable.toArray) {810 if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && 811 iterable.toArray) { 800 812 return iterable.toArray(); 801 813 } else { … … 898 910 899 911 // use native browser JS 1.6 implementation if available 900 if ( typeof Array.prototype.forEach == 'function')912 if (Object.isFunction(Array.prototype.forEach)) 901 913 Array.prototype._each = Array.prototype.forEach; 902 914 … … 967 979 var Hash = function(object) { 968 980 if (object instanceof Hash) this.merge(object); 969 else Object.extend(this, object || { });981 else Object.extend(this, object || { }); 970 982 }; 971 983 … … 1157 1169 dispatch: function(callback, request, transport, json) { 1158 1170 this.each(function(responder) { 1159 if ( typeof responder[callback] == 'function') {1171 if (Object.isFunction(responder[callback])) { 1160 1172 try { 1161 1173 responder[callback].apply(responder, [request, transport, json]); 1162 } catch (e) { }1174 } catch (e) { } 1163 1175 } 1164 1176 }); … … 1177 1189 }); 1178 1190 1179 Ajax.Base = function() { };1191 Ajax.Base = function() { }; 1180 1192 Ajax.Base.prototype = { 1181 1193 setOptions: function(options) { … … 1189 1201 evalJS: true 1190 1202 }; 1191 Object.extend(this.options, options || { });1203 Object.extend(this.options, options || { }); 1192 1204 1193 1205 this.options.method = this.options.method.toLowerCase(); 1194 if ( typeof this.options.parameters == 'string')1206 if (Object.isString(this.options.parameters)) 1195 1207 this.options.parameters = this.options.parameters.toQueryParams(); 1196 1208 } … … 1287 1299 var extras = this.options.requestHeaders; 1288 1300 1289 if ( typeof extras.push == 'function')1301 if (Object.isFunction(extras.push)) 1290 1302 for (var i = 0, length = extras.length; i < length; i += 2) 1291 1303 headers[extras[i]] = extras[i+1]; … … 1461 1473 if (receiver = $(receiver)) { 1462 1474 if (options.insertion) { 1463 if ( typeof options.insertion == 'string') {1464 var insertion = { }; insertion[options.insertion] = responseText;1475 if (Object.isString(options.insertion)) { 1476 var insertion = { }; insertion[options.insertion] = responseText; 1465 1477 receiver.insert(insertion); 1466 1478 } … … 1485 1497 this.decay = (this.options.decay || 1); 1486 1498 1487 this.updater = { };1499 this.updater = { }; 1488 1500 this.container = container; 1489 1501 this.url = url; … … 1523 1535 return elements; 1524 1536 } 1525 if ( typeof element == 'string')1537 if (Object.isString(element)) 1526 1538 element = document.getElementById(element); 1527 1539 return Element.extend(element); … … 1632 1644 element = $(element); 1633 1645 1634 if ( typeof insertions == 'string' || typeof insertions == 'number'||1646 if (Object.isString(insertions) || Object.isNumber(insertions) || 1635 1647 Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) 1636 1648 insertions = {bottom:insertions}; … … 1665 1677 if (Object.isElement(wrapper)) 1666 1678 $(wrapper).writeAttribute(attributes || { }); 1667 else if ( typeof wrapper == 'string') wrapper = new Element(wrapper, attributes);1679 else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes); 1668 1680 else wrapper = new Element('div', wrapper); 1669 1681 if (element.parentNode) … … 1728 1740 1729 1741 match: function(element, selector) { 1730 if ( typeof selector == 'string')1742 if (Object.isString(selector)) 1731 1743 selector = new Selector(selector); 1732 1744 return selector.match($(element)); … … 1902 1914 element = $(element); 1903 1915 var elementStyle = element.style, match; 1904 if ( typeof styles === 'string') {1916 if (Object.isString(styles)) { 1905 1917 element.style.cssText += ';' + styles; 1906 1918 return styles.include('opacity') ? … … 2199 2211 element = $(element); 2200 2212 2201 if ( typeof insertions == 'string' || typeof insertions == 'number'||2213 if (Object.isString(insertions) || Object.isNumber(insertions) || 2202 2214 Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) 2203 insertions = { bottom:insertions};2215 insertions = { bottom: insertions }; 2204 2216 2205 2217 var t = Element._insertionTranslations, content, position, pos, tagName; … … 2575 2587 for (property in methods) { 2576 2588 value = methods[property]; 2577 if ( typeof value == 'function'&& !(property in element))2589 if (Object.isFunction(value) && !(property in element)) 2578 2590 element[property] = value.methodize(); 2579 2591 } … … 2637 2649 for (var property in methods) { 2638 2650 var value = methods[property]; 2639 if ( typeof value != 'function') continue;2651 if (!Object.isFunction(value)) continue; 2640 2652 if (!onlyIfAbsent || !(property in destination)) 2641 2653 destination[property] = value.methodize(); … … 2676 2688 for (var tag in Element.Methods.ByTag) { 2677 2689 var klass = findDOMClass(tag); 2678 if ( typeof klass == "undefined") continue;2690 if (Object.isUndefined(klass)) continue; 2679 2691 copy(T[tag], klass.prototype); 2680 2692 } … … 2686 2698 if (Element.extend.refresh) Element.extend.refresh(); 2687 2699 Element.cache = { }; 2700 }; 2701 2702 document.viewport = { 2703 getDimensions: function() { 2704 var dimensions = { }; 2705 $w('width height').each(function(d) { 2706 var D = d.capitalize(); 2707 dimensions[d] = self['inner' + D] || 2708 (document.documentElement['client' + D] || document.body['client' + D]); 2709 }); 2710 return dimensions; 2711 }, 2712 2713 getWidth: function() { 2714 return this.getDimensions().width; 2715 }, 2716 2717 getHeight: function() { 2718 return this.getDimensions().height; 2719 }, 2720 2721 getScrollOffsets: function() { 2722 return Element._returnOffset( 2723 window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, 2724 window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); 2725 } 2688 2726 }; 2689 2727 /* Portions of the Selector class are derived from Jack Slocumâs DomQuery, … … 2708 2746 2709 2747 if (Selector._cache[e]) { 2710 this.matcher = Selector._cache[e]; return; 2711 } 2748 this.matcher = Selector._cache[e]; 2749 return; 2750 } 2751 2712 2752 this.matcher = ["this.matcher = function(root) {", 2713 2753 "var r = root, h = Selector.handlers, c = false, n;"]; … … 2718 2758 p = ps[i]; 2719 2759 if (m = e.match(p)) { 2720 this.matcher.push( typeof c[i] == 'function'? c[i](m) :2760 this.matcher.push(Object.isFunction(c[i]) ? c[i](m) : 2721 2761 new Template(c[i]).evaluate(m)); 2722 2762 e = e.replace(m[0], ''); … … 2744 2784 for (var i in ps) { 2745 2785 if (m = e.match(ps[i])) { 2746 this.matcher.push( typeof x[i] == 'function'? x[i](m) :2786 this.matcher.push(Object.isFunction(x[i]) ? x[i](m) : 2747 2787 new Template(x[i]).evaluate(m)); 2748 2788 e = e.replace(m[0], ''); … … 2808 2848 2809 2849 Object.extend(Selector, { 2810 _cache: { },2850 _cache: { }, 2811 2851 2812 2852 xpath: { … … 2830 2870 var h = Selector.xpath.pseudos[m[1]]; 2831 2871 if (!h) return ''; 2832 if ( typeof h === 'function') return h(m);2872 if (Object.isFunction(h)) return h(m); 2833 2873 return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); 2834 2874 }, … … 2859 2899 for (var i in p) { 2860 2900 if (m = e.match(p[i])) { 2861 v = typeof x[i] == 'function'? x[i](m) : new Template(x[i]).evaluate(m);2901 v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m); 2862 2902 exclusion.push("(" + v.substring(1, v.length - 1) + ")"); 2863 2903 e = e.replace(m[0], ''); … … 3293 3333 3294 3334 findElement: function(elements, expression, index) { 3295 if ( typeof expression == 'number') {3335 if (Object.isNumber(expression)) { 3296 3336 index = expression; expression = false; 3297 3337 } … … 3327 3367 var key, value, submitted = false, submit = options.submit; 3328 3368 3329 var data = elements.inject({ }, function(result, element) {3369 var data = elements.inject({ }, function(result, element) { 3330 3370 if (!element.disabled && element.name) { 3331 3371 key = element.name; value = $(element).getValue(); … … 3410 3450 3411 3451 request: function(form, options) { 3412 form = $(form), options = Object.clone(options || { });3452 form = $(form), options = Object.clone(options || { }); 3413 3453 3414 3454 var params = options.parameters, action = form.readAttribute('action') || ''; … … 3417 3457 3418 3458 if (params) { 3419 if ( typeof params == 'string') params = params.toQueryParams();3459 if (Object.isString(params)) params = params.toQueryParams(); 3420 3460 Object.extend(options.parameters, params); 3421 3461 } … … 3448 3488 var value = element.getValue(); 3449 3489 if (value != undefined) { 3450 var pair = { };3490 var pair = { }; 3451 3491 pair[element.name] = value; 3452 3492 return Hash.toQueryString(pair); … … 3485 3525 !['button', 'reset', 'submit'].include(element.type))) 3486 3526 element.select(); 3487 } catch (e) { }3527 } catch (e) { } 3488 3528 return element; 3489 3529 }, … … 3575 3615 /*--------------------------------------------------------------------------*/ 3576 3616 3577 Abstract.TimedObserver = function() { };3617 Abstract.TimedObserver = function() { }; 3578 3618 Abstract.TimedObserver.prototype = { 3579 3619 initialize: function(element, frequency, callback) { … … 3592 3632 onTimerEvent: function() { 3593 3633 var value = this.getValue(); 3594 var changed = ( 'string' == typeof this.lastValue && 'string' == typeof value3634 var changed = (Object.isString(this.lastValue) && Object.isString(value) 3595 3635 ? this.lastValue != value : String(this.lastValue) != String(value)); 3596 3636 if (changed) { … … 3617 3657 /*--------------------------------------------------------------------------*/ 3618 3658 3619 Abstract.EventObserver = function() { };3659 Abstract.EventObserver = function() { }; 3620 3660 Abstract.EventObserver.prototype = { 3621 3661 initialize: function(element, callback) { … … 3892 3932 3893 3933 event.eventName = eventName; 3894 event.memo = memo || { };3934 event.memo = memo || { }; 3895 3935 3896 3936 if (document.createEvent) { … … 4075 4115 4076 4116 clone: function(source, target, options) { 4077 options = options || { };4117 options = options || { }; 4078 4118 return Element.clonePosition(target, source, options); 4079 4119 } spinoffs/scriptaculous/src/effects.js
r7292 r7296 63 63 /*--------------------------------------------------------------------------*/ 64 64 65 Array.prototype.call = function() {66 var args = arguments;67 this.each(function(f){ f.apply(this, args) });68 }69 70 /*--------------------------------------------------------------------------*/71 72 65 var Effect = { 73 66 _elementDoesNotExistError: { … … 170 163 /* ------------- core effects ------------- */ 171 164 172 Effect.ScopedQueue = Class.create(); 173 Object.extend(Object.extend(Effect.ScopedQueue.prototype, Enumerable), { 165 Effect.ScopedQueue = Class.create(Class.mixin({ 174 166 initialize: function() { 175 167 this.effects = []; … … 223 215 this.effects[i] && this.effects[i].loop(timePos); 224 216 } 225 } );217 }, Enumerable)); 226 218 227 219 Effect.Queues = { … … 323 315 } 324 316 325 Effect.Parallel = Class.create(); 326 Object.extend(Object.extend(Effect.Parallel.prototype, Effect.Base.prototype), { 317 Effect.Parallel = Class.create(Effect.Base, { 327 318 initialize: function(effects) { 328 319 this.effects = effects || []; … … 345 336 Effect.Tween = Class.create(Effect.Base, { 346 337 initialize: function(object, from, to) { 347 object = typeof object == 'string'? $(object) : object;338 object = Object.isString(object) ? $(object) : object; 348 339 var args = $A(arguments), method = args.last(), 349 340 options = args.length == 5 ? args[3] : null; 350 this.method = typeof method == 'function'? method.bind(object) :351 typeof object[method] == 'function'? object[method].bind(object) :341 this.method = Object.isFunction(method) ? method.bind(object) : 342 Object.isFunction(object[method]) ? object[method].bind(object) : 352 343 function(value) { object[method] = value }; 353 344 this.start(Object.extend({ from: from, to: to }, options || {})); … … 358 349 }); 359 350 360 Effect.Event = Class.create(); 361 Object.extend(Object.extend(Effect.Event.prototype, Effect.Base.prototype), { 351 Effect.Event = Class.create(Effect.Base, { 362 352 initialize: function() { 363 353 var options = Object.extend({ … … 369 359 }); 370 360 371 Effect.Opacity = Class.create(); 372 Object.extend(Object.extend(Effect.Opacity.prototype, Effect.Base.prototype), { 361 Effect.Opacity = Class.create(Effect.Base, { 373 362 initialize: function(element) { 374 363 this.element = $(element); … … 388 377 }); 389 378 390 Effect.Move = Class.create(); 391 Object.extend(Object.extend(Effect.Move.prototype, Effect.Base.prototype), { 379 Effect.Move = Class.create(Effect.Base, { 392 380 initialize: function(element) { 393 381 this.element = $(element); … … 401 389 }, 402 390 setup: function() { 403 // Bug in Opera: Opera returns the "real" position of a static element or404 // relative element that does not have top/left explicitly set.405 // ==> Always set top and left for position relative elements in your stylesheets406 // (to 0 if you do not need them)407 391 this.element.makePositioned(); 408 392 this.originalLeft = parseFloat(this.element.getStyle('left') || '0'); 409 393 this.originalTop = parseFloat(this.element.getStyle('top') || '0'); 410 394 if(this.options.mode == 'absolute') { 411 // absolute movement, so we need to calc deltaX and deltaY412 395 this.options.x = this.options.x - this.originalLeft; 413 396 this.options.y = this.options.y - this.originalTop; … … 428 411 }; 429 412 430 Effect.Scale = Class.create(); 431 Object.extend(Object.extend(Effect.Scale.prototype, Effect.Base.prototype), { 413 Effect.Scale = Class.create(Effect.Base, { 432 414 initialize: function(element, percent) { 433 415 this.element = $(element); … … 503 485 }); 504 486 505 Effect.Highlight = Class.create(); 506 Object.extend(Object.extend(Effect.Highlight.prototype, Effect.Base.prototype), { 487 Effect.Highlight = Class.create(Effect.Base, { 507 488 initialize: function(element) { 508 489 this.element = $(element); … … 539 520 }); 540 521 541 Effect.ScrollTo = Class.create(); 542 Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), { 543 initialize: function(element) { 544 this.element = $(element); 522 Effect.ScrollTo = function(element) { 523 var options = arguments[1] || {}, 524 scrollOffsets = document.viewport.getScrollOffsets(), 525 elementOffsets = $(element).cumulativeOffset(), 526 max = (window.height || document.body.scrollHeight) - document.viewport.getHeight(); 527 528 if(options.offset) elementOffsets[1] += options.offset; 529 530 return new Effect.Tween(null, 531 scrollOffsets.top, 532 elementOffsets[1] > max ? max : elementOffsets[1], 533 options, 534 function(p){ scrollTo(scrollOffsets.left, p.round()) } 535 ); 536 }; 537 /* this.element = $(element); 545 538 this.start(arguments[1] || {}); 546 539 }, … … 562 555 this.scrollStart + (position*this.delta)); 563 556 } 564 }); 557 });*/ 565 558 566 559 /* ------------- combination effects ------------- */ … … 943 936 }; 944 937 945 Effect.Morph = Class.create(); 946 Object.extend(Object.extend(Effect.Morph.prototype, Effect.Base.prototype), { 938 Effect.Morph = Class.create(Effect.Base, { 947 939 initialize: function(element) { 948 940 this.element = $(element); … … 1027 1019 (Math.round(transform.originalValue[2]+ 1028 1020 (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() : 1029 transform.originalValue +1030 ( (transform.targetValue - transform.originalValue) * position).toFixed(3) + transform.unit;1021 (transform.originalValue + 1022 (transform.targetValue - transform.originalValue) * position).toFixed(3) + transform.unit; 1031 1023 this.element.setStyle(style, true); 1032 1024 } 1033 1025 }); 1034 1026 1035 Effect.Transform = Class.create(); 1036 Object.extend(Effect.Transform.prototype, { 1027 Effect.Transform = Class.create({ 1037 1028 initialize: function(tracks){ 1038 1029 this.tracks = []; spinoffs/scriptaculous/test/unit/effects_test.html
r7292 r7296 443 443 $('error_message').morph('font-size:17px').morph('opacity:0',{delay:3}); 444 444 wait(4100,function(){ // 3000ms delay + 1000ms default duration 445 assertEqual(0, $('error_message').get Opacity());445 assertEqual(0, $('error_message').getStyle('opacity')); 446 446 }); 447 447 }},