-
Notifications
You must be signed in to change notification settings - Fork 1
/
$hell.ts
204 lines (192 loc) · 5.65 KB
/
$hell.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { getChildren } from "./getChildren.js";
import { getChildFromSinglePath } from "./getChildFromSinglePath.js";
import { cd } from "./cd.js";
import { dashToCamelCase } from "./dashToCamelCase.js";
import {PetaliaConnector} from './PetaliaConnector.js';
const idSym = Symbol("iframeId");
window[idSym] = "self";
const allWindows = Array.from(document.head.querySelectorAll("iframe"))
.map(iframe => {
iframe[idSym] = iframe.id;
return iframe.contentWindow;
})
.concat(window);
export class $hell {
static $0: HTMLElement;
static pathHistory: string[] = [];
static get pwd() {
return this.pathHistory.join("/");
}
static initialize() {
this.$0 = document.body;
this.pathHistory = [""];
}
static cd(path: string) {
if (!this.$0 || path.startsWith("/")) {
this.initialize();
}
this.$0 = cd(this.$0, path, this.pathHistory);
return this.$0;
}
static monitor(el?: HTMLElement){
if(!el) el = this.$0;
const de = el.dispatchEvent.bind(el);
el.dispatchEvent = e => {
console.log(e);
return de(e);
}
}
static connect(el1: HTMLElement, el0: HTMLElement) {
const pc = new PetaliaConnector(el1, el0);
return pc;
}
static get children() {
// switch(this.$0.nodeName){
// case 'IFRAME':
// return (this.$0 as HTMLIFrameElement).contentWindow.document.body.childNodes;
// default:
// if(this.$0.childNodes.length === 0 && this.$0.shadowRoot) return this.$0.shadowRoot.childNodes;
// return this.$0.childNodes;
// }
return getChildren(this.$0);
}
static getList(children: HTMLElement[]) {
const result = [];
const matchingNodeNames: { [key: string]: HTMLElement[] } = {};
children.forEach((node: HTMLElement) => {
if (!node.matches) return;
const nodeName = node.nodeName.toLowerCase();
if (node.id) {
let id = node.id;
if(id.includes('[') || id.includes(']') || id.includes('#') || id.includes('/')){
id = '"' + id + '"';
}
result.push(nodeName + "#" + id);
//return;
}
if (!matchingNodeNames[nodeName]) {
matchingNodeNames[nodeName] = [];
}
matchingNodeNames[nodeName].push(node);
});
for (const key in matchingNodeNames) {
const matchingNodes = matchingNodeNames[key];
if (matchingNodes.length === 1) {
const matchingNode = matchingNodes[0];
if (!matchingNode.id) {
result.push(key);
continue;
}
}
const test = matchingNodes.filter(node => !node.id);
if (test.length === 0) continue;
for (let i = 0, ii = matchingNodes.length; i < ii; i++) {
const matchingNode = matchingNodes[i];
if (matchingNode.id) continue;
result.push(key + "[" + i + "]");
}
}
return result;
}
static get ls() {
if (!this.$0) {
this.initialize();
}
return this.getList(this.children);
}
static get properties() {
return this.getProperties(this.$0);
}
static getProperties(el: HTMLElement) {
this.$0 = el;
const tagName = el.tagName.toLowerCase();
const ce = customElements.get(tagName);
if (!ce) {
console.log("props only provides information for custom elements.");
return;
}
//if(!propertiesAlias) propertiesAlias = 'properties';
const props = ce["properties"] || {};
const observedAttributes = ce["observedAttributes"] as string[];
if (observedAttributes) {
observedAttributes.forEach(attrib => {
const camelCase = dashToCamelCase(attrib);
props[camelCase] = true;
});
}
Object.getOwnPropertyNames(el)
.map(name => name.replace("_", ""))
.forEach(name => {
props[name] = true;
});
if (!props) {
console.log("No properties found");
}
const debugObj = {
constructor: ce
};
for (const key in props) {
const underScore = "_" + key;
if (!this.$0[key] && this.$0[underScore]) {
debugObj[underScore] = this.$0[underScore];
} else {
debugObj[key] = this.$0[key];
}
}
return debugObj;
}
static getParent(el: Element) {
const parent = el.parentNode as HTMLElement;
if (parent.nodeType === 11) {
return parent["host"];
}
return parent;
}
static getPathFromParent(el: Element) {
const parent = this.getParent(el);
const list = this.getList(getChildren(parent));
let path = "";
list.forEach(token => {
const testEl = getChildFromSinglePath(parent, token);
if (testEl === el) {
path = token;
}
});
return path;
}
static getFullPath(el: Element) {
const reversePathArray = [];
let ancestor = el;
while (ancestor.tagName !== "BODY") {
const path = this.getPathFromParent(ancestor);
reversePathArray.push(path);
ancestor = this.getParent(ancestor);
if (ancestor.tagName === "BODY") {
const fe = ancestor.ownerDocument.defaultView.frameElement;
if (fe) {
ancestor = fe as Element;
}
}
}
reversePathArray.reverse();
return "/" + reversePathArray.join("/");
}
static get stores() {
const returnObj = {};
Array.from(allWindows).forEach(win => {
returnObj[win[idSym]] = win.history.state;
});
console.log("stores", returnObj);
return returnObj;
}
}
window["$hell"] = $hell;
Array.from(allWindows).forEach(win => {
win.addEventListener("history-state-update", e => {
console.log("history-changed", e.target[idSym], (<any>e).detail);
});
win.addEventListener("popstate", e => {
console.log("popstate", e.target[idSym], (<any>e).detail);
});
$hell.stores;
});