UNPKG

3.16 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { singleton, Syringe } from 'mana-syringe';
3export var SceneGraphSelectorFactory = Syringe.defineToken('SceneGraphSelectorFactory');
4export var SceneGraphSelector = Syringe.defineToken('SceneGraphSelector', {
5 multiple: false
6});
7var NAME_REGEXP = /\[\s*name=(.*)\s*\]/;
8/**
9 * support the following DOM API:
10 * * getElementById
11 * * getElementsByClassName
12 * * getElementsByName
13 * * getElementsByTag
14 * * querySelector
15 * * querySelectorAll
16 */
17
18var DefaultSceneGraphSelector =
19/** @class */
20function () {
21 function DefaultSceneGraphSelector() {}
22
23 DefaultSceneGraphSelector.prototype.selectOne = function (query, root) {
24 if (query.startsWith('.')) {
25 return root.find(function (node) {
26 // return !node.shadow && node.id === query.substring(1);
27 return node.className === query.substring(1);
28 });
29 } else if (query.startsWith('#')) {
30 // getElementById('id')
31 return root.find(function (node) {
32 // return !node.shadow && node.id === query.substring(1);
33 return node.id === query.substring(1);
34 });
35 } else if (query.startsWith('[name=')) {
36 var matches = query.match(NAME_REGEXP);
37
38 if (matches && matches.length > 1) {
39 var targetName_1 = matches[1].replace(/"/g, ''); // getElementByName();
40
41 return root.find(function (node) {
42 return root !== node && node.name === targetName_1;
43 });
44 } else {
45 return null;
46 }
47 } else {
48 // getElementsByTag('circle');
49 return root.find(function (node) {
50 return root !== node && node.nodeName === query;
51 });
52 }
53 };
54
55 DefaultSceneGraphSelector.prototype.selectAll = function (query, root) {
56 // only support `[name="${name}"]` `.className` `#id`
57 if (query.startsWith('.')) {
58 // getElementsByClassName('className');
59 // should not include itself
60 return root.findAll(function (node) {
61 return root !== node && node.className === query.substring(1);
62 });
63 } else if (query.startsWith('#')) {
64 return root.findAll(function (node) {
65 return root !== node && node.id === query.substring(1);
66 });
67 } else if (query.startsWith('[name=')) {
68 // getElementsByName();
69 var matches = query.match(NAME_REGEXP);
70
71 if (matches && matches.length > 1) {
72 var targetName_2 = matches[1].replace(/"/g, ''); // getElementByName();
73
74 return root.findAll(function (node) {
75 return root !== node && node.name === targetName_2;
76 });
77 } else {
78 return [];
79 }
80 } else {
81 // getElementsByTag('circle');
82 return root.findAll(function (node) {
83 return root !== node && node.nodeName === query;
84 });
85 }
86 }; // eslint-disable-next-line @typescript-eslint/no-unused-vars
87
88
89 DefaultSceneGraphSelector.prototype.is = function () {
90 // TODO: need a simple `matches` implementation
91 return true;
92 };
93
94 DefaultSceneGraphSelector = __decorate([singleton({
95 token: SceneGraphSelector
96 })], DefaultSceneGraphSelector);
97 return DefaultSceneGraphSelector;
98}();
99
100export { DefaultSceneGraphSelector };
\No newline at end of file