summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/examples/dictionary/html/js/dictionary.js
blob: 850c64cc667cb8ee43433b921079b0ba7383060e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function dictionarySource(url) {
    function displayItems(items) {
        var results = $("<dl />");
        $.each(items,
	       function(i, val) {
		   results.append($("<dt />")
				  .append($("<span />")
					  .text(val[0])
					  .click(function() {
						     $(".search").val($(this).text());
						     $("#search").submit();
						 })));
		   results.append($("<dd />")
				  .append($("<span />").text(val[1]))
				  .append($("<span />").text(val[2]))
				 );
	       });
        $("#result")
	    .empty()
	    .append(results);
    };

    var request_index = 0;
    var columns = "_key,gene95_desc,edict_desc";
    var xhr;
    function source(request, response) {
	function onSuccess(data, status) {
	    if (this.autocomplete_request != request_index) {
		return;
	    }
	    var completions = data[1]["complete"];
	    var items = [];
	    if (completions && completions.length > 2) {
		completions.shift();
		completions.shift();
		$.each(completions,
		       function(i, item) {
			   var key = item[0];
			   items.push(key);
			   if (items.length >= 3) {
			       return false;
			   }
			   return true;
		       });
	    }
	    if (completions.length > 0) {
		displayItems(completions);
	    }
	    response(items);
	}

	function onError() {
	    if (this.autocomplete_request != request_index) {
		return;
	    }
	    response([]);
	}

	if (xhr) {
	    xhr.abort();
	}
	xhr = $.ajax(url,
		     {
			 data: {
			     query: request.term,
			     types: 'complete',
			     table: 'item_dictionary',
			     column: 'kana',
			     limit: 25,
			     output_columns: columns,
			     frequency_threshold: 1,
			     prefix_search: "yes"
			 },
			 dataType: "jsonp",
			 autocomplete_request: ++request_index,
			 success: onSuccess,
			 error: onError
		     });
    };

    return source;
}