Skip to content

Commit

Permalink
Merge pull request #750 from realtymaps/dev/nem/geoJsonUnify
Browse files Browse the repository at this point in the history
geojson unification of event handling
  • Loading branch information
Joe Ibershoff committed May 13, 2015
2 parents 495eddb + dfb66bb commit 816a633
Show file tree
Hide file tree
Showing 14 changed files with 823 additions and 667 deletions.
486 changes: 266 additions & 220 deletions dist/angular-leaflet-directive.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/angular-leaflet-directive.min.js

Large diffs are not rendered by default.

482 changes: 264 additions & 218 deletions dist/angular-leaflet-directive_dev_mapped.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/angular-leaflet-directive_dev_mapped.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions examples/0125-basic-geojson-nested-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
geojson:{}
});

// Mouse over function, called from the Leaflet Map Events
var countryMouseover = function (feature, leafletEvent) {
var layer = leafletEvent.target;
layer.setStyle({
weight: 2,
color: '#666',
fillColor: 'white'
});
layer.bringToFront();
};

$scope.$on("leafletDirectiveGeoJson.mouseover", function(ev, leafletPayload) {
countryMouseover(leafletPayload.leafletObject.feature, leafletPayload.leafletEvent);
});


$scope.centerJSON = function(name) {
leafletData.getMap().then(function(map) {
window.leafletMap = map;
Expand All @@ -47,6 +63,7 @@
angular.extend($scope.geojson, {
japan: {
data: data,
resetStyleOnMouseout: true,
style: {
fillColor: "green",
weight: 2,
Expand All @@ -62,6 +79,7 @@
angular.extend($scope.geojson, {
usa:{
data: data,
resetStyleOnMouseout: true,
style: {
fillColor: "blue",
weight: 2,
Expand Down
2 changes: 1 addition & 1 deletion examples/0512-markers-events-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/leaflet/dist/leaflet.js"></script>
<script src="../dist/angular-leaflet-directive.min.js"></script>
<script src="../dist/angular-leaflet-directive_dev_mapped.js"></script>
<link rel="stylesheet" href="../bower_components/leaflet/dist/leaflet.css" />
<script>
var app = angular.module("demoapp", ["leaflet-directive"]);
Expand Down
11 changes: 6 additions & 5 deletions examples/0601-mixed-geojson-events-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/leaflet/dist/leaflet.js"></script>
<script src="../dist/angular-leaflet-directive.min.js"></script>
<script src="../dist/angular-leaflet-directive_dev_mapped.js"></script>
<link rel="stylesheet" href="../bower_components/leaflet/dist/leaflet.css" />
<link rel="stylesheet" type="text/css" href="http://cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" />
<style>
Expand Down Expand Up @@ -65,12 +65,12 @@
var app = angular.module("demoapp", ["leaflet-directive"]);
app.controller('MixedGeoJSONEventsController', [ "$scope", "$http", function($scope, $http) {

$scope.$on("leafletDirectiveMap.geojsonMouseover", function(ev, feature, leafletEvent) {
countryMouseover(feature, leafletEvent);
$scope.$on("leafletDirectiveGeoJson.mouseover", function(ev, leafletPayload) {
countryMouseover(leafletPayload.leafletObject.feature, leafletPayload.leafletEvent);
});

$scope.$on("leafletDirectiveMap.geojsonClick", function(ev, featureSelected, leafletEvent) {
countryClick(featureSelected, leafletEvent);
$scope.$on("leafletDirectiveGeoJson.click", function(ev, leafletPayload) {
countryClick(leafletPayload.leafletObject, leafletPayload.leafletEvent);
});

var continentProperties= {
Expand Down Expand Up @@ -109,6 +109,7 @@
});

function countryClick(country, event) {
country = country.feature;
console.log(country.properties.name);
}

Expand Down
36 changes: 9 additions & 27 deletions src/directives/geojson.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
angular.module("leaflet-directive")
.directive('geojson', function ($log, $rootScope, leafletData, leafletHelpers,
leafletWatchHelpers, leafletDirectiveControlsHelpers,leafletIterators) {
leafletWatchHelpers, leafletDirectiveControlsHelpers,leafletIterators,
leafletGeoJsonEvents) {

var _maybeWatch = leafletWatchHelpers.maybeWatch,
_watchOptions = leafletHelpers.watchOptions,
Expand All @@ -15,17 +16,15 @@ angular.module("leaflet-directive")
require: 'leaflet',

link: function(scope, element, attrs, controller) {
var safeApply = leafletHelpers.safeApply,
isDefined = leafletHelpers.isDefined,
var isDefined = leafletHelpers.isDefined,
leafletScope = controller.getLeafletScope(),
leafletGeoJSON = {},
_hasSetLeafletData = false;

controller.getMap().then(function(map) {
var watchOptions = leafletScope.geojsonWatchOptions || _watchOptions;

var _hookUpEvents = function(geojson){
var resetStyleOnMouseout = geojson.resetStyleOnMouseout;
var _hookUpEvents = function(geojson, maybeName){
var onEachFeature;

if (angular.isFunction(geojson.onEachFeature)) {
Expand All @@ -36,27 +35,10 @@ angular.module("leaflet-directive")
layer.bindLabel(feature.properties.description);
}

layer.on({
mouseover: function(e) {
safeApply(leafletScope, function() {
$rootScope.$broadcast('leafletDirectiveMap.geojsonMouseover', feature, e);
});
},
mouseout: function(e) {
if (resetStyleOnMouseout) {
//this is broken on nested needs to traverse
leafletGeoJSON.resetStyle(e.target);
}
safeApply(leafletScope, function() {
$rootScope.$broadcast('leafletDirectiveMap.geojsonMouseout', e);
});
},
click: function(e) {
safeApply(leafletScope, function() {
$rootScope.$broadcast('leafletDirectiveMap.geojsonClick', feature, e);
});
}
});
leafletGeoJsonEvents.bindEvents(layer, null, feature,
leafletScope, maybeName,
{resetStyleOnMouseout: geojson.resetStyleOnMouseout,
mapId: attrs.id});
};
}
return onEachFeature;
Expand Down Expand Up @@ -87,7 +69,7 @@ angular.module("leaflet-directive")
if (!(isDefined(geojson) && isDefined(geojson.data))) {
return;
}
var onEachFeature = _hookUpEvents(geojson);
var onEachFeature = _hookUpEvents(geojson, maybeName);

if (!isDefined(geojson.options)) {
//right here is why we use a clone / copy (we modify and thus)
Expand Down
5 changes: 2 additions & 3 deletions src/directives/markers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module("leaflet-directive").directive('markers',
function ($log, $rootScope, $q, leafletData, leafletHelpers, leafletMapDefaults,
leafletMarkersHelpers, leafletEvents, leafletIterators, leafletWatchHelpers,
leafletMarkersHelpers, leafletMarkerEvents, leafletIterators, leafletWatchHelpers,
leafletDirectiveControlsHelpers) {
//less terse vars to helpers
var isDefined = leafletHelpers.isDefined,
Expand All @@ -10,7 +10,6 @@ angular.module("leaflet-directive").directive('markers',
addMarkerWatcher = leafletMarkersHelpers.addMarkerWatcher,
listenMarkerEvents = leafletMarkersHelpers.listenMarkerEvents,
addMarkerToGroup = leafletMarkersHelpers.addMarkerToGroup,
bindMarkerEvents = leafletEvents.bindMarkerEvents,
createMarker = leafletMarkersHelpers.createMarker,
deleteMarker = leafletMarkersHelpers.deleteMarker,
$it = leafletIterators,
Expand Down Expand Up @@ -111,7 +110,7 @@ angular.module("leaflet-directive").directive('markers',
}

listenMarkerEvents(marker, model, leafletScope, watchOptions.individual.doWatch);
bindMarkerEvents(marker, pathToMarker, model, leafletScope, layerName);
leafletMarkerEvents.bindEvents(marker, pathToMarker, model, leafletScope, layerName);
}
}
};
Expand Down
138 changes: 131 additions & 7 deletions src/services/events/leafletEventsHelpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
angular.module("leaflet-directive")
.factory('leafletEventsHelpers', function ($rootScope, $q, $log, leafletHelpers) {
.factory('leafletEventsHelpersFactory', function ($rootScope, $q, $log, leafletHelpers) {
var safeApply = leafletHelpers.safeApply,
isDefined = leafletHelpers.isDefined;
isDefined = leafletHelpers.isDefined,
isObject = leafletHelpers.isObject,
isArray = leafletHelpers.isArray;

var _fire = function(scope, broadcastName, logic, event, lObject, model, modelName, layerName){
var EventsHelper = function(rootBroadcastName, lObjectType){
this.rootBroadcastName = rootBroadcastName;
//used to path/key out certain properties based on the type , "markers", "geojson"
this.lObjectType = lObjectType;
};

EventsHelper.prototype.getAvailableEvents = function(){return []};

/*
argument: name: Note this can be a single string or dot notation
Example:
markerModel : {
m1: { lat:_, lon: _}
}
//would yield name of
name = "m1"
If nested:
markerModel : {
cars: {
m1: { lat:_, lon: _}
}
}
//would yield name of
name = "cars.m1"
*/
EventsHelper.prototype.genDispatchEvent = function(eventName, logic, leafletScope, lObject, name, model, layerName, extra) {
var _this = this;
return function (e) {
var broadcastName = _this.rootBroadcastName + '.' + eventName;
_this.fire(leafletScope, broadcastName, logic, e, e.target || lObject, model, name, layerName, extra);
};
};

EventsHelper.prototype.fire = function(scope, broadcastName, logic, event, lObject, model, modelName, layerName, extra){
// Safely broadcast the event
safeApply(scope, function(){
var toSend = {
Expand All @@ -16,14 +52,102 @@ angular.module("leaflet-directive")
angular.extend(toSend, {layerName: layerName});

if (logic === "emit") {
scope.$emit(broadcastName, toSend);
scope.$emit(broadcastName, toSend);
} else {
$rootScope.$broadcast(broadcastName, toSend);
}
});
};

return {
fire: _fire
}
EventsHelper.prototype.bindEvents = function (lObject, name, model, leafletScope, layerName, extra) {
var events = [];
var logic = 'emit';
var _this = this;

if (!isDefined(leafletScope.eventBroadcast)) {
// Backward compatibility, if no event-broadcast attribute, all events are broadcasted
events = this.getAvailableEvents();
} else if (!isObject(leafletScope.eventBroadcast)) {
// Not a valid object
$log.error(errorHeader + "event-broadcast must be an object check your model.");
} else {
// We have a possible valid object
if (!isDefined(leafletScope.eventBroadcast[_this.lObjectType])) {
// We do not have events enable/disable do we do nothing (all enabled by default)
events = this.getAvailableEvents();
} else if (!isObject(leafletScope.eventBroadcast[_this.lObjectType])) {
// Not a valid object
$log.warn(errorHeader + 'event-broadcast.' + [_this.lObjectType] + ' must be an object check your model.');
} else {
// We have a possible valid map object
// Event propadation logic
if (isDefined(leafletScope.eventBroadcast[this.lObjectType].logic)) {
// We take care of possible propagation logic
if (leafletScope.eventBroadcast[_this.lObjectType].logic !== "emit" &&
leafletScope.eventBroadcast[_this.lObjectType].logic !== "broadcast")
$log.warn(errorHeader + "Available event propagation logic are: 'emit' or 'broadcast'.");
}
// Enable / Disable
var eventsEnable = false, eventsDisable = false;
if (isDefined(leafletScope.eventBroadcast[_this.lObjectType].enable) &&
isArray(leafletScope.eventBroadcast[_this.lObjectType].enable))
eventsEnable = true;
if (isDefined(leafletScope.eventBroadcast[_this.lObjectType].disable) &&
isArray(leafletScope.eventBroadcast[_this.lObjectType].disable))
eventsDisable = true;

if (eventsEnable && eventsDisable) {
// Both are active, this is an error
$log.warn(errorHeader + "can not enable and disable events at the same time");
} else if (!eventsEnable && !eventsDisable) {
// Both are inactive, this is an error
$log.warn(errorHeader + "must enable or disable events");
} else {
// At this point the object is OK, lets enable or disable events
if (eventsEnable) {
// Enable events
leafletScope.eventBroadcast[this.lObjectType].enable.forEach(function(eventName){
// Do we have already the event enabled?
if (events.indexOf(eventName) !== -1) {
// Repeated event, this is an error
$log.warn(errorHeader + "This event " + eventName + " is already enabled");
} else {
// Does the event exists?
if (_this.getAvailableEvents().indexOf(eventName) === -1) {
// The event does not exists, this is an error
$log.warn(errorHeader + "This event " + eventName + " does not exist");
} else {
// All ok enable the event
events.push(eventName);
}
}
});
} else {
// Disable events
events = this.getAvailableEvents();
leafletScope.eventBroadcast[_this.lObjectType].disable.forEach(function(eventName) {
var index = events.indexOf(eventName);
if (index === -1) {
// The event does not exist
$log.warn(errorHeader + "This event " + eventName + " does not exist or has been already disabled");

} else {
events.splice(index, 1);
}
});
}
}
}
}

events.forEach(function(eventName){
lObject.on(eventName,_this.genDispatchEvent(eventName, logic, leafletScope, lObject, name, model, layerName, extra));
});
return logic;
};

return EventsHelper;
})
.service('leafletEventsHelpers', function(leafletEventsHelpersFactory){
return new leafletEventsHelpersFactory();
});
50 changes: 50 additions & 0 deletions src/services/events/leafletGeoJsonEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
angular.module("leaflet-directive")
.factory('leafletGeoJsonEvents', function ($rootScope, $q, $log, leafletHelpers,
leafletEventsHelpersFactory, leafletLabelEvents, leafletData) {
var safeApply = leafletHelpers.safeApply,
isDefined = leafletHelpers.isDefined,
Helpers = leafletHelpers,
lblHelp = leafletLabelEvents,
EventsHelper = leafletEventsHelpersFactory;


var GeoJsonEvents = function(){
EventsHelper.call(this,'leafletDirectiveGeoJson', 'geojson');
};

GeoJsonEvents.prototype = new EventsHelper();


GeoJsonEvents.prototype.genDispatchEvent = function(eventName, logic, leafletScope, lObject, name, model, layerName, extra) {
var base = EventsHelper.prototype.genDispatchEvent.call(this, eventName, logic, leafletScope, lObject, name, model, layerName),
_this = this;

return function(e){
if (eventName === 'mouseout') {
if (extra.resetStyleOnMouseout) {
leafletData.getGeoJSON(extra.mapId)
.then(function(leafletGeoJSON){
//this is broken on nested needs to traverse or user layerName (nested)
var lobj = layerName? leafletGeoJSON[layerName]: leafletGeoJSON;
lobj.resetStyle(e.target);
});

}
safeApply(leafletScope, function() {
$rootScope.$broadcast(_this.rootBroadcastName + '.mouseout', e);
});
}
base(e); //common
};
};

GeoJsonEvents.prototype.getAvailableEvents = function(){ return [
'click',
'dblclick',
'mouseover',
'mouseout',
];
};

return new GeoJsonEvents();
});
Loading

0 comments on commit 816a633

Please sign in to comment.