schedule-view.js 5.21 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 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.

"use strict";

class ScheduleView extends TextView {
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

  createViewElement() {
    const pane = document.createElement('div');
    pane.setAttribute('id', "schedule");
    pane.innerHTML =
    `<pre id='schedule-text-pre' class='prettyprint prettyprinted'>
       <ul id='schedule-list' class='nolinenums noindent'>
       </ul>
     </pre>`;
    return pane;
  }

  constructor(parentId, broker) {
    super(parentId, broker, null, false);
  }

  attachSelection(s) {
    const view = this;
    if (!(s instanceof Set)) return;
    view.selectionHandler.clear();
    view.blockSelectionHandler.clear();
    view.sourcePositionSelectionHandler.clear();
    const selected = new Array();
    for (const key of s) selected.push(key);
    view.selectionHandler.select(selected, true);
  }

  createElementFromString(htmlString) {
    var div = document.createElement('div');
    div.innerHTML = htmlString.trim();
    return div.firstChild;
  }


  elementForBlock(block) {
    const view = this;
    function createElement(tag, cls, content) {
      const el = document.createElement(tag);
      if (isIterable(cls)) {
        for (const c of cls) el.classList.add(c);
      } else {
        el.classList.add(cls);
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
      if (content != undefined) el.innerHTML = content;
      return el;
    }

    function mkNodeLinkHandler(nodeId) {
      return function (e) {
        e.stopPropagation();
        if (!e.shiftKey) {
          view.selectionHandler.clear();
        }
        view.selectionHandler.select([nodeId], true);
      };
    }

    function createElementForNode(node) {
      const nodeEl = createElement("div", "node");
      const node_id = createElement("div", ["node-id", "tag", "clickable"], node.id);
      node_id.onclick = mkNodeLinkHandler(node.id);
      view.addHtmlElementForNodeId(node.id, node_id);
      nodeEl.appendChild(node_id);
      const node_label = createElement("div", "node-label", node.label);
      nodeEl.appendChild(node_label);
      if (node.inputs.length > 0) {
        const node_parameters = createElement("div", ["parameter-list", "comma-sep-list"]);
        for (const param of node.inputs) {
          const paramEl = createElement("div", ["parameter", "tag", "clickable"], param);
          node_parameters.appendChild(paramEl);
          paramEl.onclick = mkNodeLinkHandler(param);
          view.addHtmlElementForNodeId(param, paramEl);
        }
        nodeEl.appendChild(node_parameters);
82
      }
83
      return nodeEl;
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

    function mkBlockLinkHandler(blockId) {
      return function (e) {
        e.stopPropagation();
        if (!e.shiftKey) {
          view.blockSelectionHandler.clear();
        }
        view.blockSelectionHandler.select(["" + blockId], true);
      };
    }

    const schedule_block = createElement("div", "schedule-block");
    const block_id = createElement("div", ["block-id", "com", "clickable"], block.id);
    block_id.onclick = mkBlockLinkHandler(block.id);
    schedule_block.appendChild(block_id);
    const block_pred = createElement("div", ["predecessor-list", "block-list", "comma-sep-list"]);
    for (const pred of block.pred) {
      const predEl = createElement("div", ["block-id", "com", "clickable"], pred);
      predEl.onclick = mkBlockLinkHandler(pred);
      block_pred.appendChild(predEl);
    }
    if (block.pred.length) schedule_block.appendChild(block_pred);
    const nodes = createElement("div", "nodes");
    for (const node of block.nodes) {
      nodes.appendChild(createElementForNode(node, block.id));
    }
    schedule_block.appendChild(nodes);
    const block_succ = createElement("div", ["successor-list", "block-list", "comma-sep-list"]);
    for (const succ of block.succ) {
      const succEl = createElement("div", ["block-id", "com", "clickable"], succ);
      succEl.onclick = mkBlockLinkHandler(succ);
      block_succ.appendChild(succEl);
    }
    if (block.succ.length) schedule_block.appendChild(block_succ);
    this.addHtmlElementForBlockId(block.id, schedule_block);
    return schedule_block;
121
  }
122

123 124 125 126
  addBlocks(blocks) {
    for (const block of blocks) {
      const blockEl = this.elementForBlock(block);
      this.divNode.appendChild(blockEl);
127
    }
128 129 130 131 132 133 134
  }

  initializeContent(data, rememberedSelection) {
    this.clearText();
    this.schedule = data.schedule
    this.addBlocks(data.schedule.blocks);
    this.attachSelection(rememberedSelection);
135 136 137
  }

  detachSelection() {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    this.blockSelection.clear();
    this.sourcePositionSelection.clear();
    return this.selection.detachSelection();
  }

  lineString(node) {
    return `${node.id}: ${node.label}(${node.inputs.join(", ")})`
  }

  searchInputAction(view, searchBar) {
    d3.event.stopPropagation();
    this.selectionHandler.clear();
    const query = searchBar.value;
    if (query.length == 0) return;
    const select = [];
    window.sessionStorage.setItem("lastSearch", query);
    const reg = new RegExp(query);
    for (const node of this.schedule.nodes) {
      if (node === undefined) continue;
      if (reg.exec(this.lineString(node)) != null) {
        select.push(node.id)
159
      }
160 161
    }
    this.selectionHandler.select(select, true);
162
  }
163
}