Skip to content

Commit 9ce534c

Browse files
authored
[WIP] Use DOM history API for navigating directories (#3115)
* Use DOM history API for navigating directories * Fix test * Fix building URLs and states for breadcrumbs * Use base_url when constructing navigation URLs
1 parent 8e4c0bf commit 9ce534c

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

notebook/static/tree/js/notebooklist.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,54 @@ define([
354354

355355
NotebookList.prototype.load_list = function () {
356356
var that = this;
357+
// Add an event handler browser back and forward events
358+
window.onpopstate = function(e) {
359+
var path = window.history.state ? window.history.state.path : '';
360+
that.update_location(path);
361+
};
362+
var breadcrumb = $('.breadcrumb');
363+
breadcrumb.empty();
364+
var list_item = $('<li/>');
365+
var root = $('<li/>').append('<a href="/tree"><i class="fa fa-folder"></i></a>').click(function(e) {
366+
var path = '';
367+
window.history.pushState({
368+
path: path
369+
}, 'Home', utils.url_path_join(that.base_url, 'tree'));
370+
that.update_location(path);
371+
return false;
372+
});
373+
breadcrumb.append(root);
374+
var path_parts = [];
375+
this.notebook_path.split('/').forEach(function(path_part) {
376+
path_parts.push(path_part)
377+
var path = path_parts.join('/')
378+
var url = utils.url_path_join(
379+
that.base_url,
380+
'/tree',
381+
utils.encode_uri_components(path)
382+
);
383+
var crumb = $('<li/>').append('<a href="' + url + '">' + path_part + '</a>').click(function(e) {
384+
window.history.pushState({
385+
path: path
386+
}, path, url);
387+
that.update_location(path);
388+
return false;
389+
});
390+
breadcrumb.append(crumb);
391+
});
357392
this.contents.list_contents(that.notebook_path).then(
358393
$.proxy(this.draw_notebook_list, this),
359394
function(error) {
360395
that.draw_notebook_list({content: []}, i18n.msg._("Server error: ") + error.message);
361396
}
362397
);
363398
};
399+
400+
NotebookList.prototype.update_location = function (path) {
401+
this.notebook_path = path;
402+
// Update the file tree list without reloading the page
403+
this.load_list();
404+
};
364405

365406
/**
366407
* Draw the list of notebooks
@@ -723,6 +764,7 @@ define([
723764
};
724765

725766
NotebookList.prototype.add_link = function (model, item) {
767+
var that = this;
726768
var running = (model.type === 'notebook' && this.sessions[model.path] !== undefined);
727769
item.data('name',model.name);
728770
item.data('path', model.path);
@@ -762,7 +804,21 @@ define([
762804
// directory nav doesn't open new tabs
763805
// files, notebooks do
764806
if (model.type !== "directory") {
765-
link.attr('target',IPython._target);
807+
link.attr('target', IPython._target);
808+
} else {
809+
// Replace with a click handler that will use the History API to
810+
// push a new route without reloading the page
811+
link.click(function (e) {
812+
window.history.pushState({
813+
path: model.path
814+
}, model.path, utils.url_path_join(
815+
that.base_url,
816+
'tree',
817+
utils.encode_uri_components(model.path)
818+
));
819+
that.update_location(model.path);
820+
return false;
821+
});
766822
}
767823

768824
// Add in the date that the file was last modified

0 commit comments

Comments
 (0)