From 21cfd7f3d9948c90935bfee58a0b280ce52b69a3 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Tue, 17 Dec 2013 14:07:21 -0500 Subject: [PATCH] replace Events mixin with Evented inheritance --- spec/suites/core/EventsSpec.js | 56 +++++++++++++++------------------- src/dom/Draggable.js | 3 +- src/dom/PosAnimation.js | 3 +- src/layer/Layer.js | 3 +- src/map/Map.js | 4 +-- 5 files changed, 28 insertions(+), 41 deletions(-) diff --git a/spec/suites/core/EventsSpec.js b/spec/suites/core/EventsSpec.js index 69b130e0..ed948360 100644 --- a/spec/suites/core/EventsSpec.js +++ b/spec/suites/core/EventsSpec.js @@ -1,16 +1,9 @@ describe('Events', function () { - var Klass; - - beforeEach(function () { - Klass = L.Class.extend({ - includes: L.Mixin.Events - }); - }); describe('#fireEvent', function () { it('fires all listeners added through #addEventListener', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy1 = sinon.spy(), spy2 = sinon.spy(), spy3 = sinon.spy(), @@ -43,10 +36,10 @@ describe('Events', function () { }); it('provides event object to listeners and executes them in the right context', function () { - var obj = new Klass(), - obj2 = new Klass(), - obj3 = new Klass(), - obj4 = new Klass(), + var obj = new L.Evented(), + obj2 = new L.Evented(), + obj3 = new L.Evented(), + obj4 = new L.Evented(), foo = {}; function listener1(e) { @@ -89,7 +82,7 @@ describe('Events', function () { }); it('calls no listeners removed through #removeEventListener', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(), spy2 = sinon.spy(), spy3 = sinon.spy(), @@ -127,7 +120,7 @@ describe('Events', function () { }); it('can handle calls to #removeEventListener on objects with no registered event listeners', function () { - var obj = new Klass(); + var obj = new L.Evented(); var removeNonExistentListener = function () { obj.removeEventListener('test'); }; @@ -136,7 +129,7 @@ describe('Events', function () { // added due to context-sensitive removeListener optimization it('fires multiple listeners with the same context with id', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy1 = sinon.spy(), spy2 = sinon.spy(), foo = {}; @@ -153,7 +146,7 @@ describe('Events', function () { }); it('removes listeners with stamped contexts', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy1 = sinon.spy(), spy2 = sinon.spy(), foo = {}; @@ -172,7 +165,7 @@ describe('Events', function () { }); it('removes listeners with a stamp originally added without one', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy1 = sinon.spy(), spy2 = sinon.spy(), foo = {}; @@ -191,8 +184,8 @@ describe('Events', function () { }); it('removes listeners with context == this and a stamp originally added without one', function () { - var obj = new Klass(), - obj2 = new Klass(), + var obj = new L.Evented(), + obj2 = new L.Evented(), spy1 = sinon.spy(), spy2 = sinon.spy(), spy3 = sinon.spy(); @@ -214,7 +207,7 @@ describe('Events', function () { }); it('doesnt lose track of listeners when removing non existent ones', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(), spy2 = sinon.spy(), foo = {}, @@ -236,7 +229,7 @@ describe('Events', function () { }); it('correctly removes all listeners if given no fn', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(), foo = {}, foo2 = {}, @@ -257,7 +250,7 @@ describe('Events', function () { }); it('makes sure an event is not triggered if a listener is removed during dispatch', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(); obj.addEventListener('test', function () { obj.removeEventListener('test', spy); }); @@ -271,7 +264,7 @@ describe('Events', function () { describe('#on, #off & #fire', function () { it('works like #addEventListener && #removeEventListener', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(); obj.on('test', spy); @@ -290,14 +283,13 @@ describe('Events', function () { spy2 = sinon.spy(), spy3 = sinon.spy(); - var Klass2 = L.Class.extend({ - includes: L.Mixin.Events, + var Klass = L.Evented.extend({ on: spy1, off: spy2, fire: spy3 }); - var obj = new Klass2(); + var obj = new Klass(); obj.on(); expect(spy1.called).to.be(true); @@ -313,8 +305,8 @@ describe('Events', function () { describe("#clearEventListeners", function () { it("clears all registered listeners on an object", function () { var spy = sinon.spy(), - obj = new Klass(), - otherObj = new Klass(); + obj = new L.Evented(), + otherObj = new L.Evented(); obj.on('test', spy, obj); obj.on('testTwo', spy); @@ -329,7 +321,7 @@ describe('Events', function () { describe('#once', function () { it('removes event listeners after first trigger', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(); obj.once('test', spy, obj); @@ -343,7 +335,7 @@ describe('Events', function () { }); it('works with an object hash', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(), otherSpy = sinon.spy(); @@ -366,7 +358,7 @@ describe('Events', function () { }); it("doesn't call listeners to events that have been removed", function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(); obj.once('test', spy, obj); @@ -378,7 +370,7 @@ describe('Events', function () { }); it('works if called from a context that doesnt implement #Events', function () { - var obj = new Klass(), + var obj = new L.Evented(), spy = sinon.spy(), foo = {}; diff --git a/src/dom/Draggable.js b/src/dom/Draggable.js index d81790e8..5e62a38e 100644 --- a/src/dom/Draggable.js +++ b/src/dom/Draggable.js @@ -2,8 +2,7 @@ * L.Draggable allows you to add dragging capabilities to any element. Supports mobile devices too. */ -L.Draggable = L.Class.extend({ - includes: L.Mixin.Events, +L.Draggable = L.Evented.extend({ statics: { START: L.Browser.touch ? ['touchstart', 'mousedown'] : ['mousedown'], diff --git a/src/dom/PosAnimation.js b/src/dom/PosAnimation.js index 4dddda27..2630c978 100644 --- a/src/dom/PosAnimation.js +++ b/src/dom/PosAnimation.js @@ -2,8 +2,7 @@ * L.PosAnimation is used by Leaflet internally for pan animations. */ -L.PosAnimation = L.Class.extend({ - includes: L.Mixin.Events, +L.PosAnimation = L.Evented.extend({ run: function (el, newPos, duration, easeLinearity) { // (HTMLElement, Point[, Number, Number]) this.stop(); diff --git a/src/layer/Layer.js b/src/layer/Layer.js index b82bac01..99bb9d65 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -1,6 +1,5 @@ -L.Layer = L.Class.extend({ - includes: L.Mixin.Events, +L.Layer = L.Evented.extend({ options: { pane: 'overlayPane' diff --git a/src/map/Map.js b/src/map/Map.js index 5ed1b6f6..94842eb5 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -2,9 +2,7 @@ * L.Map is the central class of the API - it is used to create a map. */ -L.Map = L.Class.extend({ - - includes: L.Mixin.Events, +L.Map = L.Evented.extend({ options: { crs: L.CRS.EPSG3857,