Skip to:
Content

BuddyPress.org

Ticket #6266: 6266-jquery-scroll-to.diff

File 6266-jquery-scroll-to.diff, 8.5 KB (added by netweb, 11 years ago)
  • src/bp-core/js/jquery-scroll-to.js

     
    1 /* jshint undef: false */
    2 /* jshint -W065 */
    3 
    41/*!
    52 * jQuery.ScrollTo
    63 * Copyright (c) 2007-2014 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
     
    118 * @version 1.4.12
    129 */
    1310
    14 (function(factory) {
    15         // AMD
    16         if (typeof define === 'function' && define.amd) {
    17                 define(['jquery'], factory);
    18         // CommonJS
    19         } else if (typeof exports === 'object') {
    20                 factory(require('jquery'));
    21         // Browser globals
    22         } else {
    23                 factory(jQuery);
    24         }
     11;(function(plugin) {
     12    // AMD Support
     13    if (typeof define === 'function' && define.amd) {
     14        define(['jquery'], plugin);
     15    } else {
     16        plugin(jQuery);
     17    }
    2518}(function($) {
    2619
    27         var $scrollTo = $.scrollTo = function(target, duration, settings) {
    28                 return $(window).scrollTo(target, duration, settings);
     20        var $scrollTo = $.scrollTo = function( target, duration, settings ) {
     21                return $(window).scrollTo( target, duration, settings );
    2922        };
    3023
    3124        $scrollTo.defaults = {
    32                 axis: 'xy',
     25                axis:'xy',
    3326                duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1,
    34                 limit: true
     27                limit:true
    3528        };
    3629
    3730        // Returns the element that needs to be animated to scroll the window.
    3831        // Kept for backwards compatibility (specially for localScroll & serialScroll)
    39         $scrollTo.window = function() {
     32        $scrollTo.window = function( scope ) {
    4033                return $(window)._scrollable();
    4134        };
    4235
     
    4538        $.fn._scrollable = function() {
    4639                return this.map(function() {
    4740                        var elem = this,
    48                                         isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) !== -1;
     41                                isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
    4942
    50                         if (!isWin) {
    51                                 return elem;
    52                         }
     43                                if (!isWin)
     44                                        return elem;
    5345
    5446                        var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
    5547
    56                         return /webkit/i.test(navigator.userAgent) || doc.compatMode === 'BackCompat' ?
    57                                         doc.body :
    58                                         doc.documentElement;
     48                        return /webkit/i.test(navigator.userAgent) || doc.compatMode == 'BackCompat' ?
     49                                doc.body :
     50                                doc.documentElement;
    5951                });
    6052        };
    6153
    62         $.fn.scrollTo = function(target, duration, settings) {
    63                 if (typeof duration === 'object') {
     54        $.fn.scrollTo = function( target, duration, settings ) {
     55                if (typeof duration == 'object') {
    6456                        settings = duration;
    6557                        duration = 0;
    6658                }
    67                 if (typeof settings === 'function') {
    68                         settings = {onAfter: settings};
    69                 }
     59                if (typeof settings == 'function')
     60                        settings = { onAfter:settings };
    7061
    71                 if (target === 'max') {
     62                if (target == 'max')
    7263                        target = 9e9;
    73                 }
    7464
    75                 settings = $.extend({}, $scrollTo.defaults, settings);
     65                settings = $.extend( {}, $scrollTo.defaults, settings );
    7666                // Speed is still recognized for backwards compatibility
    7767                duration = duration || settings.duration;
    7868                // Make sure the settings are given right
    7969                settings.queue = settings.queue && settings.axis.length > 1;
    8070
    81                 // Let's keep the overall duration
    82                 if (settings.queue) {
     71                if (settings.queue)
     72                        // Let's keep the overall duration
    8373                        duration /= 2;
    84                 }
     74                settings.offset = both( settings.offset );
     75                settings.over = both( settings.over );
    8576
    86                 settings.offset = both(settings.offset);
    87                 settings.over = both(settings.over);
    88 
    8977                return this._scrollable().each(function() {
    90 
    9178                        // Null target yields nothing, just like jQuery does
    92                         if (target === null) {
    93                                 return;
    94                         }
     79                        if (target == null) return;
    9580
    9681                        var elem = this,
    97                                         $elem = $(elem),
    98                                         targ = target, toff, attr = {},
    99                                         win = $elem.is('html,body');
     82                                $elem = $(elem),
     83                                targ = target, toff, attr = {},
     84                                win = $elem.is('html,body');
    10085
    10186                        switch (typeof targ) {
    10287                                // A number will pass the regex
     
    10388                                case 'number':
    10489                                case 'string':
    10590                                        if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
    106                                                 targ = both(targ);
     91                                                targ = both( targ );
    10792                                                // We are done
    10893                                                break;
    10994                                        }
    11095                                        // Relative/Absolute selector, no break!
    11196                                        targ = win ? $(targ) : $(targ, this);
    112                                         if (!targ.length) {
    113                                                 return;
    114                                         }
    115                                         /* falls through */
     97                                        if (!targ.length) return;
    11698                                case 'object':
    11799                                        // DOMElement / jQuery
    118                                         if (targ.is || targ.style) {
     100                                        if (targ.is || targ.style)
    119101                                                // Get the real position of the target
    120102                                                toff = (targ = $(targ)).offset();
    121                                         }
    122103                        }
    123 
     104                       
    124105                        var offset = $.isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset;
     106                       
     107                        $.each( settings.axis.split(''), function( i, axis ) {
     108                                var Pos = axis == 'x' ? 'Left' : 'Top',
     109                                        pos = Pos.toLowerCase(),
     110                                        key = 'scroll' + Pos,
     111                                        old = elem[key],
     112                                        max = $scrollTo.max(elem, axis);
    125113
    126                         $.each(settings.axis.split(''), function(i, axis) {
    127                                 var Pos = axis === 'x' ? 'Left' : 'Top',
    128                                                 pos = Pos.toLowerCase(),
    129                                                 key = 'scroll' + Pos,
    130                                                 old = elem[key],
    131                                                 max = $scrollTo.max(elem, axis);
    132 
    133114                                if (toff) {// jQuery / DOMElement
    134                                         attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]);
     115                                        attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
    135116
    136117                                        // If it's a dom element, reduce the margin
    137118                                        if (settings.margin) {
    138                                                 attr[key] -= parseInt(targ.css('margin' + Pos)) || 0;
    139                                                 attr[key] -= parseInt(targ.css('border' + Pos + 'Width')) || 0;
     119                                                attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
     120                                                attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
    140121                                        }
    141122
    142123                                        attr[key] += offset[pos] || 0;
    143124
    144                                         // Scroll to a fraction of its width/height
    145                                         if (settings.over[pos]) {
    146                                                 attr[key] += targ[axis === 'x' ? 'width' : 'height']() * settings.over[pos];
    147                                         }
     125                                        if(settings.over[pos])
     126                                                // Scroll to a fraction of its width/height
     127                                                attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
    148128                                } else {
    149129                                        var val = targ[pos];
    150130                                        // Handle percentage values
    151                                         attr[key] = val.slice && val.slice(-1) === '%' ?
    152                                                         parseFloat(val) / 100 * max
    153                                                         : val;
     131                                        attr[key] = val.slice && val.slice(-1) == '%' ?
     132                                                parseFloat(val) / 100 * max
     133                                                : val;
    154134                                }
    155135
    156136                                // Number or 'number'
    157                                 if (settings.limit && /^\d+$/.test(attr[key])) {
     137                                if (settings.limit && /^\d+$/.test(attr[key]))
    158138                                        // Check the limits
    159                                         attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max);
    160                                 }
     139                                        attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );
    161140
    162141                                // Queueing axes
    163142                                if (!i && settings.queue) {
    164143                                        // Don't waste time animating, if there's no need.
    165                                         if (old !== attr[key]) {
     144                                        if (old != attr[key])
    166145                                                // Intermediate animation
    167                                                 animate(settings.onAfterFirst);
    168                                         }
     146                                                animate( settings.onAfterFirst );
    169147                                        // Don't animate this axis again in the next iteration.
    170148                                        delete attr[key];
    171149                                }
    172150                        });
    173151
    174                         animate(settings.onAfter);
     152                        animate( settings.onAfter );
    175153
    176                         function animate(callback) {
    177                                 $elem.animate(attr, duration, settings.easing, callback && function() {
     154                        function animate( callback ) {
     155                                $elem.animate( attr, duration, settings.easing, callback && function() {
    178156                                        callback.call(this, targ, settings);
    179157                                });
    180                         }
     158                        };
    181159
    182160                }).end();
    183161        };
     
    184162
    185163        // Max scrolling position, works on quirks mode
    186164        // It only fails (not too badly) on IE, quirks mode.
    187         $scrollTo.max = function(elem, axis) {
    188                 var Dim = axis === 'x' ? 'Width' : 'Height',
    189                                 scroll = 'scroll' + Dim;
     165        $scrollTo.max = function( elem, axis ) {
     166                var Dim = axis == 'x' ? 'Width' : 'Height',
     167                        scroll = 'scroll'+Dim;
    190168
    191                 if (!$(elem).is('html,body')) {
     169                if (!$(elem).is('html,body'))
    192170                        return elem[scroll] - $(elem)[Dim.toLowerCase()]();
    193                 }
    194171
    195172                var size = 'client' + Dim,
    196                                 html = elem.ownerDocument.documentElement,
    197                                 body = elem.ownerDocument.body;
     173                        html = elem.ownerDocument.documentElement,
     174                        body = elem.ownerDocument.body;
    198175
    199                 return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]);
     176                return Math.max( html[scroll], body[scroll] )
     177                         - Math.min( html[size]  , body[size]   );
    200178        };
    201179
    202         function both(val) {
    203                 return $.isFunction(val) || typeof val === 'object' ? val : {top: val, left: val};
    204         }
     180        function both( val ) {
     181                return $.isFunction(val) || typeof val == 'object' ? val : { top:val, left:val };
     182        };
    205183
    206         // AMD requirement
    207         return $scrollTo;
    208 }));
    209  No newline at end of file
     184    // AMD requirement
     185    return $scrollTo;
     186}));