2020-01-31 13:14:08 +00:00
class RessourceList extends List {
2020-02-12 11:19:54 +00:00
constructor ( idOrElement , subscriberList , type , options = { } ) {
if ( ! [ 'corpus' , 'job' ] . includes ( type ) ) {
console . error ( "Unknown Type!" ) ;
return ;
}
super ( idOrElement , { ... RessourceList . options [ 'common' ] ,
... RessourceList . options [ type ] ,
... options } ) ;
this . type = type ;
2020-01-29 09:50:31 +00:00
subscriberList . push ( this ) ;
}
2020-01-31 13:14:08 +00:00
_init ( ressources ) {
2020-02-07 15:00:48 +00:00
this . addRessources ( Object . values ( ressources ) ) ;
2020-02-07 14:21:59 +00:00
this . sort ( "creation_date" , { order : "desc" } ) ;
2020-01-29 09:50:31 +00:00
}
_update ( patch ) {
2020-01-31 13:14:08 +00:00
let item , pathArray ;
2020-01-29 09:50:31 +00:00
2020-01-31 13:14:08 +00:00
for ( let operation of patch ) {
/* "/ressourceId/valueName" -> ["ressourceId", "valueName"] */
2020-01-29 09:50:31 +00:00
pathArray = operation . path . split ( "/" ) . slice ( 1 ) ;
switch ( operation . op ) {
case "add" :
2020-02-03 15:04:47 +00:00
if ( pathArray . includes ( "results" ) ) { break ; }
2020-02-07 15:00:48 +00:00
this . addRessources ( [ operation . value ] ) ;
2020-01-29 09:50:31 +00:00
break ;
case "remove" :
this . remove ( "id" , pathArray [ 0 ] ) ;
break ;
case "replace" :
item = this . get ( "id" , pathArray [ 0 ] ) [ 0 ] ;
switch ( pathArray [ 1 ] ) {
case "status" :
item . values ( { status : operation . value } ) ;
break ;
default :
break ;
}
default :
break ;
}
}
}
2020-02-07 15:00:48 +00:00
addRessources ( ressources ) {
2020-02-12 11:19:54 +00:00
this . add ( ressources . map ( x => RessourceList . dataMapper [ this . type ] ( x ) ) ) ;
2020-01-29 09:50:31 +00:00
}
}
2020-01-31 13:14:08 +00:00
RessourceList . dataMapper = {
2020-02-07 14:21:59 +00:00
corpus : corpus => ( { creation _date : corpus . creation _date ,
description : corpus . description ,
id : corpus . id ,
2020-02-12 11:19:54 +00:00
"analyse-link" : ` /corpora/ ${ corpus . id } /analyse ` ,
"edit-link" : ` /corpora/ ${ corpus . id } ` ,
2020-02-07 14:21:59 +00:00
status : corpus . status ,
title : corpus . title } ) ,
job : job => ( { creation _date : job . creation _date ,
description : job . description ,
id : job . id ,
link : ` /jobs/ ${ job . id } ` ,
service : job . service ,
status : job . status ,
title : job . title } )
2020-01-31 13:14:08 +00:00
} ;
RessourceList . options = {
2020-02-12 11:19:54 +00:00
common : { page : 4 , pagination : { innerWindow : 8 , outerWindow : 1 } } ,
corpus : { item : ` <tr>
< td >
< a class = "btn-floating disabled" >
< i class = "material-icons service" > book < / i >
< / a >
< / t d >
< td >
< b class = "title" > < / b > < b r >
< i class = "description" > < / i >
< / t d >
< td >
< span class = "badge new status" data - badge - caption = "" > < / s p a n >
< / t d >
< td class = "right-align" >
< a class = "btn-small edit-link waves-effect waves-light" > < i class = "material-icons" > edit < / i > < / a >
< a class = "btn-small analyse-link waves-effect waves-light" > Analyse < i class = "material-icons right" > search < / i > < / a >
< / t d >
< / t r > ` ,
valueNames : [ "creation_date" , "description" , "title" ,
{ data : [ "id" ] } ,
{ name : "analyse-link" , attr : "href" } ,
{ name : "edit-link" , attr : "href" } ,
{ name : "status" , attr : "data-status" } ] } ,
job : { item : ` <tr>
< td >
< a class = "btn-floating disabled" >
< i class = "material-icons service" > < / i >
< / a >
< / t d >
< td >
< b class = "title" > < / b > < b r >
< i class = "description" > < / i >
< / t d >
< td >
< span class = "badge new status" data - badge - caption = "" > < / s p a n >
< / t d >
< td class = "right-align" >
< a class = "btn-small link waves-effect waves-light" > View < i class = "material-icons right" > send < / i > < / a >
< / t d >
< / t r > ` ,
valueNames : [ "creation_date" , "description" , "title" ,
{ data : [ "id" ] } ,
{ name : "link" , attr : "href" } ,
{ name : "service" , attr : "data-service" } ,
{ name : "status" , attr : "data-status" } ] }
} ;
2020-01-29 15:12:57 +00:00
2020-04-02 12:22:03 +00:00
class ResultsList extends List {
2020-01-29 15:12:57 +00:00
2020-01-30 13:23:19 +00:00
createResultRowElement ( item , chunk ) {
2020-04-02 09:37:45 +00:00
let values , cpos , token , matchRowElement , lcCellElement , hitCellElement , rcCellElement , textTitlesCellElement , matchNrElement , lc , c , rc ;
2020-01-29 15:12:57 +00:00
// gather values from item
values = item . values ( ) ;
2020-04-02 09:37:45 +00:00
if ( chunk . cpos _ranges == true ) {
// python range like function from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Sequence_generator_(range)
const range = ( start , stop , step ) => Array . from ( { length : ( stop - start ) / step + 1 } , ( _ , i ) => start + ( i * step ) ) ;
lc = range ( values . lc [ 0 ] , values . lc [ 1 ] , 1 )
c = range ( values . c [ 0 ] , values . c [ 1 ] , 1 )
rc = range ( values . rc [ 0 ] , values . rc [ 1 ] , 1 )
} else {
lc = values . lc ;
c = values . c ;
rc = values . rc ;
}
2020-01-29 15:12:57 +00:00
// get infos for full match row
matchRowElement = document . createElement ( "tr" ) ;
2020-02-03 11:58:40 +00:00
matchRowElement . setAttribute ( "data-index" , values [ "index" ] )
lcCellElement = document . createElement ( "td" ) ;
lcCellElement . classList . add ( "left-context" ) ;
matchRowElement . appendChild ( lcCellElement ) ;
2020-04-02 09:37:45 +00:00
for ( cpos of lc ) {
2020-01-29 15:12:57 +00:00
token = chunk [ "cpos_lookup" ] [ cpos ] ;
2020-02-03 11:58:40 +00:00
lcCellElement . insertAdjacentHTML ( "beforeend" , ` <span class="token" data-cpos=" ${ cpos } "> ${ token [ "word" ] } </span> ` ) ;
2020-01-29 15:12:57 +00:00
}
2020-02-03 11:58:40 +00:00
// get infos for hit of match
let textTitles = new Set ( ) ;
hitCellElement = document . createElement ( "td" ) ;
hitCellElement . classList . add ( "match-hit" ) ;
textTitlesCellElement = document . createElement ( "td" ) ;
textTitlesCellElement . classList . add ( "titles" ) ;
2020-02-03 12:59:37 +00:00
matchNrElement = document . createElement ( "td" ) ;
matchNrElement . classList . add ( "match-nr" ) ;
2020-02-03 11:58:40 +00:00
matchRowElement . appendChild ( hitCellElement ) ;
2020-04-02 09:37:45 +00:00
for ( cpos of c ) {
2020-02-03 11:58:40 +00:00
token = chunk [ "cpos_lookup" ] [ cpos ] ;
hitCellElement . insertAdjacentHTML ( "beforeend" , ` <span class="token" data-cpos=" ${ cpos } "> ${ token [ "word" ] } </span> ` ) ;
// get text titles of every hit cpos token
2020-04-01 11:44:06 +00:00
textTitles . add ( chunk [ "text_lookup" ] [ token [ "text" ] ] [ "text_title" ] ) ;
2020-02-03 11:58:40 +00:00
// add button to trigger more context to every match td
var inspectBtn = document . createElement ( "a" ) ;
2020-03-18 14:52:53 +00:00
inspectBtn . setAttribute ( "class" , "btn-floating btn-flat waves-effect waves-light grey right inspect disabled" ) ;
2020-02-03 11:58:40 +00:00
inspectBtn . innerHTML = '<i class="material-icons">search</i>' ;
2020-02-05 15:09:59 +00:00
inspectBtn . onclick = function ( ) { inspect ( values [ "index" ] ) } ;
2020-02-03 11:58:40 +00:00
}
// add text titles at front as first td of one row
hitCellElement . appendChild ( inspectBtn ) ;
textTitlesCellElement . innerText = [ ... textTitles ] . join ( ", " ) ;
matchRowElement . insertAdjacentHTML ( "afterbegin" , textTitlesCellElement . outerHTML ) ;
2020-02-03 12:59:37 +00:00
matchNrElement . innerText = values [ "index" ] + 1 ;
matchRowElement . insertAdjacentHTML ( "afterbegin" , matchNrElement . outerHTML ) ;
2020-02-03 11:58:40 +00:00
// get infos for right context of match
rcCellElement = document . createElement ( "td" ) ;
rcCellElement . classList . add ( "right-context" ) ;
matchRowElement . appendChild ( rcCellElement ) ;
2020-04-02 09:37:45 +00:00
for ( cpos of rc ) {
2020-02-03 11:58:40 +00:00
token = chunk [ "cpos_lookup" ] [ cpos ] ;
rcCellElement . insertAdjacentHTML ( "beforeend" , ` <span class="token" data-cpos=" ${ cpos } "> ${ token [ "word" ] } </span> ` ) ;
}
2020-01-30 13:23:19 +00:00
return matchRowElement
2020-01-29 15:12:57 +00:00
}
}