forked from jieter/leaflet-headless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (72 loc) · 2.23 KB
/
Copy pathindex.js
File metadata and controls
86 lines (72 loc) · 2.23 KB
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
/**
* leaflet-headless
*
* Server side leaflet with fake DOM using jsdom.
*/
var jsdom = require('jsdom').jsdom;
if (!GLOBAL.L) {
// make some globals to fake browser behaviour.
GLOBAL.document = jsdom('<html><head></head><body></body></html>');
GLOBAL.window = GLOBAL.document.defaultView;
GLOBAL.window.navigator.userAgent = 'webkit';
GLOBAL.navigator = GLOBAL.window.navigator;
GLOBAL.Image = require('./src/image.js');
GLOBAL.L_DISABLE_3D = true;
GLOBAL.L_PREFER_CANVAS = true;
var leafletPath = require.resolve('leaflet');
var L = require(leafletPath);
GLOBAL.L = L;
var scriptLength = leafletPath.split('/').slice(-1)[0].length;
L.Icon.Default.imagePath = leafletPath.substring(0, leafletPath.length - scriptLength) + 'images';
// Monkey patch Leaflet
var originalMap = L.Map;
L.Map = originalMap.extend({
// Override initialize to disable animations.
initialize: function (id, options) {
options = L.extend(options || {}, {
fadeAnimation: false,
zoomAnimation: false,
markerZoomAnimation: false
});
return originalMap.prototype.initialize.call(this, id, options);
},
// jsdom does not have clientHeight/clientWidth on elements.
// Adjust size with L.Map.setSize()
getSize: function () {
if (!this._size || this._sizeChanged) {
this._size = new L.Point(1024, 1024);
this._sizeChanged = false;
}
return this._size.clone();
},
setSize: function (width, height) {
this._size = new L.Point(width, height);
return this;
},
saveImage: function (outfilename, callback) {
var leafletImage = require('leaflet-image');
var fs = require('fs');
leafletImage(this, function (err, canvas) {
if (err) {
console.error(err);
return;
}
var out = fs.createWriteStream(outfilename);
var stream = canvas.pngStream();
stream.on('data', function (chunk) {
out.write(chunk);
});
stream.on('end', function () {
if (callback) {
callback(outfilename);
}
});
});
}
});
// leaflet-image checks for instanceof(layer, L.TileLayer.Canvas)
// which is not in leaflet-1.0.0-beta.*, this makes the tests work.
// TODO: remove if this is fixed upstream in leaflet-image
L.TileLayer.Canvas = function () {};
}
module.exports = GLOBAL.L;