vsn 0.1.64 → 0.1.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo/demo.html +4 -0
- package/demo/vsn.js +2 -2
- package/demo/xhr.html +8 -2
- package/dist/AST/ClassNode.d.ts +2 -1
- package/dist/AST/ClassNode.js +40 -51
- package/dist/AST/ClassNode.js.map +1 -1
- package/dist/AST/ElementAttributeNode.d.ts +1 -1
- package/dist/AST/ElementAttributeNode.js +3 -2
- package/dist/AST/ElementAttributeNode.js.map +1 -1
- package/dist/AST/ElementQueryNode.d.ts +4 -4
- package/dist/AST/ElementQueryNode.js +17 -6
- package/dist/AST/ElementQueryNode.js.map +1 -1
- package/dist/AST/ElementStyleNode.d.ts +1 -1
- package/dist/AST/ElementStyleNode.js +3 -2
- package/dist/AST/ElementStyleNode.js.map +1 -1
- package/dist/AST/FunctionNode.d.ts +1 -1
- package/dist/AST/FunctionNode.js +4 -3
- package/dist/AST/FunctionNode.js.map +1 -1
- package/dist/AST/Node.d.ts +1 -1
- package/dist/AST/Node.js +3 -2
- package/dist/AST/Node.js.map +1 -1
- package/dist/AST/OnNode.d.ts +1 -1
- package/dist/AST/OnNode.js +5 -4
- package/dist/AST/OnNode.js.map +1 -1
- package/dist/AST.d.ts +1 -1
- package/dist/AST.js +1 -1
- package/dist/AST.js.map +1 -1
- package/dist/DOM.d.ts +7 -1
- package/dist/DOM.js +23 -3
- package/dist/DOM.js.map +1 -1
- package/dist/Registry.d.ts +3 -0
- package/dist/Registry.js +13 -0
- package/dist/Registry.js.map +1 -1
- package/dist/attributes/RootAttribute.d.ts +1 -0
- package/dist/attributes/RootAttribute.js +18 -6
- package/dist/attributes/RootAttribute.js.map +1 -1
- package/dist/vsn.js +4 -0
- package/dist/vsn.js.map +1 -1
- package/package.json +1 -1
- package/src/AST/ClassNode.ts +16 -16
- package/src/AST/ElementAttributeNode.ts +2 -2
- package/src/AST/ElementQueryNode.ts +13 -5
- package/src/AST/ElementStyleNode.ts +2 -2
- package/src/AST/FunctionNode.ts +4 -3
- package/src/AST/Node.ts +2 -2
- package/src/AST/OnNode.ts +4 -4
- package/src/AST.ts +2 -3
- package/src/DOM.ts +19 -3
- package/src/Registry.ts +10 -0
- package/src/attributes/RootAttribute.ts +11 -3
- package/src/vsn.ts +4 -0
- package/test/DOM.spec.ts +27 -0
package/src/DOM.ts
CHANGED
|
@@ -7,9 +7,14 @@ import {WrappedWindow} from "./DOM/WrappedWindow";
|
|
|
7
7
|
import {WrappedDocument} from "./DOM/WrappedDocument";
|
|
8
8
|
import {Scope} from "./Scope";
|
|
9
9
|
import {EventDispatcher} from "./EventDispatcher";
|
|
10
|
-
import {Registry} from "./Registry";
|
|
11
10
|
import {ClassNode} from "./AST/ClassNode";
|
|
12
11
|
|
|
12
|
+
export enum EQuerySelectDirection {
|
|
13
|
+
ALL,
|
|
14
|
+
UP,
|
|
15
|
+
DOWN
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
export class DOM extends EventDispatcher {
|
|
14
19
|
protected static _instance: DOM;
|
|
15
20
|
protected _root: Tag;
|
|
@@ -44,7 +49,7 @@ export class DOM extends EventDispatcher {
|
|
|
44
49
|
return this._root;
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
public async get(selector: string, create: boolean = false, tag: Tag = null) {
|
|
52
|
+
public async get(selector: string, create: boolean = false, tag: Tag = null, direction: EQuerySelectDirection = EQuerySelectDirection.DOWN): Promise<TagList> {
|
|
48
53
|
switch (selector) {
|
|
49
54
|
case 'window':
|
|
50
55
|
return new TagList(this.window);
|
|
@@ -53,7 +58,14 @@ export class DOM extends EventDispatcher {
|
|
|
53
58
|
case 'body':
|
|
54
59
|
return new TagList(this.root);
|
|
55
60
|
default:
|
|
56
|
-
|
|
61
|
+
let nodes;
|
|
62
|
+
if (direction === EQuerySelectDirection.DOWN) {
|
|
63
|
+
nodes = this.querySelectorAll(selector, tag);
|
|
64
|
+
} else if (direction === EQuerySelectDirection.UP) {
|
|
65
|
+
nodes = [this.querySelectorClosest(selector, tag)];
|
|
66
|
+
} else {
|
|
67
|
+
nodes = this.querySelectorAll(selector);
|
|
68
|
+
}
|
|
57
69
|
return await this.getTagsForElements(Array.from(nodes) as Element[], create);
|
|
58
70
|
}
|
|
59
71
|
}
|
|
@@ -69,6 +81,10 @@ export class DOM extends EventDispatcher {
|
|
|
69
81
|
this.root.scope.set(`#${id}`, tag.scope);
|
|
70
82
|
}
|
|
71
83
|
|
|
84
|
+
public querySelectorClosest(q: string, tag: Tag = null): HTMLElement {
|
|
85
|
+
return tag.element.closest(q);
|
|
86
|
+
}
|
|
87
|
+
|
|
72
88
|
public querySelectorAll(q: string, tag: Tag = null): NodeList | HTMLElement[] {
|
|
73
89
|
const element: HTMLElement | Document = tag && !q.startsWith('#') ? tag.element : this.rootElement;
|
|
74
90
|
return this.querySelectorElement(element, q);
|
package/src/Registry.ts
CHANGED
|
@@ -54,10 +54,15 @@ export class RegistryStore extends EventDispatcher {
|
|
|
54
54
|
has(key: string) {
|
|
55
55
|
return !!this.store[key];
|
|
56
56
|
}
|
|
57
|
+
|
|
58
|
+
get keys(): string[] {
|
|
59
|
+
return Object.keys(this.store);
|
|
60
|
+
}
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
export class Registry extends EventDispatcher {
|
|
60
64
|
protected static _instance: Registry;
|
|
65
|
+
public readonly functions: RegistryStore;
|
|
61
66
|
public readonly controllers: RegistryStore;
|
|
62
67
|
public readonly classes: RegistryStore;
|
|
63
68
|
public readonly models: RegistryStore;
|
|
@@ -69,6 +74,7 @@ export class Registry extends EventDispatcher {
|
|
|
69
74
|
|
|
70
75
|
constructor() {
|
|
71
76
|
super();
|
|
77
|
+
this.functions = new RegistryStore();
|
|
72
78
|
this.controllers = new RegistryStore();
|
|
73
79
|
this.classes = new RegistryStore();
|
|
74
80
|
this.models = new RegistryStore();
|
|
@@ -79,6 +85,10 @@ export class Registry extends EventDispatcher {
|
|
|
79
85
|
this.attributes = new RegistryStore();
|
|
80
86
|
}
|
|
81
87
|
|
|
88
|
+
public static function(key: string = null, setup = null) {
|
|
89
|
+
return register('functions', key, setup);
|
|
90
|
+
}
|
|
91
|
+
|
|
82
92
|
public static class(cls: ClassNode) {
|
|
83
93
|
Registry.instance.classes.register(cls.name, cls);
|
|
84
94
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {Attribute} from "../Attribute";
|
|
2
2
|
import {VisionHelper} from "../helpers/VisionHelper";
|
|
3
3
|
import {Registry} from "../Registry";
|
|
4
|
-
import {Scope} from "../Scope";
|
|
5
4
|
|
|
6
5
|
@Registry.attribute('vsn-root')
|
|
7
6
|
export class RootAttribute extends Attribute {
|
|
@@ -10,8 +9,17 @@ export class RootAttribute extends Attribute {
|
|
|
10
9
|
|
|
11
10
|
public async setup() {
|
|
12
11
|
this.tag.scope.set('$mobile', VisionHelper.isMobile());
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
for (const key of Registry.instance.functions.keys) {
|
|
14
|
+
const fn = Registry.instance.functions.get(key);
|
|
15
|
+
this.tag.scope.set(key, fn);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Registry.instance.functions.on('register', this.registerFunction, this);
|
|
15
19
|
await super.setup();
|
|
16
20
|
}
|
|
21
|
+
|
|
22
|
+
public async registerFunction(name: string, fn: Function) {
|
|
23
|
+
this.tag.scope.set(name, fn);
|
|
24
|
+
}
|
|
17
25
|
}
|
package/src/vsn.ts
CHANGED
|
@@ -25,6 +25,10 @@ export class Vision extends EventDispatcher {
|
|
|
25
25
|
} else {
|
|
26
26
|
console.warn('No dom, running in CLI mode.');
|
|
27
27
|
}
|
|
28
|
+
this.registry.functions.register('log', console.log);
|
|
29
|
+
this.registry.functions.register('warn', console.warn);
|
|
30
|
+
this.registry.functions.register('error', console.error);
|
|
31
|
+
this.registry.functions.register('info', console.info);
|
|
28
32
|
this.registry.controllers.register('Object', Object);
|
|
29
33
|
this.registry.controllers.register('WrappedArray', WrappedArray);
|
|
30
34
|
this.registry.controllers.register('Data', DynamicScopeData);
|
package/test/DOM.spec.ts
CHANGED
|
@@ -33,4 +33,31 @@ describe('DOM', () => {
|
|
|
33
33
|
done();
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
|
+
|
|
37
|
+
it("should be able to query elements in all directions", (done) => {
|
|
38
|
+
document.body.innerHTML = `
|
|
39
|
+
<div id="root" vsn-set:asd|integer="1">
|
|
40
|
+
<main id="parent" vsn-set:asd|integer="123">
|
|
41
|
+
<div id="testing" vsn-controller:test="TestController" vsn-set:asd="234|integer">
|
|
42
|
+
<div id="child" vsn-set:asd|integer="345">
|
|
43
|
+
<ul>
|
|
44
|
+
<li vsn-set:asd|integer="456"></li>
|
|
45
|
+
<li id="grandchild" vsn-set:asd|integer="567"></li>
|
|
46
|
+
</ul>
|
|
47
|
+
</div>
|
|
48
|
+
</div>
|
|
49
|
+
</main>
|
|
50
|
+
</div>
|
|
51
|
+
`;
|
|
52
|
+
const dom = new DOM(document);
|
|
53
|
+
dom.once('built', async () => {
|
|
54
|
+
const root = await dom.exec('#root');
|
|
55
|
+
const child = await dom.exec('#child');
|
|
56
|
+
const grandchild = await dom.exec('#grandchild');
|
|
57
|
+
expect(await child.exec('?<(main).asd')).toBe(123);
|
|
58
|
+
expect(await child.exec('?>(li)[0].asd')).toBe(456);
|
|
59
|
+
expect(await dom.exec('?(#grandchild)[0]')).toBe(grandchild);
|
|
60
|
+
done();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
36
63
|
});
|