Skip to content

Commit 46a4729

Browse files
committed
Add date range, declared type, description for news
1 parent 17881d1 commit 46a4729

File tree

4 files changed

+571
-5
lines changed

4 files changed

+571
-5
lines changed

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Search user interface (UI) with Headless
33
lang: en
4-
dateModified: 2025-02-24
4+
dateModified: 2025-08-04
55
---
66

77
<p class="lead">This is a demo site for the GC Search UI.</p>
@@ -29,6 +29,8 @@ <h3>Advanced tests</h3>
2929
<li><a href="test/budget.html">Budget sample</a></li>
3030
<li><a href="test/gazette.html">Canada Gazette sample</a></li>
3131
<li><a href="test/template.html">Custom summary and results templates</a></li>
32+
<li><a href="test/newsadv-en.html">News Advanced Search user interface</a></li>
33+
<li><a href="test/newsadv-fr.html">Interface utilisateur de la recherche avancée d'actualités</a></li>
3234
</ul>
3335

3436
<hr>

src/connector.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
buildResultList,
55
buildQuerySummary,
66
buildPager,
7+
buildResultsPerPage,
78
buildSearchStatus,
89
buildUrlManager,
910
buildDidYouMean,
@@ -476,7 +477,7 @@ function initEngine() {
476477

477478
resultListController = buildResultList( headlessEngine, {
478479
options: {
479-
fieldsToInclude: [ "author", "date", "language", "urihash", "objecttype", "collection", "source", "permanentid", "displaynavlabel", "hostname" ]
480+
fieldsToInclude: [ "author", "date", "language", "urihash", "objecttype", "collection", "source", "permanentid", "displaynavlabel", "hostname", "disp_declared_type", "description" ]
480481
}
481482
} );
482483
querySummaryController = buildQuerySummary( headlessEngine );
@@ -485,7 +486,7 @@ function initEngine() {
485486
statusController = buildSearchStatus( headlessEngine );
486487

487488
if ( urlParams.allq || urlParams.exctq || urlParams.anyq || urlParams.noneq || urlParams.fqupdate ||
488-
urlParams.dmn || urlParams.fqocct || urlParams.elctn_cat || urlParams.filetype || urlParams.site || urlParams.year ) {
489+
urlParams.dmn || urlParams.fqocct || urlParams.elctn_cat || urlParams.filetype || urlParams.site || urlParams.year || urlParams.declaredtype || urlParams.startdate || urlParams.enddate || urlParams.dprtmnt ) {
489490
let q = [];
490491
let qString = "";
491492
if ( urlParams.allq ) {
@@ -622,6 +623,26 @@ function initEngine() {
622623
let site = urlParams.site.toLowerCase().replace( '*', '' );
623624
aqString += ' @canadagazettesite==' + site;
624625
}
626+
627+
if ( urlParams.startdate ) {
628+
const startDate = getcoveoGMTDate( urlParams.startdate );
629+
aqString += ' @date >= "' + startDate + '"';
630+
}
631+
632+
if ( urlParams.enddate ) {
633+
const endDate = getcoveoGMTDate( urlParams.enddate );
634+
aqString += ' @date <= "' + endDate + '"';
635+
}
636+
637+
if ( urlParams.dprtmnt ) {
638+
aqString += ' @author = "' + urlParams.dprtmnt + '"';
639+
640+
}
641+
642+
if ( urlParams.declaredtype ) {
643+
aqString += ' @declared_type="' + urlParams.declaredtype.replaceAll( /'/g, '&#39;' ) + '"';
644+
645+
}
625646

626647
if ( aqString ) {
627648
const action = loadAdvancedSearchQueryActions( headlessEngine ).updateAdvancedSearchQueries( {
@@ -1005,6 +1026,8 @@ function updateResultListState( newState ) {
10051026
}
10061027

10071028
let breadcrumb = "";
1029+
let disp_declared_type = "";
1030+
let description = "";
10081031
let printableUri = encodeURI( result.printableUri );
10091032
printableUri = printableUri.replaceAll( '&' , '&amp;' );
10101033
let clickUri = encodeURI( result.clickUri );
@@ -1017,6 +1040,16 @@ function updateResultListState( newState ) {
10171040
else {
10181041
breadcrumb = '<p class="location"><cite><a href="' + clickUri + '">' + printableUri + '</a></cite></p>';
10191042
}
1043+
1044+
if ( result.raw.disp_declared_type ) {
1045+
disp_declared_type = stripHtml( result.raw.disp_declared_type );
1046+
1047+
}
1048+
1049+
if ( result.raw.description ) {
1050+
description = stripHtml( result.raw.description );
1051+
1052+
}
10201053

10211054
sectionNode.innerHTML = resultTemplateHTML
10221055
.replace( '%[index]', index + 1 )
@@ -1026,8 +1059,10 @@ function updateResultListState( newState ) {
10261059
.replace( '%[result.raw.author]', author )
10271060
.replace( '%[result.breadcrumb]', breadcrumb )
10281061
.replace( '%[result.printableUri]', printableUri )
1029-
.replace( '%[short-date-en]', getShortDateFormat( resultDate ) )
1030-
.replace( '%[short-date-fr]', getShortDateFormat( resultDate ) )
1062+
.replace( '%[result.raw.disp_declared_type]', disp_declared_type )
1063+
.replace( '%[result.raw.description]', description )
1064+
.replaceAll( '%[short-date-en]', getShortDateFormat( resultDate ) )
1065+
.replaceAll( '%[short-date-fr]', getShortDateFormat( resultDate ) )
10311066
.replace( '%[long-date-en]', getLongDateFormat( resultDate, 'en' ) )
10321067
.replace( '%[long-date-fr]', getLongDateFormat( resultDate, 'fr' ) )
10331068
.replace( '%[highlightedExcerpt]', highlightedExcerpt );
@@ -1151,6 +1186,10 @@ function updatePagerState( newState ) {
11511186

11521187
buttonNode.onclick = () => {
11531188
pagerController.previousPage();
1189+
1190+
if ( params.isAdvancedSearch ) {
1191+
updateUrlParameter( pagerState.currentPage );
1192+
}
11541193
};
11551194

11561195
pagerComponentElement.appendChild( liNode );
@@ -1178,6 +1217,10 @@ function updatePagerState( newState ) {
11781217

11791218
buttonNode.onclick = () => {
11801219
pagerController.selectPage( pageNo );
1220+
1221+
if ( params.isAdvancedSearch ) {
1222+
updateUrlParameter( pagerState.currentPage );
1223+
}
11811224
};
11821225

11831226
pagerComponentElement.appendChild( liNode );
@@ -1192,11 +1235,46 @@ function updatePagerState( newState ) {
11921235

11931236
buttonNode.onclick = () => {
11941237
pagerController.nextPage();
1238+
1239+
if ( params.isAdvancedSearch ) {
1240+
updateUrlParameter( pagerState.currentPage );
1241+
}
11951242
};
11961243

11971244
pagerComponentElement.appendChild( liNode );
11981245
}
11991246
}
12001247

1248+
function updateUrlParameter( currentPage ) {
1249+
1250+
const resultsPerPage = buildResultsPerPage(headlessEngine);
1251+
const { numberOfResults } = resultsPerPage.state;
1252+
const urlParams = new URLSearchParams( window.location.search );
1253+
const paramName = 'firstResult';
1254+
const pageNum = ( currentPage - 1 ) * numberOfResults;
1255+
1256+
// Set the value of the parameter. If it doesn't exist, it will be added.
1257+
urlParams.set( paramName, pageNum );
1258+
1259+
const newSearch = urlParams.toString();
1260+
window.history.replaceState( {}, '', `${window.location.pathname}?${newSearch}${window.location.hash}` );
1261+
1262+
}
1263+
1264+
function getcoveoGMTDate( date ) {
1265+
const paramDate = new Date( date );
1266+
const coveoGMTDateTime = new Date( paramDate.getTime() - paramDate.getTimezoneOffset()*60*1000 );
1267+
1268+
const year = coveoGMTDateTime.getFullYear();
1269+
const month = coveoGMTDateTime.getMonth() + 1; // Add 1 for 1-indexed month
1270+
const day = coveoGMTDateTime.getDate();
1271+
1272+
const formattedMonth = month < 10 ? '0' + month : month;
1273+
const formattedDay = day < 10 ? '0' + day : day;
1274+
1275+
return `${year}/${formattedMonth}/${formattedDay}`;
1276+
1277+
}
1278+
12011279
// Run Search UI
12021280
initSearchUI();

0 commit comments

Comments
 (0)