Changeset 31684 for other-projects

Show
Ignore:
Timestamp:
17.05.2017 10:22:58 (2 years ago)
Author:
davidb
Message:

Code tidyup

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • other-projects/meddle/trunk/meddle-fixup.js

    r31682 r31684  
    1 // MEDDLE version 0.9 
     1/** 
     2 *    MEDDLE version 0.9 
     3 *    Copyright (C) 2017 New Zealand Digital Library, http://www.nzdl.org 
     4 * 
     5 *    This program is free software: you can redistribute it and/or modify 
     6 *    it under the terms of the GNU General Public License as published by 
     7 *    the Free Software Foundation, either version 3 of the License, or 
     8 *    (at your option) any later version. 
     9 * 
     10 *    This program is distributed in the hope that it will be useful, 
     11 *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
     12 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     13 *    GNU General Public License for more details. 
     14 * 
     15 *    You should have received a copy of the GNU General Public License 
     16 *    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     17 */ 
     18 
     19function trim(str) 
     20{ 
     21    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
     22} 
    223 
    324 
     
    627//   https://en.wikipedia.org/wiki/Typographic_ligature#Ligatures_in_Unicode_.28Latin_alphabets.29 
    728 
    8 function trim(str) 
    9 { 
    10     return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
    11 } 
    1229 
    1330var ligature_map = { 
     
    122139    } 
    123140 
    124     //console.log("*** ligature_str = " + ligature_str); 
    125  
    126141    var detected_ligatures = ligature_re.test(query_box_val); 
    127142    var do_replacement = $('#meddle-lig').is(":checked"); 
     
    167182    console.log( "amcm-dl-fixup fixupEncodeInput(form) called" ); 
    168183 
    169     //if (ligature_re === null) { 
    170      //   var ligature_str = Object.keys(ligature_map).join("|"); 
    171       //  ligature_re = new RegExp(ligature_str); 
    172     //} 
    173  
    174     //console.log("*** ligature_str = " + ligature_str); 
    175  
    176184    var query_box_val = form.elements['query'].value; 
    177185    query_box_val = opt_ligature_replacement(query_box_val); 
    178186 
    179 /* 
    180     var detected_ligatures = ligature_re.test(query_box_val); 
    181     if (detected_ligatures) { 
    182     //var do_replacement = confirm("Detected ligatures in query. Replace with individual characters?"); 
    183     var do_replacement = $('#meddle-lig').is(":checked"); 
    184  
    185     if (do_replacement) { 
    186             var query_chars = query_box_val.split(""); 
    187             var query_chars_fixed = []; 
     187 
     188    query_box_val = opt_accent_fold(query_box_val); 
     189 
     190    // e.g., content.ftsec:(+capisco)  
     191 
     192    var query_box_val = query_box_val.replace(/^\s+/,""); 
     193    var query_box_words = query_box_val.split(/\s+/); 
     194     
     195    var query_box_val_plus = ""; 
     196    var query_box_val_double = ""; 
     197     
     198    for (var i=0; i<query_box_words.length; i++) { 
     199    if (i>0) { 
     200        query_box_val_plus += " "; 
     201    } 
     202    query_box_val_plus += "+" + query_box_words[i]; 
     203     
     204    if (i>0) { 
     205        query_box_val_double += " " + query_box_words[i-1]+query_box_words[i]; 
     206    } 
     207    } 
     208 
     209 
     210    form.elements['query'].value = "content.ftsec:(" + query_box_val_plus + query_box_val_double + ")"; 
     211 
     212 
     213    return encodeInput(form); 
     214} 
     215 
     216function fixupSubmitIt() { 
     217    var search_where = $("#fld0 option:selected").text(); 
     218    var x = 0; 
     219    var search_select = $("#fld"+x+" option:selected"); 
     220    var search_where = search_select.text();     
     221 
     222    while (search_where) { 
     223    if (search_where == "Any field") { 
    188224         
    189             for (var i=0; i<query_chars.length; i++) { 
    190         var c = query_chars[i]; 
    191         if (ligature_map[c]) { 
    192             c = ligature_map[c]; 
    193         } 
    194         query_chars_fixed.push(c); 
    195             } 
    196             query_box_val = query_chars_fixed.join(""); 
    197     } 
    198     } 
    199 */ 
     225        $("#fld"+x).val('content.ftsec:'); 
     226    } 
     227    x = x + 1; 
     228    search_select = $("#fld"+x+" option:selected");     
     229    search_where = search_select.text(); 
     230    } 
     231    buildQuery(); 
     232 
     233    return submitIt(); 
     234} 
     235 
     236function meddleInfoBox() { 
     237 
     238    // Icon image 
     239    // By VistaICO.com (VistaICO Toolbar Icons) [CC BY 3.0 ] 
     240 
     241 
     242    var dlcontextoptions = '<input id="meddle-lig" name="meddle-lig" type="checkbox"><label for="meddle-lig" style="display: inline">&nbsp;Ligature&nbsp;expansion</label>&nbsp;&nbsp;<input id="meddle-acc" name="meddle-acc" type="checkbox"><label for="meddle-acc" style="display: inline">&nbsp;Accent&nbsp;folding</label>'; 
     243 
     244 
     245   //console.log("*** location = " + window.location.pathname); 
     246    var detected_dl = ""; 
     247 
     248    var domain_match = window.location.pathname.match(/nph-proxy-meddle.cgi\/.*?https?\/(.*)$/); 
     249 
     250    if (domain_match.length>1) { 
     251    var real_domain = domain_match[1]; 
     252 
     253    if (real_domain.match(/^dl\.acm\.org\//)) { 
     254        detected_dl = "ACM DL (Meddled with to provide: full-text quicksearch, word-wrap protection)"; 
     255    } 
     256    else if (real_domain.match(/^scholar\.google\.[a-z]+/)) { 
     257        detected_dl = "Google Scholar"; 
     258    } 
     259    else if (real_domain.match(/^www.springer\.com/)) { 
     260        detected_dl = "Springer"; 
     261    } 
     262    else if (real_domain.match(/^ieeexplore\.ieee\.org\//)) { 
     263        detected_dl = "IEEE Xplore"; 
     264    } 
     265    } 
     266 
     267    if (detected_dl !== "") { 
     268    $('#dlcontext').html("<i>Detected:" + detected_dl + "</i>"); 
     269    } 
     270 
     271    $('#dlcontextoptions').html('<font size="-1">'+dlcontextoptions+'</font>'); 
     272 
     273    return detected_dl; 
     274} 
     275 
     276 
     277function acmdlFixupInit() { 
     278 
     279    console.log("amc-dl-fixup initiated"); 
     280 
     281    var query_form = document.getElementsByName("qiksearch")[0]; 
     282    if (query_form) { 
     283    query_form.setAttribute("onsubmit", "fixupEncodeInput(this) ; _proxy_jslib_flush_write_buffers();"); 
     284    } 
     285 
     286    var adv_query_form = document.getElementById("theform"); 
     287    if (adv_query_form) { 
     288    // javascript:submitIt(); 
     289    // javascript:; _proxy_jslib_proxify_html(submitIt())[0] 
     290 
     291    adv_query_form.setAttribute("action","javascript:fixupSubmitIt();"); 
     292    } 
     293 
     294    var query_box = document.getElementsByName("query")[0]; 
     295    if (query_box) { 
     296    var query_box_val = query_box.getAttribute("value"); 
     297    query_box.setAttribute("value", query_box_val.replace(/content\.ftsec:\(\+(.*)\)/g,"$1")); 
     298    } 
     299     
     300    console.log("acm-dl-fixup DOM tweaks done"); 
     301} 
     302 
     303 
     304function fixupGScholarSubmit(evt) { 
     305    console.log( "gscholar-dl-fixup fixupGScholarSubmit(form) called" ); 
     306 
     307    var event = evt || window.event; 
     308    form = event.target.id; 
     309 
     310    event.preventDefault(); // to stop the form from submitting 
     311 
     312    var query_box = document.getElementById("gs_hp_tsi"); 
     313    var query_box_val = query_box.value; 
     314 
     315    query_box_val = opt_ligature_replacement(query_box_val); 
     316 
     317    query_box_val = opt_accent_fold(query_box_val); 
     318 
     319    query_box.value = query_box_val; 
     320 
     321    _proxy_jslib_flush_write_buffers(); 
     322 
     323    var query_form = document.getElementsByName("f")[0]; 
     324    query_form.submit(); 
     325} 
     326 
     327function gscholarFixupInit() { 
     328 
     329    console.log("gscholar-fixup initiated"); 
     330 
     331    var query_form = document.getElementsByName("f")[0]; 
     332    if (query_form) { 
     333 
     334    query_form.onsubmit = fixupGScholarSubmit; 
     335 
     336    } 
     337} 
     338 
     339 
     340function fixupXploreSubmit(evt) { 
     341    console.log( "xplore-dl-fixup fixupXploreSubmit(evt) called" ); 
     342 
     343    var event = evt || window.event; 
     344    event.preventDefault(); // to stop the form from submitting 
     345 
     346    var query_box = document.getElementById("input-basic"); 
     347    var query_box_val = query_box.value; 
     348 
     349    query_box_val = opt_ligature_replacement(query_box_val); 
    200350 
    201351/* 
     
    208358    query_box_val = opt_accent_fold(query_box_val); 
    209359 
    210     // e.g., content.ftsec:(+capisco)  
    211  
    212     var query_box_val = query_box_val.replace(/^\s+/,""); 
    213     var query_box_words = query_box_val.split(/\s+/); 
    214      
    215     var query_box_val_plus = ""; 
    216     var query_box_val_double = ""; 
    217      
    218     for (var i=0; i<query_box_words.length; i++) { 
    219     if (i>0) { 
    220         query_box_val_plus += " "; 
    221     } 
    222     query_box_val_plus += "+" + query_box_words[i]; 
    223      
    224     if (i>0) { 
    225         query_box_val_double += " " + query_box_words[i-1]+query_box_words[i]; 
    226     } 
    227     } 
    228  
    229  
    230     form.elements['query'].value = "content.ftsec:(" + query_box_val_plus + query_box_val_double + ")"; 
    231  
    232  
    233     return encodeInput(form); 
    234 } 
    235  
    236 function fixupSubmitIt() { 
    237     var search_where = $("#fld0 option:selected").text(); 
    238     //console.log("**** search_where = " + search_where); 
    239     var x = 0; 
    240     var search_select = $("#fld"+x+" option:selected"); 
    241     var search_where = search_select.text();     
    242  
    243     while (search_where) { 
    244     if (search_where == "Any field") { 
    245         //var sel_val = document.getElementById("fld" + x).selectedIndex].value; 
    246          
    247         //var thisField = document.getElementById("fld" + x).options[document.getElementById("fld" + x).selectedIndex].value; 
    248          
    249         $("#fld"+x).val('content.ftsec:'); 
    250         //$("#fld0 option[value='content.ftsec:']").prop('selected', true); 
    251         //alert("changed menu"); 
    252     } 
    253     x = x + 1; 
    254     search_select = $("#fld"+x+" option:selected");     
    255     search_where = search_select.text(); 
    256     //console.log("*** search select = " + search_select.text()); 
    257     //alert("ddd"); 
    258     } 
    259     buildQuery(); 
    260  
    261     return submitIt(); 
    262 } 
    263  
    264 function meddleInfoBox() { 
    265  
    266     //$('#_proxy_css_top_insertion').append('<form name="MeddleForm" style="padding-bottom: 20px;"><center><table cellpadding="5" border="1"><tbody><tr><td align="center"><img src="http://bedrock.resnet.cms.waikato.ac.nz/meddle/meddle-icon-large.png" width="64" height="64" /></td><td><b><font size="+1"><a href="https://bedrock.resnet.cms.waikato.ac.nz/meddle/">MEDDLE Proxy: ModifiED Digital Library Proxy</a></font></b><br /><div id="dlcontext"></div><div id="dlcontextoptions"</div></td></tr></tbody></table></center></form>'); 
    267  
    268     // Icon image 
    269     // By VistaICO.com (VistaICO Toolbar Icons) [CC BY 3.0 ] 
    270  
    271  
    272     var dlcontextoptions = '<input id="meddle-lig" name="meddle-lig" type="checkbox"><label for="meddle-lig" style="display: inline">&nbsp;Ligature&nbsp;expansion</label>&nbsp;&nbsp;<input id="meddle-acc" name="meddle-acc" type="checkbox"><label for="meddle-acc" style="display: inline">&nbsp;Accent&nbsp;folding</label>'; 
    273  
    274  
    275    //console.log("*** location = " + window.location.pathname); 
    276     var detected_dl = ""; 
    277  
    278     var domain_match = window.location.pathname.match(/nph-proxy-meddle.cgi\/.*?https?\/(.*)$/); 
    279  
    280     if (domain_match.length>1) { 
    281     var real_domain = domain_match[1]; 
    282     //console.log("*** real domain = " + real_domain); 
    283  
    284     if (real_domain.match(/^dl\.acm\.org\//)) { 
    285         detected_dl = "ACM DL (Meddled with to provide: full-text quicksearch, word-wrap protection)"; 
    286         //dlcontextoptions += "&nbsp;&nbsp;Also "; 
    287     } 
    288     else if (real_domain.match(/^scholar\.google\.[a-z]+/)) { 
    289         detected_dl = "Google Scholar"; 
    290     } 
    291     else if (real_domain.match(/^www.springer\.com/)) { 
    292         detected_dl = "Springer"; 
    293     } 
    294     else if (real_domain.match(/^ieeexplore\.ieee\.org\//)) { 
    295         detected_dl = "IEEE Xplore"; 
    296     } 
    297     } 
    298  
    299     if (detected_dl !== "") { 
    300     $('#dlcontext').html("<i>Detected:" + detected_dl + "</i>"); 
    301     } 
    302  
    303     $('#dlcontextoptions').html('<font size="-1">'+dlcontextoptions+'</font>'); 
    304  
    305     return detected_dl; 
    306 } 
    307  
    308  
    309 function acmdlFixupInit() { 
    310  
    311     console.log("amc-dl-fixup initiated"); 
    312  
    313     var query_form = document.getElementsByName("qiksearch")[0]; 
    314     if (query_form) { 
    315     query_form.setAttribute("onsubmit", "fixupEncodeInput(this) ; _proxy_jslib_flush_write_buffers();"); 
    316     } 
    317  
    318 /* 
    319     //var query_box = document.getElementsByName("query")[0]; 
    320     //var query_box_val = query_box.getAttribute("value"); 
    321     //query_box.setAttribute("value", "Foo"); 
    322 */ 
    323     var adv_query_form = document.getElementById("theform"); 
    324     if (adv_query_form) { 
    325     // javascript:submitIt(); 
    326     // javascript:; _proxy_jslib_proxify_html(submitIt())[0] 
    327  
    328     adv_query_form.setAttribute("action","javascript:fixupSubmitIt();"); 
    329     } 
    330  
    331     var query_box = document.getElementsByName("query")[0]; 
    332     if (query_box) { 
    333     var query_box_val = query_box.getAttribute("value"); 
    334     query_box.setAttribute("value", query_box_val.replace(/content\.ftsec:\(\+(.*)\)/g,"$1")); 
    335     } 
    336      
    337     console.log("acm-dl-fixup DOM tweaks done"); 
    338 } 
    339  
    340  
    341 function fixupGScholarSubmit(evt) { 
    342     console.log( "gscholar-dl-fixup fixupGScholarSubmit(form) called" ); 
    343  
    344     var event = evt || window.event; 
    345     form = event.target.id; 
    346  
    347     event.preventDefault(); // to stop the form from submitting 
    348  
    349 /* 
    350     if (ligature_re === null) { 
    351         var ligature_str = Object.keys(ligature_map).join("|"); 
    352         ligature_re = new RegExp(ligature_str); 
    353     } 
    354 */ 
    355  
    356     //console.log("*** ligature_str = " + ligature_str); 
    357  
    358     //var query_box_val = form.elements['q'].value; 
    359     var query_box = document.getElementById("gs_hp_tsi"); 
    360     var query_box_val = query_box.value; 
    361  
    362     query_box_val = opt_ligature_replacement(query_box_val); 
    363 /* 
    364     var detected_ligatures = ligature_re.test(query_box_val); 
    365     if (detected_ligatures) { 
    366     //var do_replacement = confirm("Detected ligatures in query. Replace with individual characters?"); 
    367     var do_replacement = $('#meddle-lig').is(":checked"); 
    368  
    369     if (do_replacement) { 
    370             var query_chars = query_box_val.split(""); 
    371             var query_chars_fixed = []; 
    372          
    373             for (var i=0; i<query_chars.length; i++) { 
    374         var c = query_chars[i]; 
    375         if (ligature_map[c]) { 
    376             c = ligature_map[c]; 
    377         } 
    378         query_chars_fixed.push(c); 
    379             } 
    380             query_box_val = query_chars_fixed.join(""); 
    381     } 
    382     } 
    383 */ 
    384  
    385 /* 
    386     var do_accent_replacement = $('#meddle-acc').is(":checked"); 
    387     if (do_accent_replacement) { 
    388     query_box_val = accent_fold(query_box_val); 
    389     } 
    390 */ 
    391  
    392     query_box_val = opt_accent_fold(query_box_val); 
    393  
    394     //form.elements['q'].value = query_box_val; 
    395     query_box.value = query_box_val; 
    396  
    397     _proxy_jslib_flush_write_buffers(); 
    398  
    399     var query_form = document.getElementsByName("f")[0]; 
    400     query_form.submit(); 
    401     //form.submit();  
    402 } 
    403  
    404 function gscholarFixupInit() { 
    405  
    406     console.log("gscholar-fixup initiated"); 
    407  
    408     var query_form = document.getElementsByName("f")[0]; 
    409     if (query_form) { 
    410     //query_form.setAttribute("onsubmit", "fixupGScholarSubmit(this) ; _proxy_jslib_flush_write_buffers();"); 
    411     //query_form.setAttribute("onsubmit", fixupGScholarSubmit); 
    412     //query_form.setAttribute("onsubmit", 'function(evt) { fixupGoogleSubmit(evt,this) }'); 
    413  
    414     query_form.onsubmit = fixupGScholarSubmit; 
    415 /* 
    416 function(evt)  { 
    417             var event = evt || window.event; 
    418         fixupGScholarSubmit(event,event.target.id); 
    419  
    420             //console.log(event.target.id); // myform 
    421          
    422     } 
    423 */ 
    424  
    425     } 
    426 } 
    427  
    428  
    429 function fixupXploreSubmit(evt) { 
    430     console.log( "xplore-dl-fixup fixupXploreSubmit(evt) called" ); 
    431  
    432     var event = evt || window.event; 
    433     event.preventDefault(); // to stop the form from submitting 
    434  
    435     var query_box = document.getElementById("input-basic"); 
    436     var query_box_val = query_box.value; 
    437  
    438     query_box_val = opt_ligature_replacement(query_box_val); 
    439  
    440 /* 
    441     var do_accent_replacement = $('#meddle-acc').is(":checked"); 
    442     if (do_accent_replacement) { 
    443     query_box_val = accent_fold(query_box_val); 
    444     } 
    445 */ 
    446  
    447     query_box_val = opt_accent_fold(query_box_val); 
    448  
    449360    query_box.value = query_box_val; 
    450361 
     
    484395 
    485396    query_box_val = opt_accent_fold(query_box_val); 
    486     //alert("*** q = " + query_box_val); 
    487397 
    488398    query_box.value = query_box_val; 
     
    491401 
    492402    _proxy_jslib_flush_write_buffers(); // is this needed? 
    493     //query_form.submit(); 
    494403    document.createElement('form').submit.call(query_form); 
    495  
    496     //var $query_form = $("#global-search"); 
    497     //$query_form.submit(); 
    498404 
    499405}