Showing 2 changed files with 73 additions and 49 deletions
+13 -1
lib/Biblesearch.pm
... ...
@@ -35,7 +35,19 @@ sub startup {
35 35
   # Route
36 36
   my $r = $self->routes;
37 37
   $r->get('/')->to(template => 'index');
38
-  $r->get('/test')->to(template => 'test');
38
+  $r->get('/api/book/:id/content')->to(cb => sub {
39
+    my $self = shift;
40
+    my $id = $self->param('id');
41
+    
42
+    my $dbi = $self->app->dbi;
43
+    my $content = $dbi->model('book')->select('content', id => $id)->value;
44
+    
45
+    my $data = {
46
+      content => $content
47
+    };
48
+    
49
+    $self->render(json => $data);
50
+  });
39 51
 }
40 52
 
41 53
 1;
+60 -48
templates/index.html.ep
... ...
@@ -3,6 +3,8 @@
3 3
   my $op = param('op') // '';
4 4
   my $book_id = param('book-id');
5 5
   
6
+  my $current_book_id;
7
+  
6 8
   my $show_word_count;
7 9
   my $word_count_h = {};
8 10
   my $word = param('word');
... ...
@@ -29,28 +31,7 @@
29 31
   }
30 32
   
31 33
   my $books = $dbi->model('book')->select(['id', 'short_name'])->all;
32
-  
33
-  my $content;
34
-  my $max_search_pos;
35
-  if ($book_id) {
36
-    $content = $dbi->model('book')->select(
37
-      'content',
38
-      where => {id => $book_id}
39
-    )->value;
40
-    
41
-    # アンカーを追加
42
-    if ($word_length) {
43
-      my $i = 1;
44
-      my $replace_cb = sub {
45
-        my $word = shift;
46
-        my $after = qq|<a style="background:#00ffff" id="word-$i">$word</a>|;
47
-        $i++;
48
-        return $after;
49
-      };
50
-      $content =~ s#($word_q)#$replace_cb->($1)#ge;
51
-      $max_search_pos = $i - 1;
52
-    }
53
-  }
34
+  my $max_search_pos = $word_count_h->{$book_id} // 0;
54 35
   
55 36
   # 前と次の見つかった書籍
56 37
   my $next_book_id;
... ...
@@ -86,29 +67,63 @@
86 67
     }
87 68
     $("#word-pos").text(current_pos);
88 69
     
89
-    $("#up-arrow").on('click', function () {
90
-      var current_pos = $('#word-pos').text();
91
-      var prev_pos = current_pos - 1;
92
-      
93
-      if (prev_pos < 1) {
94
-        location.href = "<%= url_for('/')->query(word => $word, 'book-id' => $prev_book_id)->fragment("word-$word_count_h->{$prev_book_id}") %>";
95
-      } else {
96
-        location.href = '#word-' + prev_pos;
97
-        $('#word-pos').text(prev_pos);
98
-      }
99
-    });
70
+    % if ($word_length) {
71
+      $("#up-arrow").on('click', function () {
72
+        var current_pos = $('#word-pos').text();
73
+        var prev_pos = current_pos - 1;
74
+        
75
+        if (prev_pos < 1) {
76
+          location.href = "<%= url_for('/')->query(word => $word, 'book-id' => $prev_book_id)->fragment("word-$word_count_h->{$prev_book_id}") %>";
77
+        } else {
78
+          location.href = '#word-' + prev_pos;
79
+          $('#word-pos').text(prev_pos);
80
+        }
81
+      });
100 82
 
101
-    $("#down-arrow").on('click', function () {
102
-      var current_pos = $('#word-pos').text();
103
-      var next_pos = current_pos - 0 + 1;
83
+      $("#down-arrow").on('click', function () {
84
+        var current_pos = $('#word-pos').text();
85
+        var next_pos = current_pos - 0 + 1;
86
+        
87
+        if (next_pos > <%= $max_search_pos %>) {
88
+          location.href = "<%= url_for('/')->query(word => $word, 'book-id' => $next_book_id)->fragment('word-1') %>";
89
+        }
90
+        else {
91
+          location.href = '#word-' + next_pos;
92
+          $('#word-pos').text(next_pos);
93
+        }
94
+      });
95
+    % }
96
+    
97
+    $('.book').on('click', function () {
98
+      var book_id_str = $(this).attr('id');
99
+      var ret = book_id_str.match(/book-(\d+)/);
100
+      var book_id = ret[1];
101
+      var word;
102
+      % if ($word_length) {
103
+        % my $word_quote = $word;
104
+        % $word_quote =~ s/'/\'/g;
105
+        word = '<%= $word %>';
106
+      % }
104 107
       
105
-      if (next_pos > <%= $max_search_pos %>) {
106
-        location.href = "<%= url_for('/')->query(word => $word, 'book-id' => $next_book_id)->fragment('word-1') %>";
107
-      }
108
-      else {
109
-        location.href = '#word-' + next_pos;
110
-        $('#word-pos').text(next_pos);
111
-      }
108
+      $.get('/api/book/' + book_id + '/content', function (result) {
109
+
110
+        if (word) {
111
+          var i = 1;
112
+          var replace_cb = function (all, group1) {
113
+            var after = '<a style="background:#00ffff" id="word-' + i + '">' + group1 + '</a>';
114
+            i = i + 1;
115
+            return after;
116
+          };
117
+          // var word_regex_quote = word.replace(/([^0-9A-Za-z_])/g, '\\$1');
118
+          //alert(word_regex_quote);
119
+          
120
+          var word_re = new RegExp('(' + word + ')', 'g');
121
+          result.content = result.content.replace(word_re, function (all, group1) { return replace_cb(all, group1); });
122
+        }
123
+        
124
+        $("#content").html(result.content);
125
+        current_book_id = book_id;
126
+      });
112 127
     });
113 128
   });
114 129
 % end
... ...
@@ -165,7 +180,7 @@
165 180
                   % $book_url_query->{word} = $word if $word_length;
166 181
                   % my $book_url = url_for->query($book_url_query);
167 182
                   % $book_url->fragment('word-1') if $word_length;
168
-                  <a class="book" id="<%= "book-$book->{id}" %>" href="<%= $book_url %>">
183
+                  <a class="book" id="<%= "book-$book->{id}" %>" href="javascript:void(0)">
169 184
                     <%= $book->{short_name} %>
170 185
                   </a>
171 186
                 </td>
... ...
@@ -185,10 +200,7 @@
185 200
   </div>
186 201
 
187 202
   <div id="boxC">
188
-    <div style="height:500px;overflow:auto">
189
-      % if ($book_id) {
190
-        %== $content
191
-      % }
203
+    <div id="content" style="height:500px;overflow:auto">
192 204
     </div>
193 205
   </div>
194 206