@@ -12,6 +12,7 @@ var ditto = {
1212 edit_button : true ,
1313 back_to_top_button : true ,
1414 save_progress : true , // 保存阅读进度
15+ search_bar : true ,
1516
1617 // initialize function
1718 run : initialize
@@ -40,8 +41,13 @@ function initialize() {
4041}
4142
4243function init_sidebar_section ( ) {
43- $ . get ( ditto . sidebar_file , function ( data ) {
44+ $ . get ( ditto . sidebar_file , function ( data ) {
4445 $ ( ditto . sidebar_id ) . html ( marked ( data ) ) ;
46+
47+ if ( ditto . search_bar ) {
48+ init_searchbar ( ) ;
49+ }
50+
4551 // 初始化内容数组
4652 var menuOL = $ ( ditto . sidebar_id + ' ol' ) ;
4753 menuOL . attr ( 'start' , 0 ) ;
@@ -66,9 +72,25 @@ function init_sidebar_section() {
6672 } , "text" ) . fail ( function ( ) {
6773 alert ( "Opps! can't find the sidebar file to display!" ) ;
6874 } ) ;
75+ }
6976
77+ function init_searchbar ( ) {
78+ var search = '<input name="search" type="search">' ;
79+ $ ( ditto . sidebar_id ) . find ( 'h2' ) . first ( ) . before ( $ ( search ) ) ;
80+ $ ( 'input[name=search]' ) . keydown ( searchbar_listener ) ;
7081}
7182
83+ function searchbar_listener ( event ) {
84+ if ( event . which === 13 ) {
85+ var q = $ ( 'input[name=search]' ) . val ( ) ;
86+ if ( q !== '' ) {
87+ var url = 'https://github.com/ruanyf/es6tutorial/search?utf8=✓&q=' + encodeURIComponent ( q ) ;
88+ location . href = url ;
89+ }
90+ }
91+ }
92+
93+
7294function init_back_to_top_button ( ) {
7395 $ ( ditto . back_to_top_id ) . show ( ) ;
7496 $ ( ditto . back_to_top_id ) . on ( 'click' , goTop ) ;
0 commit comments