-
Notifications
You must be signed in to change notification settings - Fork 0
/
item-props.ts
197 lines (169 loc) · 4.82 KB
/
item-props.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { InvalidParamError, ReadOnlyError } from 'internal/errors';
import hasRequiredKeys from 'helpers/has-required-keys';
type KeyValuePair = {
[key: string]: unknown;
};
type Position = {
left: number;
top: number;
right: number;
bottom: number;
};
/**
* An object that contains commonly used item properties
*/
const ItemProps = {
keepAspectRatio: {
key: 'prop:keep_ar',
setValidator: (value: boolean): boolean => {
if (typeof value !== 'boolean') {
throw new InvalidParamError(`${value} should be a boolean`);
}
return true;
},
setTransformer: (value: boolean): string => (value ? '1' : '0'),
getTransformer: (value: string): boolean => value === '1',
},
transparency: {
key: 'prop:alpha',
setValidator: (value: number): boolean => {
if (typeof value !== 'number') {
throw new InvalidParamError(`${value} should be a number`);
}
if (value < 0 || value > 255) {
throw new Error('Transparency may only be in the range 0-255.');
}
return true;
},
setTransformer: (value: number): string => String(value),
getTransformer: (value: string): number => parseInt(value),
},
browser60fps: {
key: 'prop:Browser60fps',
setValidator: (value: boolean): boolean => {
if (typeof value !== 'boolean') {
throw new InvalidParamError(`${value} should be a boolean`);
}
return true;
},
setTransformer: (value: boolean): string => (value ? '1' : '0'),
getTransformer: (value: string): boolean => value === '1',
},
customName: {
key: 'prop:cname',
setValidator: (name: string): boolean => {
if (typeof name !== 'string') {
throw new InvalidParamError(`${name} should be a string`);
}
return true;
},
},
position: {
key: 'prop:pos',
setValidator: (pos: Position): boolean => {
const [isValidParam, missingKeys] = hasRequiredKeys(pos, [
'left',
'top',
'right',
'bottom',
]);
if (isValidParam) {
throw new InvalidParamError(`Missing keys: ${missingKeys.join(', ')}`);
}
return true;
},
setTransformer: (pos: Position): string => {
const parsed = Object.keys(pos).reduce(
(stack: KeyValuePair, key: string) => {
const decimal = Number(pos[key]) / 100;
return {
...stack,
[key]: Math.min(Math.max(0, decimal), 1).toFixed(1),
};
},
{}
);
return `${parsed.left},${parsed.top},${parsed.right},${parsed.bottom}`;
},
getTransformer: (value: string): Position => {
const posArray = String(value).split(',');
const order = ['left', 'top', 'right', 'bottom'];
// Convert them to percents??
return posArray.reduce((stack: Position, pos: string, index: number) => {
return {
...stack,
[order[index]]: Number(pos) * 100,
};
}, {} as Position);
},
},
visibility: {
key: 'prop:visible',
setValidator: (isVisible: boolean): boolean => {
if (isVisible) {
throw new InvalidParamError(`${isVisible} should be a boolean`);
}
return true;
},
setTransformer: (isVisible: boolean): string => (isVisible ? '1' : '0'),
getTransformer: (isVisible: string): boolean => isVisible === '1',
},
item: {
key: 'prop:item',
},
srcid: {
key: 'prop:srcid',
},
type: {
key: 'prop:type',
setValidator: (params: { type: string; item: string }): boolean => {
const [isValidParam, missingKeys] = hasRequiredKeys(params, [
'type',
'item',
]);
if (isValidParam) {
throw new InvalidParamError(`Missing keys: ${missingKeys.join(', ')}`);
}
return true;
},
setTransformer: (params: { type: string; item: string }): string =>
`${params.type},${params.item}`,
getTransformer: (type: string): string => {
// @TODO: Should we just return strings???
const types = {
0x0: 'undefined',
0x1: 'file',
0x2: 'live',
0x3: 'text',
0x4: 'bitmap',
0x5: 'screen',
0x6: 'flash',
0x7: 'games',
0x8: 'html',
0x9: '3ds',
0xa: 'ppt',
0xb: 'scene',
0xc: 'group',
0xd: 'replay',
};
return types[type];
},
},
itemList: {
key: 'itemlist',
setValidator: (): void => {
throw new ReadOnlyError('Setting itemlist is not supported');
},
setTransformer: (): void => {
throw new ReadOnlyError('Setting itemlist is not supported');
},
getTransformer: (value: string): string[] => String(value).split(','),
},
fileInfo: {
key: 'FileInfo',
setValidator: (): void => {
throw new ReadOnlyError('Setting FileInfo is not supported');
},
},
};
export default ItemProps;