Fix clone function to support custom Shapes. close #71

This commit is contained in:
Anton Lavrenov 2015-04-26 15:20:11 +07:00
parent 42eda94675
commit 744dd61992
4 changed files with 38 additions and 11 deletions

View File

@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- context wrapper is more capable with native context.
So you can use `context.fillStyle` property in your `sceneFunc` without accessing native context.
- `toDataURL` now handle pixelRatio. So image for stage 400x400 for retina will be 800x800.
- Correct `clone()` for custom nodes
## [0.9.0][2015-02-27]

View File

@ -1340,7 +1340,7 @@
attrs[key] = obj[key];
}
var node = new Konva[className](attrs);
var node = new this.constructor(attrs);
// copy over listeners
for(key in this.eventListeners) {
allListeners = this.eventListeners[key];

View File

@ -835,18 +835,20 @@
}
},
extend: function(child, parent) {
function Ctor() {
this.constructor = child;
}
function Ctor() {
this.constructor = child;
}
Ctor.prototype = parent.prototype;
var old_proto = child.prototype;
child.prototype = new Ctor();
for (var key in old_proto) {
if (old_proto.hasOwnProperty(key)) {
child.prototype[key] = old_proto[key];
}
var old_proto = child.prototype;
child.prototype = new Ctor();
for (var key in old_proto) {
if (old_proto.hasOwnProperty(key)) {
child.prototype[key] = old_proto[key];
}
child.__super__ = parent.prototype;
}
child.__super__ = parent.prototype;
// create reference to parent
child.super = parent;
},
/**
* adds methods to a constructor prototype

View File

@ -1178,4 +1178,28 @@ suite('Shape', function() {
});
test('clone custom shape', function() {
var className = 'myCustomName'
var CustomShape = function() {
CustomShape.super.apply(this, arguments);
this.className = className;
}
CustomShape.prototype.foo = function() {};
Konva.Util.extend(CustomShape, Konva.Shape);
var myShape = new CustomShape({
fill : 'grey'
});
var clone = myShape.clone();
assert.equal(clone instanceof CustomShape, true);
assert.equal(clone instanceof Konva.Shape, true);
assert.equal(clone.className, className);
assert.equal(clone.fill(), 'grey');
assert.equal(clone.foo, CustomShape.prototype.foo);
});
});