index.mjs 9.01 KB
Newer Older
1 2 3 4
// Copyright 2020 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.

5
import { SelectionEvent, FocusEvent, SelectTimeEvent } from "./events.mjs";
6
import { State } from "./app-model.mjs";
7 8 9 10
import { SourcePositionLogEvent } from "./log/sourcePosition.mjs";
import { MapLogEvent } from "./log/map.mjs";
import { IcLogEvent } from "./log/ic.mjs";
import Processor from "./processor.mjs";
11 12 13 14 15
import { $ } from "./helper.mjs";
import "./ic-panel.mjs";
import "./timeline-panel.mjs";
import "./map-panel.mjs";
import "./log-file-reader.mjs";
16
import "./source-panel.mjs";
17
class App {
18
  #state;
19
  #view;
20 21
  #navigation;
  constructor(fileReaderId, mapPanelId, timelinePanelId,
22
    icPanelId, mapTrackId, icTrackId, sourcePanelId) {
23 24 25 26 27 28 29
    this.#view = {
      logFileReader: $(fileReaderId),
      icPanel: $(icPanelId),
      mapPanel: $(mapPanelId),
      timelinePanel: $(timelinePanelId),
      mapTrack: $(mapTrackId),
      icTrack: $(icTrackId),
30
      sourcePanel: $(sourcePanelId)
31
    };
32
    this.#state = new State();
33 34 35
    this.#navigation = new Navigation(this.#state, this.#view);
    document.addEventListener('keydown',
      e => this.#navigation.handleKeyDown(e));
36
    this.toggleSwitch = $('.theme-switch input[type="checkbox"]');
37 38 39 40 41 42 43
    this.toggleSwitch.addEventListener("change", (e) => this.switchTheme(e));
    this.#view.logFileReader.addEventListener("fileuploadstart", (e) =>
      this.handleFileUpload(e)
    );
    this.#view.logFileReader.addEventListener("fileuploadend", (e) =>
      this.handleDataUpload(e)
    );
44 45
    Object.entries(this.#view).forEach(([_, panel]) => {
      panel.addEventListener(SelectionEvent.name,
46
        e => this.handleShowEntries(e));
47
      panel.addEventListener(FocusEvent.name,
48
        e => this.handleShowEntryDetail(e));
49
      panel.addEventListener(SelectTimeEvent.name,
50
        e => this.handleTimeRangeSelect(e));
51
    });
52
  }
53
  handleShowEntries(e) {
54
    if (e.entries[0] instanceof MapLogEvent) {
55
      this.showMapEntries(e.entries);
56
    } else if (e.entries[0] instanceof IcLogEvent) {
57
      this.showIcEntries(e.entries);
58 59
    } else if (e.entries[0] instanceof SourcePositionLogEvent) {
      this.showSourcePositionEntries(e.entries);
60 61
    } else {
      console.error("Undefined selection!");
62
    }
63
  }
64 65 66 67 68 69
  showMapEntries(entries) {
    this.#state.selectedMapLogEvents = entries;
    this.#view.mapPanel.selectedMapLogEvents = this.#state.selectedMapLogEvents;
  }
  showIcEntries(entries) {
    this.#state.selectedIcLogEvents = entries;
70
    this.#view.icPanel.selectedLogEvents = this.#state.selectedIcLogEvents;
71
  }
72 73 74 75
  showSourcePositionEntries(entries) {
    //TODO(zcankara) Handle multiple source position selection events
    this.#view.sourcePanel.selectedSourcePositions = entries;
  }
76

77 78 79
  handleTimeRangeSelect(e) {
    this.selectTimeRange(e.start, e.end);
  }
80
  handleShowEntryDetail(e) {
81
    if (e.entry instanceof MapLogEvent) {
82
      this.selectMapLogEvent(e.entry);
83
    } else if (e.entry instanceof IcLogEvent) {
84
      this.selectICLogEvent(e.entry);
85
    } else if (e.entry instanceof SourcePositionLogEvent) {
86
      this.selectSourcePositionEvent(e.entry);
87
    } else {
88 89
      console.log("undefined");
    }
90
  }
91 92 93 94 95
  selectTimeRange(start, end) {
    this.#state.timeSelection.start = start;
    this.#state.timeSelection.end = end;
    this.#state.icTimeline.selectTimeRange(start, end);
    this.#state.mapTimeline.selectTimeRange(start, end);
96 97
    this.#view.mapPanel.selectedMapLogEvents =
      this.#state.mapTimeline.selection;
98
    this.#view.icPanel.selectedLogEvents = this.#state.icTimeline.selection;
99
  }
100
  selectMapLogEvent(entry) {
101 102 103
    this.#state.map = entry;
    this.#view.mapTrack.selectedEntry = entry;
    this.#view.mapPanel.map = entry;
104
  }
105
  selectICLogEvent(entry) {
106
    this.#state.ic = entry;
107
    this.#view.icPanel.selectedLogEvents = [entry];
108
  }
109
  selectSourcePositionEvent(sourcePositions) {
110
    if (!sourcePositions.script) return;
111
    console.log("source positions: ", sourcePositions);
112
    this.#view.sourcePanel.selectedSourcePositions = [sourcePositions];
113
  }
114

115
  handleFileUpload(e) {
116
    this.restartApp();
117
    $("#container").className = "initial";
118
  }
119 120 121 122
  restartApp() {
    this.#state = new State();
    this.#navigation = new Navigation(this.#state, this.#view);
  }
123 124 125 126 127
  // Event log processing
  handleLoadTextProcessor(text) {
    let logProcessor = new Processor();
    logProcessor.processString(text);
    return logProcessor;
128
  }
129

130 131
  // call when a new file uploaded
  handleDataUpload(e) {
132 133
    if (!e.detail) return;
    $("#container").className = "loaded";
134 135
    // instantiate the app logic
    let fileData = e.detail;
136
    try {
137 138 139 140 141 142
      const processor = this.handleLoadTextProcessor(fileData.chunk);
      const mapTimeline = processor.mapTimeline;
      const icTimeline = processor.icTimeline;
      //TODO(zcankara) Make sure only one instance of src event map ic id match
      // Load map log events timeline.
      this.#state.mapTimeline = mapTimeline;
143
      // Transitions must be set before timeline for stats panel.
144
      this.#view.mapPanel.transitions = this.#state.mapTimeline.transitions;
145
      this.#view.mapTrack.data = mapTimeline;
146
      this.#state.chunks = this.#view.mapTrack.chunks;
147 148 149 150 151 152
      this.#view.mapPanel.timeline = mapTimeline;
      // Load ic log events timeline.
      this.#state.icTimeline = icTimeline;
      this.#view.icPanel.timeline = icTimeline;
      this.#view.icTrack.data = icTimeline;
      // TODO(zcankara) Load source position log events timeline.
153 154 155
    } catch (error) {
      console.log(error);
    }
156
    this.fileLoaded = true;
157
  }
158

159
  refreshTimelineTrackView() {
160 161 162 163
    this.#view.mapTrack.data = this.#state.mapTimeline;
    this.#view.icTrack.data = this.#state.icTimeline;
  }

164
  switchTheme(event) {
165 166 167 168
    document.documentElement.dataset.theme = event.target.checked
      ? "light"
      : "dark";
    if (this.fileLoaded) {
169 170
      this.refreshTimelineTrackView();
    }
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
class Navigation {
  #view;
  constructor(state, view) {
    this.state = state;
    this.#view = view;
  }
  get map() {
    return this.state.map
  }
  set map(value) {
    this.state.map = value
  }
  get chunks() {
    return this.state.chunks
  }
  increaseTimelineResolution() {
    this.#view.timelinePanel.nofChunks *= 1.5;
    this.state.nofChunks *= 1.5;
  }
  decreaseTimelineResolution() {
    this.#view.timelinePanel.nofChunks /= 1.5;
    this.state.nofChunks /= 1.5;
  }
  selectNextEdge() {
    if (!this.map) return;
    if (this.map.children.length != 1) return;
    this.map = this.map.children[0].to;
    this.#view.mapTrack.selectedEntry = this.map;
    this.updateUrl();
    this.#view.mapPanel.map = this.map;
  }
  selectPrevEdge() {
    if (!this.map) return;
    if (!this.map.parent()) return;
    this.map = this.map.parent();
    this.#view.mapTrack.selectedEntry = this.map;
    this.updateUrl();
    this.#view.mapPanel.map = this.map;
  }
  selectDefaultMap() {
    this.map = this.chunks[0].at(0);
    this.#view.mapTrack.selectedEntry = this.map;
    this.updateUrl();
    this.#view.mapPanel.map = this.map;
  }
  moveInChunks(next) {
    if (!this.map) return this.selectDefaultMap();
    let chunkIndex = this.map.chunkIndex(this.chunks);
    let chunk = this.chunks[chunkIndex];
    let index = chunk.indexOf(this.map);
    if (next) {
      chunk = chunk.next(this.chunks);
    } else {
      chunk = chunk.prev(this.chunks);
    }
    if (!chunk) return;
    index = Math.min(index, chunk.size() - 1);
    this.map = chunk.at(index);
    this.#view.mapTrack.selectedEntry = this.map;
    this.updateUrl();
    this.#view.mapPanel.map = this.map;
  }
  moveInChunk(delta) {
    if (!this.map) return this.selectDefaultMap();
    let chunkIndex = this.map.chunkIndex(this.chunks)
    let chunk = this.chunks[chunkIndex];
    let index = chunk.indexOf(this.map) + delta;
    let map;
    if (index < 0) {
      map = chunk.prev(this.chunks).last();
    } else if (index >= chunk.size()) {
      map = chunk.next(this.chunks).first()
    } else {
      map = chunk.at(index);
    }
    this.map = map;
    this.#view.mapTrack.selectedEntry = this.map;
    this.updateUrl();
    this.#view.mapPanel.map = this.map;
  }
  updateUrl() {
    let entries = this.state.entries;
    let params = new URLSearchParams(entries);
    window.history.pushState(entries, '', '?' + params.toString());
  }
  handleKeyDown(event) {
    switch (event.key) {
      case "ArrowUp":
        event.preventDefault();
        if (event.shiftKey) {
          this.selectPrevEdge();
        } else {
          this.moveInChunk(-1);
        }
        return false;
      case "ArrowDown":
        event.preventDefault();
        if (event.shiftKey) {
          this.selectNextEdge();
        } else {
          this.moveInChunk(1);
        }
        return false;
      case "ArrowLeft":
        this.moveInChunks(false);
        break;
      case "ArrowRight":
        this.moveInChunks(true);
        break;
      case "+":
        this.increaseTimelineResolution();
        break;
      case "-":
        this.decreaseTimelineResolution();
        break;
    }
  }
}

293
export { App };