import { gMainApp, gEventBus, gUrlParams } from "./MainApp.js" ;
import { findTargets, getPrimaryTarget, isRuleid, getChapterResource, fixupSearchHilites, hasHilite, hideFootnotes } from "./utils.js" ;
// --------------------------------------------------------------------
gMainApp.component( "index-sr", {
props: [ "sr" ],
data() { return {
expandRulerefs: null,
iconUrl: getChapterResource( "icon", this.getChapterId() ),
cssBackground: this.makeCssBackground(),
} ; },
template: `
See also:
{{sa_no == 0 ? "" : ", "}}
{{sa}}
`,
created() {
// figure out whether ruleref's should start expanded or collapsed
if ( this.sr.rulerefs === undefined || this.sr.rulerefs.length == 0 || gUrlParams.get( "no-toggle-rulerefs" ) ) {
// there are no ruleref's - don't show the toggle button
} else {
// count how many ruleref's have a matching search term
let nHiliteRulerefs = 0 ;
this.sr.rulerefs.forEach( (ruleref) => {
if ( hasHilite( ruleref.caption ) )
++ nHiliteRulerefs;
} ) ;
if ( nHiliteRulerefs == this.sr.rulerefs.length ) {
// every ruleref is a match - don't show the toggle button
} else {
// NOTE: We start the ruleref's expanded if one of the important fields has a matching search term.
// The idea is that the index entry is probably one that the user will be interested in (since there is
// a match in one of the important fields), and so we show all of the ruleref's, since the user may well
// want to check them out.
// OTOH, if the only match is in a ruleref, then the match is probably a reference back to an index entry
// of interest, and the other ruleref's are unlikely to be relevant.
this.expandRulerefs = hasHilite(this.sr.title) || hasHilite(this.sr.subtitle) || hasHilite(this.sr.content) ;
}
}
},
computed: {
getToggleRulerefsImageUrl() {
// return the image URL for the "toggle ruleref's" button
return gImagesBaseUrl + (this.expandRulerefs ? "collapse" : "expand") + "-rulerefs.png" ; //eslint-disable-line no-undef
},
},
methods: {
onSeeAlso( see_also ) {
// search for the specified text
if ( see_also.indexOf( " " ) >= 0 )
see_also = '"' + see_also + '"' ;
gEventBus.emit( "search-for", see_also ) ;
},
onClickIcon() {
// open the search result's primary target
let target = getPrimaryTarget( this.sr ) ;
if ( target )
gEventBus.emit( "show-target", target.cdoc_id, target.ruleid ) ;
},
onToggleRulerefs() {
// expand/collapse the ruleref's
if ( this.expandRulerefs !== null )
this.expandRulerefs = ! this.expandRulerefs ;
},
showRuleref( ruleref ) {
// flag whether the ruleref should be shown or hidden
if ( gUrlParams.get( "no-toggle-rulerefs" ) )
return true ;
return this.expandRulerefs || hasHilite( ruleref.caption ) ;
},
fixupHilites( val ) {
// convert search term highlights returned to us by the search engine to HTML
return fixupSearchHilites( val ) ;
},
makeCssBackground() {
// generate the CSS background URL for the search result's title
let url = getChapterResource( "background", this.getChapterId() ) ;
return url ? "url(" + url + ")" : "#ddd" ;
},
getChapterId() {
// figure out which chapter this search result belongs to
// NOTE: This is actually a bit fiddly :-/ An index entry can have multiple main ruleid's associated
// with it, so which one do we choose? Or no ruleid's at all - these are often not associated with
// a chapter (e.g. term definitions), but that isn't necessarily always going to be the case.
// Since the only time we need to do this is in the front-end (so that we can show an icon and
// title background for each index search result), we handle it in the front-end, rather than
// in the backend search engine, or during the extraction process. Strictly speaking, each index entry
// should state which chapter it came from, but this is way overkill for what we need. Instead,
// we look at the ruleid and infer the chapter ID from the first letter (nb: we need to be careful
// handle things like "KGP SSR 2" or "RB CG2".
let target = getPrimaryTarget( this.sr ) ;
if ( ! target )
return null ;
let ruleid = target.ruleid ;
if ( isRuleid( ruleid ) )
return ruleid[0] ; // nb: we assume the 1st letter of the ruleid is the chapter ID
return null ;
},
},
} ) ;
// --------------------------------------------------------------------
gMainApp.component( "asop-entry-sr", {
props: [ "sr" ],
template: `
`,
methods: {
onClickCaption() {
gEventBus.emit( "activate-tab", "#nav", "asop" ) ;
gEventBus.emit( "show-asop-entry-sr", this.sr.section_id, this.sr.content ) ;
},
},
} ) ;
// --------------------------------------------------------------------
gMainApp.component( "ruleid", {
props: [ "csetId", "ruleId" ],
data() { return {
cdocId: null, ruleid: null,
} ; },
// NOTE: This bit of HTML is sensitive to spaces :-/
template: `[{{ruleId}}{{ruleId}}]`,
created() {
// check if the rule is one we know about
let targets = findTargets( this.ruleId, this.csetId ) ;
if ( targets && targets.length > 0 ) {
// NOTE: We assume that targets are unique within a content set. This might not be true if MMP
// ever adds Chapter Z stuff to the main index, but we'll cross that bridge if and when we come to it.
// TBH, that stuff would probably be better off as a separate content set, anyway.
this.cdocId = targets[0].cdoc_id ;
this.ruleid = targets[0].ruleid ;
}
},
methods: {
onClick() {
// show the target
hideFootnotes() ;
gEventBus.emit( "show-target", this.cdocId, this.ruleid ) ;
},
},
} ) ;