@@ -6,23 +6,35 @@ define([
66 'base/js/utils' ,
77 'base/js/events' ,
88 'base/js/markdown' ,
9- 'base/js/mathjaxutils' ,
10- ] , function ( $ , utils , events , markdown , mathjaxutils ) {
9+ ] , function ( $ , utils , events , markdown ) {
1110 "use strict" ;
1211
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+ */
1421 this . selector = selector ;
1522 this . element = $ ( selector ) ;
1623 this . notebook_list = notebook_list ;
17- this . base_url = options . base_url || utils . get_body_data ( "baseUrl" ) ;
18- this . contents = options . contents ;
1924 this . drawn_readme = null ;
2025
2126 this . init_readme ( ) ;
2227 this . bind_events ( ) ;
2328 } ;
2429
2530 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+ */
2638 var files_in_directory = this . notebook_list . model_list . content ;
2739 for ( var i = 0 ; i < files_in_directory . length ; i ++ ) {
2840 var file = files_in_directory [ i ] ;
@@ -36,6 +48,12 @@ define([
3648 }
3749
3850 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+ */
3957 if ( this . drawn_readme === readme ) return false ;
4058 if ( this . drawn_readme === null || readme === null ) return true ;
4159 if ( this . drawn_readme . path !== readme . path ) return true ;
@@ -45,14 +63,17 @@ define([
4563
4664
4765 DirectoryReadme . prototype . fetch_readme = function ( ) {
66+ /**
67+ * Find and fetch a readme file, and if necessary trigger a redraw.
68+ */
4869 var readme = this . find_readme ( ) ;
4970
5071 if ( this . needs_update ( readme ) ) {
5172 if ( readme === null ) {
5273 this . clear_readme ( ) ;
5374 } else {
5475 var that = this ;
55- this . contents . get ( readme . path , { type : 'file' } ) . then (
76+ this . notebook_list . contents . get ( readme . path , { type : 'file' } ) . then (
5677 function ( file ) {
5778 that . draw_readme ( file ) ;
5879 } ,
@@ -65,10 +86,16 @@ define([
6586 }
6687
6788 DirectoryReadme . prototype . bind_events = function ( ) {
89+ /**
90+ * When the notebook_list fires a draw_notebook event, fetch the readme.
91+ */
6892 events . on ( "draw_notebook_list.NotebookList" , $ . proxy ( this . fetch_readme , this ) ) ;
6993 }
7094
7195 DirectoryReadme . prototype . init_readme = function ( ) {
96+ /**
97+ * Build the DOM.
98+ */
7299 var element = this . element ;
73100 element . hide ( ) . addClass ( "list_container" ) ;
74101
@@ -88,17 +115,25 @@ define([
88115 }
89116
90117 DirectoryReadme . prototype . clear_readme = function ( ) {
118+ /**
119+ * If no readme is found, hide.
120+ */
91121 this . drawn_readme = null ;
92122 this . element . hide ( ) ;
93123 }
94124
95125 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+ */
96131 this . drawn_readme = file ;
97132 this . element . show ( ) ;
98133 this . title
99134 . attr ( "href" ,
100135 utils . url_path_join (
101- this . base_url ,
136+ this . notebook_list . base_url ,
102137 "edit" ,
103138 utils . encode_uri_components ( file . path )
104139 ) )
0 commit comments