getaddress-autocomplete-modal
Version:
GetAddress.io - Autocomplete modal plug-in
70 lines • 2.23 kB
JavaScript
import { Options } from "./Options.js";
import Client from 'getaddress-api';
import Style from "./Style.js";
import AttributeValues from "./AttributeValues.js";
import Modal from "./Modal.js";
import { Debug } from "./Debug.js";
class InstanceCounter {
static add(modal) {
this.instances.push(modal);
}
}
InstanceCounter.instances = [];
const modal = (id, api_key, options = {}) => {
if (!id) {
return;
}
let textbox = document.getElementById(id);
if (!textbox) {
textbox = document.querySelector(id);
}
if (!textbox) {
return;
}
if (options.placeholder === undefined) {
options.placeholder = textbox.placeholder;
}
const fullOptions = new Options(options);
if (fullOptions.touch_screen_only && !isTouchEnabled()) {
return;
}
if (fullOptions.max_screen_width && screenWidth() > fullOptions.max_screen_width) {
return;
}
const index = InstanceCounter.instances.length;
const client = new Client(api_key, fullOptions.alt_autocomplete_url, fullOptions.alt_get_url);
const attributeValues = new AttributeValues(fullOptions, index);
if (index === 0) {
const style = new Style(attributeValues);
style.inject();
}
const modal = new Modal(client, attributeValues, textbox);
textbox.addEventListener('click', (e) => {
Debug.Log(attributeValues, e);
modal.open(textbox.value);
}, false);
textbox.addEventListener('keydown', (e) => {
Debug.Log(attributeValues, e);
modal.open(textbox.value);
}, false);
textbox.addEventListener('paste', (e) => {
Debug.Log(attributeValues, e);
modal.open(textbox.value);
}, false);
return modal.element;
};
const screenWidth = () => {
return (window.innerWidth > 0) ? window.innerWidth : screen.width;
};
const isTouchEnabled = () => {
return ('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0);
};
function destroy() {
for (const instance of InstanceCounter.instances) {
instance.destroy();
}
InstanceCounter.instances = [];
}
export { modal, destroy, Options, screenWidth, isTouchEnabled };
//# sourceMappingURL=Index.js.map