1+ import { define } from "../../utils/DefineComponentHelper" ;
2+ import Resolver from "@default-js/defaultjs-expression-language/src/ExpressionResolver" ;
3+ import NODENAME from "./Nodename" ;
4+ import { EVENT_CLICK , EVENT_ACTIVATE , EVENT_DEACTIVATE } from "./Events" ;
5+ import { ATTR_TEMPLATE , ATTR_COMPONENT_TAG , ATTR_COMPONENT_TAG_ATTRIBUTES } from "./Attributes"
6+
7+ const ATTRIBUTES = [ ATTR_TEMPLATE , ATTR_COMPONENT_TAG , ATTR_COMPONENT_TAG_ATTRIBUTES ] ;
8+ const EVENTS = [ EVENT_CLICK , EVENT_ACTIVATE , EVENT_DEACTIVATE ] ;
9+ const getTagAttributes = async ( route ) => {
10+ const attributes = route . attr ( ATTR_COMPONENT_TAG_ATTRIBUTES ) || "{}" ;
11+ return Resolver . resolve ( attributes , { } , { } ) ;
12+ }
13+
14+ const buildComponent = async ( route ) => {
15+ const tag = route . attr ( ATTR_TAG ) ;
16+ const clazz = customElements . get ( tag ) ;
17+ const attributes = await getTagAttributes ( ) ;
18+
19+ const element = new clazz ( ) ;
20+ for ( attribute in attributes ) {
21+ const value = attributes [ attribute ] ;
22+ if ( typeof value === "string" )
23+ element . attr ( attribute , value ) ;
24+ }
25+
26+ return element ;
27+ }
28+
29+ class Route extends Component {
30+ static get observedAttributes ( ) {
31+ return ATTRIBUTES ;
32+ }
33+
34+ static get NODENAME ( ) { return NODENAME ; }
35+
36+ static get EVENTS ( ) { return EVENTS ; }
37+
38+ constructor ( ) {
39+ super ( ) ;
40+ this . on ( "click" , ( ) => {
41+ this . trigger ( EVENT_CLICK ) ;
42+ } ) ;
43+ }
44+
45+ async init ( ) {
46+ }
47+
48+ get active ( ) {
49+ return this . hasAttribute ( ATTR_ACTIVE ) ;
50+ }
51+
52+ set active ( active ) {
53+ const current = this . active ;
54+ if ( active != current ) {
55+ this . attr ( ATTR_ACTIVE , active ? "" : null ) ;
56+ if ( active ) {
57+ this . trigger ( EVENT_ACTIVATE ) ;
58+ } else {
59+ this . trigger ( EVENT_DEACTIVATE ) ;
60+ delete this . __component__ ;
61+ }
62+
63+ }
64+ }
65+
66+ get component ( ) {
67+ return this . __component__ ;
68+ }
69+
70+ async appendTo ( { container} ) {
71+ if ( ! this . __component__ ) {
72+ this . __component__ = await buildComponent ( this ) ;
73+ container . append ( this . __component__ ) ;
74+ }
75+ return this . __component__ ;
76+ }
77+ }
78+
79+ define ( Route ) ;
80+ export default Route ;
0 commit comments