ip_geoloc-2.0.0-alpha0/js/ip_geoloc_leaflet_tagged_marker.js
js/ip_geoloc_leaflet_tagged_marker.js
/* This thing is getting a bit big and complicated... time to refactor! */
(function ($, Drupal) {
Drupal.leaflet._create_point_orig = Drupal.leaflet.create_point;
Drupal.leaflet.create_point = function(marker) {
// Follow create_point()
var latLng = new L.LatLng(marker.lat, marker.lon);
this.bounds.push(latLng);
if (!marker.tag) {
// Handle cases where no tag is required and icon is default or none.
if (marker.icon == false) {
// No marker. Need to create an icon "stub" or we'll have no map at all!
var stub = new L.Icon({iconUrl: '//'});
return new L.Marker(latLng, {icon: stub, title: marker.tooltip});
}
if (!marker.icon) {
// Marker with default img, without tag.
// Note: marker.specialChar cannot be handled in this case and is ignored.
return new L.Marker(latLng, {title: marker.tooltip});
}
}
if (marker.icon == false) {
// Marker without img, but with tag. marker.specialChar does not apply.
var divIcon = new L.DivIcon({html: marker.tag, className: marker.cssClass});
// Prevent div style tag being set, so that upper left corner becomes anchor.
divIcon.options.iconSize = null;
return new L.Marker(latLng, {icon: divIcon, title: marker.tooltip});
}
if (marker.tag && !marker.icon) {
// Use default img, custom tag the marker.
var tagged_icon = new L.Icon.Tagged(marker.tag, marker.specialChar, {className: marker.cssClass, specialCharClass: marker.special_char_class});
return new L.Marker(latLng, {icon: tagged_icon, title: marker.tooltip});
}
// Custom img and custom tag or specialChar.
var icon = marker.tag || marker.specialChar || marker.specialCharClass
? new L.Icon.Tagged(marker.tag, marker.specialChar, {iconUrl: marker.icon.iconUrl, className: marker.cssClass, specialCharClass: marker.specialCharClass})
: new L.Icon({iconUrl: marker.icon.iconUrl});
// All of the below is like create_point (leaflet.drupal.js), but with tooltip.
if (marker.icon.iconSize) {
icon.options.iconSize = new L.Point(parseInt(marker.icon.iconSize.x), parseInt(marker.icon.iconSize.y));
}
if (marker.icon.iconAnchor) {
icon.options.iconAnchor = new L.Point(parseFloat(marker.icon.iconAnchor.x), parseFloat(marker.icon.iconAnchor.y));
}
if (marker.icon.popupAnchor) {
icon.options.popupAnchor = new L.Point(parseFloat(marker.icon.popupAnchor.x), parseFloat(marker.icon.popupAnchor.y));
}
if (marker.icon.shadowUrl !== undefined) {
icon.options.shadowUrl = marker.icon.shadowUrl;
}
if (marker.icon.shadowSize) {
icon.options.shadowSize = new L.Point(parseInt(marker.icon.shadowSize.x), parseInt(marker.icon.shadowSize.y));
}
if (marker.icon.shadowAnchor) {
icon.options.shadowAnchor = new L.Point(parseInt(marker.icon.shadowAnchor.x), parseInt(marker.icon.shadowAnchor.y));
}
return new L.Marker(latLng, {icon: icon, title: marker.tooltip});
};
})(jQuery, Drupal);
L.Icon.Tagged = L.Icon.extend({
initialize: function (tag, specialChar, options) {
L.Icon.prototype.initialize.apply(this, [options]);
this._tag = tag;
this._specialChar = specialChar;
},
// Create an icon as per normal, but wrap it in an outerdiv together with the tag.
createIcon: function() {
if (!this.options.iconUrl) {
var iconDefault = new L.Icon.Default();
this.options.iconUrl = iconDefault._getIconUrl('icon');
this.options.iconSize = iconDefault.options.iconSize;
this.options.iconAnchor = iconDefault.options.iconAnchor;
// Does this work?
this.options.popupAnchor = iconDefault.options.popupAnchor;
this.options.shadowSize = iconDefault.options.shadowSize;
}
var outer = document.createElement('div');
outer.setAttribute('class', 'leaflet-tagged-marker');
// The order of appending img, div and i makes little difference.
var img = this._createIcon('icon');
outer.appendChild(img);
if (this._specialChar || this.options.specialCharClass) {
// Convention seems to be to use the i element.
// Other elements like div and span work also, just make sure that
// display:block is set implicitly or explictly.
var specialChar = document.createElement('i');
specialChar.innerHTML = this._specialChar ? this._specialChar.trim() : '';
// Note: for Font Awesome we must have a class starting with "icon-"
if (this.options.specialCharClass) {
specialChar.setAttribute('class', this.options.specialCharClass);
}
outer.appendChild(specialChar);
}
if (this._tag) {
var tag = document.createElement('div');
tag.innerHTML = this._tag;
if (this.options.className) {
tag.setAttribute('class', this.options.className);
}
outer.appendChild(tag);
}
return outer;
},
createShadow: function() {
return this._createIcon('shadow');
}
});
