PageSourceSearch

https://www.mapanica.net/theme/js/leaflet/leaflet-hash.js

js mapanica.net collected 2026-10-02 17:26:12 UTC 4,415 bytes, 146 lines download raw bytes

1(function(window) {
2    var HAS_HASHCHANGE = (function() {
3        var doc_mode = window.documentMode;
4        return ('onhashchange' in window) &&
5            (doc_mode === undefined || doc_mode > 7);
6    })();
7
8    L.Hash = function(map) {
9        this.onHashChange = L.Util.bind(this.onHashChange, this);
10
11        if (map) {
12            this.init(map);
13        }
14    };
15
16    L.Hash.prototype = {
17        map: null,
18        lastHash: null,
19
20        parseHash: function(hash) {
21            if(hash.indexOf('#') == 0) {
22                hash = hash.substr(1);
23            }
24            var args = hash.split("/");
25            if (args.length == 3) {
26                var zoom = parseInt(args[0], 10),
27                    lat = parseFloat(args[1]),
28                    lon = parseFloat(args[2]);
29                if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
30                    return false;
31                } else {
32                    return {
33                        center: new L.LatLng(lat, lon),
34                        zoom: zoom
35                    };
36                }
37            } else {
38                return false;
39            }
40        },
41
42        formatHash: function(map) {
43            var center = map.getCenter(),
44                zoom = map.getZoom(),
45                precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
46
47            return "#" + [zoom,
48                center.lat.toFixed(precision),
49                center.lng.toFixed(precision)
50            ].join("/");
51        },
52
53        init: function(map) {
54            this.map = map;
55
56            this.map.on("moveend", this.onMapMove, this);
57
58            // reset the hash
59            this.lastHash = null;
60            this.onHashChange();
61
62            if (!this.isListening) {
63                this.startListening();
64            }
65        },
66
67        remove: function() {
68            this.map = null;
69            if (this.isListening) {
70                this.stopListening();
71            }
72        },
73
74        onMapMove: function(map) {
75            // bail if we're moving the map (updating from a hash),
76            // or if the map has no zoom set
77
78            if (this.movingMap || this.map.getZoom() === 0) {
79                return false;
80            }
81
82            var hash = this.formatHash(this.map);
83            if (this.lastHash != hash) {
84                location.replace(hash);
85                this.lastHash = hash;
86            }
87        },
88
89        movingMap: false,
90        update: function() {
91            var hash = location.hash;
92            if (hash === this.lastHash) {
93                // console.info("(no change)");
94                return;
95            }
96            var parsed = this.parseHash(hash);
97            if (parsed) {
98                // console.log("parsed:", parsed.zoom, parsed.center.toString());
99                this.movingMap = true;
100
101                this.map.setView(parsed.center, parsed.zoom);
102
103                this.movingMap = false;
104            } else {
105                // console.warn("parse error; resetting:", this.map.getCenter(), this.map.getZoom());
106                this.onMapMove(this.map);
107            }
108        },
109
110        // defer hash change updates every 100ms
111        changeDefer: 100,
112        changeTimeout: null,
113        onHashChange: function() {
114            // throttle calls to update() so that they only happen every
115            // `changeDefer` ms
116            if (!this.changeTimeout) {
117                var that = this;
118                this.changeTimeout = setTimeout(function() {
119                    that.update();
120                    that.changeTimeout = null;
121                }, this.changeDefer);
122            }
123        },
124
125        isListening: false,
126        hashChangeInterval: null,
127        startListening: function() {
128            if (HAS_HASHCHANGE) {
129                L.DomEvent.addListener(window, "hashchange", this.onHashChange);
130            } else {
131                clearInterval(this.hashChangeInterval);
132                this.hashChangeInterval = setInterval(this.onHashChange, 50);
133            }
134            this.isListening = true;
135        },
136
137        stopListening: function() {
138            if (HAS_HASHCHANGE) {
139                L.DomEvent.removeListener(window, "hashchange", this.onHashChange);
140            } else {
141                clearInterval(this.hashChangeInterval);
142            }
143            this.isListening = false;
144        }
145    };
146})(window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.