Skip to content

Commit 4db7db7

Browse files
committed
update
1 parent 7b7802b commit 4db7db7

File tree

16 files changed

+217
-110
lines changed

16 files changed

+217
-110
lines changed

package-lock.json

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"homepage": "https://github.com/default-js/defaultjs-html-components#readme",
4545
"dependencies": {
46-
"@default-js/defaultjs-common-utils": "^0.6.0",
46+
"@default-js/defaultjs-common-utils": "^0.10.0",
4747
"@default-js/defaultjs-dynamic-requester": "^1.0.0-beta.1",
4848
"@default-js/defaultjs-expression-language": "^1.0.0-beta.12",
4949
"@default-js/defaultjs-extdom": "^1.0.0-beta.25",

src/Application.js

Whitespace-only changes.

src/Application/AppComponent.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Component from "../Component";
2+
import {findClosestInDepth} from "../utils/NodeHelper"
3+
import {EVENT_CLICK as ROUTE_CLICK} from "./Route/Events";
4+
import Route from "./Route";
5+
import View from "./View";
6+
7+
class AppComponent extends Component {
8+
constructor() {
9+
super();
10+
11+
this.ready.then(() => {
12+
this.root.on(ROUTE_CLICK, (event) => {
13+
const route = event.target;
14+
if(!(route instanceof Route))
15+
return;
16+
if(this.__activeRoute__)
17+
this.__activeRoute__.active = false;
18+
19+
20+
21+
this.__activeRoute__ = route;
22+
route.active = true;
23+
});
24+
});
25+
}
26+
27+
get root() {
28+
return this;
29+
}
30+
31+
get activeRoute() {
32+
return this.__activeRoute__;
33+
}
34+
35+
get view() {
36+
if(!this.__view__)
37+
this.__view__ = findClosestInDepth(this.root, (node) => node instanceof View);
38+
39+
return this.__view__;
40+
}
41+
42+
43+
get store() {
44+
if(!this.__store__)
45+
this.__store__ = {};
46+
return this.__store__;
47+
}
48+
}
49+
50+
export default AppComponent;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const ATTR_ACTIVE = "active";
2+
export const ATTR_TEMPLATE = "template";
3+
export const ATTR_COMPONENT_TAG = "component-tag";
4+
export const ATTR_COMPONENT_TAG_ATTRIBUTES = "component-tag-attributes";

src/Application/Route/Events.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const EVENT_CLICK = NODENAME + "-click";
2+
export const EVENT_ACTIVATE = NODENAME + "-activate";
3+
export const EVENT_DEACTIVATE = NODENAME + "-deactivate";

src/Application/Route/Nodename.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { toNodeName } from "../../utils/DefineComponentHelper";
2+
export default toNodeName("route");

src/Application/Route/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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;

src/Application/View/Nodename.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { toNodeName } from "../../utils/DefineComponentHelper";
2+
export default toNodeName("view");

src/Application/View/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { define } from "../../utils/DefineComponentHelper";
2+
import NODENAME from "./Nodename";
3+
4+
class View extends Component {
5+
static get NODENAME() {
6+
return NODENAME;
7+
}
8+
9+
constructor() {
10+
super();
11+
}
12+
}
13+
define(View);
14+
export default View;

0 commit comments

Comments
 (0)