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

View file

@ -18,13 +18,12 @@
* @private * @private
*/ */
var SegmentPoint = Point.extend({ var SegmentPoint = Point.extend({
initialize: function SegmentPoint(point, owner) { initialize: function SegmentPoint(point, owner, key) {
var x, y, selected; var x, y, selected;
if (!point) { if (!point) {
x = y = 0; x = y = 0;
} else if ((x = point[0]) !== undefined) { // Array-like } else if ((x = point[0]) !== undefined) { // Array-like
y = point[1]; y = point[1];
selected = point[2]; // See #_serialize()
} else { } else {
// If not Point-like already, read Point from arguments // If not Point-like already, read Point from arguments
if ((x = point.x) === undefined) { if ((x = point.x) === undefined) {
@ -37,6 +36,9 @@ var SegmentPoint = Point.extend({
this._x = x; this._x = x;
this._y = y; this._y = y;
this._owner = owner; 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) if (selected)
this.setSelected(true); this.setSelected(true);
}, },
@ -50,11 +52,11 @@ var SegmentPoint = Point.extend({
_serialize: function(options) { _serialize: function(options) {
var f = options.formatter, var f = options.formatter,
values = [f.number(this._x), f.number(this._y)]; x = f.number(this._x),
// Included the selected state of the segment point y = f.number(this._y);
if (this.isSelected()) return this.isSelected()
values.push(true); ? { x: x, y: y, selected: true }
return values; : [x, y];
}, },
getX: function() { getX: function() {