MediaWiki:LBSSearch.js: Difference between revisions

mNo edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
addOnloadHook(init)
/*
Last-Update: 2023/01/24
*/
class LBSSearch
{
constructor()
{
this.allChain = Array();


allChain = Array();
var button = document.getElementById("sequence_search_button");
function init()
button.onclick = function(){
{
gVar.lbsSearch.sequenceSearch();
  var button = document.getElementById("mainchain");
}
  button.onclick = function(){
button = document.getElementById("advanced_search_button");
    mainchainSearch()
button.onclick = function(){
  }
var mainchain = document.getElementById("advanced_polysaccharide_sequence").value;
var searchchain = mainchain.replace(/\?/g, "\\?");
var nre = ".*";
if(document.getElementById("advanced_search_nre").checked)
nre = "^";
gVar.lbsSearch.chainSearch(nre + searchchain + ".*", mainchain, "AdvResult");
}


    // get all chain
// get all chain
  var url = '/mediawiki/api.php?action=query&prop=revisions&redirects=1&titles=Persist:ListAllChains&rvprop=content&rvparse=1&format=xml';
var url = '/mediawiki/api.php?action=query&prop=revisions&redirects=1&titles=Persist:ListAllChains&rvprop=content&rvparse=1&format=xml';
fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'plain/text; charset=utf-8' },


  var xmlHttpRequest = new XMLHttpRequest();
}).then((res) => { // 結果取得
  xmlHttpRequest.onreadystatechange = function()
if(!res.ok)
  {
throw new Error(`${res.status} ${res.statusText}`);
    var READYSTATE_COMPLETED = 4;
return res.text();
    if(this.readyState == READYSTATE_COMPLETED){
      var HTTP_STATUS_OK = 200;
      if(this.status == HTTP_STATUS_OK) {
        // success
        var xml    = this.response.trim();
        var parser  = new DOMParser();
        var dom    = parser.parseFromString(xml, 'text/xml');
        var rev    = dom.getElementsByTagName("rev")[0];
        var choices;
        if(rev.innerHTML != undefined)
          choices = rev.innerHTML.replace("<ul><li>","").replace("</li></ul>","").split("</li><li>");
        else // for Edge
          choices = rev.textContent.replace("<ui><ll>","").replace("</li></ul>","").split("</li><li>");


        for(var i = 0; i < choices.length; i ++){
}).then((text) => {
          var w = choices[i].trim().split("==");
// success
          allChain.push({"id":w[0], "chain":w[1], "img":w[2]});
var xml    = text.trim();
        }
var parser  = new DOMParser();
        // ボタン有効化
var dom    = parser.parseFromString(xml, 'text/xml');
        var button = document.getElementById("mainchain");
var rev    = dom.getElementsByTagName("rev")[0];
        button.disabled = false;
var html;
        var loading = document.getElementById("loading");
if(rev.innerHTML != undefined)
        loading.style.display = "none";
    html = rev.innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
      } else {
else // for Edge
        // error
    html = rev.textContent;
        console.log('retrieving is failed. ' + this.status + ':' + this.statusText + "/" + this.readyState);
html = html.replace(/<div.*><ul>/, '');
      }
var choices = html.split("</li>");
     }
var uniqChoices = Array();
  }
for(var i = 0; i < choices.length-1; i ++){
    var w = choices[i].trim().substring(4).split("==");
gVar.lbsSearch.allChain.push({"id":w[0], "chain":w[1], "img":w[2]});
if(uniqChoices.indexOf(w[1]) == -1)
uniqChoices.push(w[1]);
}
$('#advanced_polysaccharide_sequence').autocomplete({
source:   uniqChoices,
autoFocus: false,
delay:     0,
minLength: 4
});


  // search
// ボタン有効化
  xmlHttpRequest.open('GET', url, true);
var button = document.getElementById("sequence_search_button");
  xmlHttpRequest.responseType = 'text';
button.disabled = false;
  xmlHttpRequest.send(null);
button = document.getElementById("advanced_search_button");
}
button.disabled = false;
var loading = document.getElementById("loading");
loading.style.display = "none";
});
}


function mainchainSearch()
sequenceSearch()
{
{
  var ops = [];
var ops = [];
  ops[0] = document.getElementById("nre");
ops[0] = document.getElementById("nre");
  ops[1] = document.getElementById("4th");
ops[1] = document.getElementById("4th");
  ops[2] = document.getElementById("3th");
ops[2] = document.getElementById("3th");
  ops[3] = document.getElementById("2nd");
ops[3] = document.getElementById("2nd");
  ops[4] = document.getElementById("1st");
ops[4] = document.getElementById("1st");
  ops[5] = document.getElementById("re");
ops[5] = document.getElementById("re");


  var st;
var st;
  var mc;
var mc;
  var searchtext = "";
var searchtext = "";
  var mainchain  = "";
var mainchain  = "";
  var counter = 0;
var counter = 0;
  for(var i = 0; i < ops.length; i ++){
for(var i = 0; i < ops.length; i ++){
    st = ops[i].options[ops[i].selectedIndex].innerHTML;
st = ops[i].options[ops[i].selectedIndex].innerHTML;
    mc = ops[i].options[ops[i].selectedIndex].value;
mc = ops[i].options[ops[i].selectedIndex].value;
    searchtext += st;
searchtext += st;
    mainchain  += mc;
mainchain  += mc;
    if(i == 0 || i == ops.length-1)
if(i == 0 || i == ops.length-1)
      continue;
continue;
    if(st === "Others")
if(st === "Others")
      counter ++;
counter ++;
  }
}
  if(counter >= 3){
if(counter >= 3){
    alert("3つ以上のあいまい条件指定はできません。");
alert("3つ以上のあいまい条件指定はできません。");
    return;
return;
  }
}
this.chainSearch(mainchain, searchtext, "PolySeqResult");
}


  // search
chainSearch(mainchain, searchtext, resultDiv)
  var result = Array();
{
  var regexp = new RegExp(mainchain);
// search
  for(var i = 0; i < allChain.length; i ++){
var result = Array();
    if(regexp.test(allChain[i]["chain"])){
var regexp = new RegExp(mainchain, "i");
      result[allChain[i]["id"]] = allChain[i]["img"].replace(/&lt;/g,"<").replace(/&gt;/g,">") + allChain[i]["chain"];
for(var i = 0; i < this.allChain.length; i ++){
    }
if(regexp.test(this.allChain[i]["chain"])){
  }
result[this.allChain[i]["id"]] = this.allChain[i]["img"].replace(/&lt;/g,"<").replace(/&gt;/g,">") + this.allChain[i]["chain"];
}
}


  // display
// display
  var html = "Search Text: " + searchtext + "<br />";
var html = "Search Text: " + searchtext + "<br />";
//  html += "Search Expression: " + mainchain + "<br />";
html += "Search Expression: " + mainchain + "<br />";
  var keys = Object.keys(result);
var keys = Object.keys(result);
  html += keys.length + " Hit(s).<hr />";
html += keys.length + " Hit(s) / " + this.allChain.length + ".<hr />";
  html += "<ol>";
html += "<ol>";
  for(var i = 0; i < keys.length; i ++){
for(var i = 0; i < keys.length; i ++){
    html += "<li><a href=\"/wiki/" + keys[i] + "\" target=\"_blank\"><big><tt>" + keys[i] + "</tt></big></a></li>";
html += "<li><a href=\"/wiki/" + keys[i] + "\" target=\"_blank\"><big><tt>" + keys[i] + "</tt></big></a>" + result[keys[i]] + "</li>";
  }
}
  html += "</ol>";
html += "</ol>";


  var div = document.getElementById("PolySeqResult");
var div = document.getElementById(resultDiv);
  div.innerHTML    = html;
div.innerHTML    = html;
  div.style.display = "block";
div.style.display = "block";
}
}
}

Latest revision as of 04:19, 25 January 2023

/*
Last-Update: 2023/01/24
*/
class LBSSearch
{
	constructor()
	{
		this.allChain = Array();

		var button = document.getElementById("sequence_search_button");
		button.onclick = function(){
			gVar.lbsSearch.sequenceSearch();
		}
		button = document.getElementById("advanced_search_button");
		button.onclick = function(){
			var mainchain = document.getElementById("advanced_polysaccharide_sequence").value;
			var searchchain = mainchain.replace(/\?/g, "\\?");
			var nre = ".*";
			if(document.getElementById("advanced_search_nre").checked)
				nre = "^";
			gVar.lbsSearch.chainSearch(nre + searchchain + ".*", mainchain, "AdvResult");
		}

			// get all chain
		var url = '/mediawiki/api.php?action=query&prop=revisions&redirects=1&titles=Persist:ListAllChains&rvprop=content&rvparse=1&format=xml';
		fetch(url, {
			method: 'GET',
			headers: { 'Content-Type': 'plain/text; charset=utf-8' },

		}).then((res) => { // 結果取得
			if(!res.ok)
				throw new Error(`${res.status} ${res.statusText}`);
			return res.text();

		}).then((text) => {
			// success
			var xml     = text.trim();
			var parser  = new DOMParser();
			var dom     = parser.parseFromString(xml, 'text/xml');
			var rev     = dom.getElementsByTagName("rev")[0];
			var html;
			if(rev.innerHTML != undefined)
			    html = rev.innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
			else // for Edge
			    html = rev.textContent;
			html = html.replace(/<div.*><ul>/, '');
			var choices = html.split("</li>");
			var uniqChoices = Array();
			for(var i = 0; i < choices.length-1; i ++){
			    var w = choices[i].trim().substring(4).split("==");
				gVar.lbsSearch.allChain.push({"id":w[0], "chain":w[1], "img":w[2]});
				if(uniqChoices.indexOf(w[1]) == -1)
					uniqChoices.push(w[1]);
			}
			$('#advanced_polysaccharide_sequence').autocomplete({
				source:    uniqChoices,
				autoFocus: false,
				delay:     0,
				minLength: 4
			});

			// ボタン有効化
			var button = document.getElementById("sequence_search_button");
			button.disabled = false;
			button = document.getElementById("advanced_search_button");
			button.disabled = false;
			var loading = document.getElementById("loading");
			loading.style.display = "none";
		});
	}

	sequenceSearch()
	{
		var ops = [];
		ops[0] = document.getElementById("nre");
		ops[1] = document.getElementById("4th");
		ops[2] = document.getElementById("3th");
		ops[3] = document.getElementById("2nd");
		ops[4] = document.getElementById("1st");
		ops[5] = document.getElementById("re");

		var st;
		var mc;
		var searchtext = "";
		var mainchain  = "";
		var counter = 0;
		for(var i = 0; i < ops.length; i ++){
			st = ops[i].options[ops[i].selectedIndex].innerHTML;
			mc = ops[i].options[ops[i].selectedIndex].value;
			searchtext += st;
			mainchain  += mc;
			if(i == 0 || i == ops.length-1)
				continue;
			if(st === "Others")
				counter ++;
		}
		if(counter >= 3){
			alert("3つ以上のあいまい条件指定はできません。");
			return;
		}
		this.chainSearch(mainchain, searchtext, "PolySeqResult");
	}

	chainSearch(mainchain, searchtext, resultDiv)
	{
		// search
		var result = Array();
		var regexp = new RegExp(mainchain, "i");
		for(var i = 0; i < this.allChain.length; i ++){
			if(regexp.test(this.allChain[i]["chain"])){
				result[this.allChain[i]["id"]] = this.allChain[i]["img"].replace(/&lt;/g,"<").replace(/&gt;/g,">") + this.allChain[i]["chain"];
			}
		}

		// display
		var html = "Search Text: " + searchtext + "<br />";
		html += "Search Expression: " + mainchain + "<br />";
		var keys = Object.keys(result);
		html += keys.length + " Hit(s) / " + this.allChain.length + ".<hr />";
		html += "<ol>";
		for(var i = 0; i < keys.length; i ++){
			html += "<li><a href=\"/wiki/" + keys[i] + "\" target=\"_blank\"><big><tt>" + keys[i] + "</tt></big></a>" + result[keys[i]] + "</li>";
		}
		html += "</ol>";

		var div = document.getElementById(resultDiv);
		div.innerHTML     = html;
		div.style.display = "block";
	}
}