93 lines
4.4 KiB
JavaScript
93 lines
4.4 KiB
JavaScript
/* Where a remote role may be worked from, as the pages say it: the words of
|
|
* the badge, the picks of the agent's "Remote, open to someone in" chips, and
|
|
* how an agent's gates share out between the Where block and Other rules. No
|
|
* DOM in it, so `tests/js/remote.test.mjs` can walk it without a browser.
|
|
*
|
|
* The area is a place token in three spellings, one per layer: `remote:<place
|
|
* token>` on the Roles rail and in the address, `<place token>` in a
|
|
* `remote_from` gate, and a label on a row (`remote_where`). `anywhere` is
|
|
* the one place that is no gazetteer town, region or country.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const ANYWHERE = "anywhere";
|
|
|
|
/** The badge of a row and the reader: "remote · Europe", "remote · anywhere",
|
|
"remote · Denmark" from `remote_where`, "remote" alone when the area is
|
|
not stated, and the other words (hybrid, onsite) as they are. */
|
|
function remoteBadge(role) {
|
|
const word = role && role.remote;
|
|
if (!word) return "";
|
|
const where = word === "remote" ? String(role.remote_where || "").trim() : "";
|
|
return where ? `${word} · ${where}` : word;
|
|
}
|
|
|
|
/** A `/api/suggest/places` answer as the value of a `remote_from` gate, or
|
|
null when the Where block does not offer it: countries, regions and
|
|
Anywhere. A town, or a remote token that is not Anywhere (the block's
|
|
own country pick is the plain one), is left out. */
|
|
function fromSuggestion(s) {
|
|
const token = String(s.token || "");
|
|
if (token === `remote:${ANYWHERE}`) return ANYWHERE;
|
|
if (token.startsWith("country:") || token.startsWith("region:")) return token;
|
|
return null;
|
|
}
|
|
|
|
/** The words a person can type for Anywhere, which the suggestions may or
|
|
may not carry. */
|
|
function offersAnywhere(q) {
|
|
const text = String(q).trim().toLowerCase();
|
|
return text.length > 0 && ["anywhere", "worldwide"].some((w) => w.startsWith(text));
|
|
}
|
|
|
|
/** The chip label of a gate value when the server has not said it yet. */
|
|
function fallbackLabel(value, countryName) {
|
|
if (value === ANYWHERE) return "Anywhere";
|
|
if (value === "country:EU") return "Europe";
|
|
if (value.startsWith("country:")) return countryName(value.slice(8));
|
|
return value.slice(value.indexOf(":") + 1);
|
|
}
|
|
|
|
/** "remote from Denmark, Sweden or Germany", "remote from anywhere". */
|
|
function remoteSentence(values, label) {
|
|
const names = values.map((v) => (v === ANYWHERE ? "anywhere" : label(v)));
|
|
const list = names.length < 2 ? names.join("")
|
|
: `${names.slice(0, -1).join(", ")} or ${names[names.length - 1]}`;
|
|
return `remote from ${list}`;
|
|
}
|
|
|
|
/** Share out an agent's gates between the Where block and Other rules. Each
|
|
Where line takes the first gate it can say whole; the rest are rows. A
|
|
must or one-of gate with a weight other than the default stays a row,
|
|
since Where would drop it. The remote line is a `remote_from` one-of, or
|
|
the older one-of on `remote: [remote]`, which reads as remote from
|
|
Anywhere; both save as `remote_from`. Every require gate on country is a
|
|
row, "within Europe" among them: it holds for every role and not only
|
|
the remote ones, so the block does not fold it into the remote line. */
|
|
function distribute(gates) {
|
|
const where = { countries: null, places: null, remote: null, hybrid: null };
|
|
const rest = [];
|
|
const plain = (g) => g.weight === undefined || g.weight === 1;
|
|
const only = (g, value) => g.values.length === 1 && g.values[0] === value;
|
|
let older = null;
|
|
for (const g of gates) {
|
|
if (g.field === "country" && g.mode === "any" && plain(g) && !where.countries) where.countries = g;
|
|
else if (g.field === "place" && g.mode === "any" && plain(g) && !where.places) where.places = g;
|
|
else if (g.field === "remote_from" && g.mode === "any" && plain(g) && !where.remote) {
|
|
where.remote = { values: g.values.map(String) };
|
|
} else if (g.field === "remote" && g.mode === "any" && plain(g) && only(g, "remote") && !older) {
|
|
older = g;
|
|
} else if (g.field === "remote" && g.mode === "prefer" && only(g, "hybrid") && !where.hybrid) where.hybrid = g;
|
|
else rest.push(g);
|
|
}
|
|
if (older) {
|
|
if (where.remote) rest.push(older);
|
|
else where.remote = { values: [ANYWHERE] };
|
|
}
|
|
return { where, rest };
|
|
}
|
|
|
|
window.remoteHelpers = { ANYWHERE, remoteBadge, fromSuggestion, offersAnywhere, fallbackLabel, remoteSentence, distribute };
|
|
})();
|