konva/src/Factory.js

151 lines
4.9 KiB
JavaScript
Raw Normal View History

(function() {
2015-05-04 17:02:16 +08:00
'use strict';
// CONSTANTS
var GET = 'get',
SET = 'set';
2015-01-27 15:07:51 +08:00
Konva.Factory = {
addGetterSetter: function(constructor, attr, def, validator, after) {
this.addGetter(constructor, attr, def);
this.addSetter(constructor, attr, validator, after);
this.addOverloadedGetterSetter(constructor, attr);
},
addGetter: function(constructor, attr, def) {
2015-01-27 15:07:51 +08:00
var method = GET + Konva.Util._capitalize(attr);
constructor.prototype[method] = function() {
var val = this.attrs[attr];
return val === undefined ? def : val;
};
},
addSetter: function(constructor, attr, validator, after) {
2015-01-27 15:07:51 +08:00
var method = SET + Konva.Util._capitalize(attr);
constructor.prototype[method] = function(val) {
if (validator) {
val = validator.call(this, val);
}
this._setAttr(attr, val);
if (after) {
after.call(this);
}
2014-02-27 08:49:18 +08:00
return this;
};
},
addComponentsGetterSetter: function(constructor, attr, components, validator, after) {
var len = components.length,
2015-01-27 15:07:51 +08:00
capitalize = Konva.Util._capitalize,
2014-02-27 08:49:18 +08:00
getter = GET + capitalize(attr),
setter = SET + capitalize(attr),
n, component;
// getter
constructor.prototype[getter] = function() {
var ret = {};
2015-05-04 17:02:16 +08:00
for (n = 0; n < len; n++) {
component = components[n];
ret[component] = this.getAttr(attr + capitalize(component));
}
return ret;
};
// setter
constructor.prototype[setter] = function(val) {
var oldVal = this.attrs[attr],
key;
if (validator) {
val = validator.call(this, val);
}
for (key in val) {
2015-10-22 13:32:07 +08:00
if (!val.hasOwnProperty(key)) {
continue;
}
2014-02-27 08:49:18 +08:00
this._setAttr(attr + capitalize(key), val[key]);
}
this._fireChangeEvent(attr, oldVal, val);
2015-05-04 17:02:16 +08:00
if (after) {
after.call(this);
}
2014-02-27 08:49:18 +08:00
return this;
};
this.addOverloadedGetterSetter(constructor, attr);
},
addOverloadedGetterSetter: function(constructor, attr) {
2015-01-27 15:07:51 +08:00
var capitalizedAttr = Konva.Util._capitalize(attr),
setter = SET + capitalizedAttr,
getter = GET + capitalizedAttr;
constructor.prototype[attr] = function() {
// setting
if (arguments.length) {
this[setter](arguments[0]);
return this;
}
// getting
2015-10-22 13:32:07 +08:00
return this[getter]();
2014-02-27 08:49:18 +08:00
};
},
2015-05-07 09:48:15 +08:00
addDeprecatedGetterSetter: function(constructor, attr, def, validator) {
var method = GET + Konva.Util._capitalize(attr);
var message = attr + ' property is deprecated and will be removed soon. Look at Konva change log for more information.';
constructor.prototype[method] = function() {
Konva.Util.error(message);
var val = this.attrs[attr];
return val === undefined ? def : val;
};
this.addSetter(constructor, attr, validator, function() {
Konva.Util.error(message);
});
this.addOverloadedGetterSetter(constructor, attr);
},
backCompat: function(constructor, methods) {
2015-05-07 09:48:15 +08:00
Konva.Util.each(methods, function(oldMethodName, newMethodName) {
var method = constructor.prototype[newMethodName];
constructor.prototype[oldMethodName] = function(){
method.apply(this, arguments);
Konva.Util.error(oldMethodName + ' method is deprecated and will be removed soon. Use ' + newMethodName + ' instead');
};
});
},
afterSetFilter: function() {
this._filterUpToDate = false;
}
};
2015-01-27 15:07:51 +08:00
Konva.Validators = {
/**
* @return {number}
*/
RGBComponent: function(val) {
if (val > 255) {
return 255;
} else if (val < 0) {
return 0;
}
2015-10-22 13:32:07 +08:00
return Math.round(val);
},
alphaComponent: function(val) {
if (val > 1) {
return 1;
}
// chrome does not honor alpha values of 0
else if (val < 0.0001) {
return 0.0001;
}
2015-10-22 13:32:07 +08:00
return val;
}
};
2015-05-04 17:02:16 +08:00
})();