/
view-tree.js
57 lines (48 loc) · 1.68 KB
/
view-tree.js
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
import { assign } from '@ember/polyfills';
import TabRoute from "ember-inspector/routes/tab";
export default TabRoute.extend({
model() {
return [];
},
setupController() {
this._super(...arguments);
this.get('port').on('view:viewTree', this, this.setViewTree);
this.get('port').on('view:stopInspecting', this, this.stopInspecting);
this.get('port').on('view:startInspecting', this, this.startInspecting);
this.get('port').on('view:inspectDOMElement', this, this.inspectDOMElement);
this.set('controller.viewTreeLoaded', false);
this.get('port').send('view:setOptions', { options: this.get('controller.options') });
this.get('port').send('view:getTree');
},
deactivate() {
this.get('port').off('view:viewTree', this, this.setViewTree);
this.get('port').off('view:stopInspecting', this, this.stopInspecting);
this.get('port').off('view:startInspecting', this, this.startInspecting);
this.get('port').off('view:inspectDOMElement', this, this.inspectDOMElement);
},
setViewTree(options) {
let viewArray = topSort(options.tree);
this.set('controller.model', viewArray);
},
startInspecting() {
this.set('controller.inspectingViews', true);
},
stopInspecting() {
this.set('controller.inspectingViews', false);
},
inspectDOMElement({ elementSelector }) {
this.get('port.adapter').inspectDOMElement(elementSelector);
}
});
function topSort(tree, list) {
list = list || [];
let view = assign({}, tree);
view.parentCount = view.parentCount || 0;
delete view.children;
list.push(view);
tree.children.forEach(child => {
child.parentCount = view.parentCount + 1;
topSort(child, list);
});
return list;
}