konva/src/Validators.ts

204 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-05-05 22:54:03 +08:00
import { Konva } from './Global';
import { Util } from './Util';
2019-02-25 01:06:04 +08:00
2019-05-11 20:56:55 +08:00
function _formatValue(val: any) {
2019-02-25 01:06:04 +08:00
if (Util._isString(val)) {
return '"' + val + '"';
}
if (Object.prototype.toString.call(val) === '[object Number]') {
return val;
}
if (Util._isBoolean(val)) {
return val;
}
return Object.prototype.toString.call(val);
}
2019-05-11 20:56:55 +08:00
export function RGBComponent(val: number) {
2019-02-25 01:06:04 +08:00
if (val > 255) {
return 255;
} else if (val < 0) {
return 0;
}
return Math.round(val);
}
2019-05-11 20:56:55 +08:00
export function alphaComponent(val: number) {
2019-02-25 01:06:04 +08:00
if (val > 1) {
return 1;
} else if (val < 0.0001) {
// chrome does not honor alpha values of 0
return 0.0001;
}
return val;
}
export function getNumberValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function <T>(val: T, attr: string): T | void {
2019-02-25 01:06:04 +08:00
if (!Util._isNumber(val)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a number.'
);
}
return val;
};
}
}
2020-09-06 19:08:50 +08:00
export function getNumberOrArrayOfNumbersValidator(noOfElements: number) {
if (Konva.isUnminified) {
return function <T>(val: T, attr: string): T | void {
let isNumber = Util._isNumber(val);
let isValidArray = Util._isArray(val) && val.length == noOfElements;
if (!isNumber && !isValidArray) {
Util.warn(
2021-04-30 22:24:27 +08:00
_formatValue(val) +
2020-09-06 19:08:50 +08:00
' is a not valid value for "' +
attr +
2021-04-30 22:24:27 +08:00
'" attribute. The value should be a number or Array<number>(' +
noOfElements +
')'
2020-09-06 19:08:50 +08:00
);
}
return val;
};
}
}
2019-02-25 01:06:04 +08:00
export function getNumberOrAutoValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function <T extends string>(val: T, attr: string): T | void {
2019-02-25 01:06:04 +08:00
var isNumber = Util._isNumber(val);
var isAuto = val === 'auto';
if (!(isNumber || isAuto)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a number or "auto".'
);
}
return val;
};
}
}
2019-02-25 01:06:04 +08:00
export function getStringValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function (val: any, attr: string) {
2019-02-25 01:06:04 +08:00
if (!Util._isString(val)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a string.'
);
}
return val;
};
}
}
export function getStringOrGradientValidator() {
if (Konva.isUnminified) {
return function (val: any, attr: string) {
const isString = Util._isString(val);
const isGradient =
2021-04-30 22:24:27 +08:00
Object.prototype.toString.call(val) === '[object CanvasGradient]' ||
(val && val.addColorStop);
if (!(isString || isGradient)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a string or a native gradient.'
);
}
return val;
};
}
}
2019-02-25 01:06:04 +08:00
export function getFunctionValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function (val: any, attr: string) {
2019-02-25 01:06:04 +08:00
if (!Util._isFunction(val)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a function.'
);
}
return val;
};
}
}
export function getNumberArrayValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function (val: any, attr: string) {
if (val instanceof Uint8Array || val instanceof Uint16Array || val instanceof Uint32Array || val instanceof Uint8ClampedArray) {
return val
}
2019-02-25 01:06:04 +08:00
if (!Util._isArray(val)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a array of numbers.'
);
} else {
val.forEach(function (item: any) {
2019-02-25 01:06:04 +08:00
if (!Util._isNumber(item)) {
Util.warn(
'"' +
attr +
'" attribute has non numeric element ' +
item +
'. Make sure that all elements are numbers.'
);
}
});
}
return val;
};
}
}
export function getBooleanValidator() {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function (val: any, attr: string) {
2019-02-25 01:06:04 +08:00
var isBool = val === true || val === false;
if (!isBool) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a boolean.'
);
}
return val;
};
}
}
2019-05-11 20:56:55 +08:00
export function getComponentValidator(components: any) {
2019-03-07 11:19:32 +08:00
if (Konva.isUnminified) {
return function (val: any, attr: string) {
2019-02-25 01:06:04 +08:00
if (!Util.isObject(val)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be an object with properties ' +
components
);
}
return val;
};
}
}