Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 243x 243x 243x 243x 243x 243x 239x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 3x 239x 241x 241x 241x 241x 241x 241x 241x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { isPlainObject } from './object.js'
/**
* @typedef {object} OptionData
*
* JSON representation of option data
*
* @property {boolean} selected - option selected flag
* @property {string} text - option text
* @property {string} value - option value
* @property {"fetch"|"option"} origin - gets where the data is from
* @property { {
* [x:string]: unknown,
* text: string,
* value: string,
* group?: string
* }} data - option custom data
*/
/**
* get data object of option in a JSON represented format
*
* @param {HTMLOptionElement} option - target element in shadow DOM
* @returns {OptionData} option data
*/
export function dataObjectOfOption (option) {
const baseData = {
text: option.textContent || '',
value: option.value,
}
const dataAttr = option.getAttribute('data-of-option')
if (dataAttr != null && dataAttr.trim() !== '') {
try {
const jsonData = JSON.parse(dataAttr)
if (!isPlainObject(jsonData)) { throw Error('data-of-option attr must be serialized json object') }
return {
...baseData,
selected: option.selected,
origin: 'option',
data: {
...jsonData,
...baseData,
},
}
} catch {
// ignore
}
}
return {
...baseData,
selected: option.selected,
origin: 'option',
data: baseData,
}
}
/**
* creates an option element based on option data
*
* @param {OptionData} optionData - option data
* @returns {HTMLOptionElement} option element
*/
export function optionElementOfData (optionData) {
const option = document.createElement('option')
option.textContent = optionData.text
option.value = optionData.value
option.selected = optionData.selected
option.setAttribute('data-of-option', JSON.stringify(optionData.data))
return option
}
|