Skip to content

Commit 8724dc1

Browse files
committed
Fix back button in Sandcastle.
We store information in history state objects about the loaded demo. Navigating to new demos calls pushState, and a fresh load stores the demo specified via URL using replaceState. Fixes #6509
1 parent 5f56208 commit 8724dc1

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

Apps/Sandcastle/CesiumSandcastle.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ require({
178178
var searchTerm = '';
179179
var searchRegExp;
180180
var hintTimer;
181-
var currentTab = '';
181+
var defaultDemo = 'Hello World';
182+
var defaultLabel = 'Showcases';
183+
var currentTab = defaultLabel;
182184
var newDemo;
183185
var demoHtml = '';
184186
var demoCode = '';
@@ -700,10 +702,10 @@ require({
700702
queryObject = ioQuery.queryToObject(window.location.search.substring(1));
701703
}
702704
if (!defined(queryObject.src)) {
703-
queryObject.src = 'Hello World.html';
705+
queryObject.src = defaultDemo + '.html';
704706
}
705707
if (!defined(queryObject.label)) {
706-
queryObject.label = 'Showcases';
708+
queryObject.label = defaultLabel;
707709
}
708710

709711
function loadFromGallery(demo) {
@@ -956,6 +958,18 @@ require({
956958
window.location.href = getBaseUrl() + '?gist=' + gistId;
957959
});
958960

961+
function getPushStateUrl(demo) {
962+
var obj = {};
963+
if (demo.name !== defaultDemo) {
964+
obj.src = demo.name + '.html';
965+
}
966+
if (currentTab !== defaultLabel) {
967+
obj.label = currentTab;
968+
}
969+
var query = ioQuery.objectToQuery(obj);
970+
return query === '' ? query : '?' + query;
971+
}
972+
959973
registry.byId('buttonNew').on('click', function() {
960974
var htmlText = (htmlEditor.getValue()).replace(/\s/g, '');
961975
var jsText = (jsEditor.getValue()).replace(/\s/g, '');
@@ -965,16 +979,7 @@ require({
965979
}
966980
if (confirmChange) {
967981
loadFromGallery(newDemo).then(function() {
968-
var demoSrc = newDemo.name + '.html';
969-
var queries = window.location.search.substring(1).split('&');
970-
for (var i = 0; i < queries.length; i++) {
971-
var key = queries[i].split('=')[0];
972-
if (key === "src") {
973-
if (demoSrc !== queries[i].split('=')[1].replace('%20', ' ')) {
974-
window.history.pushState(newDemo, newDemo.name, '?src=' + demoSrc + '&label=' + currentTab);
975-
}
976-
}
977-
}
982+
window.history.pushState(newDemo, newDemo.name, getPushStateUrl(newDemo));
978983
document.title = newDemo.name + ' - Cesium Sandcastle';
979984
});
980985
}
@@ -1068,13 +1073,12 @@ require({
10681073

10691074
function requestDemo(name) {
10701075
return xhr.get({
1071-
url : 'gallery/' + name + '.html',
1072-
handleAs : 'text',
1073-
error : function(error) {
1074-
loadFromGallery(gallery_demos[hello_world_index])
1075-
.then(function() {
1076-
deferredLoadError = true;
1077-
});
1076+
url: 'gallery/' + name + '.html',
1077+
handleAs: 'text',
1078+
error: function(error) {
1079+
loadFromGallery(gallery_demos[hello_world_index]).then(function() {
1080+
deferredLoadError = true;
1081+
});
10781082
}
10791083
});
10801084
}
@@ -1107,6 +1111,8 @@ require({
11071111
if (defined(queryObject.src)) {
11081112
if (demo.name === queryObject.src.replace('.html', '')) {
11091113
loadFromGallery(demo).then(function() {
1114+
window.history.replaceState(demo, demo.name, getPushStateUrl(demo));
1115+
11101116
if (defined(queryObject.gist)) {
11111117
document.title = 'Gist Import - Cesium Sandcastle';
11121118
} else {
@@ -1207,12 +1213,11 @@ require({
12071213
if (confirmChange) {
12081214
delete queryObject.gist;
12091215
delete queryObject.code;
1210-
history.replaceState(null, document.title, window.location.pathname + window.location.search);
12111216

12121217
loadFromGallery(demo).then(function() {
12131218
var demoSrc = demo.name + '.html';
12141219
if (demoSrc !== window.location.search.substring(1)) {
1215-
window.history.pushState(demo, demo.name, '?src=' + demoSrc + '&label=' + currentTab);
1220+
window.history.pushState(demo, demo.name, getPushStateUrl(demo));
12161221
}
12171222
document.title = demo.name + ' - Cesium Sandcastle';
12181223
});

0 commit comments

Comments
 (0)