Showing 1 changed files with 42 additions and 35 deletions
+42 -35
templates/index.html.ep
... ...
@@ -1,37 +1,14 @@
1 1
 <%
2 2
   my $dbi = app->dbi;
3
+
4
+  my $books = $dbi->model('book')->select(['id', 'short_name'])->all;
5
+
3 6
   my $op = param('op') // '';
4 7
   my $book_id = param('book-id');
5 8
   
6
-  my $current_book_id;
7
-  
8
-  my $show_word_count;
9 9
   my $word_count_h = {};
10 10
   my $word = param('word');
11 11
   my $word_length = length $word;
12
-  my $word_q = quotemeta($word) if defined $word;
13
-  if ($word_length) {
14
-    $show_word_count = 1;
15
-    for (my $i = 0; $i < 66; $i++) {
16
-      my $num = sprintf "%02d", $i + 1;
17
-      
18
-      my $content = $dbi->select(
19
-        'content_no_tag',
20
-        table => 'book',
21
-        where => {id => $num}
22
-      )->value;
23
-      my $content_length = length $content;
24
-      $content =~ s/$word_q//g;
25
-      my $content_length_no_word = length $content;
26
-      
27
-      # 文字の個数
28
-      my $word_count = ($content_length - $content_length_no_word) / $word_length;
29
-      $word_count_h->{$num} = $word_count;
30
-    }
31
-  }
32
-  
33
-  my $books = $dbi->model('book')->select(['id', 'short_name'])->all;
34
-  my $max_search_pos = $word_count_h->{$book_id} // 0;
35 12
   
36 13
   # 前と次の見つかった書籍
37 14
   my $next_book_id;
... ...
@@ -55,6 +32,9 @@
55 32
 
56 33
 %= javascript begin
57 34
   $(document).ready(function () {
35
+  
36
+    var current_word;
37
+    var current_book_id;
58 38
     
59 39
     var fragment = location.hash;
60 40
     var current_pos;
... ...
@@ -84,7 +64,7 @@
84 64
         var current_pos = $('#word-pos').text();
85 65
         var next_pos = current_pos - 0 + 1;
86 66
         
87
-        if (next_pos > <%= $max_search_pos %>) {
67
+        if (next_pos > 10) {
88 68
           location.href = "<%= url_for('/')->query(word => $word, 'book-id' => $next_book_id)->fragment('word-1') %>";
89 69
         }
90 70
         else {
... ...
@@ -94,6 +74,7 @@
94 74
       });
95 75
     % }
96 76
     
77
+    // 書をクリック
97 78
     $('.book').on('click', function () {
98 79
       var book_id_str = $(this).attr('id');
99 80
       var ret = book_id_str.match(/book-(\d+)/);
... ...
@@ -125,6 +106,33 @@
125 106
         current_book_id = book_id;
126 107
       });
127 108
     });
109
+    
110
+    // 検索をクリック
111
+    $('#search').on('click', function () {
112
+      var word = $(this).closest('div').find('[name=word]').val();
113
+      
114
+      if (word) {
115
+        var word_count_h;
116
+        $.get('/api/word-count/' + word, function (result) {
117
+          var word_count_h = result.word_count;
118
+          
119
+          $('#books tr').each(function () {
120
+            var book_id_str = $(this).attr('id');
121
+            if (book_id_str) {
122
+              var book_id = (book_id_str.match(/book-(\d+)/))[1];
123
+              if (word_count_h[book_id] === 0) {
124
+                $(this).css('display', 'none');
125
+              }
126
+              $(this).find('.word-count').text(word_count_h[book_id]);
127
+            }
128
+          });
129
+        });
130
+        
131
+        current_word = word;
132
+      }
133
+      
134
+      return false;
135
+    });
128 136
   });
129 137
 % end
130 138
 
... ...
@@ -135,10 +143,10 @@
135 143
   </div>
136 144
 
137 145
   <div id="boxB">
138
-      <form style="margin-bottom:5px" action="<%= url_for('current') %>" method="get">
139
-        <%= text_field 'word' , style => "width:160px"%>
140
-        <input type="submit" value="検索" style="width:50px;padding:2px;">
141
-      </form>
146
+      <div style="margin-bottom:5px">
147
+        <%= text_field 'word' , style => "width:160px" %>
148
+        <button id="search" style="width:50px;padding:2px;">検索</button>
149
+      </div>
142 150
       <div style="margin-bottom:10px;">
143 151
         <table style="border-collapse: collapse;width:100%;color:#333333">
144 152
           <tr>
... ...
@@ -160,7 +168,7 @@
160 168
         </table>
161 169
       </div>
162 170
       <div style="border:1px solid gray;width:218px;height:400px;overflow:auto;padding:5px">
163
-        <table style="border-collapse: collapse;width:100%;color:#333333">
171
+        <table id="books" style="border-collapse: collapse;width:100%;color:#333333">
164 172
             <tr style="border-bottom:1px solid #EEEEEE">
165 173
               <td>
166 174
... ...
@@ -173,7 +181,7 @@
173 181
             </tr>
174 182
           % my $prev_book_id;
175 183
           % for my $book (@$books) {
176
-            <tr>
184
+            <tr id="<%= "book-$book->{id}" %>">
177 185
               % if (!$word_length || $word_count_h->{$book->{id}} > 0) {
178 186
                 <td style="<%= ($book_id // '') eq $book->{id} ? 'background:#DDDDDD' : '' %>">
179 187
                   % my $book_url_query = {'book-id' => $book->{id}};
... ...
@@ -184,8 +192,7 @@
184 192
                     <%= $book->{short_name} %>
185 193
                   </a>
186 194
                 </td>
187
-                <td style="text-align:right">
188
-                  <%= $word_count_h->{$book->{id}} %>
195
+                <td class="word-count" style="text-align:right">
189 196
                 </td>
190 197
                 
191 198
                 % if ($prev_book_id && $book_id) {