Script and makefile adjustments for updating extlib
[larjonas-mediagoblin.git] / extlib / leaflet / src / core / Events.js
blob53ea20fad4ff711f3bca3b90733dbafa03a61502
1 /*
2 * L.Mixin.Events adds custom events functionality to Leaflet classes
3 */
5 L.Mixin = {};
7 L.Mixin.Events = {
8 addEventListener: function(/*String*/ type, /*Function*/ fn, /*(optional) Object*/ context) {
9 var events = this._leaflet_events = this._leaflet_events || {};
10 events[type] = events[type] || [];
11 events[type].push({
12 action: fn,
13 context: context
14 });
15 return this;
18 hasEventListeners: function(/*String*/ type) /*-> Boolean*/ {
19 var k = '_leaflet_events';
20 return (k in this) && (type in this[k]) && (this[k][type].length > 0);
23 removeEventListener: function(/*String*/ type, /*Function*/ fn, /*(optional) Object*/ context) {
24 if (!this.hasEventListeners(type)) { return this; }
26 for (var i = 0, events = this._leaflet_events, len = events[type].length; i < len; i++) {
27 if (
28 (events[type][i].action === fn) &&
29 (!context || (events[type][i].context === context))
30 ) {
31 events[type].splice(i, 1);
32 return this;
35 return this;
38 fireEvent: function(/*String*/ type, /*(optional) Object*/ data) {
39 if (!this.hasEventListeners(type)) { return; }
41 var event = L.Util.extend({
42 type: type,
43 target: this
44 }, data);
46 var listeners = this._leaflet_events[type].slice();
48 for (var i = 0, len = listeners.length; i < len; i++) {
49 listeners[i].action.call(listeners[i].context || this, event);
52 return this;
56 L.Mixin.Events.on = L.Mixin.Events.addEventListener;
57 L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
58 L.Mixin.Events.fire = L.Mixin.Events.fireEvent;