text-view.js 7.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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 TextView extends View {
  constructor(id, broker, patterns, allowSpanSelection) {
    super(id, broker);
    let view = this;
11
    view.hide();
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    view.textListNode = view.divNode.getElementsByTagName('ul')[0];
    view.fillerSvgElement = view.divElement.append("svg").attr('version','1.1').attr("width", "0");
    view.patterns = patterns;
    view.allowSpanSelection = allowSpanSelection;
    view.nodeToLineMap = [];
    var selectionHandler = {
      clear: function() {
        broker.clear(selectionHandler);
      },
      select: function(items, selected) {
        for (let i of items) {
          if (selected) {
            i.classList.add("selected");
          } else {
            i.classList.remove("selected");
          }
        }
29
        broker.clear(selectionHandler);
30
        broker.select(selectionHandler, view.getLocations(items), selected);
31 32 33 34
      },
      selectionDifference: function(span1, inclusive1, span2, inclusive2) {
        return null;
      },
35
      brokeredSelect: function(locations, selected) {
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
        view.selectLocations(locations, selected, true);
      },
      brokeredClear: function() {
        view.selection.clear();
      }
    };
    view.selection = new Selection(selectionHandler);
    broker.addSelectionHandler(selectionHandler);
  }

  setPatterns(patterns) {
    let view = this;
    view.patterns = patterns;
  }

  clearText() {
    let view = this;
    while (view.textListNode.firstChild) {
      view.textListNode.removeChild(view.textListNode.firstChild);
    }
  }

  sameLocation(l1, l2) {
    let view = this;
    if (l1.block_id != undefined && l2.block_id != undefined &&
      l1.block_id == l2.block_id && l1.node_id === undefined) {
      return true;
    }

    if (l1.address != undefined && l1.address == l2.address) {
      return true;
    }

    let node1 = l1.node_id;
    let node2 = l2.node_id;

72
    if (node1 === undefined || node2 == undefined) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      if (l1.pos_start === undefined || l2.pos_start == undefined) {
        return false;
      }
      if (l1.pos_start == -1 || l2.pos_start == -1) {
        return false;
      }
      if (l1.pos_start < l2.pos_start) {
        return l1.pos_end > l2.pos_start;
      } {
        return l1.pos_start < l2.pos_end;
      }
    }

    return l1.node_id == l2.node_id;
  }

  selectLocations(locations, selected, makeVisible) {
    let view = this;
91
    let s = new Set();
92 93 94 95
    for (let l of locations) {
      for (let i = 0; i < view.textListNode.children.length; ++i) {
        let child = view.textListNode.children[i];
        if (child.location != undefined && view.sameLocation(l, child.location)) {
96
          s.add(child);
97 98 99
        }
      }
    }
100
    view.selectCommon(s, selected, makeVisible);
101 102
  }

103
  getLocations(items) {
104 105 106 107
    let result = [];
    let lastObject = null;
    for (let i of items) {
      if (i.location) {
108
        result.push(i.location);
109 110 111 112 113 114 115 116 117 118 119 120 121 122
      }
    }
    return result;
  }

  createFragment(text, style) {
    let view = this;
    let span = document.createElement("SPAN");
    span.onmousedown = function(e) {
      view.mouseDownSpan(span, e);
    }
    if (style != undefined) {
      span.classList.add(style);
    }
123
    span.innerHTML = text;
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
    return span;
  }

  appendFragment(li, fragment) {
    li.appendChild(fragment);
  }

  processLine(line) {
    let view = this;
    let result = [];
    let patternSet = 0;
    while (true) {
      let beforeLine = line;
      for (let pattern of view.patterns[patternSet]) {
        let matches = line.match(pattern[0]);
        if (matches != null) {
          if (matches[0] != '') {
            let style = pattern[1] != null ? pattern[1] : {};
            let text = matches[0];
            if (text != '') {
              let fragment = view.createFragment(matches[0], style.css);
              if (style.link) {
                fragment.classList.add('linkable-text');
                fragment.link = style.link;
              }
              result.push(fragment);
              if (style.location != undefined) {
                let location = style.location(text);
                if (location != undefined) {
                  fragment.location = location;
                }
              }
            }
            line = line.substr(matches[0].length);
          }
          let nextPatternSet = patternSet;
          if (pattern.length > 2) {
            nextPatternSet = pattern[2];
          }
          if (line == "") {
            if (nextPatternSet != -1) {
              throw("illegal parsing state in text-view in patternSet" + patternSet);
            }
            return result;
          }
          patternSet = nextPatternSet;
          break;
        }
      }
      if (beforeLine == line) {
        throw("input not consumed in text-view in patternSet" + patternSet);
      }
    }
  }

  select(s, selected, makeVisible) {
    let view = this;
    view.selection.clear();
    view.selectCommon(s, selected, makeVisible);
  }

  selectCommon(s, selected, makeVisible) {
    let view = this;
    let firstSelect = makeVisible && view.selection.isEmpty();
    if ((typeof s) === 'function') {
      for (let i = 0; i < view.textListNode.children.length; ++i) {
        let child = view.textListNode.children[i];
        if (child.location && s(child.location)) {
          if (firstSelect) {
            makeContainerPosVisible(view.parentNode, child.offsetTop);
            firstSelect = false;
          }
          view.selection.select(child, selected);
        }
      }
199
    } else if (typeof s[Symbol.iterator] === 'function') {
200 201
      if (firstSelect) {
        for (let i of s) {
202
          makeContainerPosVisible(view.parentNode, i.offsetTop);
203
          break;
204 205
        }
      }
206
      view.selection.select(s, selected);
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
    } else {
      if (firstSelect) {
        makeContainerPosVisible(view.parentNode, s.offsetTop);
      }
      view.selection.select(s, selected);
    }
  }

  mouseDownLine(li, e) {
    let view = this;
    e.stopPropagation();
    if (!e.shiftKey) {
      view.selection.clear();
    }
    if (li.location != undefined) {
      view.selectLocations([li.location], true, false);
    }
  }

  mouseDownSpan(span, e) {
    let view = this;
    if (view.allowSpanSelection) {
      e.stopPropagation();
      if (!e.shiftKey) {
        view.selection.clear();
      }
      select(li, true);
    } else if (span.link) {
      span.link(span.textContent);
      e.stopPropagation();
    }
  }

  processText(text) {
    let view = this;
    let textLines = text.split(/[\n]/);
    let lineNo = 0;
    for (let line of textLines) {
      let li = document.createElement("LI");
      li.onmousedown = function(e) {
        view.mouseDownLine(li, e);
      }
      li.className = "nolinenums";
      li.lineNo = lineNo++;
      let fragments = view.processLine(line);
      for (let fragment of fragments) {
        view.appendFragment(li, fragment);
      }
      let lineLocation = view.lineLocation(li);
      if (lineLocation != undefined) {
        li.location = lineLocation;
      }
      view.textListNode.appendChild(li);
    }
  }

  initializeContent(data, rememberedSelection) {
    let view = this;
265
    view.selection.clear();
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
    view.clearText();
    view.processText(data);
    var fillerSize = document.documentElement.clientHeight -
        view.textListNode.clientHeight;
    if (fillerSize < 0) {
      fillerSize = 0;
    }
    view.fillerSvgElement.attr("height", fillerSize);
  }

  deleteContent() {
  }

  isScrollable() {
    return true;
  }

  detachSelection() {
    return null;
  }

  lineLocation(li) {
    let view = this;
    for (let i = 0; i < li.children.length; ++i) {
      let fragment = li.children[i];
      if (fragment.location != undefined && !view.allowSpanSelection) {
        return fragment.location;
      }
    }
  }
}