Skip to content

Commit 0da79be

Browse files
author
hongxun.lhx
committed
update router
1 parent 2bcf97c commit 0da79be

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎js/router.js‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@
33
"use strict";
44

55
var Router = function() {
6-
window.addEventListener('popstate', $.proxy(this.onpopstate, this));
76
this.state = sessionStorage;
87
this.state.setItem("stateid", this.state.getItem("stateid") || 1);
98
this.state.setItem("currentStateID", this.state.getItem("stateid"));
10-
9+
this.urlIDMap = {};
10+
this.init();
11+
}
12+
Router.prototype.init = function() {
1113
var currentPage = this.getCurrentPage();
14+
if(!currentPage[0]) currentPage = $(".page").eq(0).addClass("page-current");
1215
var hash = location.hash;
16+
if(currentPage[0] && !currentPage[0].id) currentPage[0].id = (hash ? hash.slice(1) : this.genRandomID());
17+
18+
if(!currentPage[0]) throw new Error("can't find .page element");
19+
var newCurrentPage = $(hash);
1320

14-
if(hash && (!currentPage[0] || hash.slice(1) !== currentPage[0].id)) {
21+
if(newCurrentPage[0] && (!currentPage[0] || hash.slice(1) !== currentPage[0].id)) {
1522
currentPage.removeClass("page-current");
16-
$(hash).addClass("page-current");
23+
newCurrentPage.addClass("page-current");
1724
this.pushstate(hash);
1825
} else {
1926
this.pushstate("#"+currentPage[0].id);
2027
}
2128

22-
this.urlIDMap = {};
29+
window.addEventListener('popstate', $.proxy(this.onpopstate, this));
2330
}
2431

2532
//加载一个页面,传入的参数是页面id或者url
2633
Router.prototype.loadPage = function(url) {
2734
this.getPage(url, function(page) {
2835
this.animatePages(this.getCurrentPage(), page);
2936
this.pushstate(url);
37+
page.trigger("pageInit", {url: url});
3038
});
3139
}
3240

@@ -107,8 +115,11 @@
107115
var html = response.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[1];
108116
if(!html) html = response;
109117
html = "<div>"+html+"</div>";
110-
var $page = $(html).find(".page");
118+
var tmp = $(html);
119+
var $page = tmp.find(".page");
120+
if(!$page[0]) $page = tmp.addClass("page");
111121
$page.insertAfter($(".page")[0]);
122+
if(!$page[0].id) $page[0].id = this.genRandomID();
112123
this.urlIDMap[url] = "#"+$page[0].id;
113124
callback.apply(this, [$page]);
114125
}, this)
@@ -126,13 +137,23 @@
126137
Router.prototype.setCurrentStateID = function(id) {
127138
this.state.setItem("currentStateID", id);
128139
}
140+
Router.prototype.genRandomID = function() {
141+
return "page-"+(+new Date());
142+
}
129143

130144
$(function() {
131145
var router = new Router();
132146
$(document).on("click", "a", function(e) {
133-
e.preventDefault();
134-
var $target = $(e.target);
147+
var $target = $(e.currentTarget);
148+
if($target.hasClass("external") ||
149+
$target.attr("external") ||
150+
$target.hasClass("tab-link") ||
151+
$target.hasClass("open-popup") ||
152+
$target.hasClass("open-panel")
153+
) return;
135154
var url = $target.attr("href");
155+
if(!url || url === "#") return;
156+
e.preventDefault();
136157
router.loadPage(url);
137158
})
138159
});

‎less/pages.less‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
}
7979
.page-from-left-to-center {
8080
animation: pageFromLeftToCenter @pageDuration forwards;
81+
z-index: 2002;
8182
}
8283

8384
@keyframes pageFromCenterToLeft {

0 commit comments

Comments
 (0)