@morbidick/lit-element-notify
Advanced tools
Comparing version 0.1.0 to 0.2.0
export const notify = (baseElement) => class extends baseElement { | ||
constructor() { | ||
super(); | ||
/** | ||
* Extend the LitElement `createProperty` method to map properties to events | ||
*/ | ||
static createProperty(name, options) { | ||
super.createProperty(name, options); | ||
this._propertyEventMap = new Map(); | ||
const props = this.constructor.properties; | ||
for (const prop of Object.getOwnPropertyNames(props)) { | ||
const config = props[prop]; | ||
if (config.notify) { | ||
this._propertyEventMap.set(prop, this.constructor._eventName(prop, config)); | ||
} | ||
if (!this.hasOwnProperty('_propertyEventMap')) { | ||
this._propertyEventMap = new Map(); | ||
} | ||
if (options.notify) { | ||
this._propertyEventMap.set(name, this._eventNameForProperty(name, options)); | ||
} | ||
} | ||
/** | ||
* check for changed properties with notify option and fire the events | ||
*/ | ||
update(props) { | ||
super.update(props); | ||
for (const [eventProp, eventName] of this._propertyEventMap.entries()) { | ||
if (!this.constructor._propertyEventMap) { | ||
return; | ||
} | ||
for (const [eventProp, eventName] of this.constructor._propertyEventMap.entries()) { | ||
if (props.has(eventProp)) { | ||
@@ -32,13 +40,16 @@ this.dispatchEvent(new CustomEvent(eventName, { | ||
static _eventName(property, config) { | ||
if (config.notify && typeof config.notify === 'string') | ||
return config.notify; | ||
/** | ||
* Returns the event name for the given property. | ||
*/ | ||
static _eventNameForProperty(name, options) { | ||
if (options.notify && typeof options.notify === 'string') | ||
return options.notify; | ||
if (config.attribute && typeof config.attribute === 'string') | ||
return `${config.attribute}-changed`; | ||
if (options.attribute && typeof options.attribute === 'string') | ||
return `${options.attribute}-changed`; | ||
return `${property.toLowerCase()}-changed`; | ||
return `${name.toLowerCase()}-changed`; | ||
} | ||
}; | ||
export default notify; | ||
export default notify; |
{ | ||
"name": "@morbidick/lit-element-notify", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Small mixin for LitElement to get easy change events via the properties getter", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
3337
4
45