-
Notifications
You must be signed in to change notification settings - Fork 482
/
setPropertyDropdown.js
346 lines (328 loc) · 10 KB
/
setPropertyDropdown.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* This file manages logic for the dropdown used in our setProperty block
*/
import _ from 'lodash';
import {getFirstParam, getSecondParam} from '../dropletUtils';
import library from './designElements/library';
import getAssetDropdown from '../assetManagement/getAssetDropdown';
var ElementType = library.ElementType;
/**
* A set of all properties that can be set using setProperty. Elsewhere, we
* filter these according to element type. Note: There are some properties
* where friendlyName is shared (canvasWidth and width both use 'width') but
* internalName is not.
* Note: The order here affects the order of items in the dropdown for unknown
* element types
* friendlyName: Name used in the code editor to refer to this property
* internalName: Name used in updateProperty to refer to this property
* type: Type of this property, used for validation at run time.
* alias (optional): True if this property should not be displayed to the user
* in the drop down list of properties
*/
var PROP_INFO = {
width: { friendlyName: 'width', internalName: 'style-width', type: 'number'},
height: { friendlyName: 'height', internalName: 'style-height', type: 'number' },
canvasWidth: { friendlyName: 'width', internalName: 'width', type: 'number'},
canvasHeight: { friendlyName: 'height', internalName: 'height', type: 'number' },
x: { friendlyName: 'x', internalName: 'left', type: 'number' },
y: { friendlyName: 'y', internalName: 'top', type: 'number' },
textColor: { friendlyName: 'text-color', internalName: 'textColor', type: 'string' },
backgroundColor: { friendlyName: 'background-color', internalName: 'backgroundColor', type: 'string' },
fontSize: { friendlyName: 'font-size', internalName: 'fontSize', type: 'number' },
textAlign: { friendlyName: 'text-align', internalName: 'textAlign', type: 'string' },
hidden: { friendlyName: 'hidden', internalName: 'hidden', type: 'boolean' },
text: { friendlyName: 'text', internalName: 'text', type: 'string' },
placeholder: { friendlyName: 'placeholder', internalName: 'placeholder', type: 'string' },
image: { friendlyName: 'image', internalName: 'image', type: 'string' },
screenImage: { friendlyName: 'image', internalName: 'screen-image', type: 'string' },
// pictureImage and picture both map to 'picture' internally, but allow us to accept
// either 'image' or 'picture' as the property name. picture is marked as an alias so
// it won't show up in the dropdown.
pictureImage: { friendlyName: 'image', internalName: 'picture', type: 'string' },
picture: { friendlyName: 'picture', internalName: 'picture', type: 'string', alias: true },
groupId: { friendlyName: 'group-id', internalName: 'groupId', type: 'string' },
checked: { friendlyName: 'checked', internalName: 'checked', type: 'boolean' },
readonly: { friendlyName: 'readonly', internalName: 'readonly', type: 'boolean' },
options: { friendlyName: 'options', internalName: 'options', type: 'array' },
value: { friendlyName: 'value', internalName: 'defaultValue', type: 'number' },
min: { friendlyName: 'min', internalName: 'min', type: 'number' },
max: { friendlyName: 'max', internalName: 'max', type: 'number' },
step: { friendlyName: 'step', internalName: 'step', type: 'number' }
};
// When we don't know the element type, we display all possible friendly names
var fullDropdownOptions = _.uniq(Object.keys(PROP_INFO).map(function (key) {
return '"' + PROP_INFO[key].friendlyName + '"';
}));
/**
* Information about properties pertaining to each element type. Values have the following
* fields. The latter two fields (friendlyNames and infoForFriendlyNames) are initialized
* through code run at startup time.
* propertyNames: a list of names (keys for PROP_INFO) associated with that element
* dropdownOptions: an array of property friendly names to be shown to the user for that element
* infoForFriendlyName: map from all friendly names for that element to property info
*/
var PROPERTIES = {};
PROPERTIES[ElementType.BUTTON] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'textColor',
'backgroundColor',
'fontSize',
'textAlign',
'image',
'hidden'
]
};
PROPERTIES[ElementType.TEXT_INPUT] = {
propertyNames: [
'text',
'placeholder',
'width',
'height',
'x',
'y',
'textColor',
'backgroundColor',
'fontSize',
'textAlign',
'hidden'
]
};
PROPERTIES[ElementType.LABEL] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'textColor',
'backgroundColor',
'fontSize',
'textAlign',
'hidden'
]
};
PROPERTIES[ElementType.DROPDOWN] = {
propertyNames: [
'text',
'options',
'width',
'height',
'x',
'y',
'textColor',
'backgroundColor',
'fontSize',
'textAlign',
'hidden'
]
};
PROPERTIES[ElementType.RADIO_BUTTON] = {
propertyNames: [
'text',
'groupId',
'width',
'height',
'x',
'y',
'hidden',
'checked'
]
};
PROPERTIES[ElementType.CHECKBOX] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'hidden',
'checked'
]
};
PROPERTIES[ElementType.IMAGE] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'pictureImage',
'picture', // Since this is an alias, it is not shown in the dropdown but is allowed as a value
'hidden'
]
};
PROPERTIES[ElementType.CANVAS] = {
propertyNames: [
'text',
'canvasWidth',
'canvasHeight',
'x',
'y'
]
};
PROPERTIES[ElementType.SCREEN] = {
propertyNames: [
'text',
'backgroundColor',
'screenImage'
]
};
PROPERTIES[ElementType.TEXT_AREA] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'textColor',
'backgroundColor',
'fontSize',
'textAlign',
'readonly',
'hidden'
]
};
PROPERTIES[ElementType.CHART] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'hidden'
]
};
PROPERTIES[ElementType.SLIDER] = {
propertyNames: [
'text',
'width',
'height',
'x',
'y',
'value',
'min',
'max',
'step',
'hidden'
]
};
// Initialize dropdownOptions and infoForFriendlyNames fields in PROPERTIES map.
for (var elementType in PROPERTIES) {
var elementProperties = PROPERTIES[elementType];
elementProperties.dropdownOptions = [];
elementProperties.infoForFriendlyName = {};
elementProperties.propertyNames.forEach(function (propName) {
var friendlyName = PROP_INFO[propName].friendlyName;
if (elementProperties.infoForFriendlyName[friendlyName]) {
throw new Error('Multiple props for friendlyName: ' + friendlyName +
' in elementType: ' + elementType);
}
elementProperties.infoForFriendlyName[friendlyName] = PROP_INFO[propName];
if (!PROP_INFO[propName].alias) {
elementProperties.dropdownOptions.push('"' + friendlyName + '"');
}
});
}
/**
* @param {DropletBlock} block Droplet block, or undefined if in text mode
* @param {AceEditor}
*/
function getFirstSetPropertyParam(block, editor) {
return getFirstParam('setProperty', block, editor);
}
/**
* @param {DropletBlock} block Droplet block, or undefined if in text mode
* @param {AceEditor}
*/
function getSecondSetPropertyParam(block, editor) {
return getSecondParam('setProperty', block, editor);
}
/**
* Given a string like <"asdf"> strips quotes and returns <asdf>
*/
function stripQuotes(str) {
var match = str.match(/^(['"])(.*)\1$/);
if (match) {
return match[2];
}
return str;
}
/**
* Gets the properties that should be shown in the dropdown list for elements of the given type.
* @param {string} elementType
* @returns {!Array<string>} list of quoted property names
*/
function getDropdownProperties(elementType) {
if (!elementType) {
return fullDropdownOptions;
}
if (!(elementType in PROPERTIES)) {
return fullDropdownOptions;
}
return PROPERTIES[elementType].dropdownOptions;
}
/**
* Given an element and a friendly name for that element, returns an object
* containing the internal equivalent for that friendly name, or undefined
* if we don't have info for this element/property.
*/
export function getInternalPropertyInfo(element, friendlyPropName) {
var elementType = library.getElementType(element, true);
var info;
if (elementType) {
info = PROPERTIES[elementType].infoForFriendlyName[friendlyPropName];
}
return info;
}
/**
* @returns {function} Gets the value of the second param for this block, checks
* if it's an image, and then displays the image selector. If it can't determine element
* types, displays the value 100, which is the default value in droplet config.
*/
export function setImageSelector() {
const dropletConfigDefaultValue = "100";
return function (editor) {
const param2 = getSecondSetPropertyParam(this.parent, editor);
if (!param2) {
return [dropletConfigDefaultValue];
}
const formattedParam = stripQuotes(param2);
if (formattedParam === "image") {
return getAssetDropdown('image');
} else {
return [dropletConfigDefaultValue];
}
};
}
/**
* @returns {function} Gets the value of the first param for this block, gets
* the element that it refers to, and then enumerates a list of possible
* properties that can be set on this element. If it can't determine element
* types, provides full list of properties across all types.
*/
export function setPropertyDropdown() {
return function (editor) {
// Note: We depend on "this" being the droplet socket when in block mode,
// such that parent ends up being the block. In text mode, this.parent
// ends up being undefined.
var param1 = getFirstSetPropertyParam(this.parent, editor);
if (!param1) {
return fullDropdownOptions;
}
var elementId = stripQuotes(param1);
var element = document.querySelector("#divApplab #" + elementId);
if (!element) {
return fullDropdownOptions;
}
return getDropdownProperties(library.getElementType(element));
};
}
export var __TestInterface = {
stripQuotes: stripQuotes,
getDropdownProperties: getDropdownProperties
};