Skip to content

Commit 04b93d0

Browse files
committed
Simplify pushstate usage for transitions
1 parent e225fb0 commit 04b93d0

File tree

15 files changed

+63
-46
lines changed

15 files changed

+63
-46
lines changed

src/application/cache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const cache: Cache = {
1212
archivesJson: null,
1313
detailJson: {},
1414
details: {},
15-
currentState: null,
1615
cache: {},
1716
};
1817

src/application/listeners.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export function setupColumnControlListener(): void {
1010
}
1111

1212
export function teardownEventListeners(): void {
13-
var anchors = document.querySelectorAll('a.js-nav');
14-
for (var i = 0; i < anchors.length; i++) {
13+
const anchors = document.querySelectorAll('a.js-nav');
14+
for (let i = 0; i < anchors.length; i++) {
1515
anchors[i].removeEventListener('click', clickHandler);
1616
}
1717
}
1818

1919
export function setupEventListeners(): void {
20-
var anchors = document.querySelectorAll('a.js-nav');
21-
for (var i = 0; i < anchors.length; i++) {
20+
const anchors = document.querySelectorAll('a.js-nav');
21+
for (let i = 0; i < anchors.length; i++) {
2222
anchors[i].addEventListener('click', clickHandler);
2323
}
2424
}
@@ -30,10 +30,9 @@ export function setupFormListeners(): void {
3030
export function setupHistoryListener(): void {
3131
window.onpopstate = function(evt) {
3232
const state: State = evt.state;
33-
if (state && cache.currentState.url !== state.url) {
34-
transitionTo(state, false, false);
35-
} else if (state) {
36-
window.history.go(-1);
33+
if (!state) {
34+
return;
3735
}
36+
transitionTo(state, false);
3837
};
3938
}

src/application/render.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,46 @@ import TagsComponent from '../components/tags';
1818

1919
import postsRepo from '../repositories/posts';
2020
import tagsRepo from '../repositories/tags';
21+
import Url from '../utils/url';
2122

22-
declare var Promise: any;
23+
declare let Promise: any;
2324

2425
export function render(state: State, title: string, url: string): Promise<any> {
2526
const promise: Promise<any> = new Promise(function(resolve: any) {
2627
teardownEventListeners();
28+
const _url = Url(url);
2729
window.scroll(0, 0);
28-
if (!location.search) {
30+
if (!_url.search) {
2931
SearchComponent.resetForm();
3032
}
31-
if (location.search && location.search.match(routes.search)) {
32-
SearchComponent.render(location.search.slice(1).split('search=')[1])
33+
if (_url.search && _url.search.match(routes.search)) {
34+
SearchComponent.render(_url.search.slice(1).split('search=')[1])
3335
.then(resolve);
34-
} else if (location.pathname === '/') {
36+
} else if (_url.pathname === '/') {
3537
postsRepo.getPosts().then(function(posts: Array<Record>) {
3638
ExcerptsComponent.render(posts);
3739
resolve();
3840
});
39-
} else if (location.pathname.match(routes.about)) {
41+
} else if (_url.pathname.match(routes.about)) {
4042
AboutComponent.render();
4143
resolve();
42-
} else if (location.pathname.match(routes.archive)) {
44+
} else if (_url.pathname.match(routes.archive)) {
4345
postsRepo.getArchives().then(ArchivesComponent.render);
4446
resolve();
45-
} else if (location.pathname.match(routes.tags)) {
47+
} else if (_url.pathname.match(routes.tags)) {
4648
tagsRepo.getTags().then(function(tags: Array<Record>) {
4749
TagsComponent.render(tags);
4850
resolve();
4951
});
50-
} else if (location.pathname.match(routes.tag)) {
51-
tagsRepo.getTag(location.pathname.split('/')[2]).then(function (tag: Record) {
52+
} else if (_url.pathname.match(routes.tag)) {
53+
tagsRepo.getTag(_url.pathname.split('/')[2]).then(function (tag: Record) {
5254
postsRepo.getPostsByTag(tag).then(function(posts: Array<Record>) {
5355
TagComponent.render(tag, posts);
5456
resolve();
5557
});
5658
});
57-
} else if (location.pathname.match(routes.post)) {
58-
var slug = location.pathname.split('/')[2];
59+
} else if (_url.pathname.match(routes.post)) {
60+
const slug = _url.pathname.split('/')[2];
5961
postsRepo.getPostBySlug(slug).then(function() {
6062
postsRepo.loadPost(slug, function(post: Record) {
6163
PostComponent.render(post);

src/application/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function start(): Promise<any> {
2020
});
2121
const url = location.toString();
2222
const state: State = { page: url, title: url, url: url };
23-
transitionTo(state, false, true);
23+
transitionTo(state);
2424
});
2525
});
2626
}

src/application/transition.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,13 @@ const finishLoading: ()=>void =
2222
}, 100);
2323
};
2424

25-
const transitionTo: (state: State, replace?: any, push?: any)=>Promise<any> =
26-
function (state, replace?, push?) {
25+
const transitionTo: (state: State, push?: boolean)=>Promise<any> =
26+
function (state: State, push?: boolean) {
2727
startLoading();
28-
if (replace === null || replace === undefined || !!replace) {
29-
history.replaceState(state, state.title, state.url);
30-
}
3128
return render(state, state.title, state.url).then(function() {
3229
finishLoading();
33-
if (push === null || push === undefined || !!push) {
34-
const currentState: State = history.state;
35-
if (currentState !== state) {
36-
setTimeout(function(_state: State) {
37-
history.pushState(_state, _state.title, _state.url);
38-
cache.currentState = _state;
39-
}, 250, state);
40-
}
30+
if (push === undefined || push === null || !!push) {
31+
history.pushState(state, state.title, state.url);
4132
}
4233
});
4334
};

src/components/archives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Post from '../types/post';
33
import constants from '../utils/constants';
44
import cloneTemplate from '../utils/clone-template';
55
// import * as moment from 'moment';
6-
declare var moment: any;
6+
declare let moment: any;
77

88
const render: (p: Array<Record>)=>void =
99
function (posts: Array<Record>): void {

src/components/post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import constants from '../utils/constants';
55
import cloneTemplate from '../utils/clone-template';
66
import markdown from '../utils/markdown';
77
// import * as moment from 'moment';
8-
declare var moment: any;
8+
declare let moment: any;
99

1010
const render: (p: Record)=>void =
1111
function (post: Record): void {

src/components/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const resetForm: ()=>void =
1010
input.value = '';
1111
};
1212

13-
declare var Promise: any;
13+
declare let Promise: any;
1414

1515
const render: (q: string)=>Promise<any> =
1616
function (query: string): Promise<any> {

src/interfaces/cache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ interface Cache {
1717
archivesJson: string | null,
1818
detailJson: cacheRecord,
1919
details: cacheRecord,
20-
currentState: State,
2120
cache: cacheRecord,
2221
}
2322

src/interfaces/url.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface Url {
2+
search: string,
3+
pathname: string,
4+
[index:string]: any,
5+
}
6+
7+
export default Url;

0 commit comments

Comments
 (0)