Skip to content

Commit 80727e9

Browse files
committed
refactored js
1 parent 70826eb commit 80727e9

File tree

4 files changed

+45
-52
lines changed

4 files changed

+45
-52
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function(grunt) {
22

33
grunt.initConfig({
44
uglify: {
5-
my_target: {
5+
build: {
66
files: [{
77
expand: true,
88
cwd: 'src/',

build/ExportMap.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ExportMap.js

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@
77

88
OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
99
/**
10-
*
10+
*
1111
*/
1212
type: OpenLayers.Control.TYPE_BUTTON,
13+
canvasComponents: [],
14+
tileData: {},
1315
/**
1416
* A function to trigger the ExportMap functions.
15-
*
17+
*
1618
* @public
17-
* @param {HTMLElement|undefined} canvas The HTML Element of the Canvas
19+
* @param {HTMLElement|undefined} canvas The HTML Element of the Canvas
1820
* @returns {HTMLElement} The generated canvas element
1921
*/
20-
trigger: function(canvas) {
21-
22+
trigger: function (canvas) {
23+
2224
this.setUpCanvas(canvas);
23-
25+
26+
this.canvasComponents = [];
27+
this.tileData = {};
28+
2429
// Create this function so we can use it later on
25-
Object.size = function(object) {
30+
Object.size = function (object) {
2631
var size = 0;
2732
for (var key in object) {
2833
if (object.hasOwnProperty(key)) {
@@ -31,46 +36,34 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
3136
}
3237
return size;
3338
};
34-
35-
36-
var layers = this.map.layers;
37-
this.canvasComponents = new Array();
38-
this.tileData = {};
39-
// Loop through all the layers on the map and export the layers which are visible
40-
for (var layerCount = 0; layerCount < layers.length; layerCount++) {
41-
var layer = layers[layerCount];
42-
if (layer.visibility === false) {
43-
// We shouldnt export layers that are invisible
44-
continue;
45-
}
46-
47-
if (layer instanceof OpenLayers.Layer.Vector) {
48-
this.exportVectorLayer(layer);
49-
} else if (layer instanceof OpenLayers.Layer.Grid) {
50-
this.exportGridLayer(layer);
51-
} else {
52-
// We cant export this type of layer
53-
continue;
39+
40+
this.map.layers.forEach(function (layer) {
41+
if (layer.visibility) {
42+
if (layer instanceof OpenLayers.Layer.Vector) {
43+
this.exportVectorLayer(layer);
44+
} else if (layer instanceof OpenLayers.Layer.Grid) {
45+
this.exportGridLayer(layer);
46+
}
5447
}
55-
}
56-
48+
}.bind(this));
49+
5750
return this.canvas;
5851
},
5952
/**
60-
* A function to set up the canvas, if none are provided then we'll create
53+
* A function to set up the canvas, if none are provided then we'll create
6154
* a new canvas element
62-
*
55+
*
6356
* @private
6457
* @param {type} canvas The canvas being used
6558
* @returns {undefined}
6659
*/
67-
setUpCanvas: function(canvas) {
60+
setUpCanvas: function (canvas) {
6861
if (canvas === undefined) {
6962
this.canvas = document.createElement('canvas');
7063
} else {
7164
this.canvas = canvas;
7265
}
73-
66+
7467
this.canvasContext = this.canvas.getContext('2d');
7568
this.canvas.width = this.map.viewPortDiv.clientWidth;
7669
this.canvas.height = this.map.viewPortDiv.clientHeight;
@@ -79,16 +72,16 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
7972
* A function to export a Vector Layer onto the canvas. Due to stacking issues
8073
* canvas produced in this function are added to our canvasComponents array before
8174
* being added to the canvas at the end of execution
82-
*
75+
*
8376
* @private
8477
* @param {<OpenLayers.Layer.Vector>} layer The layer to export
8578
* @returns {undefined}
8679
*/
87-
exportVectorLayer: function(layer) {
80+
exportVectorLayer: function (layer) {
8881
if (!(layer.renderer instanceof OpenLayers.Renderer.Canvas)) {
8982
return;
9083
}
91-
84+
9285
var canvasRenderer = layer.renderer;
9386
if (canvasRenderer.canvas !== null) {
9487
var canvasContext = canvasRenderer.canvas;
@@ -98,54 +91,54 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
9891
/**
9992
* A function to export the Grid layer. We need to generate all the tiles for the layer
10093
* and then stitch them together onto the canvas.
101-
*
94+
*
10295
* @private
10396
* @param {<OpenLayers.Layer.Grid>} layer The layer to export
10497
* @returns {undefined}
10598
*/
106-
exportGridLayer: function(layer) {
99+
exportGridLayer: function (layer) {
107100
this.offsetX = parseInt(this.map.layerContainerDiv.style.left);
108101
this.offsetY = parseInt(this.map.layerContainerDiv.style.top);
109102
this.stitchTiles(layer);
110103
},
111104
/**
112-
* This function calculates all the tiles on display and finds their positions
105+
* This function calculates all the tiles on display and finds their positions
113106
* relative to the other tiles.
114-
*
107+
*
115108
* @private
116109
* @param {<OpenLayers.Layer.Grid>} layer The layer to export
117110
* @returns {undefined}
118111
*/
119-
stitchTiles: function(layer) {
112+
stitchTiles: function (layer) {
120113
for (var gridIndex in layer.grid) {
121114
var grid = layer.grid[gridIndex];
122115
for (var tileIndex in grid) {
123116
var tile = grid[tileIndex];
124117
var url = layer.getURL(tile.bounds);
125118
var tileXPosition = tile.position.x + this.offsetX;
126119
var tileYPosition = tile.position.y + this.offsetY;
127-
120+
128121
if (this.tileData[url] === undefined) {
129122
this.tileData[url] = {x: tileXPosition, y: tileYPosition};
130123
}
131-
124+
132125
this.loadImage(url);
133-
134126
}
135127
}
136128
},
137129
/**
138130
* A function to load the tile image from a URL. When all the images have been loaded
139131
* it will proceed to draw the images on the canvas using the drawCanvasComponent function
140-
*
132+
*
141133
* @private
142134
* @param {String} url
143135
* @returns {undefined}
144136
*/
145-
loadImage: function(url) {
137+
loadImage: function (url) {
146138
var image = new Image();
147139
var that = this;
148-
image.onload = function() {
140+
141+
image.onload = function () {
149142
var canvasComponents = that.canvasComponents;
150143
// Add the tile to the front of the array
151144
canvasComponents.unshift(this);
@@ -166,14 +159,14 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
166159
},
167160
/**
168161
* Draws a canvas component onto the canvas context
169-
*
162+
*
170163
* @private
171164
* @param {Canvas|Image} canvasComponent The Canvas or tile image being loaded on to the canvas
172165
* @param {integer} x The X coordinate of the image
173166
* @param {integer} y The Y coordinate of the image
174167
* @returns {undefined}
175168
*/
176-
drawCanvasComponent: function(canvasComponent, x, y) {
169+
drawCanvasComponent: function (canvasComponent, x, y) {
177170
this.canvasContext.drawImage(canvasComponent, x, y);
178171
},
179172
CLASS_NAME: "OpenLayers.Control.ExportMap"

test/osmfeatures.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<script src="http://openlayers.org/api/OpenLayers.js" type="text/javascript"></script>
8-
<script src="../ExportMap.js" type="text/javascript"></script>
8+
<script src="../build/ExportMap.min.js" type="text/javascript"></script>
99
<link rel="stylesheet" href="style.css" type="text/css" />
1010
<script>
1111
var exportMapControl;

0 commit comments

Comments
 (0)