Events.js (Leaflet-1.8.0) | : | Events.js (Leaflet-1.9.0) | ||
---|---|---|---|---|
skipping to change at line 98 | skipping to change at line 98 | |||
} else { | } else { | |||
this._off(types[i], fn, context); | this._off(types[i], fn, context); | |||
} | } | |||
} | } | |||
} | } | |||
return this; | return this; | |||
}, | }, | |||
// attach listener (without syntactic sugar now) | // attach listener (without syntactic sugar now) | |||
_on: function (type, fn, context) { | _on: function (type, fn, context, _once) { | |||
if (typeof fn !== 'function') { | if (typeof fn !== 'function') { | |||
console.warn('wrong listener type: ' + typeof fn); | console.warn('wrong listener type: ' + typeof fn); | |||
return; | return; | |||
} | } | |||
this._events = this._events || {}; | ||||
/* get/init listeners for type */ | // check if fn already there | |||
var typeListeners = this._events[type]; | if (this._listens(type, fn, context) !== false) { | |||
if (!typeListeners) { | return; | |||
typeListeners = []; | ||||
this._events[type] = typeListeners; | ||||
} | } | |||
if (context === this) { | if (context === this) { | |||
// Less memory footprint. | // Less memory footprint. | |||
context = undefined; | context = undefined; | |||
} | } | |||
var newListener = {fn: fn, ctx: context}, | ||||
listeners = typeListeners; | ||||
// check if fn already there | var newListener = {fn: fn, ctx: context}; | |||
for (var i = 0, len = listeners.length; i < len; i++) { | if (_once) { | |||
if (listeners[i].fn === fn && listeners[i].ctx === contex | newListener.once = true; | |||
t) { | ||||
return; | ||||
} | ||||
} | } | |||
listeners.push(newListener); | this._events = this._events || {}; | |||
this._events[type] = this._events[type] || []; | ||||
this._events[type].push(newListener); | ||||
}, | }, | |||
_off: function (type, fn, context) { | _off: function (type, fn, context) { | |||
var listeners, | var listeners, | |||
i, | i, | |||
len; | len; | |||
if (!this._events) { return; } | if (!this._events) { | |||
return; | ||||
} | ||||
listeners = this._events[type]; | listeners = this._events[type]; | |||
if (!listeners) { | if (!listeners) { | |||
return; | return; | |||
} | } | |||
if (arguments.length === 1) { // remove all | if (arguments.length === 1) { // remove all | |||
if (this._firingCount) { | if (this._firingCount) { | |||
// Set all removed listeners to noop | // Set all removed listeners to noop | |||
// so they are not called if remove happens in fi re | // so they are not called if remove happens in fi re | |||
for (i = 0, len = listeners.length; i < len; i++) { | for (i = 0, len = listeners.length; i < len; i++) { | |||
listeners[i].fn = Util.falseFn; | listeners[i].fn = Util.falseFn; | |||
} | } | |||
} | } | |||
// clear all listeners for a type if function isn't speci fied | // clear all listeners for a type if function isn't speci fied | |||
delete this._events[type]; | delete this._events[type]; | |||
return; | return; | |||
} | } | |||
if (context === this) { | ||||
context = undefined; | ||||
} | ||||
if (typeof fn !== 'function') { | if (typeof fn !== 'function') { | |||
console.warn('wrong listener type: ' + typeof fn); | console.warn('wrong listener type: ' + typeof fn); | |||
return; | return; | |||
} | } | |||
// find fn and remove it | ||||
for (i = 0, len = listeners.length; i < len; i++) { | ||||
var l = listeners[i]; | ||||
if (l.ctx !== context) { continue; } | ||||
if (l.fn === fn) { | ||||
if (this._firingCount) { | ||||
// set the removed listener to noop so th | ||||
at's not called if remove happens in fire | ||||
l.fn = Util.falseFn; | ||||
/* copy array in case events are being fi | // find fn and remove it | |||
red */ | var index = this._listens(type, fn, context); | |||
this._events[type] = listeners = listener | if (index !== false) { | |||
s.slice(); | var listener = listeners[index]; | |||
} | if (this._firingCount) { | |||
listeners.splice(i, 1); | // set the removed listener to noop so that's not | |||
called if remove happens in fire | ||||
listener.fn = Util.falseFn; | ||||
return; | /* copy array in case events are being fired */ | |||
this._events[type] = listeners = listeners.slice( | ||||
); | ||||
} | } | |||
listeners.splice(index, 1); | ||||
} | } | |||
console.warn('listener not found'); | ||||
}, | }, | |||
// @method fire(type: String, data?: Object, propagate?: Boolean): this | // @method fire(type: String, data?: Object, propagate?: Boolean): this | |||
// Fires an event of the specified type. You can optionally provide a dat a | // Fires an event of the specified type. You can optionally provide a dat a | |||
// object — the first argument of the listener function will contain its | // object — the first argument of the listener function will contain its | |||
// properties. The event can optionally be propagated to event parents. | // properties. The event can optionally be propagated to event parents. | |||
fire: function (type, data, propagate) { | fire: function (type, data, propagate) { | |||
if (!this.listens(type, propagate)) { return this; } | if (!this.listens(type, propagate)) { return this; } | |||
var event = Util.extend({}, data, { | var event = Util.extend({}, data, { | |||
type: type, | type: type, | |||
target: this, | target: this, | |||
sourceTarget: data && data.sourceTarget || this | sourceTarget: data && data.sourceTarget || this | |||
}); | }); | |||
if (this._events) { | if (this._events) { | |||
var listeners = this._events[type]; | var listeners = this._events[type]; | |||
if (listeners) { | if (listeners) { | |||
this._firingCount = (this._firingCount + 1) || 1; | this._firingCount = (this._firingCount + 1) || 1; | |||
for (var i = 0, len = listeners.length; i < len; i++) { | for (var i = 0, len = listeners.length; i < len; i++) { | |||
var l = listeners[i]; | var l = listeners[i]; | |||
l.fn.call(l.ctx || this, event); | // off overwrites l.fn, so we need to cop | |||
y fn to a var | ||||
var fn = l.fn; | ||||
if (l.once) { | ||||
this.off(type, fn, l.ctx); | ||||
} | ||||
fn.call(l.ctx || this, event); | ||||
} | } | |||
this._firingCount--; | this._firingCount--; | |||
} | } | |||
} | } | |||
if (propagate) { | if (propagate) { | |||
// propagate the event to parents (set with addEventParen t) | // propagate the event to parents (set with addEventParen t) | |||
this._propagateEvent(event); | this._propagateEvent(event); | |||
} | } | |||
return this; | return this; | |||
}, | }, | |||
// @method listens(type: String, propagate?: Boolean): Boolean | // @method listens(type: String, propagate?: Boolean): Boolean | |||
// @method listens(type: String, fn: Function, context?: Object, propagat e?: Boolean): Boolean | ||||
// Returns `true` if a particular event type has any listeners attached t o it. | // Returns `true` if a particular event type has any listeners attached t o it. | |||
// The verification can optionally be propagated, it will return `true` i f parents have the listener attached to it. | // The verification can optionally be propagated, it will return `true` i f parents have the listener attached to it. | |||
listens: function (type, propagate) { | listens: function (type, fn, context, propagate) { | |||
if (typeof type !== 'string') { | if (typeof type !== 'string') { | |||
console.warn('"string" type argument expected'); | console.warn('"string" type argument expected'); | |||
} | } | |||
if (typeof fn !== 'function') { | ||||
propagate = !!fn; | ||||
fn = undefined; | ||||
context = undefined; | ||||
} | ||||
var listeners = this._events && this._events[type]; | var listeners = this._events && this._events[type]; | |||
if (listeners && listeners.length) { return true; } | if (listeners && listeners.length) { | |||
if (this._listens(type, fn, context) !== false) { | ||||
return true; | ||||
} | ||||
} | ||||
if (propagate) { | if (propagate) { | |||
// also check parents for listeners if event propagates | // also check parents for listeners if event propagates | |||
for (var id in this._eventParents) { | for (var id in this._eventParents) { | |||
if (this._eventParents[id].listens(type, propagat | if (this._eventParents[id].listens(type, fn, cont | |||
e)) { return true; } | ext, propagate)) { return true; } | |||
} | ||||
} | ||||
return false; | ||||
}, | ||||
// returns the index (number) or false | ||||
_listens: function (type, fn, context) { | ||||
if (!this._events) { | ||||
return false; | ||||
} | ||||
var listeners = this._events[type] || []; | ||||
if (!fn) { | ||||
return !!listeners.length; | ||||
} | ||||
if (context === this) { | ||||
// Less memory footprint. | ||||
context = undefined; | ||||
} | ||||
for (var i = 0, len = listeners.length; i < len; i++) { | ||||
if (listeners[i].fn === fn && listeners[i].ctx === contex | ||||
t) { | ||||
return i; | ||||
} | } | |||
} | } | |||
return false; | return false; | |||
}, | }, | |||
// @method once(…): this | // @method once(…): this | |||
// Behaves as [`on(…)`](#evented-on), except the listener will only get f ired once and then removed. | // Behaves as [`on(…)`](#evented-on), except the listener will only get f ired once and then removed. | |||
once: function (types, fn, context) { | once: function (types, fn, context) { | |||
// types can be a map of types/handlers | ||||
if (typeof types === 'object') { | if (typeof types === 'object') { | |||
for (var type in types) { | for (var type in types) { | |||
this.once(type, types[type], fn); | // we don't process space-separated events here f | |||
or performance; | ||||
// it's a hot path since Layer uses the on(obj) s | ||||
yntax | ||||
this._on(type, types[type], fn, true); | ||||
} | } | |||
return this; | ||||
} | ||||
var handler = Util.bind(function () { | } else { | |||
this | // types can be a string of space-separated words | |||
.off(types, fn, context) | types = Util.splitWords(types); | |||
.off(types, handler, context); | ||||
}, this); | for (var i = 0, len = types.length; i < len; i++) { | |||
this._on(types[i], fn, context, true); | ||||
} | ||||
} | ||||
// add a listener that's executed once and removed after that | return this; | |||
return this | ||||
.on(types, fn, context) | ||||
.on(types, handler, context); | ||||
}, | }, | |||
// @method addEventParent(obj: Evented): this | // @method addEventParent(obj: Evented): this | |||
// Adds an event parent - an `Evented` that will receive propagated event s | // Adds an event parent - an `Evented` that will receive propagated event s | |||
addEventParent: function (obj) { | addEventParent: function (obj) { | |||
this._eventParents = this._eventParents || {}; | this._eventParents = this._eventParents || {}; | |||
this._eventParents[Util.stamp(obj)] = obj; | this._eventParents[Util.stamp(obj)] = obj; | |||
return this; | return this; | |||
}, | }, | |||
End of changes. 27 change blocks. | ||||
57 lines changed or deleted | 89 lines changed or added |