Commit 3d1c44ff authored by Tejesh's avatar Tejesh 🖖
Browse files

support topojson/geojson or mix of these, fix eclint issues

parent 24c83443
Pipeline #49442 passed with stage
in 2 minutes and 50 seconds
......@@ -5,7 +5,7 @@ cache:
validate:
script:
- eclint check '**/*.html' '**/*.js' '**/*.css' '**/*.yaml' '**/*.md'
- eclint check "**/*.html" "**/*.js" "**/*.css" "**/*.yaml" "**/*.md"
- yarn install
- npm run lint
- npm run test
......
......@@ -31,7 +31,7 @@ var defaults = {
* // initialize the map on the "map" div with a given center and zoom
* var mapviewer = g1.mapviewer(config)
* ```
*/
*/
export var MapViewer = class MapViewer {
constructor(config) {
var self = this
......@@ -61,18 +61,20 @@ export var MapViewer = class MapViewer {
}
}
}
// * @method cacheData(<String> datasetName, 'String' URL || <Object> data): <Object> data
// * GETs data if not already there in this.gData
// *
// * TODO: If dataset itself is given, that is understood as data update.
// * layers rendered based on this data must be reRendered.
// * ?? Do a data diff and then load dependant layers ??
// *
// * @example
// *
// *```js
// mapviewer.cacheData('india_states', 'india-states.geojson')
// *```
/*
* @method cacheData(<String> datasetName, 'String' URL || <Object> data): <Object> data
* GETs data if not already there in this.gData
*
* TODO: If dataset itself is given, that is understood as data update.
* layers rendered based on this data must be reRendered.
* ?? Do a data diff and then load dependant layers ??
*
* @example
*
*```js
mapviewer.cacheData('india_states', 'india-states.geojson')
*```
*/
MapViewer.prototype.cacheData = function (layerName, url) {
var self = this
switch (typeof (url)) {
......@@ -94,10 +96,12 @@ MapViewer.prototype.cacheData = function (layerName, url) {
}()
}
}
// * @method _saveLayer(<Layer> layer, <String> layerName ): this
// * Adds a layer to gLayers object, and
// *
// * Private method
/*
* @method _saveLayer(<Layer> layer, <String> layerName ): this
* Adds a layer to gLayers object, and
*
* Private method
*/
MapViewer.prototype._saveLayer = function (layerName, layer) {
var self = this, allLayersLoaded = true
self.gLayers[layerName] = layer
......@@ -172,7 +176,7 @@ MapViewer.prototype.buildLayer = function (layerName, layerConfig) {
gLayer = L.tileLayer(layerConfig.url, layerConfig.options)
this._saveLayer(layerName, gLayer)
break
// TODO: remove duplicate code topojson and geojson
case 'geojson':
case 'topojson':
self.cacheData(layerName, layerConfig[dataOrURL(layerConfig)]).then(function (mapJSON) {
gLayer = new L.TopoJSON(mapJSON, layerConfig.options)
......@@ -189,22 +193,6 @@ MapViewer.prototype.buildLayer = function (layerName, layerConfig) {
}
})
break
case 'geojson':
self.cacheData(layerName, layerConfig[dataOrURL(layerConfig)]).then(function (mapJSON) {
gLayer = L.geoJSON(mapJSON, layerConfig.options)
self._saveLayer(layerName, gLayer)
if ('link' in layerConfig) {
self.cacheData(layerName, layerConfig.link[dataOrURL(layerConfig.link)]).then(function (tableData) {
self.mergeData(mapJSON, tableData, layerConfig.link.mapKey, layerConfig.link.dataKey)
self.fitToLayer(layerName)
if ('attrs' in layerConfig) self._choropleth(layerName)
})
} else {
if ('attrs' in layerConfig) self._choropleth(layerName)
self.fitToLayer(layerName)
}
})
break
// TODO: remove duplicate code from marker and circkeMarker
case 'marker':
var pointLayers = []
......@@ -254,7 +242,7 @@ MapViewer.prototype._choropleth = function (layerName) {
layer.eachLayer(function (sublayer) {
var style = {}, prop, metricFormula, metric, domain
for (prop in self.options.layers[layerName].attrs) {
if(prop.toLowerCase() == 'tooltip') continue
if (prop.toLowerCase() == 'tooltip') continue
metric = self.options.layers[layerName].attrs[prop].metric
if (typeof (metric) === 'string')
metricFormula = (row) => row[metric]
......@@ -295,14 +283,16 @@ MapViewer.prototype._calculateMinMax = function (layer, metricFormula) {
})
return [minVal, maxVal]
}
// * @method fitToLayer(<String> layerName ): this
// * Zooms map view to fit the layer
// *
// * @example
// *
// *```js
// mapviewer.fitToLayer('indiaGeojson')
// *```
/*
* @method fitToLayer(<String> layerName ): this
* Zooms map view to fit the layer
*
* @example
*
*```js
mapviewer.fitToLayer('indiaGeojson')
*```
*/
MapViewer.prototype.fitToLayer = function (layerName, options = {}) {
this.map.fitBounds(this.gLayers[layerName].getBounds(), options)
}
......@@ -341,7 +331,8 @@ L.TopoJSON = L.GeoJSON.extend({
L.GeoJSON.prototype.addData.call(this, geojson)
}
else
throw new Error('json data is not of TopoJSON type')
L.GeoJSON.prototype.addData.call(this, jsonData)
return this
}
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment