app-model.mjs 4.22 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 { V8Map } from './map-processor.mjs';
6 7

class State {
8 9 10 11 12 13 14 15 16 17 18 19 20 21
  #mapPanel;
  #timelinePanel;
  #mapTrack;
  #icTrack;
  #map;
  #ic;
  #navigation;
  constructor(mapPanel, timelinePanel, mapTrack, icTrack) {
    this.#mapPanel = mapPanel;
    this.#timelinePanel = timelinePanel;
    this.#mapTrack = mapTrack;
    this.#icTrack = icTrack;
    this.#navigation = new Navigation(this);
    this.#timelinePanel.addEventListener(
22
      'mapchange', e => this.handleMapChange(e));
23
    this.#timelinePanel.addEventListener(
24
      'showmaps', e => this.handleShowMaps(e));
25 26 27
    this.#mapPanel.addEventListener(
      'mapchange', e => this.handleMapChange(e));
    this.#mapPanel.addEventListener(
28
      'selectmapdblclick', e => this.handleDblClickSelectMap(e));
29
    this.#mapPanel.addEventListener(
30 31
      'sourcepositionsclick', e => this.handleClickSourcePositions(e));
  }
32 33 34 35
  get chunks(){
    //TODO(zcankara) for timeline dependency
    return this.#mapTrack.chunks;
  }
36
  handleMapChange(e){
37
    if (!(e.detail instanceof V8Map)) return;
38 39
    this.map = e.detail;
  }
40 41
  handleStateMapChange(e){
    this.map = e.detail;
42
  }
43 44 45
  handleShowMaps(e){
    if (!(e.detail instanceof V8Map)) return;
    this.#mapPanel.mapEntries = e.detail.filter();
46
  }
47
  set transitions(value) {
48
    this.#mapPanel.transitions = value;
49
  }
50 51
  set mapTimeline(value) {
    this.#mapPanel.timeline = value;
52 53 54 55 56 57 58 59
  }
  get nofChunks() {
    return this.timelinePanel.nofChunks;
  }
  set nofChunks(count) {
    this.timelinePanel.nofChunks = count;
  }
  get mapPanel() {
60
    return this.#mapPanel;
61 62
  }
  get timelinePanel() {
63
    return this.#timelinePanel;
64 65
  }
  get navigation() {
66
    return this.#navigation
67
  }
68

69
  get map() {
70
    return this.#map;
71 72 73
  }
  set map(value) {
    if(!value) return;
74 75 76
    this.#map = value;
    this.#mapTrack.selectedEntry = value;
    this.#navigation.updateUrl();
77 78
    this.mapPanel.map = value;
  }
79 80 81 82 83 84 85 86
  get ic() {
    return this.#ic;
  }
  set ic(value) {
    if(!value) return;
    this.#ic = value;
  }

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
  get entries() {
    if (!this.map) return {};
    return {
      map: this.map.id, time: this.map.time
    }
  }

  handleClickSourcePositions(e){
    //TODO(zcankara) Handle source position
    console.log("source position map detail: ", e.detail);
  }

  handleDblClickSelectMap(e){
    //TODO(zcankara) Handle double clicked map
    console.log("double clicked map: ", e.detail);
  }
}

class Navigation {
  constructor(state) {
    this.state = state;
  }
  get map() {
    return this.state.map
  }
  set map(value) {
    this.state.map = value
  }
  get chunks() {
    return this.state.chunks
  }

  increaseTimelineResolution() {
    this.state.nofChunks *= 1.5;
  }

  decreaseTimelineResolution() {
    this.state.nofChunks /= 1.5;
  }

  selectNextEdge() {
    if (!this.map) return;
    if (this.map.children.length != 1) return;
    this.map = this.map.children[0].to;
  }

  selectPrevEdge() {
    if (!this.map) return;
    if (!this.map.parent()) return;
    this.map = this.map.parent();
  }

  selectDefaultMap() {
    this.map = this.chunks[0].at(0);
  }
  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);
  }

  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;
  }

  updateUrl() {
    let entries = this.state.entries;
    let params = new URLSearchParams(entries);
    window.history.pushState(entries, '', '?' + params.toString());
  }
}

180
export { State };