-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjuicy-dom-tree-view.html
336 lines (321 loc) · 15.1 KB
/
juicy-dom-tree-view.html
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!--
`juicy-dom-tree-view` - Awesome, Juicy Polymer Element
@element juicy-dom-tree-view
version: 0.0.1
-->
<!-- Imports Polymer -->
<link rel="import" href="../polymer/polymer.html">
<!-- Defines element markup -->
<dom-module id="juicy-dom-tree-view">
<template>
<style>
:host {
display: block;
}
code {
background-color: #e6e6e6;
color: black;
}
code.attribute {
background-color: #transparent;
}
p, ul { margin: 0; }
p { border: inset thin; }
:host { border: inset thin; padding: 0.5em 0.5em 0.5em 1em; color: black; min-height: 5em; font-family: monospace; background: white; }
:host ul { padding: 0 0 0 1em; margin: 0; }
:host li { padding: 0; margin: 0; list-style: none; position: relative; }
:host li li { list-style: none; }
:host li:first-child::before { position: absolute; top: 0; height: 0.7em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
:host li:not(:last-child)::after { position: absolute; top: 0; bottom: -0.7em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
li.selected {
background-color: rbga(242,101,41,0.3);
background-color: var(--juicy-dom-tree-selected-background-color, rgba(242,101,41,0.3));
}
</style>
<ul id="root"></ul>
</template>
<!-- Registers custom element -->
<script>
(function() {
Polymer({
is: 'juicy-dom-tree-view',
properties: {
doc: {
type: Object,
observer: '_docChanged'
},
selectedElements:{
type: Object,
notify: true,
value: function(){return []}
},
selectedClones:{
type: Object,
notify: false,
value: function(){return []}
},
multiple: {
type: Boolean,
value: false,
observer: '_multipleChanged'
},
fakeV1: {
type: Boolean,
value: false
},
wrappedContent: {
type: Boolean,
value: false
}
},
observers: [
// 'selectedElementsAddedOrRemoved(selectedElements.splices)',
// 'selectedClonesAddedOrRemoved(selectedClones.splices)'
],
listeners: {
'root.tap': 'elementTapped'
},
/**
* Helper getter to support non-multiple mode
* For backward compatibility
* @readonly
* @deprecated
* @return first from selectedElements
*/
get selectedElement(){
return this.selectedElements[0];
},
/**
* Helper getter to retrieve selected elements in tree order
* @readonly
* @return {Array} selectedElements sorted in tree order (querySelectorAll order)
*/
get selectedElementInTreeOrder(){
return Array.prototype.map.call(
this.$.root.querySelectorAll('.selected'),
function(element){
return element._juicyDocumentElement;
}
);
},
/**
* Sets properites when element is selected
* @param {Event} event
*/
elementTapped: function (event) {
let ctrlPressed = event.detail.sourceEvent.ctrlKey;
let target = Polymer.dom(event).localTarget;
let tapped = getCloneElement(target);
let isSelected = this.selectedElements.indexOf(tapped._juicyDocumentElement);
if(this.multiple){
if(ctrlPressed){
if(isSelected > -1){
tapped.classList.remove('selected');
this.splice('selectedElements', isSelected, 1);
this.splice('selectedClones', isSelected, 1);
} else {
tapped.classList.add('selected');
this.push('selectedElements', tapped._juicyDocumentElement);
this.push('selectedClones', tapped);
}
} else {
if(isSelected > -1 && this.selectedElements.length === 1){
tapped.classList.remove('selected');
this.splice('selectedElements', isSelected, 1);
this.splice('selectedClones', isSelected, 1);
} else {
removeSelection(this.$.root);
tapped.classList.add('selected');
this.splice('selectedElements', 0, this.selectedElements.length, tapped._juicyDocumentElement);
this.splice('selectedClones', 0, this.selectedClones.length, tapped);
}
}
} else {
if(isSelected > -1){
tapped.classList.remove('selected');
this.splice('selectedElements', isSelected, 1);
this.splice('selectedClones', isSelected, 1);
} else {
this.selectedClones[0] && this.selectedClones[0].classList.remove('selected');
tapped.classList.add('selected');
this.splice('selectedElements', 0, 1, tapped._juicyDocumentElement);
this.splice('selectedClones', 0, 1, tapped);
}
}
},
_multipleChanged: function(){
if(!this.multiple){
removeSelection(this.$.root, this.selectedClones[0]);
this.splice('selectedElements', 1);
this.splice('selectedClones', 1);
}
},
/**
* Clears selected elements and clones
* @return {Array<Array>} [deselectedElements, deselectedClones]
*/
clearSelection: function(){
removeSelection(this.$.root);
return [this.splice('selectedElements', 0), this.splice('selectedClones', 0)];
},
// selectedClonesAddedOrRemoved: function(changeRecord) {
// if (changeRecord) {
// changeRecord.indexSplices.forEach(function(s) {
// s.removed.forEach(function(user) {
// console.log(user.name + ' was removed');
// });
// for (var i=0; i<s.addedCount; i++) {
// var index = s.index + i;
// var newUser = s.object[index];
// console.log('User ' + newUser.name + ' added at index ' + index);
// }
// }, this);
// }
// },
// // set Local DOM classes
// _selectedClonesChanged: function (newVal, oldVal) {
// debugger
// if(newVal !== oldVal) {
// [].forEach.call(this.$.root.querySelectorAll('.selected'), function(elem) {
// elem.classList.remove('selected');
// });
// newVal.classList.add('selected');
// }
// },
/**
* document have changed, so we chould re-render tree
* @param {DocumentFragment} doc changed document
*/
_docChanged: function(doc) {
printDOM(this.$.root, doc, this.selectedElements, this.fakeV1, this.wrappedContent);
}
});
function printDOM(ul, node, selectedElements, fakeV1, wrappedContent) {
while (ul.firstChild) ul.removeChild(ul.firstChild);
for (var i = 0, len = node && node.childNodes.length || 0; i < len; i += 1) {
if(wrappedContent && node.childNodes[i].parentNode &&
node.childNodes[i].parentNode.hasAttribute &&
node.childNodes[i].parentNode.hasAttribute('content-wrapper')){
continue;
}
var li = document.createElement('li');
// GC fregile
li._juicyDocumentElement = node.childNodes[i];
li.className = 't' + node.childNodes[i].nodeType;
// add support for shady DOM elements
li.className += ' juicy-dom-tree-view';
// re-select clones for previously selected elements
if(selectedElements && selectedElements.indexOf(li._juicyDocumentElement) > -1 ){
li.className += ' selected';
}
if (node.childNodes[i].nodeType == 10) {
li.appendChild(document.createTextNode('DOCTYPE: '));
}
if (node.childNodes[i].nodeName) {
var code = document.createElement('code');
// add support for shady DOM elements
code.className += ' juicy-dom-tree-view';
let nodeName;
// TODO: clean it as we are spreading internal details among elements
if(wrappedContent && node.childNodes[i].nodeName === 'DIV' && node.childNodes[i].hasAttribute('content-wrapper')){
nodeName = 'SLOT';
} else if(fakeV1 && node.childNodes[i].nodeName === 'CONTENT'){
// pretend it's SD v1
nodeName = 'SLOT';
} else {
nodeName = node.childNodes[i].nodeName;
}
code.appendChild(document.createTextNode(nodeName));
li.appendChild(code);
// hide wrapped slots
li.appendChild(code);
} else {
var span = document.createElement('span');
span.appendChild(document.createTextNode("no name"));
span.className = 'unnamed';
// add support for shady DOM elements
span.className += ' juicy-dom-tree-view';
li.appendChild(span);
}
if (node.childNodes[i].nodeValue) {
var span = document.createElement('span');
// add support for shady DOM elements
span.className += ' juicy-dom-tree-view';
span.appendChild(document.createTextNode(node.childNodes[i].nodeValue));
li.appendChild(document.createTextNode(': '));
li.appendChild(span);
}
if (node.childNodes[i].attributes)
for (var j = 0; j < node.childNodes[i].attributes.length; j += 1) {
if (node.childNodes[i].attributes[j].specified) {
// pretend we are SD v1
let attrNameContent = node.childNodes[i].attributes[j].nodeName;
if(wrappedContent && attrNameContent === 'content-wrapper'){
continue;
}
let attrValueContent = node.childNodes[i].attributes[j].nodeValue;
if(fakeV1 && attrNameContent === 'select' && node.childNodes[i].nodeName === 'CONTENT' ){
attrNameContent = 'name';
attrValueContent = attrValueContent.replace(/\[slot=['"](.*)['"]\]/,'$1');
}
var attName = document.createElement('code');
attName.appendChild(document.createTextNode(attrNameContent));
attName.className = 'attribute name';
// add support for shady DOM elements
attName.className += ' juicy-dom-tree-view';
var attValue = document.createElement('code');
attValue.appendChild(document.createTextNode(attrValueContent));
attValue.className = 'attribute value';
// add support for shady DOM elements
attValue.className += ' juicy-dom-tree-view';
var att = document.createElement('span');
att.className = 't2';
// add support for shady DOM elements
att.className += ' juicy-dom-tree-view';
att.appendChild(attName);
att.appendChild(document.createTextNode('="'));
att.appendChild(attValue);
att.appendChild(document.createTextNode('"'));
li.appendChild(document.createTextNode(' '));
li.appendChild(att);
}
}
if (node.childNodes[i].parentNode == node) {
if (node.childNodes[i].childNodes.length) {
var ul2 = document.createElement('ul');
// add support for shady DOM elements
ul2.className += ' juicy-dom-tree-view';
li.appendChild(ul2);
printDOM(ul2, node.childNodes[i], selectedElements, fakeV1, wrappedContent);
}
if (node.childNodes[i].content) {
var ul2 = document.createElement('ul');
li.appendChild(ul2);
ul2.className = 'template';
// add support for shady DOM elements
ul2.className += ' juicy-dom-tree-view';
printDOM(ul2, node.childNodes[i].content, selectedElements, fakeV1, wrappedContent);
}
} else {
li.className += ' misparented';
}
ul.appendChild(li);
}
}
function getCloneElement(element){
var clone = element;
while(clone && !clone._juicyDocumentElement){
clone = clone.parentNode;
}
return clone;
}
function removeSelection(root, except){
Array.prototype.forEach.call(root.querySelectorAll('.selected'), function(elem) {
if(elem !== except){
elem.classList.remove('selected');
}
});
}
}());
</script>
</dom-module>