nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
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
define(['dojo/_base/declare',
  'dojo/_base/lang',
  'dojo/topic',
  './SyncMapState'
],
  function (declare, lang, topic, SyncMapState) {
  var instance = null, clazz;
  /* global JSON */
  clazz =  declare(null, {
 
    map: null,
    isMainView: true,
    syncEventKey: 'wab_sync_event',
 
    constructor: function () {
      if(!window.localStorage){
        console.error('Localstorage is not supported by your browser, so the sync between browsers are not supported.');
        return;
      }
      this._windows = [];
      this.isMainView = window.queryObject.ismain === 'false'? false: true;
      this.syncMapState = SyncMapState.getInstance();
 
      topic.subscribe("mapLoaded", lang.hitch(this, this._onMapLoaded));
      topic.subscribe("mapChanged", lang.hitch(this, this._onMapChanged));
 
      if(!this.isMainView){
        this._listenSyncEvent();
      }
    },
 
    _onMapLoaded: function(map) {
      this.map = map;
      this.syncMapState.setMap(map);
      this._bindMapEvents();
    },
 
    _onMapChanged: function(map) {
      this.map = map;
      this.syncMapState.setMap(map);
      this._bindMapEvents();
    },
 
    _bindMapEvents: function(){
      this.map.on("extent-change", lang.hitch(this, function(evt) {
        this._broadcastMapEvent('extent-change', evt.extent);
      }));
    },
 
    _broadcastMapEvent: function(evtName, evt){
      this._broadcastEvent('map/' + evtName, evt);
    },
 
    _broadcastEvent: function(evtName, evt){
      evtName = 'sync/' + evtName;
      localStorage.setItem(this.syncEventKey, JSON.stringify({
        evtName: evtName,
        evt: evt
      }));
    },
 
    _listenSyncEvent: function(){
      window.addEventListener('storage', lang.hitch(this, function(e){
        if(e.key !== this.syncEventKey){
          return;
        }
 
        var evtInfo = JSON.parse(window.localStorage.getItem(e.key));
        if(/^sync\/map/.test(evtInfo.evtName)){
          this.syncMapState.handleMapChangeEvent(evtInfo);
        }
      }));
    }
 
  });
 
  clazz.getInstance = function() {
    if(instance === null) {
      instance = new clazz();
    }
    return instance;
  };
  return clazz;
});