Permalink
Browse files

let the browser handle in-page anchors and only scroll to top of page…

… after render instead of before resource fetch
  • Loading branch information...
1 parent 9a4d922 commit d6925404077510863a1d5eb84db0ee08dfa02767 @caolan caolan committed Feb 15, 2012
Showing with 18 additions and 12 deletions.
  1. +18 −12 duality/core.js
View
@@ -236,6 +236,12 @@ exports.init = function () {
var href = $(this).attr('href');
var rel = $(this).attr('rel');
+ if (/#[A-Za-z_\-:\.]+/.test(href)) {
+ exports._in_page = true;
+ // in-page anchor
+ return;
+ }
+ console.log('no in-page anchor');
if (href && exports.isAppURL(href) && rel !== 'external') {
var url = exports.appPath(href);
ev.preventDefault();
@@ -252,10 +258,16 @@ exports.init = function () {
// button
window.open(exports.getBaseURL() + url);
}
+ return false;
}
});
window.onpopstate = function (ev) {
+ if (exports._in_page) {
+ exports._in_page = false;
+ // let the browser handle in-page anchors
+ return;
+ }
var url = exports.getAppURL();
var state = ev.state || {};
var method = state.method || 'GET';
@@ -578,7 +590,7 @@ exports.runShowBrowser = function (req, name, docid, callback) {
query: req.query,
fn: fn
};
- events.emit('beforeResource', info);
+ events.emit('beforeResource', info, req);
if (docid) {
var appdb = db.use(exports.getDBURL(req));
@@ -777,7 +789,7 @@ exports.runUpdateBrowser = function (req, name, docid, callback) {
query: req.query,
fn: fn
};
- events.emit('beforeResource', info);
+ events.emit('beforeResource', info, req);
if (docid) {
var appdb = db.use(exports.getDBURL(req));
@@ -944,7 +956,7 @@ exports.runListBrowser = function (req, name, view, callback) {
query: req.query,
fn: fn
};
- events.emit('beforeResource', info);
+ events.emit('beforeResource', info, req);
if (view) {
// update_seq used in head parameter passed to list function
@@ -1132,6 +1144,9 @@ exports.handle = function (method, url, data) {
}
// TODO: handle invalid values?
}
+ else if (!utils.initial_hit) {
+ window.scrollTo(0, 0);
+ }
};
var src, fn, name;
@@ -1140,25 +1155,16 @@ exports.handle = function (method, url, data) {
exports.runShowBrowser(
req, req.path[1], req.path.slice(2).join('/'), after
);
- if (!utils.initial_hit) {
- window.scrollTo(0, 0);
- }
}
else if (req.path[0] === '_list') {
exports.runListBrowser(
req, req.path[1], req.path.slice(2).join('/'), after
);
- if (!utils.initial_hit) {
- window.scrollTo(0, 0);
- }
}
else if (req.path[0] === '_update') {
exports.runUpdateBrowser(
req, req.path[1], req.path.slice(2).join('/'), after
);
- if (!utils.initial_hit) {
- window.scrollTo(0, 0);
- }
}
else {
console.log('Unknown rewrite target: ' + req.path.join('/'));

0 comments on commit d692540

Please sign in to comment.