@rr0/cms
Version:
RR0 Content Management System (CMS)
83 lines (82 loc) • 5.02 kB
JavaScript
import { HtmlTag } from "../util/html/HtmlTag.js";
import { DirectoryStep } from "ssg-api";
import { StringUtil } from "../util/string/StringUtil.js";
import { Gender } from "@rr0/common";
import { People } from "@rr0/data";
export function peopleOccupationFilter(filterOccupations) {
return (p) => p.occupations.some(o => filterOccupations.includes(o));
}
/**
* Scan directories for people information, then populates a template with collected data.
*/
export class PeopleDirectoryStep extends DirectoryStep {
constructor(name, rootDirs, excludedDirs, templateFileName, outputFunc, config, service, renderer, filter = (_people) => true) {
super({ rootDirs, excludedDirs, templateFileName, getOutputPath: config.getOutputPath }, name);
this.outputFunc = outputFunc;
this.service = service;
this.renderer = renderer;
this.filter = filter;
}
async processDirs(context, dirNames) {
const allPeopleList = await this.service.getAll();
let peopleList = allPeopleList.filter(this.filter);
const config = this.config;
const outputPath = config.getOutputPath(context);
const output = context.newOutput(outputPath);
const pseudoPeopleList = peopleList.reduce((prevPeopleList, peopleInfo) => {
var _a;
if (((_a = peopleInfo.pseudonyms) === null || _a === void 0 ? void 0 : _a.length) > 0) {
for (const pseudonym of peopleInfo.pseudonyms) {
const pseudo = new People(peopleInfo.firstNames, peopleInfo.lastName, peopleInfo.pseudonyms, peopleInfo.occupations, peopleInfo.countries, peopleInfo.discredited, peopleInfo.gender, peopleInfo.id, peopleInfo.dirName, peopleInfo.image);
pseudo.lastAndFirstName = pseudonym;
prevPeopleList.push(pseudo);
}
}
return prevPeopleList;
}, []);
peopleList = peopleList.concat(pseudoPeopleList).sort((p1, p2) => p1.lastAndFirstName.localeCompare(p2.lastAndFirstName));
const allCountries = new Set();
const occupations = new Set();
const ul = this.toList(context, peopleList, pseudoPeopleList, allCountries, occupations);
output.contents = context.file.contents.replace(`<!--#echo var="directories" -->`, ul.outerHTML);
{
let countriesHtml = "<div class='all'>";
countriesHtml += `<label><input type="button" id="country-none" value="Aucun" onclick="setAll('country', false)"></label>`;
countriesHtml += `<label><input type="button" id="country-all" value="Tous" onclick="setAll('country', true)"></label>`;
countriesHtml += `</div>`;
for (const country of Array.from(allCountries).sort()) {
const countryStr = context.messages.country[country].title;
countriesHtml += `<span class="option"><label><input type="checkbox" id="country-${country}" onchange="findPeople(event)"> ${countryStr}</label></span>`;
}
output.contents = output.contents.replace(`<!--#echo var="countries" -->`, HtmlTag.toString("div", countriesHtml, { id: "countries" }));
}
{
let occupationsHtml = "<div class='all'>";
occupationsHtml += `<label><input type="button" id="occupation-none" value="Aucun" onclick="setAll('occupation', false)"></label>`;
occupationsHtml += `<label><input type="button" id="occupation-all" value="Tous" onclick="setAll('occupation', true)"></label>`;
occupationsHtml += `</div>`;
for (const occupation of Array.from(occupations).sort()) {
const occupationStr = StringUtil.capitalizeFirstLetter(context.messages.people.occupation[occupation](Gender.male));
occupationsHtml += `<span class="option"><label><input type="checkbox" id="occupation-${occupation}" onchange="findPeople(event)"> ${occupationStr}</label></span>`;
}
output.contents = output.contents.replace(`<!--#echo var="occupations" -->`, HtmlTag.toString("div", occupationsHtml, { id: "occupations" }));
}
await this.outputFunc(context, output);
}
toList(context, peopleList, pseudoPeopleList, allCountries, occupations) {
const file = context.file;
const listItems = peopleList.map(people => this.toListItem(context, people, pseudoPeopleList, allCountries, occupations));
const ul = file.document.createElement("ul");
ul.append(...listItems);
ul.className = "links";
return ul;
}
toListItem(context, people, pseudoPeopleList, allCountries, occupations) {
const ref = this.renderer.renderLink(context, people, pseudoPeopleList, allCountries, occupations
/*this.filter*/ // TODO: Restore removal of already-known (as from people occupation subset) occupation
);
const item = context.file.document.createElement("li");
item.appendChild(ref);
return item;
}
}