From 8f3764caef2012ac188f49e1dd39ea03118d3f0a Mon Sep 17 00:00:00 2001 From: Aaron Ogle Date: Thu, 17 Jul 2014 14:36:58 -0400 Subject: [PATCH] Make comment scrolling much more robust. - Add scrollTo jquery plugin - Add :after content using CSS so the JS can know what kind of layout is being used --- src/sa_web/static/css/default.css | 11 ++ src/sa_web/static/js/views/app-view.js | 28 ++- src/sa_web/static/js/views/survey-view.js | 12 +- src/sa_web/static/libs/jquery.scrollTo.js | 186 ++++++++++++++++++ src/sa_web/static/libs/jquery.scrollTo.min.js | 7 + src/sa_web/templates/base.html | 1 + 6 files changed, 236 insertions(+), 9 deletions(-) create mode 100755 src/sa_web/static/libs/jquery.scrollTo.js create mode 100755 src/sa_web/static/libs/jquery.scrollTo.min.js diff --git a/src/sa_web/static/css/default.css b/src/sa_web/static/css/default.css index d7fd62aff..24b9a982c 100644 --- a/src/sa_web/static/css/default.css +++ b/src/sa_web/static/css/default.css @@ -10,6 +10,12 @@ body { text-align: left; } +/* A helper to let the JS know what kind of layout we're using */ +body:after { + content: "mobile"; + display: none; +} + /* Links */ a, a:active, a:visited { color: #007fbf; @@ -1273,6 +1279,11 @@ a.close-unsupported-overlay { position: relative; } + /* A helper to let the JS know what kind of layout we're using */ + body:after { + content: "desktop"; + } + /* Header */ #site-header { padding-left: 1em; diff --git a/src/sa_web/static/js/views/app-view.js b/src/sa_web/static/js/views/app-view.js index 9231a77c3..fc0bef2d7 100644 --- a/src/sa_web/static/js/views/app-view.js +++ b/src/sa_web/static/js/views/app-view.js @@ -376,6 +376,8 @@ var Shareabouts = Shareabouts || {}; viewPlace: function(model, responseId, zoom) { var self = this, includeSubmissions = S.Config.flavor.app.list_enabled !== false, + // will be "mobile" or "desktop", as defined in default.css + layout = window.getComputedStyle(document.body,':after').getPropertyValue('content'), onPlaceFound, onPlaceNotFound, modelId; onPlaceFound = function(model) { @@ -397,7 +399,7 @@ var Shareabouts = Shareabouts || {}; } self.$panel.removeClass().addClass('place-detail place-detail-' + model.id); - self.showPanel(placeDetailView.render().$el, true); + self.showPanel(placeDetailView.render().$el, !!responseId); self.hideNewPin(); self.destroyNewModels(); self.hideCenterPoint(); @@ -422,7 +424,13 @@ var Shareabouts = Shareabouts || {}; // call scrollIntoView() if ($responseToScrollTo.length > 0) { - $responseToScrollTo.get(0).scrollIntoView(); + if (layout === 'desktop') { + // For desktop, the panel content is scrollable + self.$panelContent.scrollTo($responseToScrollTo, 500); + } else { + // For mobile, it's the window + $(window).scrollTo($responseToScrollTo, 500); + } } } @@ -484,11 +492,17 @@ var Shareabouts = Shareabouts || {}; this.$panelContent.html(markup); this.$panel.show(); - if (preventScrollToTop) { - this.$panelContent.scrollTop(0); - // Scroll to the top of window when showing new content on mobile. Does - // nothing on desktop. - window.scrollTo(0, 0); + if (!preventScrollToTop) { + // will be "mobile" or "desktop", as defined in default.css + var layout = window.getComputedStyle(document.body,':after').getPropertyValue('content'); + if (layout === 'desktop') { + // For desktop, the panel content is scrollable + this.$panelContent.scrollTo(0, 0); + } else { + // Scroll to the top of window when showing new content on mobile. Does + // nothing on desktop. (Except when embedded in a scrollable site.) + window.scrollTo(0, 0); + } } $('body').addClass('content-visible'); diff --git a/src/sa_web/static/js/views/survey-view.js b/src/sa_web/static/js/views/survey-view.js index 1dc0fe481..858d056e3 100644 --- a/src/sa_web/static/js/views/survey-view.js +++ b/src/sa_web/static/js/views/survey-view.js @@ -20,6 +20,8 @@ var Shareabouts = Shareabouts || {}; responses = [], url = window.location.toString(), urlParts = url.split('response/'), + // will be "mobile" or "desktop", as defined in default.css + layout = window.getComputedStyle(document.body,':after').getPropertyValue('content'), responseIdToScrollTo, $responseToScrollTo; @@ -58,8 +60,14 @@ var Shareabouts = Shareabouts || {}; // call scrollIntoView() if ($responseToScrollTo.length > 0) { setTimeout(function() { - $responseToScrollTo.get(0).scrollIntoView(); - }, 500); + // For desktop, the panel content is scrollable + if (layout === 'desktop') { + $('#content article').scrollTo($responseToScrollTo); + } else { + // For mobile, it's the window + $(window).scrollTo($responseToScrollTo); + } + }, 700); } return this; diff --git a/src/sa_web/static/libs/jquery.scrollTo.js b/src/sa_web/static/libs/jquery.scrollTo.js new file mode 100755 index 000000000..b9bccc34e --- /dev/null +++ b/src/sa_web/static/libs/jquery.scrollTo.js @@ -0,0 +1,186 @@ +/*! + * jQuery.ScrollTo + * Copyright (c) 2007-2014 Ariel Flesler - afleslergmailcom | http://flesler.blogspot.com + * Licensed under MIT + * http://flesler.blogspot.com/2007/10/jqueryscrollto.html + * @projectDescription Easy element scrolling using jQuery. + * @author Ariel Flesler + * @version 1.4.12 + */ + +;(function(plugin) { + // AMD Support + if (typeof define === 'function' && define.amd) { + define(['jquery'], plugin); + } else { + plugin(jQuery); + } +}(function($) { + + var $scrollTo = $.scrollTo = function( target, duration, settings ) { + return $(window).scrollTo( target, duration, settings ); + }; + + $scrollTo.defaults = { + axis:'xy', + duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1, + limit:true + }; + + // Returns the element that needs to be animated to scroll the window. + // Kept for backwards compatibility (specially for localScroll & serialScroll) + $scrollTo.window = function( scope ) { + return $(window)._scrollable(); + }; + + // Hack, hack, hack :) + // Returns the real elements to scroll (supports window/iframes, documents and regular nodes) + $.fn._scrollable = function() { + return this.map(function() { + var elem = this, + isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1; + + if (!isWin) + return elem; + + var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem; + + return /webkit/i.test(navigator.userAgent) || doc.compatMode == 'BackCompat' ? + doc.body : + doc.documentElement; + }); + }; + + $.fn.scrollTo = function( target, duration, settings ) { + if (typeof duration == 'object') { + settings = duration; + duration = 0; + } + if (typeof settings == 'function') + settings = { onAfter:settings }; + + if (target == 'max') + target = 9e9; + + settings = $.extend( {}, $scrollTo.defaults, settings ); + // Speed is still recognized for backwards compatibility + duration = duration || settings.duration; + // Make sure the settings are given right + settings.queue = settings.queue && settings.axis.length > 1; + + if (settings.queue) + // Let's keep the overall duration + duration /= 2; + settings.offset = both( settings.offset ); + settings.over = both( settings.over ); + + return this._scrollable().each(function() { + // Null target yields nothing, just like jQuery does + if (target == null) return; + + var elem = this, + $elem = $(elem), + targ = target, toff, attr = {}, + win = $elem.is('html,body'); + + switch (typeof targ) { + // A number will pass the regex + case 'number': + case 'string': + if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) { + targ = both( targ ); + // We are done + break; + } + // Relative/Absolute selector, no break! + targ = win ? $(targ) : $(targ, this); + if (!targ.length) return; + case 'object': + // DOMElement / jQuery + if (targ.is || targ.style) + // Get the real position of the target + toff = (targ = $(targ)).offset(); + } + + var offset = $.isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset; + + $.each( settings.axis.split(''), function( i, axis ) { + var Pos = axis == 'x' ? 'Left' : 'Top', + pos = Pos.toLowerCase(), + key = 'scroll' + Pos, + old = elem[key], + max = $scrollTo.max(elem, axis); + + if (toff) {// jQuery / DOMElement + attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] ); + + // If it's a dom element, reduce the margin + if (settings.margin) { + attr[key] -= parseInt(targ.css('margin'+Pos)) || 0; + attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0; + } + + attr[key] += offset[pos] || 0; + + if(settings.over[pos]) + // Scroll to a fraction of its width/height + attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos]; + } else { + var val = targ[pos]; + // Handle percentage values + attr[key] = val.slice && val.slice(-1) == '%' ? + parseFloat(val) / 100 * max + : val; + } + + // Number or 'number' + if (settings.limit && /^\d+$/.test(attr[key])) + // Check the limits + attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max ); + + // Queueing axes + if (!i && settings.queue) { + // Don't waste time animating, if there's no need. + if (old != attr[key]) + // Intermediate animation + animate( settings.onAfterFirst ); + // Don't animate this axis again in the next iteration. + delete attr[key]; + } + }); + + animate( settings.onAfter ); + + function animate( callback ) { + $elem.animate( attr, duration, settings.easing, callback && function() { + callback.call(this, targ, settings); + }); + }; + + }).end(); + }; + + // Max scrolling position, works on quirks mode + // It only fails (not too badly) on IE, quirks mode. + $scrollTo.max = function( elem, axis ) { + var Dim = axis == 'x' ? 'Width' : 'Height', + scroll = 'scroll'+Dim; + + if (!$(elem).is('html,body')) + return elem[scroll] - $(elem)[Dim.toLowerCase()](); + + var size = 'client' + Dim, + html = elem.ownerDocument.documentElement, + body = elem.ownerDocument.body; + + return Math.max( html[scroll], body[scroll] ) + - Math.min( html[size] , body[size] ); + }; + + function both( val ) { + return $.isFunction(val) || typeof val == 'object' ? val : { top:val, left:val }; + }; + + // AMD requirement + return $scrollTo; +})); diff --git a/src/sa_web/static/libs/jquery.scrollTo.min.js b/src/sa_web/static/libs/jquery.scrollTo.min.js new file mode 100755 index 000000000..73e0f050f --- /dev/null +++ b/src/sa_web/static/libs/jquery.scrollTo.min.js @@ -0,0 +1,7 @@ +/** + * Copyright (c) 2007-2014 Ariel Flesler - afleslergmailcom | http://flesler.blogspot.com + * Licensed under MIT + * @author Ariel Flesler + * @version 1.4.12 + */ +;(function(a){if(typeof define==='function'&&define.amd){define(['jquery'],a)}else{a(jQuery)}}(function($){var j=$.scrollTo=function(a,b,c){return $(window).scrollTo(a,b,c)};j.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1,limit:true};j.window=function(a){return $(window)._scrollable()};$.fn._scrollable=function(){return this.map(function(){var a=this,isWin=!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!isWin)return a;var b=(a.contentWindow||a).document||a.ownerDocument||a;return/webkit/i.test(navigator.userAgent)||b.compatMode=='BackCompat'?b.body:b.documentElement})};$.fn.scrollTo=function(f,g,h){if(typeof g=='object'){h=g;g=0}if(typeof h=='function')h={onAfter:h};if(f=='max')f=9e9;h=$.extend({},j.defaults,h);g=g||h.duration;h.queue=h.queue&&h.axis.length>1;if(h.queue)g/=2;h.offset=both(h.offset);h.over=both(h.over);return this._scrollable().each(function(){if(f==null)return;var d=this,$elem=$(d),targ=f,toff,attr={},win=$elem.is('html,body');switch(typeof targ){case'number':case'string':if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=win?$(targ):$(targ,this);if(!targ.length)return;case'object':if(targ.is||targ.style)toff=(targ=$(targ)).offset()}var e=$.isFunction(h.offset)&&h.offset(d,targ)||h.offset;$.each(h.axis.split(''),function(i,a){var b=a=='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,old=d[key],max=j.max(d,a);if(toff){attr[key]=toff[pos]+(win?0:old-$elem.offset()[pos]);if(h.margin){attr[key]-=parseInt(targ.css('margin'+b))||0;attr[key]-=parseInt(targ.css('border'+b+'Width'))||0}attr[key]+=e[pos]||0;if(h.over[pos])attr[key]+=targ[a=='x'?'width':'height']()*h.over[pos]}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)=='%'?parseFloat(c)/100*max:c}if(h.limit&&/^\d+$/.test(attr[key]))attr[key]=attr[key]<=0?0:Math.min(attr[key],max);if(!i&&h.queue){if(old!=attr[key])animate(h.onAfterFirst);delete attr[key]}});animate(h.onAfter);function animate(a){$elem.animate(attr,g,h.easing,a&&function(){a.call(this,targ,h)})}}).end()};j.max=function(a,b){var c=b=='x'?'Width':'Height',scroll='scroll'+c;if(!$(a).is('html,body'))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,html=a.ownerDocument.documentElement,body=a.ownerDocument.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return $.isFunction(a)||typeof a=='object'?a:{top:a,left:a}};return j})); diff --git a/src/sa_web/templates/base.html b/src/sa_web/templates/base.html index 41e42328c..bf6ccee48 100644 --- a/src/sa_web/templates/base.html +++ b/src/sa_web/templates/base.html @@ -161,6 +161,7 @@

+