Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to the tutorial editors to keep history in the browser. #926

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions examples/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ var exampleUtils = {
* location and history. This will also remove undefined values from the
* set properites of params.
*
* @param {object} params: the query parameters as a dictionary.
* @param {object} params The query parameters as a dictionary.
* @param {boolean} [updateHistory] If true, update the browser history. If
* falsy, replace the history state.
*/
setQuery: function (params) {
setQuery: function (params, updateHistory) {
$.each(params, function (key, value) {
if (value === undefined) {
delete params[key];
}
});
var newurl = window.location.protocol + '//' + window.location.host +
window.location.pathname + '?' + $.param(params);
window.history.replaceState(params, '', newurl);
if (updateHistory) {
window.history.pushState(params, '', newurl);
} else {
window.history.replaceState(params, '', newurl);
}
}
};

Expand Down
36 changes: 27 additions & 9 deletions tutorials/common/tutorials.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,11 @@ function run_tutorial() {
}

/**
* Get query parameters for each step and update them as we start. Monitor the
* code blocks and update the url when they change if appropriate.
* Fill the codeblocks based on the encoded values in a query.
*
* @param {boolean} alwaysKeep If `true`, update the url even if the `keep` url
* parameter is not specified.
* @param {object} query An object as returned from `utils.getQuery()`.
*/
function start_keeper(alwaysKeep) {
var query = utils.getQuery(),
keep = query.keep || (alwaysKeep === true);

function fill_codeblocks(query) {
$('.codeblock').each(function () {
var block = $(this),
key = 'src' + (block.attr('step') !== '1' ? block.attr('step') : '');
Expand All @@ -259,6 +254,21 @@ function start_keeper(alwaysKeep) {
} catch (err) { }
}
});
}

/**
* Get query parameters for each step and update them as we start. Monitor the
* code blocks and update the url when they change if appropriate.
*
* @param {boolean} alwaysKeep If `true`, update the url even if the `keep` url
* parameter is not specified.
*/
function start_keeper(alwaysKeep) {
var query = utils.getQuery(),
keep = query.keep || (alwaysKeep === true),
inPop = false;

fill_codeblocks(query);
if (keep) {
$(document).on('geojs-tutorial-run', '.codeblock', function () {
var newQuery = {};
Expand All @@ -280,9 +290,17 @@ function start_keeper(alwaysKeep) {
newQuery[key] = comp;
}
});
utils.setQuery(newQuery);
if (!inPop) {
utils.setQuery(newQuery, true);
}
});
}
window.addEventListener('popstate', function (evt) {
inPop = true;
fill_codeblocks(utils.getQuery());
run_tutorial();
inPop = false;
}, false);
}

/**
Expand Down