I have come to a point where I have been wondering if I can extend a module. Again, I think it's my javascript skills that are letting me down here. I don't want to instantiate a duplicate via the Kernel.register, as I want to override all the listeners.
Kernel.module.define('InspectionItemsMap', {
renderTo: "#inspection-items-map",
map: {},
geojsonLayer: [],
display: true,
layers: {},
mapElementName: function () {
var self = this;
var mapElementName = self.renderTo.substr(1, self.renderTo.length);
return mapElementName;
},
hideButtonName: function () {
var name = "hide-" + this.mapElementName() + "-button";
return name;
},
init: function () {
var self = this;
if (self.display === false) {
hub.quickHide(self.renderTo);
}
self.map = self.createMap();
$("#" + self.hideButtonName()).click(function () {
hub.hide(self.renderTo);
});
hub.listen('show-inspections-on-map', function (inspections) {
//show the data
self.addGeoJsonAndZoomTo(inspections);
self.showMap();
});
hub.listen('show-map', function () {
self.showMap();
});
hub.listen('clear-inspection-map', function () {
self.clearMap();
});
},
refreshMap: function () {
var self = this;
//this is a hack to refresh the map because of the dynamic nature of it's loading...
L.Util.requestAnimFrame(self.map.invalidateSize, self.map, !1, self.map._container);
},
showMap: function () {
var self = this;
hub.show(self.renderTo);
self.refreshMap();
},
addGeoJsonAndZoomTo: function (geoJson) {
var self = this;
var layer = self.layers.geojson;
var map = self.map;
self.clearMap();
layer.addData(geoJson);
map.addLayer(layer);
var bounds = layer.getBounds();
map.fitBounds(bounds);
self.refreshMap();
},
clearMap: function () {
var self = this;
var map = self.map;
var layer = self.layers.geojson;
//if we have the geojsonlayer, clear it!
if (typeof (layer !== "undefined")) {
layer.clearLayers();
map.removeLayer(layer);
}
},
createMap: function () {
var self = this;
self.createLayers();
var map = L.map(self.mapElementName());
map.addLayer(self.layers.lur);
map.addLayer(self.layers.mapquest);
map.addLayer(self.layers.geojson);
map.setView([-37.9771, 143.7560], 10);
self.addHideControl(map);
return map;
},
onEachFeature: function (feature, layer) {
// does this feature have a property named popupContent?
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);
}
},
createLayers: function () {
var self = this;
maxZoom: 18,
subdomains: ["1", "2", "3", "4"]
});
var lur = new L.TileLayer.WMS(geoserver, {
layers: 'MIDWAY:LUR',
format: 'image/png',
transparent: true,
TILED: true
});
var geojsonLayer = L.geoJson([], { onEachFeature: self.onEachFeature });
self.layers = { "lur": lur, "mapquest": mapquest, "geojson": geojsonLayer };
},
addHideControl: function (map) {
//TODO: template this. It's nasty.
var self = this;
var hideControl = L.control({ position: 'topright' });
hideControl.onAdd = function (map) {
var div = L.DomUtil.create('div', 'hide-map');
div.innerHTML += '<button id= "' + self.hideButtonName() + '" class="btn">Hide Map</button>'
return div;
};
hideControl.addTo(map);
},
render: function () {
//my rendering magic in here...not really needed though, leaflet handles all that itself
}
});
Does this module look ok? I can see some of these types of modules, particularly map based ones getting quite large as far as functions go, do you have tips for separating them out? Do these separations get pulled out into other classes (or whatever they are called in js), or if they are being reused, attached onto an extension of the kernel?