new naming methods

This commit is contained in:
lavrton
2015-02-08 06:24:11 +07:00
parent b51c5a0816
commit 5df69badbf
5 changed files with 135 additions and 8 deletions

View File

@@ -1,3 +1,9 @@
## 0.8.1 2015-02-30
* Bug Fixes
* Enhancements
* new methods for working with node's name: `addName`, `removeName`, `hasName`.
## 0.8.0 2015-02-04 ## 0.8.0 2015-02-04
* Bug Fixes * Bug Fixes

View File

@@ -3,7 +3,7 @@
* Konva JavaScript Framework v0.8.0 * Konva JavaScript Framework v0.8.0
* http://konvajs.github.io/ * http://konvajs.github.io/
* Licensed under the MIT or GPL Version 2 licenses. * Licensed under the MIT or GPL Version 2 licenses.
* Date: 2015-02-04 * Date: 2015-02-08
* *
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS) * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
* Modified work Copyright (C) 2014 - 2015 by Anton Lavrenov (Konva) * Modified work Copyright (C) 2014 - 2015 by Anton Lavrenov (Konva)
@@ -3704,6 +3704,60 @@ var Konva = {};
this._setAttr(NAME, name); this._setAttr(NAME, name);
return this; return this;
}, },
// naming methods
/**
* add name to node
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Konva.Node}
* @example
* node.name('red');
* node.addName('selected');
* node.name(); // return 'red selected'
*/
addName : function(name) {
if (!this.hasName(name)) {
var newName = this.name() + ' ' + name;
this.setName(newName);
}
return this;
},
/**
* check is node has name
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Boolean}
* @example
* node.name('red');
* node.hasName('red'); // return true
* node.hasName('selected'); // return false
*/
hasName : function(name) {
var names = this.name().split(/\s/g);
return names.indexOf(name) !== -1;
},
/**
* remove name from node
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Konva.Node}
* @example
* node.name('red selected');
* node.removeName('selected');
* node.hasName('selected'); // return false
* node.name(); // return 'red'
*/
removeName : function(name) {
var names = this.name().split(/\s/g);
var index = names.indexOf(name);
if (index !== -1) {
names.splice(index, 1);
this.setName(names.join(' '));
}
},
/** /**
* set attr * set attr
* @method * @method

8
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1428,6 +1428,60 @@
this._setAttr(NAME, name); this._setAttr(NAME, name);
return this; return this;
}, },
// naming methods
/**
* add name to node
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Konva.Node}
* @example
* node.name('red');
* node.addName('selected');
* node.name(); // return 'red selected'
*/
addName : function(name) {
if (!this.hasName(name)) {
var newName = this.name() + ' ' + name;
this.setName(newName);
}
return this;
},
/**
* check is node has name
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Boolean}
* @example
* node.name('red');
* node.hasName('red'); // return true
* node.hasName('selected'); // return false
*/
hasName : function(name) {
var names = this.name().split(/\s/g);
return names.indexOf(name) !== -1;
},
/**
* remove name from node
* @method
* @memberof Konva.Node.prototype
* @param {String} name
* @returns {Konva.Node}
* @example
* node.name('red selected');
* node.removeName('selected');
* node.hasName('selected'); // return false
* node.name(); // return 'red'
*/
removeName : function(name) {
var names = this.name().split(/\s/g);
var index = names.indexOf(name);
if (index !== -1) {
names.splice(index, 1);
this.setName(names.join(' '));
}
},
/** /**
* set attr * set attr
* @method * @method

View File

@@ -1094,7 +1094,7 @@ suite('Node', function() {
}); });
// ====================================================== // ======================================================
test('get shape name', function() { test('test name methods', function() {
var stage = addStage(); var stage = addStage();
var layer = new Konva.Layer(); var layer = new Konva.Layer();
var circle = new Konva.Circle({ var circle = new Konva.Circle({
@@ -1104,13 +1104,26 @@ suite('Node', function() {
fill: 'green', fill: 'green',
stroke: 'black', stroke: 'black',
strokeWidth: 4, strokeWidth: 4,
name: 'myCircle' name: 'myCircle foo'
}); });
layer.add(circle); layer.add(circle);
stage.add(layer); stage.add(layer);
assert.equal(circle.getName(),'myCircle foo');
assert.equal(circle.getName(),'myCircle'); // add existing name
circle.addName('foo');
assert.equal(circle.getName(),'myCircle foo');
// check hasName
assert.equal(circle.hasName('myCircle'), true);
assert.equal(circle.hasName('foo'), true);
assert.equal(circle.hasName('boo'), false);
assert.equal(stage.findOne('.foo'), circle);
// removing name
circle.removeName('foo');
assert.equal(circle.getName(), 'myCircle');
}); });
// ====================================================== // ======================================================