Improve selection serialization to JSON and fix issue in deserialization.

This commit is contained in:
Jürg Lehni 2013-10-18 21:12:25 +02:00
parent 782f5c8f7e
commit 5db6db3b76
2 changed files with 15 additions and 15 deletions

View file

@ -125,9 +125,7 @@ var Segment = Base.extend(/** @lends Segment# */{
} else {
point = arg0;
}
} else if ((count === 2 || count === 3) && typeof arg0 === 'number') {
// We check for 3 and 2 because there is an optional boolean
// argument for segment points to mark them as selected.
} else if (count === 2 && typeof arg0 === 'number') {
point = arguments;
} else if (count <= 3) {
point = arg0;
@ -140,9 +138,9 @@ var Segment = Base.extend(/** @lends Segment# */{
handleIn = arg2 !== undefined ? [ arg2, arg3 ] : null;
handleOut = arg4 !== undefined ? [ arg4, arg5 ] : null;
}
this._point = new SegmentPoint(point, this);
this._handleIn = new SegmentPoint(handleIn, this);
this._handleOut = new SegmentPoint(handleOut, this);
new SegmentPoint(point, this, '_point');
new SegmentPoint(handleIn, this, '_handleIn');
new SegmentPoint(handleOut, this, '_handleOut');
},
_serialize: function(options) {
@ -316,7 +314,7 @@ var Segment = Base.extend(/** @lends Segment# */{
!!(state & /*#=*/ SelectionState.HANDLE_IN),
!!(state & /*#=*/ SelectionState.HANDLE_OUT)
];
if (point == this._point) {
if (point === this._point) {
if (selected) {
// We're selecting point, deselect the handles
selection[1] = selection[2] = false;
@ -332,7 +330,7 @@ var Segment = Base.extend(/** @lends Segment# */{
}
selection[0] = selected;
} else {
var index = point == this._handleIn ? 1 : 2;
var index = point === this._handleIn ? 1 : 2;
if (selection[index] != selected) {
// When selecting handles, the point get deselected.
if (selected)

View file

@ -18,13 +18,12 @@
* @private
*/
var SegmentPoint = Point.extend({
initialize: function SegmentPoint(point, owner) {
initialize: function SegmentPoint(point, owner, key) {
var x, y, selected;
if (!point) {
x = y = 0;
} else if ((x = point[0]) !== undefined) { // Array-like
y = point[1];
selected = point[2]; // See #_serialize()
} else {
// If not Point-like already, read Point from arguments
if ((x = point.x) === undefined) {
@ -37,6 +36,9 @@ var SegmentPoint = Point.extend({
this._x = x;
this._y = y;
this._owner = owner;
// We have to set the owner's property that points to this point already
// now, so #setSelected(true) can work.
owner[key] = this;
if (selected)
this.setSelected(true);
},
@ -50,11 +52,11 @@ var SegmentPoint = Point.extend({
_serialize: function(options) {
var f = options.formatter,
values = [f.number(this._x), f.number(this._y)];
// Included the selected state of the segment point
if (this.isSelected())
values.push(true);
return values;
x = f.number(this._x),
y = f.number(this._y);
return this.isSelected()
? { x: x, y: y, selected: true }
: [x, y];
},
getX: function() {