1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.getComponentValidator = exports.getBooleanValidator = exports.getNumberArrayValidator = exports.getFunctionValidator = exports.getStringOrGradientValidator = exports.getStringValidator = exports.getNumberOrAutoValidator = exports.getNumberOrArrayOfNumbersValidator = exports.getNumberValidator = exports.alphaComponent = exports.RGBComponent = void 0;
|
---|
4 | const Global_1 = require("./Global");
|
---|
5 | const Util_1 = require("./Util");
|
---|
6 | function _formatValue(val) {
|
---|
7 | if (Util_1.Util._isString(val)) {
|
---|
8 | return '"' + val + '"';
|
---|
9 | }
|
---|
10 | if (Object.prototype.toString.call(val) === '[object Number]') {
|
---|
11 | return val;
|
---|
12 | }
|
---|
13 | if (Util_1.Util._isBoolean(val)) {
|
---|
14 | return val;
|
---|
15 | }
|
---|
16 | return Object.prototype.toString.call(val);
|
---|
17 | }
|
---|
18 | function RGBComponent(val) {
|
---|
19 | if (val > 255) {
|
---|
20 | return 255;
|
---|
21 | }
|
---|
22 | else if (val < 0) {
|
---|
23 | return 0;
|
---|
24 | }
|
---|
25 | return Math.round(val);
|
---|
26 | }
|
---|
27 | exports.RGBComponent = RGBComponent;
|
---|
28 | function alphaComponent(val) {
|
---|
29 | if (val > 1) {
|
---|
30 | return 1;
|
---|
31 | }
|
---|
32 | else if (val < 0.0001) {
|
---|
33 | return 0.0001;
|
---|
34 | }
|
---|
35 | return val;
|
---|
36 | }
|
---|
37 | exports.alphaComponent = alphaComponent;
|
---|
38 | function getNumberValidator() {
|
---|
39 | if (Global_1.Konva.isUnminified) {
|
---|
40 | return function (val, attr) {
|
---|
41 | if (!Util_1.Util._isNumber(val)) {
|
---|
42 | Util_1.Util.warn(_formatValue(val) +
|
---|
43 | ' is a not valid value for "' +
|
---|
44 | attr +
|
---|
45 | '" attribute. The value should be a number.');
|
---|
46 | }
|
---|
47 | return val;
|
---|
48 | };
|
---|
49 | }
|
---|
50 | }
|
---|
51 | exports.getNumberValidator = getNumberValidator;
|
---|
52 | function getNumberOrArrayOfNumbersValidator(noOfElements) {
|
---|
53 | if (Global_1.Konva.isUnminified) {
|
---|
54 | return function (val, attr) {
|
---|
55 | let isNumber = Util_1.Util._isNumber(val);
|
---|
56 | let isValidArray = Util_1.Util._isArray(val) && val.length == noOfElements;
|
---|
57 | if (!isNumber && !isValidArray) {
|
---|
58 | Util_1.Util.warn(_formatValue(val) +
|
---|
59 | ' is a not valid value for "' +
|
---|
60 | attr +
|
---|
61 | '" attribute. The value should be a number or Array<number>(' +
|
---|
62 | noOfElements +
|
---|
63 | ')');
|
---|
64 | }
|
---|
65 | return val;
|
---|
66 | };
|
---|
67 | }
|
---|
68 | }
|
---|
69 | exports.getNumberOrArrayOfNumbersValidator = getNumberOrArrayOfNumbersValidator;
|
---|
70 | function getNumberOrAutoValidator() {
|
---|
71 | if (Global_1.Konva.isUnminified) {
|
---|
72 | return function (val, attr) {
|
---|
73 | var isNumber = Util_1.Util._isNumber(val);
|
---|
74 | var isAuto = val === 'auto';
|
---|
75 | if (!(isNumber || isAuto)) {
|
---|
76 | Util_1.Util.warn(_formatValue(val) +
|
---|
77 | ' is a not valid value for "' +
|
---|
78 | attr +
|
---|
79 | '" attribute. The value should be a number or "auto".');
|
---|
80 | }
|
---|
81 | return val;
|
---|
82 | };
|
---|
83 | }
|
---|
84 | }
|
---|
85 | exports.getNumberOrAutoValidator = getNumberOrAutoValidator;
|
---|
86 | function getStringValidator() {
|
---|
87 | if (Global_1.Konva.isUnminified) {
|
---|
88 | return function (val, attr) {
|
---|
89 | if (!Util_1.Util._isString(val)) {
|
---|
90 | Util_1.Util.warn(_formatValue(val) +
|
---|
91 | ' is a not valid value for "' +
|
---|
92 | attr +
|
---|
93 | '" attribute. The value should be a string.');
|
---|
94 | }
|
---|
95 | return val;
|
---|
96 | };
|
---|
97 | }
|
---|
98 | }
|
---|
99 | exports.getStringValidator = getStringValidator;
|
---|
100 | function getStringOrGradientValidator() {
|
---|
101 | if (Global_1.Konva.isUnminified) {
|
---|
102 | return function (val, attr) {
|
---|
103 | const isString = Util_1.Util._isString(val);
|
---|
104 | const isGradient = Object.prototype.toString.call(val) === '[object CanvasGradient]' ||
|
---|
105 | (val && val.addColorStop);
|
---|
106 | if (!(isString || isGradient)) {
|
---|
107 | Util_1.Util.warn(_formatValue(val) +
|
---|
108 | ' is a not valid value for "' +
|
---|
109 | attr +
|
---|
110 | '" attribute. The value should be a string or a native gradient.');
|
---|
111 | }
|
---|
112 | return val;
|
---|
113 | };
|
---|
114 | }
|
---|
115 | }
|
---|
116 | exports.getStringOrGradientValidator = getStringOrGradientValidator;
|
---|
117 | function getFunctionValidator() {
|
---|
118 | if (Global_1.Konva.isUnminified) {
|
---|
119 | return function (val, attr) {
|
---|
120 | if (!Util_1.Util._isFunction(val)) {
|
---|
121 | Util_1.Util.warn(_formatValue(val) +
|
---|
122 | ' is a not valid value for "' +
|
---|
123 | attr +
|
---|
124 | '" attribute. The value should be a function.');
|
---|
125 | }
|
---|
126 | return val;
|
---|
127 | };
|
---|
128 | }
|
---|
129 | }
|
---|
130 | exports.getFunctionValidator = getFunctionValidator;
|
---|
131 | function getNumberArrayValidator() {
|
---|
132 | if (Global_1.Konva.isUnminified) {
|
---|
133 | return function (val, attr) {
|
---|
134 | const TypedArray = Int8Array ? Object.getPrototypeOf(Int8Array) : null;
|
---|
135 | if (TypedArray && val instanceof TypedArray) {
|
---|
136 | return val;
|
---|
137 | }
|
---|
138 | if (!Util_1.Util._isArray(val)) {
|
---|
139 | Util_1.Util.warn(_formatValue(val) +
|
---|
140 | ' is a not valid value for "' +
|
---|
141 | attr +
|
---|
142 | '" attribute. The value should be a array of numbers.');
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | val.forEach(function (item) {
|
---|
146 | if (!Util_1.Util._isNumber(item)) {
|
---|
147 | Util_1.Util.warn('"' +
|
---|
148 | attr +
|
---|
149 | '" attribute has non numeric element ' +
|
---|
150 | item +
|
---|
151 | '. Make sure that all elements are numbers.');
|
---|
152 | }
|
---|
153 | });
|
---|
154 | }
|
---|
155 | return val;
|
---|
156 | };
|
---|
157 | }
|
---|
158 | }
|
---|
159 | exports.getNumberArrayValidator = getNumberArrayValidator;
|
---|
160 | function getBooleanValidator() {
|
---|
161 | if (Global_1.Konva.isUnminified) {
|
---|
162 | return function (val, attr) {
|
---|
163 | var isBool = val === true || val === false;
|
---|
164 | if (!isBool) {
|
---|
165 | Util_1.Util.warn(_formatValue(val) +
|
---|
166 | ' is a not valid value for "' +
|
---|
167 | attr +
|
---|
168 | '" attribute. The value should be a boolean.');
|
---|
169 | }
|
---|
170 | return val;
|
---|
171 | };
|
---|
172 | }
|
---|
173 | }
|
---|
174 | exports.getBooleanValidator = getBooleanValidator;
|
---|
175 | function getComponentValidator(components) {
|
---|
176 | if (Global_1.Konva.isUnminified) {
|
---|
177 | return function (val, attr) {
|
---|
178 | if (val === undefined || val === null) {
|
---|
179 | return val;
|
---|
180 | }
|
---|
181 | if (!Util_1.Util.isObject(val)) {
|
---|
182 | Util_1.Util.warn(_formatValue(val) +
|
---|
183 | ' is a not valid value for "' +
|
---|
184 | attr +
|
---|
185 | '" attribute. The value should be an object with properties ' +
|
---|
186 | components);
|
---|
187 | }
|
---|
188 | return val;
|
---|
189 | };
|
---|
190 | }
|
---|
191 | }
|
---|
192 | exports.getComponentValidator = getComponentValidator;
|
---|