Commit 77a0f02a authored by Romain Loth's avatar Romain Loth

basic search hits templates mecanism

parent 7fa9d71a
......@@ -8,7 +8,7 @@
"reldbtype": "csv",
"reldbfile": "shale_and_ice.csv",
"reldbqcols": ["title"],
"reltemplate": "_todo"
"reltemplate": "bib_details"
}
},
"model_calibration.gexf": {
......@@ -17,7 +17,7 @@
"reldbtype": "csv",
"reldbfile": "model_calibration.csv",
"reldbqcols": ["title"],
"reltemplate": "_todo"
"reltemplate": "bib_details"
}
}
}
......@@ -40,14 +40,14 @@
"reldbtype": "csv",
"reldbfile": "mini_for_csv.csv",
"reldbqcols": ["title","keywords","text"],
"reltemplate": "_todo"
"reltemplate": "bib_details"
},
"node1": {
"name": "person",
"reldbtype": "csv",
"reldbfile": "mini_for_csv.csv",
"reldbqcols": ["author"],
"reltemplate": "_todo"
"reltemplate": "bib_details"
}
},
"test_with_various_atts.gexf": {}
......@@ -73,7 +73,8 @@
"name": "ISItermsWhitelistV2Oct_5 & ISItermsWhitelistV2Oct_5",
"reldbqtable": "ISItermsWhitelistV2Oct_5",
"reldbfile" : "wos_climate-change_title_2014-2015.db",
"reldbtype": "CortextDB"
"reldbtype": "CortextDB",
"reltemplate": "universal"
}
}
}
......
......@@ -159,7 +159,9 @@ TW.conf = (function(TW){
// ------------
TWConf.paths = {
'ourlibs': 'twlibs',
'templates': "twlibs/hit_templates",
'modules': 'twmodules',
'sourceFile': "", // server-side .gexf|.json default source
'sourceMenu': "db.json" // ...or server-side gexf default source list
}
......
......@@ -10,6 +10,10 @@ ini_set('display_startup_errors',1);
include('tools.php');
include('parameters_details.php');
if ($output_mode == "json") {
header('Content-Type: application/json');
}
$dbtype = null;
if (array_key_exists('reldbtype', $my_conf[$ntid])) {
$dbtype = $my_conf[$ntid]['reldbtype'];
......
......@@ -196,6 +196,13 @@ html.waiting {
font-family: "Crete Round", "garamond", serif ;
padding: 5px;
}
.hit-keywords {
color: #000;
font-style: italic;
font-family: "Crete Round", "garamond", serif ;
padding: 5px;
}
/* small messages */
p.micromessage{
font-size: 85%;
......
<li class="searchhit" title='$${score}'>
<a href="$${link}">$${tit}</a> <span class="author">$${au}</span>
</li>
<li class="searchhit" title="$${score}">
<p>
<a href="$${link}"><b>$${tit}</b></a>
by
<span class="author">$${au}</span>
,
<i>$${src}</i>
[$${date}]
</p>
<p>
<span class="hit-keywords">
$${kws}
</span>
<span class="hit-text">
$${txt}
</span>
</p>
</li>
......@@ -373,12 +373,12 @@ function getTopPapers(){
topPapersFetcher(
swNodetypes[0],
qWordsbySwType[swNodetypes[0]],
`<h2>${swNodetypes[0]}</h2>`,
function(enrichedHtml) {
[[],[]],
function(priorJsonHits) {
topPapersFetcher(
swNodetypes[1],
qWordsbySwType[swNodetypes[1]],
enrichedHtml+`<p class=centered>---</p><h2>${swNodetypes[1]}</h2>`,
priorJsonHits,
displayTopPapers
)
}
......@@ -396,31 +396,30 @@ function getTopPapers(){
// - cbNext is a partial function to handle the follow-up
// (just pass it resHTML, to continue enriching or display)
//
function topPapersFetcher(swType, qWords, priorHtml, cbNext){
function topPapersFetcher(swType, qWords, accumulHits, cbNext){
if (isUndef(priorHtml)) priorHtml = ''
if (isUndef(cbNext)) cbNext = displayTopPapers
// list of json object with hit metadata (title, author, etc.) by nodetype
if (isUndef(accumulHits)) accumulHits = [[],[]]
// callback to continue accumulating json hits or display to html
if (isUndef(cbNext)) cbNext = displayTopPapers
// introducing the modern node type thanks to updated db.json specs
let nodetype = (swType == 'semantic') ? 0 : 1
let stockErrMsg = `<p class="micromessage">
Your settings for relatedDocsType are set on ${TW.conf.relatedDocsType}
API but it couldn't be connected to.</p>`
if (TW.conf.relatedDocsType == "api") {
stockErrMsg += `<p class="micromessage">Check if it is running and
accessible:<br><span class=code>${TW.conf.relatedDocsAPI}</span></p>`
}
let resHTML = ''
let apiurl = TW.conf.relatedDocsAPIS[TW.conf.relatedDocsType]
if (! apiurl) {
apiurl = TW.conf.relatedDocsAPI
}
let stockErrMsg = `
<p class="micromessage">The API ${TW.conf.relatedDocsType} couldn't be connected to.</p>
<p class="micromessage">The queried route found in TW.conf was: <span class=code>${apiurl}</span>
<br>Check if it is running and accessible.</p>`
let resHTML = ''
if (TW.conf.relatedDocsType == "twitter") {
let joinedQ = qWords.map(function(w){return'('+w+')'}).join(' AND ')
$.ajax({
......@@ -430,34 +429,37 @@ function topPapersFetcher(swType, qWords, priorHtml, cbNext){
contentType: "application/json",
success : function(data){
if (data.length) {
for (var k in data) {
let tweetJson = data[k]
resHTML += renderTweet(tweetJson)
}
accumulHits[nodetype] = accumulHits[nodetype].concat(data)
}
else {
resHTML += `<p class="micromessage centered">The query
<span class=code>${joinedQ}</span> delivers
no results on Twitter.</p>`
accumulHits[nodetype].push({
"error": `<p class="micromessage centered">The query
<span class=code>${joinedQ}</span> delivers
no results on Twitter.</p>`
})
}
cbNext(priorHtml + resHTML)
cbNext(accumulHits)
},
error: function(){
console.log(`Not found: relatedDocs for ${apiurl}`)
cbNext(priorHtml + stockErrMsg)
accumulHits[nodetype].push({
"error": stockErrMsg
})
cbNext(accumulHits)
}
});
}
else if (TW.conf.relatedDocsType == "LocalDB") {
let thisRelDocsConf = TW.gmenuInfos[TW.File][nodetype]
if (!thisRelDocsConf) {
resHTML =
`<p>Your settings for relatedDocsType are set on a local database,
accumulHits[nodetype].push({
"error": `<p>Your settings for relatedDocsType are set on a local database,
but your servermenu file does not provide any information about
the CSV or DB table to query for related documents
(on nodetype ${nodetype}: ${swType})</p>`
cbNext(priorHtml + resHTML)
return
})
cbNext(accumulHits)
return
}
else {
// /!\ documentation and specification needed for the php use cases /!\
......@@ -474,22 +476,95 @@ function topPapersFetcher(swType, qWords, priorHtml, cbNext){
type: 'GET',
url: apiurl + '/info_div.php',
data: urlParams,
contentType: "application/json",
success : function(data){
console.log(`relatedDocs: ${apiurl}/info_div.php?${urlParams}`);
resHTML = data
cbNext(priorHtml + resHTML)
if (data.hits) {
accumulHits[nodetype] = accumulHits[nodetype].concat(data.hits)
}
cbNext(accumulHits)
},
error: function(){
console.log(`Not found: relatedDocs for ${apiurl}`)
cbNext(priorHtml + stockErrMsg)
accumulHits[nodetype].push({
"error": stockErrMsg
})
cbNext(accumulHits)
}
});
}
}
}
function displayTopPapers(enrichedHtml) {
$("#topPapers").html(enrichedHtml);
function makeRendererFromTemplate(tmplName) {
let tmplURL = TW.conf.paths.templates + '/' + tmplName + '.html'
let gotTemplate = AjaxSync({ url: tmplURL });
var tmplStr = ''
if (gotTemplate['OK']) {
tmplStr = gotTemplate.data
}
// we return a customized renderJsonToHtml function
return function(jsonHit) {
let htmlOut = tmplStr
for (key in jsonHit) {
// our tags look like this in the template ====> by $${author}, [$${date}]
let reKey = new RegExp('\\$\\$\\{'+key+'\\}', 'g')
// we replace them by value
htmlOut = htmlOut.replace(reKey, jsonHit[key])
}
// we also replace any not found keys by 'N/A'
let reKeyAll = new RegExp('\\$\\$\\{[^\\}]+\\}', 'g')
htmlOut = htmlOut.replace(reKeyAll, "N/A")
return htmlOut
}
}
function displayTopPapers(jsonHits) {
// console.log('jsonHits', jsonHits)
let resHTML = '<ul class="infoitems">'
let toHtmlFun = function(){}
for (var ndtypeId in TW.categories) {
if (TW.conf.relatedDocsType == 'twitter') {
toHtmlFun = renderTweet
}
else {
let thisRelDocsConf = TW.gmenuInfos[TW.File][ndtypeId]
if (thisRelDocsConf && thisRelDocsConf.reltemplate) {
// console.log("my rendering hits template", thisRelDocsConf.reltemplate)
toHtmlFun = makeRendererFromTemplate(thisRelDocsConf.reltemplate)
}
else {
console.warn(`no rendering template found in ${TW.conf.paths.sourceMenu} for this source ${TW.File}...`)
// try the universal template
toHtmlFun = makeRendererFromTemplate("universal")
}
}
// console.log("my rendering fun", toHtmlFun)
for (var k in jsonHits[ndtypeId]) {
let hitJson = jsonHits[ndtypeId][k]
if (hitJson.error) {
resHTML += hitJson.error
}
else {
resHTML += toHtmlFun(hitJson)
}
}
}
resHTML += '</ul>'
// effect the changes in topPapers
$("#topPapers").html(resHTML);
}
function newPopup(url) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment