From c9300ae1efe9b000bf1626a58546c8db877a15d0 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 4 May 2017 13:53:47 +0200 Subject: [PATCH] system.js scroll to anchor --- lib/system/lib/system.js | 48 ++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/system/lib/system.js b/lib/system/lib/system.js index 2a440cf..410523c 100644 --- a/lib/system/lib/system.js +++ b/lib/system/lib/system.js @@ -53,21 +53,13 @@ SYSTEM.prototype.hashchange = function () { if(system.hash_change){ system.hash_change(system.cur_state().split(';')[0].split('(')[0]);} }; -SYSTEM.prototype.handle_call_pages_page = function (html,entry,id,forced,cached,trycount) { +SYSTEM.prototype.handle_call_pages_page = function (html,entry,id,forced,cached,callback) { var url = entry['url']+(window.location.search.substr(1) ? '&'+window.location.search.substr(1) : '' ); - if(!trycount){ - trycount = 0;} - trycount++; if($(entry['div']).length){ $(entry['div']).html(html); - this.log_info('load page: '+id+entry['div']+' '+url+' - try '+trycount+' - success'); + this.log_info('load page: '+id+entry['div']+' '+url+' - success'); } else { - this.log_error('load page: '+id+entry['div']+' '+url+' - try '+trycount+' - div not found'); - //Try again - if(trycount < 3){ - var tc = trycount; - setTimeout(function() { system.handle_call_pages_page(html,entry,id,forced,cached,tc); },1000); - } + this.log_error('load page: '+id+entry['div']+' '+url+' - div not found'); return; } //Title @@ -135,19 +127,27 @@ SYSTEM.prototype.handle_call_pages_page = function (html,entry,id,forced,cached, } //update state this.state[entry['div']] = url; + callback(); } -SYSTEM.prototype.handle_call_pages_entry = function (entry,id,forced,cached) { +SYSTEM.prototype.handle_call_pages_entry = function (entry,id,forced,cached, callback) { var url = entry['url']+(window.location.search.substr(1) ? '&'+window.location.search.substr(1) : '' ); //check loaded state of div - reload only if required if(forced || this.state[entry['div']] !== url || !$(entry['div']).length || $(entry['div']).html() === ''){ //load page - this.call_url(url,function(data){system.handle_call_pages_page(data,entry,id,forced,cached);},{},'html',true); + this.call_url(url,function(data){system.handle_call_pages_page(data,entry,id,forced,cached,callback);},{},'html',true); } else { this.log_info('load page: '+id+entry['div']+' '+url+' - cached'); + callback(); } } //internal function to handle pagestate results SYSTEM.prototype.handle_call_pages = function (data,id,forced,cached) { + var hash = null; + if (id.indexOf('#') != -1) { + var splinters = id.split('#'); + hash = splinters.pop() + id = splinters.pop();} + if(data['status']){ this.log_info('load pages: '+this.endpoint+'?call=pages&group='+this.group+'&state='+id+' - '+(cached ? 'cached ' : (forced ? 'forced' : 'success'))); //state not found? @@ -163,9 +163,24 @@ SYSTEM.prototype.handle_call_pages = function (data,id,forced,cached) { //update history? if( !(id === this.start_state && this.cur_state() === '') && //avoid start state in url unless called explicit id !== this.cur_state()){//new state? - window.history.pushState(null, "", '#!'+id);} + window.history.pushState(null, "", '#!'+id+(hash ? '#'+hash : null));} + + var count = []; data['result'].forEach( - function(entry) { system.handle_call_pages_entry(entry,id,forced,cached);}); + function(entry){ + system.handle_call_pages_entry(entry,id,forced,cached, + function(d){ + count.push(true); + if(count.length === data['result'].length){ + if(hash && $('#'+hash).length){ + $(document.body).animate({'scrollTop': $('#'+hash).offset().top-50}, 750); + } else { + $(document.body).animate({'scrollTop': 0}, 750); + } + } + } + ); + }); } else { this.log_info('Problem with your Pages: '+data['result']['message']); } @@ -228,9 +243,10 @@ SYSTEM.prototype.load_css = function loadCSS(csssrc,forced) { SYSTEM.prototype.cur_state = function() { var pathName = window.location.href; + if (pathName.indexOf('#!') != -1) { return pathName.split('#!').pop();} - return ''; + return null; }; SYSTEM.prototype.go_state = function(default_state,forced){ var pageName = this.cur_state();