UNPKG

konva

Version:

HTML5 2d canvas library.

124 lines (123 loc) 4.95 kB
import { Util } from "./Util.js"; import { getComponentValidator } from "./Validators.js"; const GET = 'get'; const SET = 'set'; export const Factory = { addGetterSetter(constructor, attr, def, validator, after) { Factory.addGetter(constructor, attr, def); Factory.addSetter(constructor, attr, validator, after); Factory.addOverloadedGetterSetter(constructor, attr); }, addGetter(constructor, attr, def) { const method = GET + Util._capitalize(attr); constructor.prototype[method] = constructor.prototype[method] || function () { const val = this.attrs[attr]; return val === undefined ? def : val; }; }, addSetter(constructor, attr, validator, after) { const method = SET + Util._capitalize(attr); if (!constructor.prototype[method]) { Factory.overWriteSetter(constructor, attr, validator, after); } }, overWriteSetter(constructor, attr, validator, after) { const method = SET + Util._capitalize(attr); constructor.prototype[method] = function (val) { if (validator && val !== undefined && val !== null) { val = validator.call(this, val, attr); } this._setAttr(attr, val); if (after) { after.call(this); } return this; }; }, addComponentsGetterSetter(constructor, attr, components, validator, after) { const len = components.length, capitalize = Util._capitalize, getter = GET + capitalize(attr), setter = SET + capitalize(attr); constructor.prototype[getter] = function () { const ret = {}; for (let n = 0; n < len; n++) { const component = components[n]; ret[component] = this.getAttr(attr + capitalize(component)); } return ret; }; const basicValidator = getComponentValidator(components); constructor.prototype[setter] = function (val) { const oldVal = this.attrs[attr]; if (validator) { val = validator.call(this, val, attr); } if (basicValidator) { basicValidator.call(this, val, attr); } for (const key in val) { if (!val.hasOwnProperty(key)) { continue; } this._setAttr(attr + capitalize(key), val[key]); } if (!val) { components.forEach((component) => { this._setAttr(attr + capitalize(component), undefined); }); } this._fireChangeEvent(attr, oldVal, val); if (after) { after.call(this); } return this; }; Factory.addOverloadedGetterSetter(constructor, attr); }, addOverloadedGetterSetter(constructor, attr) { const capitalizedAttr = Util._capitalize(attr), setter = SET + capitalizedAttr, getter = GET + capitalizedAttr; constructor.prototype[attr] = function () { if (arguments.length) { this[setter](arguments[0]); return this; } return this[getter](); }; }, addDeprecatedGetterSetter(constructor, attr, def, validator) { Util.error('Adding deprecated ' + attr); const method = GET + Util._capitalize(attr); const message = attr + ' property is deprecated and will be removed soon. Look at Konva change log for more information.'; constructor.prototype[method] = function () { Util.error(message); const val = this.attrs[attr]; return val === undefined ? def : val; }; Factory.addSetter(constructor, attr, validator, function () { Util.error(message); }); Factory.addOverloadedGetterSetter(constructor, attr); }, backCompat(constructor, methods) { Util.each(methods, function (oldMethodName, newMethodName) { const method = constructor.prototype[newMethodName]; const oldGetter = GET + Util._capitalize(oldMethodName); const 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; }, };