#
Junjie
9 天以前 dc3f9cc91759823ce59486f19b138be4b296a0f1
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
Vue.component('map-replay-canvas', {
  template: `
    <div class="dl-replay-map-shell" style="display: flex; flex-direction: column; width: 100%; height: 100%; min-height: 0;">
      <div class="dl-replay-floor-bar" v-if="floorList.length > 1 && floorBarVisible">
        <button
          v-for="floor in floorList"
          :key="'replay-floor-' + floor"
          type="button"
          class="dl-floor-btn"
          :class="{ 'is-active': activeFloor === floor }"
          @click="handleFloorClick(floor)"
        >{{ floor }}F</button>
      </div>
      <div class="dl-replay-map-stage" style="flex: 1; min-height: 0; position: relative;">
        <map-canvas
          ref="mapCanvas"
          :lev="activeFloor"
          :lev-list="floorList"
          :viewport-padding="viewportPadding"
          :hud-padding="hudPadding"
          :realtime-enabled="false"
          :tool-panel-enabled="true"
          :cycle-capacity-visible="false"
          :highlight-on-param-change="false"
          :station-task-range="stationTaskRange"
          @switch-lev="handleToolFloorSwitch"
          @crn-click="emitDeviceClick('Crn', $event, null)"
          @dual-crn-click="emitDeviceClick('DualCrn', $event, null)"
          @rgv-click="emitDeviceClick('Rgv', $event, null)"
          @station-click="emitDeviceClick('Devp', null, $event)"
        ></map-canvas>
      </div>
    </div>
  `,
  props: ['frame', 'lev', 'stationTaskRange', 'viewportPadding', 'hudPadding'],
  data: function () {
    return {
      floorList: [],
      activeFloor: null,
      lastAppliedFrameKey: '',
      pendingFrameValue: null,
      applyFrameRaf: null,
      floorSyncTimers: [],
      floorBarVisible: false,
      lastLoadedFloorForLayout: null
    };
  },
  watch: {
    frame: function (value) {
      this.scheduleApplyFrame(value);
    },
    lev: function (value) {
      if (value == null || value === '') {
        return;
      }
      this.activeFloor = Number(value);
      this.scheduleFloorFrameSync();
    }
  },
  mounted: function () {
    this.loadFloorList();
    this.$nextTick(() => {
      this.scheduleMapCanvasLayoutSync();
    });
  },
  beforeDestroy: function () {
    if (this.applyFrameRaf) {
      cancelAnimationFrame(this.applyFrameRaf);
      this.applyFrameRaf = null;
    }
    this.clearFloorSyncTimers();
  },
  methods: {
    loadFloorList: function () {
      var self = this;
      $.ajax({
        url: baseUrl + '/basMap/getLevList',
        headers: { token: localStorage.getItem('token') },
        method: 'GET',
        success: function (res) {
          var floors = res && res.code === 200 ? (res.data || []) : [];
          self.floorList = floors.map(function (item) { return Number(item); }).filter(function (item) { return !isNaN(item); });
          self.activeFloor = self.lev != null && self.lev !== '' ? Number(self.lev) : (self.floorList[0] || 1);
          self.$emit('lev-list-change', self.floorList.slice());
          self.scheduleMapCanvasLayoutSync();
          self.scheduleFloorFrameSync();
        }
      });
    },
    scheduleMapCanvasLayoutSync: function () {
      var syncSteps = [0, 80, 220, 420];
      for (var i = 0; i < syncSteps.length; i++) {
        var delay = syncSteps[i];
        var timer = setTimeout(() => {
          this.syncMapCanvasLayout();
        }, delay);
        this.floorSyncTimers.push(timer);
      }
    },
    syncMapCanvasLayout: function () {
      if (!this.$refs.mapCanvas) {
        return;
      }
      if (typeof this.$refs.mapCanvas.resizeToContainer === 'function') {
        this.$refs.mapCanvas.resizeToContainer();
      }
      var currentFloor = Number(this.activeFloor || this.$refs.mapCanvas.currentLev || 0);
      if (currentFloor > 0
        && this.lastLoadedFloorForLayout !== currentFloor
        && typeof this.$refs.mapCanvas.getMap === 'function') {
        this.lastLoadedFloorForLayout = currentFloor;
        this.$refs.mapCanvas.getMap();
      }
    },
    scheduleApplyFrame: function (value) {
      this.pendingFrameValue = value;
      if (this.applyFrameRaf) {
        return;
      }
      this.applyFrameRaf = requestAnimationFrame(() => {
        this.flushPendingFrame();
      });
    },
    flushPendingFrame: function () {
      this.applyFrameRaf = null;
      this.applyFrame(this.pendingFrameValue);
    },
    applyFrame: function (value) {
      if (!this.$refs.mapCanvas || !value) {
        return;
      }
      var frameKey = [value.sampleSeq || '', value.timestamp || ''].join('|');
      if (frameKey && frameKey === this.lastAppliedFrameKey) {
        return;
      }
      this.lastAppliedFrameKey = frameKey;
      this.$refs.mapCanvas.applyReplayFrame(value);
    },
    handleFloorClick: function (floor) {
      this.activeFloor = Number(floor);
      this.lastLoadedFloorForLayout = null;
      this.$emit('floor-change', this.activeFloor);
      this.scheduleFloorFrameSync();
    },
    handleToolFloorSwitch: function (floor) {
      this.handleFloorClick(floor);
    },
    clearFloorSyncTimers: function () {
      while (this.floorSyncTimers.length) {
        clearTimeout(this.floorSyncTimers.pop());
      }
    },
    scheduleFloorFrameSync: function () {
      this.clearFloorSyncTimers();
      var syncSteps = [80, 220, 420];
      for (var i = 0; i < syncSteps.length; i++) {
        var timer = setTimeout(() => {
          this.syncMapCanvasLayout();
          this.applyFrame(this.frame);
        }, syncSteps[i]);
        this.floorSyncTimers.push(timer);
      }
    },
    emitDeviceClick: function (type, deviceNo, stationId) {
      var target = window.MapCanvasShared && typeof window.MapCanvasShared.buildReplayTarget === 'function'
        ? window.MapCanvasShared.buildReplayTarget(type, deviceNo, stationId)
        : { type: type, deviceNo: deviceNo, stationId: stationId };
      this.$emit('target-click', target);
    }
  }
});