Skip to content

Commit 2cd62d8

Browse files
committed
add SUPPORT_HISTORY. fix redirect bug
1 parent 1d8ac61 commit 2cd62d8

File tree

7 files changed

+238
-214
lines changed

7 files changed

+238
-214
lines changed

examples/browsers.html

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<input class="btn3" type="button" value="data('enableUrl', 2)">
3838
<input class="btn4" type="button" value="data()">
3939

40+
<input class="btn5" type="button" value="pushState">
41+
4042
</div>
4143

4244

examples/dist/js/main.js

+87-132
Original file line numberDiff line numberDiff line change
@@ -2578,58 +2578,6 @@ define("../cardkit/render", [
25782578

25792579
});
25802580

2581-
/* @source ../cardkit/pagesession.js */;
2582-
2583-
2584-
define("../cardkit/pagesession", [
2585-
"mo/lang"
2586-
], function(){
2587-
2588-
var exports = {
2589-
2590-
init: function(){
2591-
this.name = 'ck_ss';
2592-
if (sessionStorage[this.name]) {
2593-
this.list = JSON.parse(sessionStorage[this.name]);
2594-
} else {
2595-
this.reset();
2596-
}
2597-
},
2598-
2599-
reset: function(){
2600-
this.list = [];
2601-
this.save();
2602-
},
2603-
2604-
save: function(){
2605-
sessionStorage[this.name] = JSON.stringify(this.list);
2606-
},
2607-
2608-
indexOf: function(url){
2609-
var n = this.list.map(function(item){
2610-
return item[0];
2611-
}).indexOf(url);
2612-
return (n === -1 || this.list[n][1] < history.length) ? n : -1;
2613-
},
2614-
2615-
push: function(url){
2616-
this.list.push([url, history.length]);
2617-
this.save();
2618-
},
2619-
2620-
clear: function(n){
2621-
if (n !== -1) {
2622-
this.list = this.list.slice(0, n + 2);
2623-
this.save();
2624-
}
2625-
}
2626-
2627-
};
2628-
2629-
return exports;
2630-
2631-
});
2632-
26332581
/* @source eventmaster.js */;
26342582

26352583
/**
@@ -6515,6 +6463,7 @@ define('soviet', [
65156463
define("../cardkit/app", [
65166464
"dollar",
65176465
"mo/lang",
6466+
"mo/browsers",
65186467
"mo/template",
65196468
"soviet",
65206469
"choreo",
@@ -6531,21 +6480,22 @@ define("../cardkit/app", [
65316480
"../cardkit/view/growl",
65326481
"../cardkit/view/slidelist",
65336482
"../cardkit/bus",
6534-
"../cardkit/pagesession",
65356483
"../cardkit/render",
65366484
"mo/domready"
6537-
], function($, _, tpl, soviet, choreo,
6485+
], function($, _, tpl, browsers, soviet, choreo,
65386486
momoBase, momoTap, momoSwipe, momoDrag, momoScroll,
65396487
control, picker, stars, modalCard, actionView, growl, slidelist,
6540-
bus, pageSession, render){
6488+
bus, render){
65416489

65426490
var window = this,
6491+
history = window.history,
65436492
location = window.location,
65446493
document = window.document,
65456494
body = document.body,
65466495
back_timeout,
65476496
gc_id = 0,
65486497

6498+
SUPPORT_HISTORY = !browsers.crios && 'pushState' in history,
65496499
SUPPORT_OVERFLOWSCROLL = "webkitOverflowScrolling" in body.style,
65506500

65516501
TPL_MASK = '<div class="ck-globalmask"></div>';
@@ -6885,90 +6835,97 @@ define("../cardkit/app", [
68856835

68866836
initState: function(){
68876837

6888-
$(window).bind("popstate", function(e){
6889-
// alert(['pop', e.state && [e.state.prev, e.state.next], ck.viewport && ck.viewport[0].id].join(', '))
6890-
if (ck.sessionLocked) {
6891-
pageSession.reset();
6892-
location.reload();
6893-
return;
6894-
}
6895-
clearTimeout(back_timeout);
6896-
var loading = ck.viewport[0].id === 'ckLoading';
6897-
if (e.state) {
6898-
if (e.state.next === '_modal_') {
6899-
// 11. forward from normal card, show modal card. alert(11)
6900-
if (modalCard.isOpened || loading || !ck.viewport) {
6901-
modalCard.event.once('close', function(){
6902-
pageSession.reset();
6903-
});
6838+
ck.sessionLocked = false;
6839+
6840+
var is_forward, restore_state;
6841+
6842+
if (SUPPORT_HISTORY) {
6843+
$(window).bind("popstate", function(e){
6844+
// alert(['pop', e.state && [e.state.prev, e.state.next].join('-'), ck.viewport && ck.viewport[0].id].join(', '))
6845+
if (ck.sessionLocked) {
6846+
location.reload();
6847+
return;
6848+
}
6849+
clearTimeout(back_timeout);
6850+
var loading = ck.viewport[0].id === 'ckLoading';
6851+
if (e.state) {
6852+
if (e.state.next === '_modal_') {
6853+
// 11. forward from normal card, show modal card. alert(11)
6854+
if (modalCard.isOpened || loading || !ck.viewport) {
6855+
history.back();
6856+
} else {
6857+
modalCard.set(e.state.opt).open();
6858+
}
6859+
} else if (modalCard.isOpened) {
6860+
// 12. back from modal card. alert(12)
6861+
ck.closeModal();
6862+
} else if (loading) {
6863+
if (e.state.next === 'ckLoading') {
6864+
// 6. back from other page, no GC.
6865+
// go to 2. alert(6)
6866+
history.back();
6867+
} else if (e.state.next) {
6868+
// 7. from 6, hide loading immediately. alert(7)
6869+
ck.changeView(e.state.next);
6870+
ck.hideLoading();
6871+
}
6872+
} else if (e.state.prev === ck.viewport[0].id) {
6873+
// 3. forward from normal card. alert(3)
6874+
link_handler(e.state.next, e.state.link);
6875+
} else if (e.state.next === ck.viewport[0].id){ // @TODO hotfix for chrome
69046876
history.back();
69056877
} else {
6906-
modalCard.set(e.state.opt).open();
6878+
// 2. back from normal card. alert(2)
6879+
back_handler(e.state.next);
69076880
}
6908-
} else if (modalCard.isOpened) {
6909-
// 12. back from modal card. alert(12)
6910-
ck.closeModal();
69116881
} else if (loading) {
6912-
if (e.state.next === 'ckLoading') {
6913-
// 6. back from other page, no GC.
6914-
// go to 2. alert(6)
6915-
history.back();
6916-
} else {
6917-
// 7. from 6, hide loading immediately. alert(7)
6918-
ck.changeView(e.state.next);
6919-
ck.hideLoading();
6920-
}
6921-
} else if (e.state.prev === ck.viewport[0].id) {
6922-
// 3. forward from normal card. alert(3)
6923-
link_handler(e.state.next, e.state.link);
6924-
} else if (e.state.next === ck.viewport[0].id){ // @TODO hotfix for chrome
6925-
history.back();
6926-
} else {
6927-
// 2. back from normal card. alert(2)
6928-
back_handler(e.state.next);
6882+
// 5. forward from other page, no GC. alert(5)
6883+
history.forward();
6884+
} else {
6885+
// 4. back to other page, shift left and show loading.
6886+
// if no GC: go to 6.
6887+
// if no prev page: reload, go to 8
6888+
// else: go to 8. alert(4)
6889+
back_handler('ckLoading');
69296890
}
6930-
} else if (loading) {
6931-
// 5. forward from other page, no GC. alert(5)
6932-
history.forward();
6933-
} else {
6934-
// 4. back to other page, shift left and show loading.
6935-
// if no GC: go to 6.
6936-
// if no prev page: reload, go to 8
6937-
// else: go to 8. alert(4)
6938-
back_handler('ckLoading');
6939-
}
6940-
});
6941-
6942-
ck.sessionLocked = false;
6943-
6944-
pageSession.init();
6891+
});
69456892

6946-
var current_state = history.state,
6947-
restore_state = current_state && current_state.next; // alert(['init', current_state && [current_state.prev, current_state.next], ck.viewport && ck.viewport[0].id].join(', '))
6948-
if (restore_state === '_modal_') { // @TODO
6949-
restore_state = current_state.prev;
6950-
if (!modalCard.isOpened && ck.viewport) {
6951-
modalCard.set(history.state.opt).open();
6893+
//alert('length: ' + history.length + ', ' + document.referrer)
6894+
var last_length = sessionStorage['ck_ss_n'],
6895+
last_loc = sessionStorage['ck_ss_loc'];
6896+
is_forward = last_length && last_length == history.length
6897+
&& document.referrer && last_loc === document.referrer;
6898+
sessionStorage['ck_ss_n'] = history.length;
6899+
sessionStorage['ck_ss_loc'] = location.href;
6900+
6901+
var current_state = history.state,
6902+
restore_state = current_state && current_state.next; // alert(['init', current_state && [current_state.prev, current_state.next].join('-'), ck.viewport && ck.viewport[0].id].join(', '))
6903+
if (restore_state === '_modal_') { // @TODO
6904+
restore_state = current_state.prev;
6905+
if (!modalCard.isOpened && ck.viewport) {
6906+
modalCard.set(history.state.opt).open();
6907+
}
69526908
}
6909+
69536910
}
6911+
69546912
if (restore_state) {
6955-
// 1. reload from normal card. alert(0)
6913+
// 1. reload from normal card. alert(1)
69566914
ck.changeView(restore_state);
69576915
if (restore_state === 'ckLoading') {
69586916
// 9. alert(9)
69596917
history.back();
69606918
}
69616919
} else {
6962-
if (pageSession.indexOf(location.href) !== -1) {
6963-
// 8. reload from loading card.
6964-
// or forward from other page. alert(8)
6920+
//console.info(is_refresh, history.state, ck.viewport[0].id, last_url, location.href)
6921+
if (is_forward) {
6922+
// 8. alert(8)
69656923
ck.changeView(ck.loadingCard);
69666924
history.forward();
69676925
} else {
6968-
// 0. new page. alert(1)
6926+
// 0. alert(0)
69696927
ck.changeView(ck.defaultCard);
69706928
push_history(ck.loadingCard[0].id, ck.defaultCard[0].id);
6971-
pageSession.push(location.href);
69726929
}
69736930
}
69746931

@@ -7145,8 +7102,6 @@ define("../cardkit/app", [
71457102
next_id = 'ckLoading';
71467103
next = ck.loadingCard;
71477104
true_link = me.href;
7148-
//pageSession.clear(pageSession.indexOf(location.href));
7149-
pageSession.reset();
71507105
} else {
71517106
return;
71527107
}
@@ -7169,7 +7124,7 @@ define("../cardkit/app", [
71697124
ck.globalMask.hide();
71707125
ck.sessionLocked = false;
71717126
if (true_link) {
7172-
if (is_forward) {
7127+
if (is_forward && SUPPORT_HISTORY) {
71737128
history.forward();
71747129
} else {
71757130
location.href = true_link;
@@ -7197,7 +7152,7 @@ define("../cardkit/app", [
71977152
current.hide().removeClass('moving');
71987153
ck.globalMask.hide();
71997154
ck.sessionLocked = false;
7200-
if (prev_id === 'ckLoading') {
7155+
if (prev_id === 'ckLoading' && SUPPORT_HISTORY) {
72017156
history.back();
72027157
back_timeout = setTimeout(function(){
72037158
location.reload();
@@ -7207,13 +7162,15 @@ define("../cardkit/app", [
72077162
}
72087163

72097164
function push_history(prev_id, next_id, link, opt){
7210-
history.pushState({
7211-
prev: prev_id,
7212-
next: next_id,
7213-
link: link,
7214-
opt: opt,
7215-
i: history.length
7216-
}, document.title, location.href);
7165+
if (SUPPORT_HISTORY) {
7166+
history.pushState({
7167+
prev: prev_id,
7168+
next: next_id,
7169+
link: link,
7170+
opt: opt,
7171+
i: history.length
7172+
}, document.title, location.href);
7173+
}
72177174
}
72187175

72197176
function prevent_window_scroll(){
@@ -7250,8 +7207,6 @@ define("../cardkit/app", [
72507207
ck.sessionLocked = true;
72517208
var next_id = 'ckLoading';
72527209
var next = ck.loadingCard;
7253-
//pageSession.clear(pageSession.indexOf(location.href));
7254-
pageSession.reset();
72557210
var current = ck.viewport;
72567211
ck.globalMask.show();
72577212
push_history(current[0].id, next_id, true_link);

examples/js/browsers_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,26 @@ require([
6969
console.info('data("enableUrl"): ', test.data('enableUrl'));
7070
});
7171

72+
var n = 0;
73+
74+
$('.btn5').bind('click', function(){
75+
push_history();
76+
});
77+
78+
$(window).bind("popstate", function(e){
79+
console.warn('pop', e, e.state, history, history.state);
80+
});
81+
82+
console.warn('load', history, history.state);
83+
84+
function push_history(){
85+
history.pushState({
86+
prev: n,
87+
next: ++n,
88+
i: history.length
89+
}, document.title, location.href);
90+
console.warn('push', history, history.state);
91+
}
92+
7293
});
7394

examples/js/browsers_test_pack.js

+21
Original file line numberDiff line numberDiff line change
@@ -3000,5 +3000,26 @@ require([
30003000
console.info('data("enableUrl"): ', test.data('enableUrl'));
30013001
});
30023002

3003+
var n = 0;
3004+
3005+
$('.btn5').bind('click', function(){
3006+
push_history();
3007+
});
3008+
3009+
$(window).bind("popstate", function(e){
3010+
console.warn('pop', e, e.state, history, history.state);
3011+
});
3012+
3013+
console.warn('load', history, history.state);
3014+
3015+
function push_history(){
3016+
history.pushState({
3017+
prev: n,
3018+
next: ++n,
3019+
i: history.length
3020+
}, document.title, location.href);
3021+
console.warn('push', history, history.state);
3022+
}
3023+
30033024
});
30043025

0 commit comments

Comments
 (0)