summaryrefslogtreecommitdiff
path: root/deps/v8/tools/system-analyzer/view/property-link-table.mjs
blob: 631b779ceb3135720e54c3be1f96a7749bf4959b (plain)
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
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {App} from '../index.mjs'

import {FocusEvent, SelectRelatedEvent} from './events.mjs';
import {DOM, entriesEquals, ExpandableText, V8CustomElement} from './helper.mjs';

DOM.defineCustomElement('view/property-link-table',
                        template =>
                            class PropertyLinkTable extends V8CustomElement {
  _object;
  _propertyDict;
  _instanceLinkButtons = false;

  _showHandler = this._handleShow.bind(this);
  _showSourcePositionHandler = this._handleShowSourcePosition.bind(this);
  _showRelatedHandler = this._handleShowRelated.bind(this);
  _arrayValueSelectHandler = this._handleArrayValueSelect.bind(this);

  constructor() {
    super(template);
  }

  set instanceLinkButtons(newValue) {
    this._instanceLinkButtons = newValue;
  }

  set propertyDict(propertyDict) {
    if (entriesEquals(this._propertyDict, propertyDict)) return;
    if (typeof propertyDict !== 'object') {
      throw new Error(
          `Invalid property dict, expected object: ${propertyDict}`);
    }
    this._propertyDict = propertyDict;
    this.requestUpdate();
  }

  _update() {
    this._fragment = new DocumentFragment();
    this._table = DOM.table();
    for (let key in this._propertyDict) {
      const value = this._propertyDict[key];
      this._addKeyValue(key, value);
    }

    const tableDiv = DOM.div('properties');
    tableDiv.appendChild(this._table);
    this._fragment.appendChild(tableDiv);
    this._createFooter();

    const newContent = DOM.div();
    newContent.appendChild(this._fragment);
    this.$('#content').replaceWith(newContent);
    newContent.id = 'content';
    this._fragment = undefined;
  }

  _addKeyValue(key, value) {
    if (key == 'title') {
      this._addTitle(value);
      return;
    }
    if (key == '__this__') {
      this._object = value;
      return;
    }
    const row = this._table.insertRow();
    row.insertCell().innerText = key;
    const cell = row.insertCell();
    if (value == undefined) return;
    if (Array.isArray(value)) {
      cell.appendChild(this._addArrayValue(value));
      return;
    } else if (App.isClickable(value)) {
      cell.className = 'clickable';
      cell.onclick = this._showHandler;
      cell.data = value;
    }
    if (value.isCode) {
      cell.classList.add('code');
    }
    new ExpandableText(cell, value.toString());
  }

  _addArrayValue(array) {
    if (array.length == 0) {
      return DOM.text('empty');
    } else if (array.length > 200) {
      return DOM.text(`${array.length} items`);
    }
    const select = DOM.element('select');
    select.onchange = this._arrayValueSelectHandler;
    for (let value of array) {
      const option = DOM.element('option');
      option.innerText = value === undefined ? '' : value.toString();
      option.data = value;
      select.add(option);
    }
    return select;
  }

  _addTitle(value) {
    const title = DOM.element('h3');
    title.innerText = value;
    this._fragment.appendChild(title);
  }

  _createFooter() {
    if (this._object === undefined) return;
    if (!this._instanceLinkButtons) return;
    const footer = DOM.div('footer');
    let showButton =
        footer.appendChild(DOM.button('🔍 Details', this._showHandler));
    showButton.data = this._object;
    showButton.title = `Show details for ${this._object}`
    if (this._object.sourcePosition) {
      let showSourcePositionButton = footer.appendChild(
          DOM.button('📍 Source Position', this._showSourcePositionHandler));
      showSourcePositionButton.data = this._object;
      showSourcePositionButton.title = 'Open the source position';
    }
    let showRelatedButton =
        footer.appendChild(DOM.button('🕸 Related', this._showRelatedHandler));
    showRelatedButton.data = this._object;
    showRelatedButton.title = 'Show all related events in all panels';
    this._fragment.appendChild(footer);
  }

  _handleArrayValueSelect(event) {
    const logEntry = event.currentTarget.selectedOptions[0].data;
    this.dispatchEvent(new FocusEvent(logEntry));
  }

  _handleShow(event) {
    this.dispatchEvent(new FocusEvent(event.currentTarget.data));
  }

  _handleShowSourcePosition(event) {
    this.dispatchEvent(new FocusEvent(event.currentTarget.data.sourcePosition));
  }

  _handleShowRelated(event) {
    this.dispatchEvent(new SelectRelatedEvent(event.currentTarget.data));
  }
});