Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 084a5972

Přidáno uživatelem Tomáš Ballák před asi 4 roky(ů)

Re #8092 redesign of markers with popups and little refactor

Zobrazit rozdíly:

website/public/js/zcu-heatmap.js
15 15
var isAnimationRunning = false;
16 16
var data = [];
17 17

  
18

  
19

  
18
// holds all instances of markers for bind/unbind popup purpose
19
// contains: {key:[L.circle,L.pupup]}
20
// key: x and y, x + '' + y string
21
const globalMarkersHolder = {};
22

  
23
//all marker from which popup was removed 
24
// contains: {key:[L.circle,L.pupup]}
25
// key: x and y, x + '' + y string
26
let globalMarkersChanged = {};
27

  
28
const genPopUpControls = (controls) => {
29
  return `<div class="popup-controls">${controls.reduce((sum, item) => sum + item, '')}</div>`
30
}
31
const genPopUp = (place, number, sum, currentPos, maxPos) => {
32
  const header = `<strong>Zařízení a počet:</strong><div id="place-info">${place}</div>`;
33
  const currentNum = `<span id="digit-info">${number}</span>`;
34
  const sumNum = `<span id="total-info" style="font-size: large">${sum ? '/'+sum : '' }</span>`;
35
  const digitInfo = `<div id="number-info">${currentNum}${sumNum}</div>`;
36
  let previousButton = `<button id="previous-info-btn" class="circle-button" onclick="previousInfo()"></button>`;
37
  let nextButton = `<button id="next-info-btn" onclick="nextInfo()" class="circle-button next"></button>`;
38
  let posInfo = `<div id="count-info">${currentPos} z ${maxPos}</div>`;
39

  
40
  if (!sum) {
41
    previousButton = '';
42
    nextButton = '';
43
    posInfo = '';
44
  }
45
  return `
46
  ${header}
47
  ${digitInfo}
48
  ${genPopUpControls([previousButton, posInfo, nextButton])}
49
  `
50
}
20 51
function initMap() {
21 52
  mymap = L.map('heatmap').setView([startX, startY], startZoom);
22 53

  
......
60 91

  
61 92
  
62 93
  if (info.length > 0) {
94
    const { place, number } = info[currenInfo];
63 95
    var popup = L.popup({
64 96
      autoPan: false
65 97
                  })
66 98
                  .setLatLng([lat / i, lng / i])
67
                  .setContent('<strong>Zařízení a počet:</strong><div id=\'place-info\'>' + info[currenInfo].place + '</div>' +
68
                              '<div id=\'number-info\'><span id=\'digit-info\'>' + info[currenInfo].number + '</span>' + '<span id=\'total-info\' style="font-size: large">/' + total +'</span></div>' + 
69
                              '<div class=\'popup-controls\'>' +
70
                                '<button id=\'previous-info-btn\' class=\'circle-button\' onclick=\'previousInfo()\'></button>' +
71
                                '<div id=\'count-info\'>' + (currenInfo + 1) + ' z ' + info.length + '</div>' +
72
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\' class=\'circle-button next\'></button>' +
73
                              '</div>'
74
                              )
99
                  .setContent(genPopUp(place, number, total, currenInfo+1,info.length))
75 100
                  .openOn(mymap);
76 101
    
77 102
    if (info.length == 1) {
......
167 192
  date = $('#date').val()
168 193
  currentTime = parseInt($('#time').children("option:selected").val());
169 194
  setTimeline();
170

  
171
  $.ajax({
172
    type: "POST",
173
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
174
    success: function(result) {
175
      data[currentTime] = result;
176
      drawHeatmap(data[currentTime]);
177
    }
178
  });
179

  
180 195
  $.ajax({
181 196
    type: 'POST',
182 197
    url: positionsRoute + '/' + name,
183 198
    success: function(result) {
184
      drawDataSourceMarks(result)
199
      drawDataSourceMarks(result);
200
      $.ajax({
201
        type: "POST",
202
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
203
        success: function(result) {
204
          data[currentTime] = result;
205
          drawHeatmap(data[currentTime]);
206
        }
207
      })
185 208
    }
186 209
  });
187 210

  
......
191 214
}
192 215

  
193 216
function drawDataSourceMarks(data) {
194

  
195 217
  if (marksLayer != null) {
196 218
    L.removeLayer(marksLayer);
197 219
  }
198

  
199 220
  marksLayer = L.layerGroup();
200 221
  for (var key in data) {
201
    marksLayer.addLayer(L.circle([data[key]['x'], data[key]['y']], {radius: 1, color: 'black'}));
222
    const {x,y,name} = data[key];
223
    const pop = 
224
      L.popup({autoPan: false})
225
        .setLatLng([x, y])
226
        .setContent(genPopUp(name, 0, 0, 1,1));
227
    const newCircle = 
228
      L.circle([x, y], {radius: 2,fillOpacity: 0.8, color: '#004fb3',fillColor: '#004fb3', bubblingMouseEvents: true})
229
        .bindPopup(pop);
230
    globalMarkersHolder[x+''+y] = [newCircle, pop]; //add new marker to global holders
231
    marksLayer.addLayer(
232
      newCircle
233
    );
202 234
  }
203 235

  
204
  marksLayer.addTo(mymap);
236
  marksLayer.setZIndex(-1).addTo(mymap);
205 237

  
206 238
}
207 239

  
......
226 258

  
227 259
  // Todo still switched
228 260
  if (data['items'] != null) {
229
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
261
    //Bind back popups for markers (we dont know if there is any data for this marker or not)
262
    if (Object.keys(globalMarkersChanged).length){
263
      Object.keys(globalMarkersChanged).forEach(function(key) {
264
        globalMarkersChanged[key][0].bindPopup(globalMarkersChanged[key][1]);
265
    });
266
      globalMarkersChanged = {};
267
    }
268
    points = data['items'].map((point) => { 
269
      const {x,y,number} = point;
270
      const key = x+''+y;
271
      const holder = globalMarkersHolder[key];
272
      if (!globalMarkersChanged[key] && number){
273
        //There is data for this marker => unbind popup with zero value
274
        holder[0] = holder[0].unbindPopup();
275
        globalMarkersChanged[key] = holder;
276
      }
277
      return [x, y, number]; 
278
    });
230 279
    if (heatmapLayer != null) {
231 280
      mymap.removeLayer(heatmapLayer);
232 281
    }

Také k dispozici: Unified diff