var domain = 'api.jglobal.jst.go.jp'; var jg_api_key='JDEkcmFzbXVzbGUkQ0paMnJIUE10VFl0cGVVaHBiOTc0Lw=='; //--- 呼び出し元URLから、http, httpsを判断する var uri = ""; var base_url = location.href; if(!base_url.match(/^https:/)) uri = "http://" else uri = "https://"; uri += domain + "/"; var RESULT_SIZE = "20"; //これはユーザーがsetResultSize()で設定する。 var CURRENT_PAGE; var CURRENT_URL = document.URL; var REFVAL = "&referer=" + CURRENT_URL; jglobal = function(){ this.hoge=function(){alert("hoge")}; this.version="0.5"; }; (function(){ jglobal.util={}; jglobal.util.DISPLAY_TYPE={}; jglobal.util.DISPLAY_TYPE.NORMAL=0; jglobal.util.DISPLAY_TYPE.LIST=1; })(); jglobal.callJSONP = function(url) { var target = document.createElement('script'); target.charset = 'utf-8'; target.src = url; document.body.appendChild(target); } jglobal.search = function(){ //////////////////////////////////////////////////////////// // Initialize //////////////////////////////////////////////////////////// this.category = "0"; this.query = ""; this.htmlType = "normal"; this.page = "1"; this.resultSize = "20"; //////////////////////////////////////////////////////////// // Setting function //////////////////////////////////////////////////////////// this.setResultSize=function(resultSize){ var value_int; var value_str; if(typeof(resultSize) == "string") { value_str = resultSize; value_int = parseInt(resultSize); } else if(typeof(resultSize) == "number") { value_int = resultSize; value_str = resultSize.toString(10); } else { return; } if(isNaN(value_int) || value_int <= 1) { this.resultSize = "1"; RESULT_SIZE = "1"; } else if(value_int >= 100) { this.resultSize = "100"; RESULT_SIZE = "100"; } else { this.resultSize = value_str; RESULT_SIZE = value_str; } } this.setCategory=function(category){ this.category=category; } this.setPage=function(page){ this.page=page; } this.setQuery=function(query){ this.query=encodeURIComponent(query); } this.setHTMLType=function(htmlType){ if(htmlType==jglobal.util.DISPLAY_TYPE.NORMAL){ this.htmlType="normal"; }else{ this.htmlType="list"; } } this.getHTML = function(callback){ var msec = (new Date()).getTime(); jglobal.callJSONP(uri + "/webapi/search.php?cb="+callback+"&q="+this.query+"&t="+this.category+"&page="+this.page+"&format=html"+"&salt="+msec+"&type="+this.htmlType+"&key="+jg_api_key+"&l="+this.resultSize+REFVAL); } this.getJSON = function(callback){ var msec = (new Date()).getTime(); jglobal.callJSONP(uri + "/webapi/search.php?cb="+callback+"&q="+this.query+"&t="+this.category+"&page="+this.page+"&format=json"+"&salt="+msec+"&key="+jg_api_key+"&l="+this.resultSize+REFVAL); CURRENT_PAGE = this.page; //jg_create_pager表示用 } //////////////////////////////////////////////////////////// // GAWA //////////////////////////////////////////////////////////// this.getHTMLforArticle = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 2; this.getHTML(callback); } this.getHTMLforTerm = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 6; this.getHTML(callback); } this.getHTMLforChemical = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 7; this.getHTML(callback); } this.getHTMLforMaterials = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 9; this.getHTML(callback); } this.getJSONforArticle = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 2; this.getJSON(callback); } this.getJSONforTerm = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 6; this.getJSON(callback); } this.getJSONforChemical = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 7; this.getJSON(callback); } this.getJSONforMaterials = function(callback, query, page){ this.setQuery(query); this.page = page; this.category = 9; this.getJSON(callback); } } function jglobal_callback(msg){ alert(msg); } jglobal.detail = function(){ //////////////////////////////////////////////////////////// // Initialize //////////////////////////////////////////////////////////// this.jid = ""; this.htmlType = "normal"; //////////////////////////////////////////////////////////// // Setting functions //////////////////////////////////////////////////////////// this.setJID = function(jid){ this.jid=jid; } this.setHTMLType=function(htmlType){ if(htmlType==jglobal.util.DISPLAY_TYPE.NORMAL){ this.htmlType="normal"; }else{ this.htmlType="list"; } } //////////////////////////////////////////////////////////// // Public functions //////////////////////////////////////////////////////////// this.getHTML = function(callback){ var msec = (new Date()).getTime(); if(this.jid instanceof Array){ jids=""; for (var i=0;i= 100) { this.results = "100"; RESULT_SIZE = "100"; } else { this.results = value_str; RESULT_SIZE = value_str; } } this.setScoreLimit = function(a) { this.score_limit = a; } this.setHTMLType = function(a) { if(a==jglobal.util.DISPLAY_TYPE.NORMAL){ this.h_type="normal"; }else{ this.h_type="list"; } } //////////////////////////////////////////////////////////// // Public functions //////////////////////////////////////////////////////////// this.getHTML = function(callback){ this.getRelated(callback, "html"); } this.getJSON = function(callback){ this.getRelated(callback, "json"); CURRENT_PAGE = this.page; //jg_create_pager表示用 } } ////////////////////////////////////////////////////// // Extend Query ////////////////////////////////////////////////////// jglobal.extendquery = function() { //////////////////////////////////////////////////////////// // Initialize //////////////////////////////////////////////////////////// this.query = ""; this.results = "0"; this.getExtendQuery = function(callback, type) { var url = uri + "/webapi/extendquery.php?format=" + type + "&cb=" + callback; url += "&q=" + encodeURIComponent(this.query) +"&l=" + this.results + "&key=" + jg_api_key + REFVAL; jglobal.callJSONP(url); } //////////////////////////////////////////////////////////// // Setting functions //////////////////////////////////////////////////////////// this.setQuery = function(a) { this.query = a; } this.setResultSize = function(a) { this.results = a; } //////////////////////////////////////////////////////////// // Public functions //////////////////////////////////////////////////////////// this.getJSON = function(callback) { this.getExtendQuery(callback, "json"); } this.getHTML = function(callback){ this.getExtendQuery(callback, "html"); } } ////////////////////////////////////////////////////// // Relational search from external sites ////////////////////////////////////////////////////// jglobal.ext_relate = function() { //////////////////////////////////////////////////////////// // Initialize //////////////////////////////////////////////////////////// this.file_url = ""; this.file_enc = "UTF-8"; this.r_type = ""; this.show_query = "0"; this.limit = "5"; //////////////////////////////////////////////////////////// // Setting functions //////////////////////////////////////////////////////////// this.setURL = function(a) { this.file_url = encodeURIComponent(a); } this.setEncode = function(a) { this.file_enc = encodeURIComponent(a); } this.setRelationType = function(a) { this.r_type = a; } this.setPageLimit = function(a) { var value_int; var value_str; if(typeof(a) == "string") { value_str = a; value_int = parseInt(a); } else if(typeof(a) == "number") { value_int = a; value_str = a.toString(10); } else { return; } if(isNaN(value_int) || value_int <= 1) { this.limit = "1"; RESULT_SIZE = "1"; } else if(value_int >= 100) { this.limit = "100"; RESULT_SIZE = "100"; } else { this.limit = value_str; RESULT_SIZE = value_str; } } this.showQuery = function(a) { this.show_query = a; } this.getRelate = function(callback, format) { var url = uri + "/webapi/ext_relate.php?key=" + jg_api_key; url += "&cb=" + callback + "&url=" + this.file_url + "&enc=" + this.file_enc; url += "&format=" + format + "&l=" + this.limit + "&sq=" + this.show_query; url += "&rt=" + this.r_type + REFVAL; jglobal.callJSONP(url); } //////////////////////////////////////////////////////////// // Public functions //////////////////////////////////////////////////////////// this.getHTML = function(callback) { this.getRelate(callback, "html"); } this.getJSON = function(callback) { this.getRelate(callback, "json"); } } ////////////////////////////////////////////////////// // Utility Function ////////////////////////////////////////////////////// function jg_create_pager(hit_count){ var PAGES_PER_PAGER = 10; //pagerに表示する数字の最大個数 var html=""; var result = parseInt(RESULT_SIZE); var last_page = Math.floor(hit_count/result)+1; var prev_page = CURRENT_PAGE-1; var next_page = CURRENT_PAGE+1; //--- current_pager_base: 最も左側に来る数値 (CURRENT_PAGEが中心に来るように) var current_pager_base = CURRENT_PAGE-(PAGES_PER_PAGER/2); if(current_pager_base < 0) { current_pager_base = 0; } if(last_page==1) { document.getElementById("jg_pager").innerHTML=""; return; } if(CURRENT_PAGE >= 2) { html += "[先頭] "; html += "前へ "; } for(i = 1; i <= PAGES_PER_PAGER; i++){ var p = current_pager_base + i; if(p != CURRENT_PAGE) { html+="" + p +" "; } else { html += "" + p + " "; } if(p >= last_page) break; } if(CURRENT_PAGE < last_page) { html += "次へ "; // html += "[末尾] "; } document.getElementById("jg_pager").innerHTML = html; }