-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] Proxy Element abstract class of ECharts elements
- Loading branch information
Showing
3 changed files
with
74 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ECElementEvent } from 'echarts'; | ||
import { CustomElement, proxyPrototype, toHyphenCase } from 'web-utility'; | ||
|
||
import { EventKeyPattern } from './utility'; | ||
|
||
export abstract class ProxyElement<T extends object> | ||
extends HTMLElement | ||
implements CustomElement | ||
{ | ||
#data = {} as T; | ||
|
||
toJSON() { | ||
return this.#data; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
proxyPrototype(this, this.#data, (key, value) => | ||
this.setProperty(key.toString(), value) | ||
); | ||
} | ||
|
||
abstract on(event: string, handler: (event: any) => any): any; | ||
abstract off(event: string, handler: (event: any) => any): any; | ||
|
||
setProperty(key: string, value: any) { | ||
const oldValue = this.#data[key], | ||
name = toHyphenCase(key), | ||
eventName = key.slice(2) as ECElementEvent['type']; | ||
this.#data[key] = value; | ||
|
||
switch (typeof value) { | ||
case 'object': | ||
if (!value) this.removeAttribute(name); | ||
break; | ||
case 'boolean': | ||
if (value) super.setAttribute(name, name + ''); | ||
else super.removeAttribute(name); | ||
break; | ||
case 'function': | ||
if (EventKeyPattern.test(key)) this.on(eventName, value); | ||
break; | ||
default: | ||
if (value != null) super.setAttribute(name, value + ''); | ||
else if ( | ||
EventKeyPattern.test(key) && | ||
typeof oldValue === 'function' | ||
) | ||
this.off(eventName, value); | ||
else super.removeAttribute(name); | ||
} | ||
} | ||
} |
5a7c5d6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for wuhan2020 ready!
✅ Preview
https://wuhan2020-fumy53u5n-techquery.vercel.app
Built with commit 5a7c5d6.
This pull request is being automatically deployed with vercel-action