Skip to content

Commit 8ef36d1

Browse files
committed
only update when necessary
1 parent 5897787 commit 8ef36d1

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

notebook/static/tree/js/directoryreadme.js

+41-8
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,52 @@ define([
1515
this.notebook_list = notebook_list;
1616
this.base_url = options.base_url || utils.get_body_data("baseUrl");
1717
this.contents = options.contents;
18+
this.drawn_readme = null;
1819

1920
this.init_readme();
2021
this.bind_events();
2122
};
2223

24+
DirectoryReadme.prototype.find_readme = function() {
25+
var files_in_directory = this.notebook_list.model_list.content;
26+
for (var i = 0; i < files_in_directory.length; i++) {
27+
var file = files_in_directory[i];
28+
if(file.type === "file"
29+
&& file.mimetype === "text/markdown"
30+
&& file.name.toLowerCase().split(".")[0] === "readme"){
31+
return file;
32+
}
33+
}
34+
return null;
35+
}
36+
37+
DirectoryReadme.prototype.needs_update = function(readme) {
38+
if(this.drawn_readme === readme) return false;
39+
if(this.drawn_readme === null || readme === null) return true;
40+
if(this.drawn_readme.path !== readme.path) return true;
41+
if(this.draw_readme.last_modified < readme.last_modified) return true;
42+
return false;
43+
}
44+
45+
2346
DirectoryReadme.prototype.fetch_readme = function() {
24-
var that = this;
25-
this.contents.get(utils.url_path_join(this.notebook_list.notebook_path, 'readme.md'), {type: 'file'}).then(
26-
function(file) {
27-
that.draw_readme(file);
28-
},
29-
function() {
30-
that.clear_readme();
47+
var readme = this.find_readme();
48+
49+
if(this.needs_update(readme)) {
50+
if(readme === null) {
51+
this.clear_readme();
52+
} else {
53+
var that = this;
54+
this.contents.get(readme.path, {type: 'file'}).then(
55+
function(file) {
56+
that.draw_readme(file);
57+
},
58+
function() {
59+
that.clear_readme();
60+
}
61+
);
3162
}
32-
);
63+
}
3364
}
3465

3566
DirectoryReadme.prototype.bind_events = function () {
@@ -71,10 +102,12 @@ define([
71102
}
72103

73104
DirectoryReadme.prototype.clear_readme = function () {
105+
this.drawn_readme = null;
74106
this.element.hide();
75107
}
76108

77109
DirectoryReadme.prototype.draw_readme = function (file) {
110+
this.drawn_readme = file;
78111
this.element.show();
79112
this.title
80113
.attr("href",

0 commit comments

Comments
 (0)