2020-11-24 10:31:54 +01:00
|
|
|
/**
|
|
|
|
* Helper to build a CSS string from a style object
|
|
|
|
*/
|
2021-01-04 19:39:17 +01:00
|
|
|
const buildStyleString = (styleObject, customStyles, selected) => {
|
2020-11-17 13:08:24 +01:00
|
|
|
let str = ""
|
2021-01-04 19:39:17 +01:00
|
|
|
Object.entries(styleObject).forEach(([style, value]) => {
|
2020-11-24 10:31:54 +01:00
|
|
|
if (style && value != null) {
|
2020-11-17 13:08:24 +01:00
|
|
|
str += `${style}: ${value}; `
|
|
|
|
}
|
|
|
|
})
|
2021-01-04 19:39:17 +01:00
|
|
|
str += customStyles || ""
|
|
|
|
if (selected) {
|
|
|
|
str += ";border: 2px solid #0055ff !important;"
|
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Svelte action to apply correct component styles.
|
|
|
|
*/
|
|
|
|
export const styleable = (node, styles = {}) => {
|
2020-11-24 10:31:54 +01:00
|
|
|
let applyNormalStyles
|
|
|
|
let applyHoverStyles
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Creates event listeners and applies initial styles
|
|
|
|
const setupStyles = newStyles => {
|
2020-12-02 14:49:24 +01:00
|
|
|
const selected = newStyles.selected
|
2021-01-04 19:39:17 +01:00
|
|
|
const customStyles = newStyles.custom
|
2020-11-24 10:31:54 +01:00
|
|
|
const normalStyles = newStyles.normal || {}
|
|
|
|
const hoverStyles = {
|
|
|
|
...normalStyles,
|
|
|
|
...newStyles.hover,
|
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
applyNormalStyles = () => {
|
2021-01-04 19:39:17 +01:00
|
|
|
node.style = buildStyleString(normalStyles, customStyles, selected)
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
applyHoverStyles = () => {
|
2021-01-04 19:39:17 +01:00
|
|
|
node.style = buildStyleString(hoverStyles, customStyles, selected)
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Add listeners to toggle hover styles
|
|
|
|
node.addEventListener("mouseover", applyHoverStyles)
|
|
|
|
node.addEventListener("mouseout", applyNormalStyles)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Apply initial normal styles
|
|
|
|
applyNormalStyles()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the current event listeners
|
|
|
|
const removeListeners = () => {
|
|
|
|
node.removeEventListener("mouseover", applyHoverStyles)
|
|
|
|
node.removeEventListener("mouseout", applyNormalStyles)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply initial styles
|
|
|
|
setupStyles(styles)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
return {
|
2020-11-24 10:31:54 +01:00
|
|
|
// Clean up old listeners and apply new ones on update
|
|
|
|
update: newStyles => {
|
|
|
|
removeListeners()
|
|
|
|
setupStyles(newStyles)
|
|
|
|
},
|
|
|
|
// Clean up listeners when component is destroyed
|
2020-11-17 13:08:24 +01:00
|
|
|
destroy: () => {
|
2020-11-24 10:31:54 +01:00
|
|
|
removeListeners()
|
2020-11-17 13:08:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|