@@ -6,23 +6,35 @@ define([
6
6
'base/js/utils' ,
7
7
'base/js/events' ,
8
8
'base/js/markdown' ,
9
- 'base/js/mathjaxutils' ,
10
- ] , function ( $ , utils , events , markdown , mathjaxutils ) {
9
+ ] , function ( $ , utils , events , markdown ) {
11
10
"use strict" ;
12
11
13
- var DirectoryReadme = function ( selector , notebook_list , options ) {
12
+ var DirectoryReadme = function ( selector , notebook_list ) {
13
+ /**
14
+ * Constructor
15
+ *
16
+ * Parameters:
17
+ * selector: string
18
+ * notebook_list: NotebookList
19
+ * Used to obtain a file listing of the active directory.
20
+ */
14
21
this . selector = selector ;
15
22
this . element = $ ( selector ) ;
16
23
this . notebook_list = notebook_list ;
17
- this . base_url = options . base_url || utils . get_body_data ( "baseUrl" ) ;
18
- this . contents = options . contents ;
19
24
this . drawn_readme = null ;
20
25
21
26
this . init_readme ( ) ;
22
27
this . bind_events ( ) ;
23
28
} ;
24
29
25
30
DirectoryReadme . prototype . find_readme = function ( ) {
31
+ /**
32
+ * Find a readme in the current directory. Look for files with
33
+ * a name like 'readme.md' (case insensitive) or similar and
34
+ * mimetype 'text/markdown'.
35
+ *
36
+ * @return null or { name, path, last_modified... }
37
+ */
26
38
var files_in_directory = this . notebook_list . model_list . content ;
27
39
for ( var i = 0 ; i < files_in_directory . length ; i ++ ) {
28
40
var file = files_in_directory [ i ] ;
@@ -36,6 +48,12 @@ define([
36
48
}
37
49
38
50
DirectoryReadme . prototype . needs_update = function ( readme ) {
51
+ /**
52
+ * Checks if readme is newer or different from the current drawn readme.
53
+ *
54
+ * @private
55
+ * @return if a redraw should happen
56
+ */
39
57
if ( this . drawn_readme === readme ) return false ;
40
58
if ( this . drawn_readme === null || readme === null ) return true ;
41
59
if ( this . drawn_readme . path !== readme . path ) return true ;
@@ -45,14 +63,17 @@ define([
45
63
46
64
47
65
DirectoryReadme . prototype . fetch_readme = function ( ) {
66
+ /**
67
+ * Find and fetch a readme file, and if necessary trigger a redraw.
68
+ */
48
69
var readme = this . find_readme ( ) ;
49
70
50
71
if ( this . needs_update ( readme ) ) {
51
72
if ( readme === null ) {
52
73
this . clear_readme ( ) ;
53
74
} else {
54
75
var that = this ;
55
- this . contents . get ( readme . path , { type : 'file' } ) . then (
76
+ this . notebook_list . contents . get ( readme . path , { type : 'file' } ) . then (
56
77
function ( file ) {
57
78
that . draw_readme ( file ) ;
58
79
} ,
@@ -65,10 +86,16 @@ define([
65
86
}
66
87
67
88
DirectoryReadme . prototype . bind_events = function ( ) {
89
+ /**
90
+ * When the notebook_list fires a draw_notebook event, fetch the readme.
91
+ */
68
92
events . on ( "draw_notebook_list.NotebookList" , $ . proxy ( this . fetch_readme , this ) ) ;
69
93
}
70
94
71
95
DirectoryReadme . prototype . init_readme = function ( ) {
96
+ /**
97
+ * Build the DOM.
98
+ */
72
99
var element = this . element ;
73
100
element . hide ( ) . addClass ( "list_container" ) ;
74
101
@@ -88,17 +115,25 @@ define([
88
115
}
89
116
90
117
DirectoryReadme . prototype . clear_readme = function ( ) {
118
+ /**
119
+ * If no readme is found, hide.
120
+ */
91
121
this . drawn_readme = null ;
92
122
this . element . hide ( ) ;
93
123
}
94
124
95
125
DirectoryReadme . prototype . draw_readme = function ( file ) {
126
+ /**
127
+ * Draw the given readme file. This function is used by fetch_readme.
128
+ *
129
+ * @param file: {name, path, content}
130
+ */
96
131
this . drawn_readme = file ;
97
132
this . element . show ( ) ;
98
133
this . title
99
134
. attr ( "href" ,
100
135
utils . url_path_join (
101
- this . base_url ,
136
+ this . notebook_list . base_url ,
102
137
"edit" ,
103
138
utils . encode_uri_components ( file . path )
104
139
) )
0 commit comments