|
import * as components from './components.mjs' |
|
|
|
export { components } |
|
|
|
/** |
|
* Most of this comes from |
|
* https://jsbin.com/jegati/13/edit?html,css,js,console,output |
|
* |
|
* Bookmarks: |
|
* - https://single-spa.js.org/docs/configuration/ |
|
* - https://jsbin.com/jegati/13/edit?html,css,js,output |
|
* |
|
* Scratch pad |
|
* |
|
* ```js |
|
* (() => { const s = document.createElement('script'); s.type='module'; s.src='http://localhost:5000/main.mjs'; document.head.appendChild(s); })() |
|
* ``` |
|
* |
|
*/ |
|
|
|
// https://raw.githubusercontent.com/7kfpun/flag-css/master/dist/flags/png/abw.png |
|
const GITHUB_DATA_BASE_URL = 'https://min.gitcdn.link/repo/' |
|
|
|
const countryNamesLanguageCode = 'en' |
|
|
|
function fetchJson(url) { |
|
return fetch(url) |
|
.then(recv => recv.json()) |
|
.then(payload => payload) |
|
} |
|
|
|
function fetchDataset() { |
|
const names = fetchJson( |
|
`${GITHUB_DATA_BASE_URL}michaelwittig/node-i18n-iso-countries/master/langs/${countryNamesLanguageCode}.json`, |
|
) |
|
const codes = fetchJson(`${GITHUB_DATA_BASE_URL}michaelwittig/node-i18n-iso-countries/master/codes.json`) |
|
console.log('fetchDataset', codes) |
|
return Promise.all([names, codes]).then(recv => { |
|
const packaged = { names: recv[0], codes: recv[1] } |
|
return packaged |
|
}) |
|
} |
|
|
|
function userAgentSupports(featureName = '') { |
|
let isSupported = false |
|
if (featureName === 'localStorage') { |
|
try { |
|
isSupported = window.localStorage instanceof Storage |
|
} catch (e) { |
|
/* fail silently, this is an isser anyway */ |
|
} |
|
} |
|
return isSupported |
|
} |
|
|
|
function fromStorage() { |
|
return Promise.resolve().then(() => { |
|
const recv = window.localStorage.getItem('countries') |
|
if (typeof recv === 'string' || recv instanceof String) { |
|
console.log(`fromStorage: localStorage('countries') had something, we will return`) |
|
return JSON.parse(recv) |
|
} |
|
console.log(`fromStorage: localStorage('countries') had nothing, we will throw`) |
|
}) |
|
} |
|
|
|
function handleNothingFromStorage() { |
|
console.log(`handleNothingFromStorage: Nothing was found in storage`) |
|
|
|
return fetchDataset().then(countries => { |
|
console.log( |
|
`handleNothingFromStorage: doing fetchDataset(), we just downloaded from GitHub and we will add to localStorage('countries') and resume`, |
|
) |
|
window.localStorage.setItem('countries', JSON.stringify(countries)) |
|
return countries |
|
}) |
|
} |
|
|
|
// Clumsy detail here is that we support User-Agents without localStorage |
|
// but we assume blindly Promise suport. |
|
// localStorage is older than Promises. |
|
function loadDataset() { |
|
// TODO: Make this asynchronous. |
|
// because we can't either return undefined, or data. |
|
// we gotta get the data the same way, |
|
// regardless of if we had it locally or not. |
|
if (userAgentSupports('localStorage') === true) { |
|
// How to handle errors. |
|
// Here we only have |
|
console.log(`loadDataset: localStorage is supported`) |
|
return fromStorage() |
|
.then(handleNothingFromStorage) |
|
.then(c => { |
|
console.log(`loadDataset, when supporting localStorage: attach to window.countries`) |
|
return c |
|
}) |
|
} else { |
|
console.log(`loadDataset: localStorage is supported`) |
|
return fetchDataset().then(c => { |
|
console.log( |
|
`loadDataset: doing fetchDataset(), when NOT supporting localStorage: attach to window.countries like in the other case`, |
|
) |
|
return c |
|
}) |
|
} |
|
} |
|
|
|
function toCountriesGlobalObj(recv) { |
|
let newList = Object.create(null) |
|
const { codes = [], names = { countries: {} } } = recv |
|
|
|
codes.map(c => { |
|
const k = c[1].toLowerCase() |
|
const v = names.countries[c[0]] |
|
newList[k] = v |
|
}) |
|
return newList |
|
} |
|
|
|
export const createCountriesList = () => |
|
loadDataset().then(e => { |
|
const countries = toCountriesGlobalObj(e) |
|
let dataListObj = document.createElement('datalist') |
|
dataListObj.setAttribute('id', 'countries_list') |
|
const option = (label, value) => `<option value="${value}" label="${label}">${label}</option>` |
|
let out = [] |
|
for (let [key, label] of Object.entries(countries)) { |
|
out.push(option(label, key)) |
|
} |
|
dataListObj.innerHTML = out.join('') |
|
document.querySelector('input').parentNode.appendChild(dataListObj) |
|
document.querySelector('input').setAttribute('list', 'countries_list') |
|
document.defaultView.countries = Object.freeze(countries) |
|
console.log('done') |
|
return e |
|
}) |
|
|
|
export const customElementsDefineOn = w => { |
|
if ('customElements' in w === false) { |
|
throw new Error('Must pass window object to initialize') |
|
} |
|
for (const [_a, b] of Object.entries(components)) { |
|
let alreadyRegistered = false |
|
try { |
|
w.customElements.define(b.TAG_NAME, b) |
|
alreadyRegistered = true |
|
} catch (_e) { |
|
console.warn(_e) |
|
// caught |
|
} |
|
console.log('customElementsDefineOn', { |
|
alreadyRegistered, |
|
tagName: b.TAG_NAME, |
|
}) |
|
} |
|
} |