File size: 9,670 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * NodeDetailPanel - Displays detailed information for a selected graph node
 * Shows node name, opType, domain, attributes, and input/output tensors.
 * Listens for 'node:selected' events via EventBus and hides when no node is selected.
 * Requirements: 16.1, 16.2, 16.3, 16.4, 16.5
 */

class NodeDetailPanel {
  /**
   * @param {string} containerId - ID of the container element
   */
  constructor(containerId) {
    this._containerId = containerId;
    this._container = document.getElementById(containerId);
    this._currentNodeData = null;
    /** @type {Function|null} */
    this._unsubscribeEvent = null;
    /** @type {Function|null} */
    this._unsubscribeState = null;

    if (!this._container) {
      console.warn(`[NodeDetailPanel] Container #${containerId} not found`);
    }

    this._bindEvents();
    this._renderGuidance();
  }

  // ─── Private ──────────────────────────────────────────────────────────────

  /**
   * Subscribe to EventBus and StateManager for node selection changes.
   * @private
   */
  _bindEvents() {
    // Listen for node:selected via EventBus
    if (typeof EventBus !== 'undefined' && typeof CONFIG !== 'undefined') {
      this._unsubscribeEvent = EventBus.on(CONFIG.EVENTS.NODE_SELECTED, (data) => {
        if (data && data.nodeData) {
          this._onNodeSelected(data.nodeData);
        } else if (data && data.nodeId) {
          // Minimal data β€” show what we have
          this._onNodeSelected({ id: data.nodeId });
        }
      });
    }

    // Listen for selectedNodeId becoming null (background click)
    if (typeof StateManager !== 'undefined') {
      this._unsubscribeState = StateManager.subscribe('selectedNodeId', (newId) => {
        if (!newId) {
          this._onNodeDeselected();
        }
      });
    }
  }

  /**
   * Handle node selection.
   * @param {Object} nodeData
   * @private
   */
  _onNodeSelected(nodeData) {
    this._currentNodeData = nodeData;
    this._render(nodeData);
  }

  /**
   * Handle node deselection (background click).
   * @private
   */
  _onNodeDeselected() {
    this._currentNodeData = null;
    this._renderGuidance();
  }

  /**
   * Escape HTML special characters.
   * @param {string} str
   * @returns {string}
   * @private
   */
  _escapeHtml(str) {
    const div = document.createElement('div');
    div.appendChild(document.createTextNode(String(str)));
    return div.innerHTML;
  }

  /**
   * Format an attribute value for display.
   * @param {*} val
   * @returns {string} Escaped HTML string
   * @private
   */
  _formatAttrValue(val) {
    if (val === null || val === undefined) {
      return '<span class="text-muted fst-italic">null</span>';
    }
    if (Array.isArray(val)) {
      const preview = val.slice(0, 8).map((v) => String(v)).join(', ');
      const suffix = val.length > 8 ? `, … (+${val.length - 8})` : '';
      return this._escapeHtml(`[${preview}${suffix}]`);
    }
    if (typeof val === 'object') {
      try {
        const json = JSON.stringify(val, null, 2);
        return `<pre class="mb-0 small">${this._escapeHtml(json)}</pre>`;
      } catch (_) {
        return this._escapeHtml(String(val));
      }
    }
    return this._escapeHtml(String(val));
  }

  /**
   * Build the header section (name, opType, domain).
   * @param {Object} nodeData
   * @returns {string}
   * @private
   */
  _buildHeader(nodeData) {
    const name = nodeData.name || nodeData.label || nodeData.id || 'Unnamed';
    const opType = nodeData.opType || 'N/A';
    const domain = nodeData.domain || 'ai.onnx';

    return `
      <div class="node-detail-header mb-3">
        <h6 class="mb-1 text-truncate" title="${this._escapeHtml(name)}">
          <i class="fas fa-circle-nodes me-1 text-primary"></i>${this._escapeHtml(name)}
        </h6>
        <div class="small">
          <span class="badge bg-info me-1">${this._escapeHtml(opType)}</span>
          <span class="badge bg-secondary">${this._escapeHtml(domain)}</span>
        </div>
      </div>`;
  }

  /**
   * Build the attributes section.
   * @param {Record<string, any>} attributes
   * @returns {string}
   * @private
   */
  _buildAttributes(attributes) {
    if (!attributes || Object.keys(attributes).length === 0) {
      return `
        <div class="node-detail-section mb-3">
          <h6 class="small text-uppercase text-secondary mb-2">
            <i class="fas fa-sliders me-1"></i>Attributes
          </h6>
          <p class="text-muted small mb-0">No attributes</p>
        </div>`;
    }

    const rows = Object.entries(attributes).map(([key, val]) => `
      <tr>
        <td class="text-nowrap pe-2 small fw-medium">${this._escapeHtml(key)}</td>
        <td class="small">${this._formatAttrValue(val)}</td>
      </tr>`).join('');

    return `
      <div class="node-detail-section mb-3">
        <h6 class="small text-uppercase text-secondary mb-2">
          <i class="fas fa-sliders me-1"></i>Attributes (${Object.keys(attributes).length})
        </h6>
        <table class="table table-sm table-borderless mb-0">
          <tbody>${rows}</tbody>
        </table>
      </div>`;
  }

  /**
   * Build the input/output tensors section.
   * @param {Array<string>} inputs
   * @param {Array<string>} outputs
   * @returns {string}
   * @private
   */
  _buildTensors(inputs, outputs) {
    const buildList = (items, label, icon, colorClass) => {
      if (!items || items.length === 0) {
        return `<p class="text-muted small mb-0">No ${label.toLowerCase()}</p>`;
      }
      const listItems = items.map((name) =>
        `<li class="list-group-item py-1 px-2 small">${this._escapeHtml(String(name))}</li>`
      ).join('');
      return `
        <h6 class="small text-uppercase ${colorClass} mb-1">
          <i class="fas ${icon} me-1"></i>${label} (${items.length})
        </h6>
        <ul class="list-group list-group-flush mb-2">${listItems}</ul>`;
    };

    return `
      <div class="node-detail-section mb-3">
        ${buildList(inputs, 'Inputs', 'fa-arrow-right', 'text-primary')}
        ${buildList(outputs, 'Outputs', 'fa-arrow-left', 'text-success')}
      </div>`;
  }

  /**
   * Render the full node detail panel.
   * @param {Object} nodeData
   * @private
   */
  _render(nodeData) {
    if (!this._container) return;

    const nodeId = nodeData.id || '';

    const html = `
      <div class="node-detail-panel-content">
        ${this._buildHeader(nodeData)}
        <div class="d-flex gap-2 mb-2">
          <button class="btn btn-sm btn-outline-primary flex-fill trace-path-btn" data-node-id="${this._escapeHtml(nodeId)}" title="Highlight data flow path through this node">
            <i class="fas fa-route me-1"></i>Trace Path
          </button>
          <button class="btn btn-sm btn-outline-warning flex-fill annotate-node-btn" data-node-id="${this._escapeHtml(nodeId)}" title="Add or edit annotation for this node">
            <i class="fas fa-sticky-note me-1"></i>Annotate
          </button>
        </div>
        <hr class="my-2">
        ${this._buildAttributes(nodeData.attributes)}
        <hr class="my-2">
        ${this._buildTensors(nodeData.inputs, nodeData.outputs)}
      </div>`;

    this._container.innerHTML = html;

    // Bind Trace Path button
    var traceBtn = this._container.querySelector('.trace-path-btn');
    if (traceBtn) {
      traceBtn.addEventListener('click', function () {
        var nid = traceBtn.dataset.nodeId;
        if (nid && window.EventBus) {
          window.EventBus.emit('path:highlight-requested', { nodeId: nid });
        }
      });
    }

    // Bind Annotate button
    var annotateBtn = this._container.querySelector('.annotate-node-btn');
    if (annotateBtn) {
      annotateBtn.addEventListener('click', function () {
        var nid = annotateBtn.dataset.nodeId;
        if (nid && window._onnxApp && window._onnxApp.getGraphAnnotation) {
          var ga = window._onnxApp.getGraphAnnotation();
          if (ga) {
            ga.showAnnotationPopup(nid);
          }
        }
      });
    }
  }

  /**
   * Render guidance message when no node is selected.
   * @private
   */
  _renderGuidance() {
    if (!this._container) return;

    this._container.innerHTML = `
      <div class="text-center text-muted py-4">
        <i class="fas fa-hand-pointer fa-2x mb-2 d-block"></i>
        <p class="mb-0 small">Click a node in the graph to view its details</p>
      </div>`;
  }

  // ─── Public API ───────────────────────────────────────────────────────────

  /**
   * Programmatically show details for a node.
   * @param {Object} nodeData
   */
  showNode(nodeData) {
    if (nodeData) {
      this._onNodeSelected(nodeData);
    }
  }

  /**
   * Clear the panel and show guidance.
   */
  clear() {
    this._currentNodeData = null;
    this._renderGuidance();
  }

  /**
   * Get the currently displayed node data.
   * @returns {Object|null}
   */
  getCurrentNode() {
    return this._currentNodeData;
  }

  /**
   * Destroy and clean up event subscriptions.
   */
  destroy() {
    if (this._unsubscribeEvent) {
      this._unsubscribeEvent();
      this._unsubscribeEvent = null;
    }
    if (this._unsubscribeState) {
      this._unsubscribeState();
      this._unsubscribeState = null;
    }
    this._currentNodeData = null;
    if (this._container) {
      this._container.innerHTML = '';
    }
  }
}

window.NodeDetailPanel = NodeDetailPanel;