konva/src/Factory.ts

159 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-05-05 22:19:24 +08:00
import { Util } from './Util.js';
import { getComponentValidator } from './Validators.js';
2019-01-02 04:59:27 +08:00
var GET = 'get',
SET = 'set';
export const Factory = {
addGetterSetter(constructor, attr, def?, validator?, after?) {
2020-06-03 01:16:44 +08:00
Factory.addGetter(constructor, attr, def);
Factory.addSetter(constructor, attr, validator, after);
Factory.addOverloadedGetterSetter(constructor, attr);
2019-01-02 04:59:27 +08:00
},
addGetter(constructor, attr, def?) {
var method = GET + Util._capitalize(attr);
2019-01-06 16:01:20 +08:00
constructor.prototype[method] =
constructor.prototype[method] ||
2020-06-03 01:16:44 +08:00
function () {
2019-01-06 16:01:20 +08:00
var val = this.attrs[attr];
return val === undefined ? def : val;
};
2019-01-02 04:59:27 +08:00
},
2019-02-20 22:13:39 +08:00
2019-01-02 04:59:27 +08:00
addSetter(constructor, attr, validator?, after?) {
var method = SET + Util._capitalize(attr);
2019-02-20 22:13:39 +08:00
if (!constructor.prototype[method]) {
Factory.overWriteSetter(constructor, attr, validator, after);
}
},
overWriteSetter(constructor, attr, validator?, after?) {
var method = SET + Util._capitalize(attr);
2020-06-03 01:16:44 +08:00
constructor.prototype[method] = function (val) {
2019-02-20 22:13:39 +08:00
if (validator && val !== undefined && val !== null) {
val = validator.call(this, val, attr);
}
2019-01-02 04:59:27 +08:00
2019-02-20 22:13:39 +08:00
this._setAttr(attr, val);
2019-01-02 04:59:27 +08:00
2019-02-20 22:13:39 +08:00
if (after) {
after.call(this);
}
2019-01-02 04:59:27 +08:00
2019-02-20 22:13:39 +08:00
return this;
};
2019-01-02 04:59:27 +08:00
},
addComponentsGetterSetter(constructor, attr, components, validator?, after?) {
var len = components.length,
capitalize = Util._capitalize,
getter = GET + capitalize(attr),
setter = SET + capitalize(attr),
n,
component;
// getter
2020-06-03 01:16:44 +08:00
constructor.prototype[getter] = function () {
2019-01-02 04:59:27 +08:00
var ret = {};
for (n = 0; n < len; n++) {
component = components[n];
ret[component] = this.getAttr(attr + capitalize(component));
}
return ret;
};
2019-02-25 01:06:04 +08:00
var basicValidator = getComponentValidator(components);
2019-01-02 04:59:27 +08:00
// setter
2020-06-03 01:16:44 +08:00
constructor.prototype[setter] = function (val) {
2019-01-02 04:59:27 +08:00
var oldVal = this.attrs[attr],
key;
if (validator) {
val = validator.call(this, val);
}
if (basicValidator) {
basicValidator.call(this, val, attr);
}
2019-01-02 04:59:27 +08:00
for (key in val) {
if (!val.hasOwnProperty(key)) {
continue;
}
this._setAttr(attr + capitalize(key), val[key]);
}
this._fireChangeEvent(attr, oldVal, val);
if (after) {
after.call(this);
}
return this;
};
2020-06-03 01:16:44 +08:00
Factory.addOverloadedGetterSetter(constructor, attr);
2019-01-02 04:59:27 +08:00
},
addOverloadedGetterSetter(constructor, attr) {
var capitalizedAttr = Util._capitalize(attr),
setter = SET + capitalizedAttr,
getter = GET + capitalizedAttr;
2020-06-03 01:16:44 +08:00
constructor.prototype[attr] = function () {
2019-01-02 04:59:27 +08:00
// setting
if (arguments.length) {
this[setter](arguments[0]);
return this;
}
// getting
return this[getter]();
};
},
addDeprecatedGetterSetter(constructor, attr, def, validator) {
Util.error('Adding deprecated ' + attr);
var method = GET + Util._capitalize(attr);
var message =
attr +
' property is deprecated and will be removed soon. Look at Konva change log for more information.';
2020-06-03 01:16:44 +08:00
constructor.prototype[method] = function () {
2019-01-02 04:59:27 +08:00
Util.error(message);
var val = this.attrs[attr];
return val === undefined ? def : val;
};
2020-06-03 01:16:44 +08:00
Factory.addSetter(constructor, attr, validator, function () {
2019-01-02 04:59:27 +08:00
Util.error(message);
});
2020-06-03 01:16:44 +08:00
Factory.addOverloadedGetterSetter(constructor, attr);
2019-01-02 04:59:27 +08:00
},
backCompat(constructor, methods) {
2020-06-03 01:16:44 +08:00
Util.each(methods, function (oldMethodName, newMethodName) {
2019-01-02 04:59:27 +08:00
var method = constructor.prototype[newMethodName];
var oldGetter = GET + Util._capitalize(oldMethodName);
var oldSetter = SET + Util._capitalize(oldMethodName);
function deprecated() {
method.apply(this, arguments);
Util.error(
'"' +
oldMethodName +
'" method is deprecated and will be removed soon. Use ""' +
newMethodName +
'" instead.'
);
}
constructor.prototype[oldMethodName] = deprecated;
constructor.prototype[oldGetter] = deprecated;
constructor.prototype[oldSetter] = deprecated;
});
},
afterSetFilter() {
this._filterUpToDate = false;
2020-06-03 01:16:44 +08:00
},
2019-01-02 04:59:27 +08:00
};