source: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/web-portal/index.js@ 31383

Last change on this file since 31383 was 31383, checked in by davidb, 7 years ago

Files for initial functioning search page

File size: 5.3 KB
Line 
1
2var langs_with_pos =["da","de","en","nl","pt"];
3var langs_without_pos =["af","ar","bg","bn","cs","el","es","et","fa","fi","fr","he","hi","hr","hu","id","it","ja","kn","ko","lt","lv","mk","ml","mr","ne","no","pa","pl","ro","ru","sk","sl","so","sq","sv","sw","ta","te","th","tl","tr","uk","ur","vi","zh-cn","zh-tw"];
4
5function lang_pos_toggle(event) {
6 var $this =$(this);
7 var checked_state = $this.prop("checked");
8
9 var id = $this.attr("id");
10 var split_id = id.split("-");
11 var related_id = split_id[0] + "-pos-choice";
12
13 //console.log("checked state = " + checked_state);
14
15 var disable_state = !checked_state;
16 $('#'+related_id + " *").prop('disabled',disable_state);
17};
18
19function ajax_error(jqXHR, textStatus, errorThrown) {
20 alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
21
22 console.log('jqXHR:' + jqXHR);
23 console.log('textStatus:' + textStatus);
24 console.log('errorThrown:' + errorThrown);
25}
26
27
28function add_titles(json_data) {
29 $.each( json_data, function( htid_with_prefix, htid_val ) {
30 var htid = htid_with_prefix.replace(/^htid:/,"");
31
32 $.each(htid_val.records, function( internalid, metadata ) {
33 var title = metadata.titles[0];
34 $("[name='"+htid+"']").each(function() {$(this).html(title)});
35 console.log(htid + ", title = " + metadata.titles[0]);
36 });
37 });
38}
39
40function show_results(jsonData) {
41 var response = jsonData.response;
42 var num_found = response.numFound;
43 var docs = response.docs;
44 var num_docs = docs.length;
45
46 var $search_results = $('#search-results');
47 if (num_docs>0) {
48 $search_results.html("<p>Results: " + num_found + " volumes/documents matched</p>");
49 $search_results.append("<p>Showing matches: 1-" + num_docs + "</p>");
50 }
51 else {
52 $search_results.html("<p>No volumes/documents matched your query</p>");
53 }
54
55 // https://babel.hathitrust.org/cgi/pt?id=hvd.hnnssu;view=1up;seq=11
56
57 var htids = [];
58
59 for (var i=0; i<num_docs; i++) {
60 var doc = docs[i];
61 var id_and_page = doc.id.split(".page-");
62 var id = id_and_page[0];
63 var page = parseInt(id_and_page[1]);
64
65
66 var css_class = (i%2 == 0) ? 'class="evenline"' : 'class="oddline"';
67 //var restful_safe_id = id.replace(/\./g,",").replace(/:/g, "+").replace(/\//g, "=");
68
69 var babel_url = "https://babel.hathitrust.org/cgi/pt?id="+id+";view=1up;seq="+page;
70
71 var result_item = '<a target="_blank" href="' + babel_url + '">' + id + ', page ' + page + '</a>';
72 result_item += ' <span style="font-style: italic;" name="'+id+'"><span style="cursor: progress;">Loading ...</span></span>';
73 $search_results.append('<p ' + css_class + '>'+result_item+'</p>');
74
75 htids.push("htid:"+id);
76
77 }
78
79 // Example URL for catalog metadata (multiple items)
80 // http://catalog.hathitrust.org/api/volumes/brief/json/id:552;lccn:70628581|isbn:0030110408
81
82 var htids_str = htids.join("|",htids);
83 var cat_url = "http://catalog.hathitrust.org/api/volumes/brief/json/" + htids_str;
84 $.ajax({
85 url: cat_url,
86 dataType: 'jsonp',
87 jsonpCallback: "add_titles"
88 });
89
90
91 //var json_pretty = JSON.stringify(jsonData.response, null, '\t');
92 //$('#search-results').text(json_pretty)
93}
94
95$(function() {
96 $('#en-enabled').click(lang_pos_toggle);
97 $('#de-enabled').click(lang_pos_toggle);
98 $('#de-pos-choice *').prop('disabled',true);
99
100 // setup other languages
101 // for each 'langs_without_pos' generate HTML of the form:
102 // <input type="checkbox" name="fr-enabled" id="fr-enabled" />French
103 var $other_langs = $('#other-langs');
104 for (var i=0; i<langs_without_pos.length; i++) {
105 var lang = langs_without_pos[i];
106 $other_langs.append('<input type="checkbox" name="'+lang+'-enabled" id="'+lang+'-enabled" />');
107 /*
108 if (lang === "zh-cn") {
109 console.log("Mapping zh-cn => zh");
110 lang = "zh";
111 }
112 if (lang === "zh-tw") {
113 console.log("Mapping zh-tw => zh");
114 lang = "zh";
115 }
116*/
117 var lang_full = isoLangs[lang].name;
118 var lang_native_full = isoLangs[lang].nativeName;
119 var opt_title = (lang_full !== lang_native_full) ? 'title="'+lang_native_full+'"' : "";
120
121 $other_langs.append('<span style="padding-right: 10px;" ' + opt_title + '>'+lang_full+'</span> ');
122
123 }
124
125
126
127 $('#search-submit').click(function(event) {
128 event.preventDefault();
129
130 var action = $('#search-form').attr("action");
131
132 var arg_indent = $('#indent').attr('value');
133 var arg_wt = $('#wt').attr('value');
134 var q_text = $('#q').val();
135
136 var fields = [];
137 var langs_with_pos = [ "da", "de", "en", "nl", "pt", "se" ];
138 var universal_pos_tags = [ "VERB", "NOUN", "ADJ", "ADV", "ADP", "CONJ", "DET", "NUM", "PRT", "X" ];
139
140 for (var l=0; l<langs_with_pos.length; l++) {
141 var lang = langs_with_pos[l];
142 for (var t=0; t<universal_pos_tags.length; t++) {
143 var tag = universal_pos_tags[t];
144 var lang_tag_id = lang+"-"+tag+"-htrctoken-cb";
145 var $lang_tag_cb = $('#'+lang_tag_id);
146 if ($lang_tag_cb.is(':checked')) {
147 var lang_tag_field = "xxxx"+lang+"_"+tag+"_htrctoken";
148 fields.push(lang_tag_field+":"+q_text);
149 }
150 }
151 }
152
153 var arg_q = fields.join(" OR ");
154
155
156 // Example search on one of the htrc-full-ef fields is:
157 // q=xxxxen_NOUN_htrctoken:farming
158
159 var args = { q: arg_q, indent: arg_indent, wt: arg_wt };
160
161 $.ajax({
162 type: 'GET',
163 url: action,
164 data: args,
165 dataType: 'json',
166 success: show_results,
167 error: ajax_error
168 });
169
170 });
171
172});
Note: See TracBrowser for help on using the repository browser.