@ideal-postcodes/jsutil
Version:
Browser Address Autocomplete for api.ideal-postcodes.co.uk
153 lines (152 loc) • 5.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssEscape = exports.toId = exports.setupBind = exports.setup = exports.generateTimer = exports.config = exports.defaults = exports.relevantPage = exports.getTargets = void 0;
const dom_1 = require("./dom");
const getTargets = (parent, selectors) => {
const line_1 = (0, dom_1.toHtmlElem)(parent, selectors.line_1);
if (line_1 === null)
return null;
const post_town = (0, dom_1.toHtmlElem)(parent, selectors.post_town);
if (post_town === null)
return null;
const postcode = parent.querySelector(selectors.postcode);
if (postcode === null)
return null;
const line_2 = (0, dom_1.toHtmlElem)(parent, selectors.line_2);
const line_3 = (0, dom_1.toHtmlElem)(parent, selectors.line_3);
const country = (0, dom_1.toHtmlElem)(parent, selectors.country);
const county = (0, dom_1.toHtmlElem)(parent, selectors.county);
const organisation = (0, dom_1.toHtmlElem)(parent, selectors.organisation);
return {
line_1,
line_2,
line_3,
post_town,
county,
postcode,
organisation,
country,
};
};
exports.getTargets = getTargets;
const relevantPage = (bindings) => bindings.some((b) => b.pageTest());
exports.relevantPage = relevantPage;
exports.defaults = {
enabled: true,
apiKey: "",
populateCounty: false,
autocomplete: true,
autocompleteOverride: {},
postcodeLookup: true,
postcodeLookupOverride: {},
};
const config = () => {
const c = window.idpcConfig;
if (c === undefined)
return;
return { ...exports.defaults, ...c };
};
exports.config = config;
const generateTimer = ({ pageTest, bind, interval = 1000, }) => {
let timer = null;
const start = (config) => {
if (!pageTest())
return null;
timer = window.setInterval(() => {
try {
bind(config);
}
catch (e) {
stop();
console.log(e);
}
}, interval);
return timer;
};
const stop = () => {
if (timer === null)
return;
window.clearInterval(timer);
timer = null;
};
return { start, stop };
};
exports.generateTimer = generateTimer;
const NOOP = () => { };
const setup = ({ bindings, callback = NOOP }) => {
const c = (0, exports.config)();
if (c === undefined)
return callback();
if (!(0, exports.relevantPage)(bindings))
return callback();
const result = bindings.reduce((prev, binding) => {
const { pageTest, bind } = binding;
if (!pageTest())
return prev;
const { start, stop } = (0, exports.generateTimer)({ pageTest, bind });
start(c);
prev.push({ binding, start, stop });
return prev;
}, []);
return callback(result);
};
exports.setup = setup;
const DEFAULT_SCOPE = "form";
const DEFAULT_ANCHOR = "line_1";
const setupBind = ({ selectors, anchorSelector, parentScope, doc, parentTest, }) => {
const anchors = (0, dom_1.getAnchors)(anchorSelector || selectors[DEFAULT_ANCHOR], doc);
return anchors.reduce((prev, anchor) => {
const parent = (0, dom_1.getParent)(anchor, parentScope || DEFAULT_SCOPE, parentTest);
if (!parent)
return prev;
const targets = (0, exports.getTargets)(parent, selectors);
if (targets === null)
return prev;
prev.push({ targets, parent, anchor });
return prev;
}, []);
};
exports.setupBind = setupBind;
const toId = (elem) => `#${elem.id}`;
exports.toId = toId;
const cssEscape = (value) => {
value = String(value);
const length = value.length;
let index = -1;
let codeUnit;
let result = "";
const firstCodeUnit = value.charCodeAt(0);
while (++index < length) {
codeUnit = value.charCodeAt(index);
if (codeUnit == 0x0000) {
result += "\uFFFD";
continue;
}
if ((codeUnit >= 0x0001 && codeUnit <= 0x001f) ||
codeUnit == 0x007f ||
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
(index == 1 &&
codeUnit >= 0x0030 &&
codeUnit <= 0x0039 &&
firstCodeUnit == 0x002d)) {
result += "\\" + codeUnit.toString(16) + " ";
continue;
}
if (index == 0 && length == 1 && codeUnit == 0x002d) {
result += "\\" + value.charAt(index);
continue;
}
if (codeUnit >= 0x0080 ||
codeUnit == 0x002d ||
codeUnit == 0x005f ||
(codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
(codeUnit >= 0x0041 && codeUnit <= 0x005a) ||
(codeUnit >= 0x0061 && codeUnit <= 0x007a)) {
result += value.charAt(index);
continue;
}
result += "\\" + value.charAt(index);
}
return result;
};
exports.cssEscape = cssEscape;