demarches-normaliennes/app/assets/javascripts/api_carto/lib/easy-button.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

if (typeof L != 'undefined') {
L.Control.EasyButtons = L.Control.extend({
options: {
position: 'topleft',
title: '',
intendedIcon: 'fa-circle-o'
},
2015-08-10 11:05:06 +02:00
onAdd: function () {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control');
2015-08-10 11:05:06 +02:00
this.link = L.DomUtil.create('a', 'leaflet-bar-part', container);
this._addImage()
this.link.href = '#';
2015-08-10 11:05:06 +02:00
L.DomEvent.on(this.link, 'click', this._click, this);
this.link.title = this.options.title;
2015-08-10 11:05:06 +02:00
return container;
},
2015-08-10 11:05:06 +02:00
intendedFunction: function(){ alert('no function selected');},
2015-08-10 11:05:06 +02:00
_click: function (e) {
L.DomEvent.stopPropagation(e);
L.DomEvent.preventDefault(e);
this.intendedFunction();
this.link.blur();
},
2015-08-10 11:05:06 +02:00
_addImage: function () {
var extraClasses = this.options.intendedIcon.lastIndexOf('fa', 0) === 0 ? ' fa fa-lg' : ' glyphicon';
2015-08-10 11:05:06 +02:00
var icon = L.DomUtil.create('i', this.options.intendedIcon + extraClasses, this.link);
icon.id = this.options.id;
}
});
2015-08-10 11:05:06 +02:00
L.easyButton = function( btnIcon , btnFunction , btnTitle , btnMap , btnId) {
var newControl = new L.Control.EasyButtons();
2015-08-10 11:05:06 +02:00
if (btnIcon) newControl.options.intendedIcon = btnIcon;
if (btnId) newControl.options.id = btnId;
2015-08-10 11:05:06 +02:00
if ( typeof btnFunction === 'function'){
newControl.intendedFunction = btnFunction;
}
2015-08-10 11:05:06 +02:00
if (btnTitle) newControl.options.title = btnTitle;
2015-08-10 11:05:06 +02:00
if ( btnMap === '' ){
// skip auto addition
} else if ( btnMap ) {
btnMap.addControl(newControl);
} else {
map.addControl(newControl);
}
return newControl;
};
}