|
3 | 3 | "use strict"; |
4 | 4 |
|
5 | 5 | var Router = function() { |
6 | | - window.addEventListener('popstate', $.proxy(this.onpopstate, this)); |
7 | 6 | this.state = sessionStorage; |
8 | 7 | this.state.setItem("stateid", this.state.getItem("stateid") || 1); |
9 | 8 | this.state.setItem("currentStateID", this.state.getItem("stateid")); |
10 | | - |
| 9 | + this.urlIDMap = {}; |
| 10 | + this.init(); |
| 11 | + } |
| 12 | + Router.prototype.init = function() { |
11 | 13 | var currentPage = this.getCurrentPage(); |
| 14 | + if(!currentPage[0]) currentPage = $(".page").eq(0).addClass("page-current"); |
12 | 15 | 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); |
13 | 20 |
|
14 | | - if(hash && (!currentPage[0] || hash.slice(1) !== currentPage[0].id)) { |
| 21 | + if(newCurrentPage[0] && (!currentPage[0] || hash.slice(1) !== currentPage[0].id)) { |
15 | 22 | currentPage.removeClass("page-current"); |
16 | | - $(hash).addClass("page-current"); |
| 23 | + newCurrentPage.addClass("page-current"); |
17 | 24 | this.pushstate(hash); |
18 | 25 | } else { |
19 | 26 | this.pushstate("#"+currentPage[0].id); |
20 | 27 | } |
21 | 28 |
|
22 | | - this.urlIDMap = {}; |
| 29 | + window.addEventListener('popstate', $.proxy(this.onpopstate, this)); |
23 | 30 | } |
24 | 31 |
|
25 | 32 | //加载一个页面,传入的参数是页面id或者url |
26 | 33 | Router.prototype.loadPage = function(url) { |
27 | 34 | this.getPage(url, function(page) { |
28 | 35 | this.animatePages(this.getCurrentPage(), page); |
29 | 36 | this.pushstate(url); |
| 37 | + page.trigger("pageInit", {url: url}); |
30 | 38 | }); |
31 | 39 | } |
32 | 40 |
|
|
107 | 115 | var html = response.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[1]; |
108 | 116 | if(!html) html = response; |
109 | 117 | 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"); |
111 | 121 | $page.insertAfter($(".page")[0]); |
| 122 | + if(!$page[0].id) $page[0].id = this.genRandomID(); |
112 | 123 | this.urlIDMap[url] = "#"+$page[0].id; |
113 | 124 | callback.apply(this, [$page]); |
114 | 125 | }, this) |
|
126 | 137 | Router.prototype.setCurrentStateID = function(id) { |
127 | 138 | this.state.setItem("currentStateID", id); |
128 | 139 | } |
| 140 | + Router.prototype.genRandomID = function() { |
| 141 | + return "page-"+(+new Date()); |
| 142 | + } |
129 | 143 |
|
130 | 144 | $(function() { |
131 | 145 | var router = new Router(); |
132 | 146 | $(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; |
135 | 154 | var url = $target.attr("href"); |
| 155 | + if(!url || url === "#") return; |
| 156 | + e.preventDefault(); |
136 | 157 | router.loadPage(url); |
137 | 158 | }) |
138 | 159 | }); |
|
0 commit comments