2013-12-05 01:10:24 +08:00
|
|
|
(function() {
|
2013-08-11 12:11:34 +08:00
|
|
|
// CONSTANTS
|
|
|
|
var ABSOLUTE_OPACITY = 'absoluteOpacity',
|
|
|
|
ABSOLUTE_TRANSFORM = 'absoluteTransform',
|
|
|
|
ADD = 'add',
|
|
|
|
B = 'b',
|
|
|
|
BEFORE = 'before',
|
|
|
|
BLACK = 'black',
|
|
|
|
CHANGE = 'Change',
|
|
|
|
CHILDREN = 'children',
|
|
|
|
DEG = 'Deg',
|
|
|
|
DOT = '.',
|
|
|
|
EMPTY_STRING = '',
|
|
|
|
G = 'g',
|
|
|
|
GET = 'get',
|
|
|
|
HASH = '#',
|
|
|
|
ID = 'id',
|
|
|
|
KINETIC = 'kinetic',
|
|
|
|
LISTENING = 'listening',
|
|
|
|
MOUSEENTER = 'mouseenter',
|
|
|
|
MOUSELEAVE = 'mouseleave',
|
|
|
|
NAME = 'name',
|
|
|
|
OFF = 'off',
|
|
|
|
ON = 'on',
|
|
|
|
PRIVATE_GET = '_get',
|
|
|
|
R = 'r',
|
|
|
|
RGB = 'RGB',
|
|
|
|
SET = 'set',
|
|
|
|
SHAPE = 'Shape',
|
|
|
|
SPACE = ' ',
|
|
|
|
STAGE = 'Stage',
|
|
|
|
TRANSFORM = 'transform',
|
|
|
|
UPPER_B = 'B',
|
|
|
|
UPPER_G = 'G',
|
|
|
|
UPPER_HEIGHT = 'Height',
|
|
|
|
UPPER_R = 'R',
|
|
|
|
UPPER_WIDTH = 'Width',
|
|
|
|
UPPER_X = 'X',
|
|
|
|
UPPER_Y = 'Y',
|
|
|
|
VISIBLE = 'visible',
|
|
|
|
X = 'x',
|
|
|
|
Y = 'y';
|
|
|
|
|
|
|
|
Kinetic.Factory = {
|
2013-12-05 01:10:24 +08:00
|
|
|
// getter setter adders
|
2013-12-05 00:15:04 +08:00
|
|
|
addGetterSetter: function(constructor, attr, def) {
|
|
|
|
this.addGetter(constructor, attr, def);
|
|
|
|
this.addSetter(constructor, attr);
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
2013-12-02 15:08:15 +08:00
|
|
|
},
|
2013-12-05 01:10:24 +08:00
|
|
|
addPointGetterSetter: function(constructor, attr, def) {
|
|
|
|
this.addPointGetter(constructor, attr, def);
|
2013-12-05 00:15:04 +08:00
|
|
|
this.addPointSetter(constructor, attr);
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
2013-12-02 15:08:15 +08:00
|
|
|
|
2013-12-05 01:10:24 +08:00
|
|
|
// add invdividual component getters and setters
|
|
|
|
this.addGetter(constructor, attr + UPPER_X, def);
|
|
|
|
this.addGetter(constructor, attr + UPPER_Y, def);
|
|
|
|
this.addSetter(constructor, attr + UPPER_X);
|
|
|
|
this.addSetter(constructor, attr + UPPER_Y);
|
2013-12-14 03:02:07 +08:00
|
|
|
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_X);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_Y);
|
2013-08-11 12:11:34 +08:00
|
|
|
},
|
2013-12-05 01:10:24 +08:00
|
|
|
addBoxGetterSetter: function(constructor, attr, def) {
|
|
|
|
this.addBoxGetter(constructor, attr, def);
|
2013-12-05 00:37:28 +08:00
|
|
|
this.addBoxSetter(constructor, attr);
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
2013-08-11 12:11:34 +08:00
|
|
|
|
2013-12-05 01:10:24 +08:00
|
|
|
// add invdividual component getters and setters
|
|
|
|
this.addGetter(constructor, attr + UPPER_X, def);
|
|
|
|
this.addGetter(constructor, attr + UPPER_Y, def);
|
|
|
|
this.addGetter(constructor, attr + UPPER_WIDTH, def);
|
|
|
|
this.addGetter(constructor, attr + UPPER_HEIGHT, def);
|
2013-12-05 00:15:04 +08:00
|
|
|
|
2013-12-05 01:10:24 +08:00
|
|
|
this.addSetter(constructor, attr + UPPER_X);
|
|
|
|
this.addSetter(constructor, attr + UPPER_Y);
|
|
|
|
this.addSetter(constructor, attr + UPPER_WIDTH);
|
|
|
|
this.addSetter(constructor, attr + UPPER_HEIGHT);
|
2013-12-14 03:02:07 +08:00
|
|
|
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_X);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_Y);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_WIDTH);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_HEIGHT);
|
2013-12-05 01:10:24 +08:00
|
|
|
},
|
|
|
|
addPointsGetterSetter: function(constructor, attr) {
|
|
|
|
this.addPointsGetter(constructor, attr);
|
|
|
|
this.addPointsSetter(constructor, attr);
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
2013-08-11 12:11:34 +08:00
|
|
|
},
|
|
|
|
addColorGetterSetter: function(constructor, attr) {
|
|
|
|
this.addGetter(constructor, attr);
|
|
|
|
this.addSetter(constructor, attr);
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
2013-08-11 12:11:34 +08:00
|
|
|
|
|
|
|
// component getters
|
|
|
|
this.addColorRGBGetter(constructor, attr);
|
|
|
|
this.addColorComponentGetter(constructor, attr, R);
|
|
|
|
this.addColorComponentGetter(constructor, attr, G);
|
|
|
|
this.addColorComponentGetter(constructor, attr, B);
|
|
|
|
|
|
|
|
// component setters
|
|
|
|
this.addColorRGBSetter(constructor, attr);
|
|
|
|
this.addColorComponentSetter(constructor, attr, R);
|
|
|
|
this.addColorComponentSetter(constructor, attr, G);
|
|
|
|
this.addColorComponentSetter(constructor, attr, B);
|
2013-12-14 03:02:07 +08:00
|
|
|
|
2014-01-01 05:04:05 +08:00
|
|
|
// overloaders
|
2013-12-14 03:02:07 +08:00
|
|
|
this.addOverloadedGetterSetter(constructor, attr + RGB);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_R);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_G);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr + UPPER_B);
|
2013-08-11 12:11:34 +08:00
|
|
|
},
|
2014-01-01 05:04:05 +08:00
|
|
|
addFilterGetterSetter: function(constructor, attr, def) {
|
|
|
|
this.addGetter(constructor, attr, def);
|
|
|
|
this.addFilterSetter(constructor, attr);
|
|
|
|
this.addOverloadedGetterSetter(constructor, attr);
|
|
|
|
},
|
2013-08-11 12:11:34 +08:00
|
|
|
|
|
|
|
// getter adders
|
|
|
|
addColorRGBGetter: function(constructor, attr) {
|
|
|
|
var method = GET + Kinetic.Util._capitalize(attr) + RGB;
|
|
|
|
constructor.prototype[method] = function() {
|
|
|
|
return Kinetic.Util.getRGB(this.attrs[attr]);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
addColorComponentGetter: function(constructor, attr, c) {
|
|
|
|
var prefix = GET + Kinetic.Util._capitalize(attr),
|
|
|
|
method = prefix + Kinetic.Util._capitalize(c);
|
|
|
|
constructor.prototype[method] = function() {
|
|
|
|
return this[prefix + RGB]()[c];
|
|
|
|
};
|
|
|
|
},
|
2013-12-05 01:10:24 +08:00
|
|
|
addPointsGetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
method = GET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[method] = function() {
|
|
|
|
var val = this.attrs[attr];
|
|
|
|
return val === undefined ? [] : val;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addGetter: function(constructor, attr, def) {
|
|
|
|
var that = this,
|
|
|
|
method = GET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[method] = function() {
|
|
|
|
var val = this.attrs[attr];
|
|
|
|
return val === undefined ? def : val;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addPointGetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
baseMethod = GET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[baseMethod] = function() {
|
|
|
|
var that = this;
|
|
|
|
return {
|
|
|
|
x: that[baseMethod + UPPER_X](),
|
|
|
|
y: that[baseMethod + UPPER_Y]()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addBoxGetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
baseMethod = GET + Kinetic.Util._capitalize(attr);
|
2013-08-11 12:11:34 +08:00
|
|
|
|
2013-12-05 01:10:24 +08:00
|
|
|
constructor.prototype[baseMethod] = function() {
|
|
|
|
var that = this;
|
|
|
|
return {
|
|
|
|
x: that[baseMethod + UPPER_X](),
|
|
|
|
y: that[baseMethod + UPPER_Y](),
|
|
|
|
width: that[baseMethod + UPPER_WIDTH](),
|
|
|
|
height: that[baseMethod + UPPER_HEIGHT]()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
2013-08-11 12:11:34 +08:00
|
|
|
|
|
|
|
// setter adders
|
|
|
|
addColorRGBSetter: function(constructor, attr) {
|
|
|
|
var method = SET + Kinetic.Util._capitalize(attr) + RGB;
|
|
|
|
|
|
|
|
constructor.prototype[method] = function(obj) {
|
|
|
|
var r = obj && obj.r !== undefined ? obj.r | 0 : this.getAttr(attr + UPPER_R),
|
|
|
|
g = obj && obj.g !== undefined ? obj.g | 0 : this.getAttr(attr + UPPER_G),
|
|
|
|
b = obj && obj.b !== undefined ? obj.b | 0 : this.getAttr(attr + UPPER_B);
|
|
|
|
|
|
|
|
this._setAttr(attr, HASH + Kinetic.Util._rgbToHex(r, g, b));
|
2013-12-10 02:33:31 +08:00
|
|
|
return this;
|
2013-08-11 12:11:34 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
addColorComponentSetter: function(constructor, attr, c) {
|
|
|
|
var prefix = SET + Kinetic.Util._capitalize(attr),
|
|
|
|
method = prefix + Kinetic.Util._capitalize(c);
|
|
|
|
constructor.prototype[method] = function(val) {
|
|
|
|
var obj = {};
|
|
|
|
obj[c] = val;
|
|
|
|
this[prefix + RGB](obj);
|
2013-12-10 02:33:31 +08:00
|
|
|
return this;
|
2013-08-11 12:11:34 +08:00
|
|
|
};
|
|
|
|
},
|
2013-12-05 01:10:24 +08:00
|
|
|
addPointsSetter: function(constructor, attr) {
|
|
|
|
var method = SET + Kinetic.Util._capitalize(attr);
|
|
|
|
constructor.prototype[method] = function(val) {
|
|
|
|
this._setAttr('points', val);
|
2013-12-10 02:33:31 +08:00
|
|
|
return this;
|
2013-12-05 01:10:24 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
addSetter: function(constructor, attr) {
|
|
|
|
var method = SET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[method] = function(val) {
|
2013-12-10 02:33:31 +08:00
|
|
|
this._setAttr(attr, val);
|
|
|
|
return this;
|
2013-12-05 01:10:24 +08:00
|
|
|
};
|
|
|
|
},
|
2014-01-01 05:04:05 +08:00
|
|
|
addFilterSetter: function(constructor, attr) {
|
|
|
|
var method = SET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[method] = function(val) {
|
|
|
|
this._setAttr(attr, val);
|
|
|
|
this._filterUpToDate = false;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
},
|
2013-12-05 01:10:24 +08:00
|
|
|
addPointSetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
baseMethod = SET + Kinetic.Util._capitalize(attr);
|
|
|
|
|
|
|
|
constructor.prototype[baseMethod] = function(pos) {
|
|
|
|
var oldVal = this.attrs[attr],
|
|
|
|
x = 0,
|
|
|
|
y = 0;
|
|
|
|
|
|
|
|
if (pos) {
|
|
|
|
x = pos.x;
|
|
|
|
y = pos.y;
|
|
|
|
|
|
|
|
if (x !== undefined) {
|
|
|
|
this[baseMethod + UPPER_X](x);
|
|
|
|
}
|
|
|
|
if (y !== undefined) {
|
|
|
|
this[baseMethod + UPPER_Y](y);
|
|
|
|
}
|
|
|
|
this._fireChangeEvent(attr, oldVal, pos);
|
|
|
|
}
|
2013-12-10 02:33:31 +08:00
|
|
|
|
|
|
|
return this;
|
2013-12-05 01:10:24 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
addBoxSetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
baseMethod = SET + Kinetic.Util._capitalize(attr);
|
2013-08-11 12:11:34 +08:00
|
|
|
|
2013-12-05 01:10:24 +08:00
|
|
|
constructor.prototype[baseMethod] = function(box) {
|
|
|
|
var oldVal = this.attrs[attr],
|
|
|
|
x, y, width, height;
|
|
|
|
|
|
|
|
if (box) {
|
|
|
|
x = box.x;
|
|
|
|
y = box.y;
|
|
|
|
width = box.width;
|
|
|
|
height = box.height;
|
|
|
|
|
|
|
|
if (x !== undefined) {
|
|
|
|
this[baseMethod + UPPER_X](x);
|
|
|
|
}
|
|
|
|
if (y !== undefined) {
|
|
|
|
this[baseMethod + UPPER_Y](y);
|
|
|
|
}
|
|
|
|
if (width !== undefined) {
|
|
|
|
this[baseMethod + UPPER_WIDTH](width);
|
|
|
|
}
|
|
|
|
if (height !== undefined) {
|
|
|
|
this[baseMethod + UPPER_HEIGHT](height);
|
|
|
|
}
|
|
|
|
this._fireChangeEvent(attr, oldVal, box);
|
|
|
|
}
|
2013-12-10 02:33:31 +08:00
|
|
|
|
|
|
|
return this;
|
2013-12-05 01:10:24 +08:00
|
|
|
};
|
|
|
|
},
|
2013-12-14 03:02:07 +08:00
|
|
|
addOverloadedGetterSetter: function(constructor, attr) {
|
|
|
|
var that = this,
|
|
|
|
capitalizedAttr = Kinetic.Util._capitalize(attr),
|
|
|
|
setter = SET + capitalizedAttr,
|
|
|
|
getter = GET + capitalizedAttr;
|
|
|
|
|
|
|
|
constructor.prototype[attr] = function() {
|
|
|
|
// setting
|
|
|
|
if (arguments.length) {
|
|
|
|
this[setter](arguments[0]);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// getting
|
|
|
|
else {
|
|
|
|
return this[getter]();
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 12:11:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|