Skip to content

Commit

Permalink
Remove 'update' event from L.Illustrate.Textbox and substitute more s…
Browse files Browse the repository at this point in the history
…pecific events based on the updated property: 'resize', 'textedit', 'move', and 'rotate'. Remove getLatLng and getRotation methods of L.Illustrate.Textbox to cause L.Illustrate.Textbox to inherit these methods from their parent classes.
  • Loading branch information
justinmanley committed Sep 12, 2014
1 parent 42f76ef commit fdafa2a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 101 deletions.
73 changes: 37 additions & 36 deletions dist/Leaflet.Illustrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ L.RotatableMarker = L.Marker.extend({

setRotation: function(theta) {
this._rotation = theta;

this.update();
this.fire('rotate', { rotation: this._rotation });

return this;
},

getRotation: function() {
Expand Down Expand Up @@ -350,7 +354,7 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
L.RotatableMarker.prototype.onAdd.call(this, map);

this.setContent(this._textContent);
this._updateCenter();
this.setLatLng(this._latlng);
this._updateSize();

/* Enable typing, text selection, etc. */
Expand Down Expand Up @@ -380,18 +384,6 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
L.RotatableMarker.prototype.onRemove.call(this, map);
},

setCenter: function(latlng) {
this._latlng = latlng;

this._updateCenter();
this.fire('update');

return this;
},

getLatLng: function() {
return this._latlng;
},

getSize: function() {
return this._size;
Expand All @@ -410,14 +402,8 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
if (this._map) {
this._updateSize();
}
this.fire('update');

return this;
},
this.fire('resize', { size: this._size });

setRotation: function(theta) {
L.RotatableMarker.prototype.setRotation.call(this, theta);
this.fire('update');
return this;
},

Expand Down Expand Up @@ -462,9 +448,7 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
/* When user leaves the textarea, fire a 'draw:edited' event if they changed anything. */
_onTextEdit: function() {
if (this._text_edited) {
this._map.fire('draw:edited', {
layers: new L.LayerGroup().addLayer(this)
});
this.fire('textedit', { textContent: this.getContent() });
this._text_edited = false;
}
},
Expand Down Expand Up @@ -797,18 +781,25 @@ L.Illustrate.EditHandle = L.RotatableMarker.extend({
},

initialize: function(shape, options) {
L.setOptions(this, options);

this._handleOffset = new L.Point(options.offset.x || 0, options.offset.y || 0);
this._handled = shape;

var latlng = this._handled._map.layerPointToLatLng(this._textboxCoordsToLayerPoint(
this._handleOffset
));
)),
markerOptions = {
draggable: true,
icon: this.options.resizeIcon,
zIndexOffset: 10
};

L.RotatableMarker.prototype.initialize.call(this, latlng, {
draggable: true,
icon: this.options.resizeIcon,
zIndexOffset: 10
});
if (this._handled.getRotation) {
markerOptions.rotation = this._handled.getRotation();
}

L.RotatableMarker.prototype.initialize.call(this, latlng, markerOptions);
},

onAdd: function(map) {
Expand Down Expand Up @@ -860,7 +851,10 @@ L.Illustrate.EditHandle = L.RotatableMarker.extend({
}, this);

this._handled._map.on('zoomend', this.updateHandle, this);
this._handled.on('update', this.updateHandle, this);

this._handled.on('rotate', this.updateHandle, this);
this._handled.on('resize', this.updateHandle, this);
this._handled.on('move', this.updateHandle, this);
},

_unbindListeners: function() {
Expand Down Expand Up @@ -942,7 +936,7 @@ L.Illustrate.MoveHandle = L.Illustrate.EditHandle.extend({
_onHandleDrag: function(event) {
var handle = event.target;

this._handled.setCenter(handle.getLatLng());
this._handled.setLatLng(handle.getLatLng());
}
});
L.Illustrate.PointerHandle = L.Illustrate.EditHandle.extend({
Expand Down Expand Up @@ -1151,11 +1145,8 @@ L.Illustrate.RotateHandle = L.Illustrate.EditHandle.extend({
weight: Math.round(this._handled.options.borderWidth)
};

this._pointerStart = this._handleOffset.multiplyBy(0.5);
this._pointer = new L.Illustrate.Pointer(this._handled.getLatLng(), [
this._pointerStart,
this._handleOffset
], options);
this._pointer = new L.Illustrate.Pointer(this._handled.getLatLng(), [], options);
this._updatePointer();

this._handled.on({ 'update': this._updatePointer }, this);
},
Expand Down Expand Up @@ -1363,6 +1354,7 @@ L.Illustrate.Edit.Textbox = L.Edit.SimpleShape.extend({
this._map = this._shape._map;

this._initHandles();
this._initEvents();
}
},

Expand All @@ -1387,6 +1379,15 @@ L.Illustrate.Edit.Textbox = L.Edit.SimpleShape.extend({
}
},

_initEvents: function() {
var fireEdit = function() { this._shape.fire('edit'); },
changeEvents = [ 'resize', 'rotate', 'textedit', 'move' ];

for (var i = 0; i < changeEvents.length; i++) {
this._shape.on(changeEvents[i], fireEdit, this);
}
},

_addRotateHandle: function() {
this._rotateHandle = new L.Illustrate.RotateHandle(this._shape, {
offset: new L.Point(0, -this._shape.getSize().y)
Expand Down
26 changes: 3 additions & 23 deletions src/core/L.Illustrate.Textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
L.RotatableMarker.prototype.onAdd.call(this, map);

this.setContent(this._textContent);
this._updateCenter();
this.setLatLng(this._latlng);
this._updateSize();

/* Enable typing, text selection, etc. */
Expand Down Expand Up @@ -61,18 +61,6 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
L.RotatableMarker.prototype.onRemove.call(this, map);
},

setCenter: function(latlng) {
this._latlng = latlng;

this._updateCenter();
this.fire('update');

return this;
},

getLatLng: function() {
return this._latlng;
},

getSize: function() {
return this._size;
Expand All @@ -91,14 +79,8 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
if (this._map) {
this._updateSize();
}
this.fire('update');

return this;
},
this.fire('resize', { size: this._size });

setRotation: function(theta) {
L.RotatableMarker.prototype.setRotation.call(this, theta);
this.fire('update');
return this;
},

Expand Down Expand Up @@ -143,9 +125,7 @@ L.Illustrate.Textbox = L.RotatableMarker.extend({
/* When user leaves the textarea, fire a 'draw:edited' event if they changed anything. */
_onTextEdit: function() {
if (this._text_edited) {
this._map.fire('draw:edited', {
layers: new L.LayerGroup().addLayer(this)
});
this.fire('textedit', { textContent: this.getContent() });
this._text_edited = false;
}
},
Expand Down
10 changes: 10 additions & 0 deletions src/edit/L.Illustrate.Edit.Textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ L.Illustrate.Edit.Textbox = L.Edit.SimpleShape.extend({
this._map = this._shape._map;

this._initHandles();
this._initEvents();
}
},

Expand All @@ -33,6 +34,15 @@ L.Illustrate.Edit.Textbox = L.Edit.SimpleShape.extend({
}
},

_initEvents: function() {
var fireEdit = function() { this._shape.fire('edit'); },
changeEvents = [ 'resize', 'rotate', 'textedit', 'move' ];

for (var i = 0; i < changeEvents.length; i++) {
this._shape.on(changeEvents[i], fireEdit, this);
}
},

_addRotateHandle: function() {
this._rotateHandle = new L.Illustrate.RotateHandle(this._shape, {
offset: new L.Point(0, -this._shape.getSize().y)
Expand Down
24 changes: 17 additions & 7 deletions src/edit/handles/L.Illustrate.EditHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ L.Illustrate.EditHandle = L.RotatableMarker.extend({
},

initialize: function(shape, options) {
L.setOptions(this, options);

this._handleOffset = new L.Point(options.offset.x || 0, options.offset.y || 0);
this._handled = shape;

var latlng = this._handled._map.layerPointToLatLng(this._textboxCoordsToLayerPoint(
this._handleOffset
));
)),
markerOptions = {
draggable: true,
icon: this.options.resizeIcon,
zIndexOffset: 10
};

if (this._handled.getRotation) {
markerOptions.rotation = this._handled.getRotation();
}

L.RotatableMarker.prototype.initialize.call(this, latlng, {
draggable: true,
icon: this.options.resizeIcon,
zIndexOffset: 10
});
L.RotatableMarker.prototype.initialize.call(this, latlng, markerOptions);
},

onAdd: function(map) {
Expand Down Expand Up @@ -74,7 +81,10 @@ L.Illustrate.EditHandle = L.RotatableMarker.extend({
}, this);

this._handled._map.on('zoomend', this.updateHandle, this);
this._handled.on('update', this.updateHandle, this);

this._handled.on('rotate', this.updateHandle, this);
this._handled.on('resize', this.updateHandle, this);
this._handled.on('move', this.updateHandle, this);
},

_unbindListeners: function() {
Expand Down
2 changes: 1 addition & 1 deletion src/edit/handles/L.Illustrate.MoveHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ L.Illustrate.MoveHandle = L.Illustrate.EditHandle.extend({
_onHandleDrag: function(event) {
var handle = event.target;

this._handled.setCenter(handle.getLatLng());
this._handled.setLatLng(handle.getLatLng());
}
});
7 changes: 2 additions & 5 deletions src/edit/handles/L.Illustrate.RotateHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ L.Illustrate.RotateHandle = L.Illustrate.EditHandle.extend({
weight: Math.round(this._handled.options.borderWidth)
};

this._pointerStart = this._handleOffset.multiplyBy(0.5);
this._pointer = new L.Illustrate.Pointer(this._handled.getLatLng(), [
this._pointerStart,
this._handleOffset
], options);
this._pointer = new L.Illustrate.Pointer(this._handled.getLatLng(), [], options);
this._updatePointer();

this._handled.on({ 'update': this._updatePointer }, this);
},
Expand Down
4 changes: 4 additions & 0 deletions src/extends-leaflet/L.RotatableMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ L.RotatableMarker = L.Marker.extend({

setRotation: function(theta) {
this._rotation = theta;

this.update();
this.fire('rotate', { rotation: this._rotation });

return this;
},

getRotation: function() {
Expand Down
9 changes: 9 additions & 0 deletions test/core/TextboxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe("L.Illustrate.Textbox", function() {
});
});

describe("#getLatLng", function() {
it("Should return the new value after center is updated using #setLatLng", function() {
var newLatLng = new L.LatLng(2, 4);

textbox.setLatLng(newLatLng);
expect(textbox.getLatLng()).to.deep.equal(newLatLng);
});
});

describe("#getContent", function() {
it("Should return the empty string for a new textbox with no default value.", function() {
expect(textbox.getContent()).to.equal('');
Expand Down
29 changes: 2 additions & 27 deletions test/edit/EditTextboxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,8 @@ describe("L.Illustrate.Edit.Textbox", function() {
textbox.editing.enable();
});

describe("#_onHandleDrag", function() {
it("Should not change the textbox dimensions when called with initial latlng.", function() {
var handle = textbox.editing._resizeHandles[0],
size = textbox.getSize();

handle._onHandleDrag({ target: handle });

/* textbox.getSize() is equal to 2*this._minSize */
expect(textbox.getSize()).to.be.closeToPoint(size);
});
});

describe("draw:edited", function() {
it.skip("Should fire when content inside the textarea is changed.", function(done) {
var textarea = textbox.getTextarea(),
text = 'Some new text';

map.on('draw:edited', function(event) {
event.layers.eachLayer(function(layer) {
expect(layer.getTextarea().value).to.equal(text);
});
done();
});

/* Setting the textarea value programmatically doesn't fire the change event. */
textarea.innerHTML = text;
});
it("", function() {
expect(1).to.equal(1);
});

});

0 comments on commit fdafa2a

Please sign in to comment.