forked from NetsBlox/Snap--Build-Your-Own-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblocks-ext.js
More file actions
428 lines (369 loc) · 12.6 KB
/
Copy pathblocks-ext.js
File metadata and controls
428 lines (369 loc) · 12.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* global nop, DialogBoxMorph, ScriptsMorph, BlockMorph, InputSlotMorph, StringMorph, Color
ReporterBlockMorph, CommandBlockMorph, MultiArgMorph, SnapActions, isNil,
ReporterSlotMorph, RingMorph, SyntaxElementMorph, contains, world, utils*/
// Extensions to the Snap blocks
// support for help dialogbox on service blocks
BlockMorph.prototype._showHelp = BlockMorph.prototype.showHelp;
BlockMorph.prototype.showHelp = function() {
if (!this.isServiceBlock()) return this._showHelp();
var myself = this,
help,
block,
inputs = this.inputs(),
serviceName = inputs[0].evaluate(),
methodName = inputs[1].evaluate()[0],
metadata = JSON.parse(RPCInputSlotMorph.prototype.getURL.call(this, '/rpc/' + serviceName));
// build the help message
if (serviceName !== '') {
// service description will go here
// if a method is selected append rpc specific description
if (methodName !== '') {
metadata = metadata.rpcs[methodName];
help = metadata.description;
// add argument descriptions, if available
var args = metadata.args;
for (var i = 0; i < args.length; i++) {
var arg = args[i];
if (arg.description) {
var optionalStr = arg.optional ? '[optional]' : '';
help += '\n' + arg.name + ': ' + arg.description + ' ' + optionalStr;
}
}
} else { // get service description
help = metadata.description;
}
if (!help) help = 'Description not available';
} else {
help = 'Get information from different providers, save information and more. \nTo get more help select one of the services:'
+ metadata.slice(0,3).join(', ') + ' ...';
}
block = this.fullCopy();
block.addShadow();
new DialogBoxMorph().inform(
'Help',
help,
myself.world(),
block.fullImage()
);
};
BlockMorph.prototype.isServiceBlock = function() {
var serviceSpecs = ['getJSFromRPCStruct', 'doRunRPC'];
return contains(serviceSpecs, this.selector);
};
MultiHintArgMorph.prototype = new MultiArgMorph();
MultiHintArgMorph.prototype.constructor = MultiHintArgMorph;
MultiHintArgMorph.uber = MultiArgMorph.prototype;
// MultiHintArgMorph preferences settings:
MultiHintArgMorph.prototype.executeOnSliderEdit = false;
function MultiHintArgMorph(
hintText,
labelTxt,
min,
eSpec,
arrowColor,
labelColor,
shadowColor,
shadowOffset,
isTransparent
) {
this.init(
hintText,
labelTxt,
min,
eSpec,
arrowColor,
labelColor,
shadowColor,
shadowOffset,
isTransparent
);
}
MultiHintArgMorph.prototype.init = function(
hintText,
labelTxt,
min,
eSpec,
arrowColor,
labelColor,
shadowColor,
shadowOffset,
isTransparent
) {
this.hintText = hintText || '';
MultiHintArgMorph.uber.init.call(
this,
'%s', // all multi hint args are strings
labelTxt,
min,
eSpec,
arrowColor,
labelColor,
shadowColor,
shadowOffset,
isTransparent
);
if (this.inputs().length === 0) this.addInput();
};
MultiHintArgMorph.prototype.addInput = function () {
var newPart = this.labelPart('%hint' + this.hintText),
idx = this.children.length - 1;
newPart.parent = this;
this.children.splice(idx, 0, newPart);
newPart.drawNew();
this.fixLayout();
};
StructInputSlotMorph.prototype = new InputSlotMorph();
StructInputSlotMorph.prototype.constructor = StructInputSlotMorph;
StructInputSlotMorph.uber = InputSlotMorph.prototype;
// StructInputSlotMorph preferences settings:
StructInputSlotMorph.prototype.executeOnSliderEdit = false;
function StructInputSlotMorph(
value,
isNumeric,
choiceDict,
fieldValues,
isReadOnly
) {
this.fields = [];
this.fieldContent = [];
this.getFieldNames = typeof fieldValues === 'string' ? this[fieldValues] : fieldValues || nop;
InputSlotMorph.call(this, value, isNumeric, choiceDict, isReadOnly);
this.isStatic = true;
}
StructInputSlotMorph.prototype.evaluate = function() {
var myself = this;
return [
StructInputSlotMorph.uber.evaluate.call(myself),
myself.fields
];
};
StructInputSlotMorph.prototype.setContents = function(name, values) {
// Set the value for the dropdown
InputSlotMorph.prototype.setContents.call(this, name);
if (this.parent) { // update fields
var children = this.parent.children,
myIndex = children.indexOf(this),
currentFields = this.fields,
i = currentFields.length + myIndex + 1,
input = children[--i],
removed = [],
scripts = this.parentThatIsA(ScriptsMorph),
inputs = this.parent.inputs(),
myInpIndex = inputs.indexOf(this);
// Remove the "i" fields after the current morph
for (i = 0; i < this.fieldContent.length; i++) {
input = inputs[myInpIndex +1 + i];
removed.push(input);
this.parent.removeChild(input);
// remove the field
this.parent.removeChild(this.fieldContent[i]);
}
this.fields = this.getFieldNames(name);
if (scripts) {
removed
.filter(function(arg) {
return arg instanceof BlockMorph;
})
.forEach(scripts.add.bind(scripts));
}
// Create new struct fields
values = values || [];
this.fieldContent = [];
for (i = 0; i < this.fields.length; i++) {
this.fieldContent.push(this.updateField(this.fields[i], values[i]));
}
inputs = this.parent.inputs();
for (i = this.fields.length; i < values.length && i < inputs.length; i++) {
inputs[i].setContents(values[i]);
}
this.fixLayout();
this.drawNew();
this.parent.cachedInputs = null;
this.parent.fixLayout();
this.parent.changed();
}
};
StructInputSlotMorph.prototype.getFieldValue = function(fieldname, value) {
// Input slot is empty or has a string
if (!value || typeof value === 'string') {
var result = new HintInputSlotMorph(value || '', fieldname);
return result;
}
return value; // The input slot is occupied by another block
};
StructInputSlotMorph.prototype.setDefaultFieldArg = function(index) {
// Reset the field and return it
var isStructField = index < this.fields.length,
parentIndex,
arg;
if (isStructField) {
parentIndex = this.parent.children.indexOf(this) + index + 1;
arg = this.fieldContent[index] = this.getFieldValue(this.fields[index]);
this.parent.children.splice(parentIndex, 1, arg);
arg.parent = this.parent;
}
arg.drawNew();
arg.fixLayout();
arg.drawNew();
this.parent.drawNew();
this.parent.fixLayout();
this.parent.drawNew();
return arg;
};
StructInputSlotMorph.prototype.updateField = function(field, value) {
// Create the input slot w/ greyed out text
// Value is either:
// + scripts
// + blocks
// + values
// + colors
// + undefined
// + string
// Add the fields at the correct place wrt the current morph
var index = this.parent.children.indexOf(this) + this.fields.indexOf(field) + 1,
result = this.getFieldValue(field, value);
this.parent.children.splice(index, 0, result);
result.parent = this.parent;
return result;
};
RPCInputSlotMorph.prototype = new StructInputSlotMorph();
RPCInputSlotMorph.prototype.constructor = RPCInputSlotMorph;
RPCInputSlotMorph.uber = StructInputSlotMorph.prototype;
function RPCInputSlotMorph() {
StructInputSlotMorph.call(
this,
null,
false,
'methodSignature',
function(rpcMethod) {
if (!this.fieldsFor || !this.fieldsFor[rpcMethod]) {
try {
this.methodSignature();
} catch (e) { // let the projects load when the service is not supported
/* eslint-disable no-console */
console.error(e);
/* eslint-enable no-console */
world.children[0].showMessage && world.children[0].showMessage(e.message);
this.fieldsFor = {};
}
}
if (this.fieldsFor[rpcMethod]) {
return this.fieldsFor[rpcMethod].args.map(function(arg) {
return arg.name;
});
} else { // the requested action is undefined
return [];
}
},
true
);
}
RPCInputSlotMorph.prototype.getRPCName = function () {
var fields = this.parent.inputs(),
field,
i;
// assume that the rpc is right before this input
i = fields.indexOf(this);
field = fields[i-1];
if (field) {
return field.evaluate();
}
return null;
};
// sets this.fieldsFor and returns the method signature dict
RPCInputSlotMorph.prototype.methodSignature = function () {
var actionNames,
rpc,
dict = {};
rpc = this.getRPCName();
if (rpc) {
// stores information on a specific service's rpcs
try {
this.fieldsFor = JSON.parse(utils.getUrlSyncCached('/rpc/' + rpc)).rpcs;
} catch (e) {
throw new Error('Service "' + rpc + '" is not available');
}
actionNames = Object.keys(this.fieldsFor);
for (var i = actionNames.length; i--;) {
var aName = actionNames[i];
if (!this.fieldsFor[aName].deprecated) dict[aName] = aName;
}
}
return dict;
};
// HintInputSlotMorph //////////////////////////////////////////////
// I am an input slot with greyed out hint text when I am empty
HintInputSlotMorph.prototype = new InputSlotMorph();
HintInputSlotMorph.prototype.constructor = HintInputSlotMorph;
HintInputSlotMorph.uber = InputSlotMorph.prototype;
function HintInputSlotMorph(text, hint, isNumeric) {
var self = this;
this.hintText = hint;
this.empty = true;
InputSlotMorph.call(this, text, isNumeric);
// If the StringMorph gets clicked on when empty, the hint text
// should be "ghostly"
this.contents().mouseClickLeft = function() {
if (self.empty) {
this.text = '';
}
StringMorph.prototype.mouseClickLeft.apply(this, arguments);
};
}
HintInputSlotMorph.prototype.evaluate = function() {
if (this.empty) { // ignore grey text
return this.isNumeric ? 0 : '';
}
return InputSlotMorph.prototype.evaluate.call(this);
};
HintInputSlotMorph.prototype.setContents = function(value) {
var color = new Color(0, 0, 0),
contents = this.contents();
// If empty, set to the hint text
InputSlotMorph.prototype.setContents.apply(this, arguments);
this.empty = value === '';
if (this.empty) { // Set the contents to the hint text
// Set the text to the hint text
contents.text = this.hintText;
color = new Color(100, 100, 100);
}
contents.color = color;
contents.drawNew();
};
// Check if the given morph has been changed
HintInputSlotMorph.prototype.changed = function() {
var txtMorph = this.contents();
if (txtMorph) {
this.empty = txtMorph.text === this.hintText;
}
return InputSlotMorph.prototype.changed.call(this);
};
var addStructReplaceSupport = function(fn) {
return function(arg) {
var structInput,
structInputIndex = -1,
inputs = this.inputs(),
inputIndex = inputs.indexOf(arg),
relIndex;
// Check if 'arg' follows a MessageInputSlotMorph (these are a special case)
for (var i = inputs.length; i--;) {
if (inputs[i] instanceof StructInputSlotMorph) {
structInputIndex = i;
structInput = inputs[i];
}
}
if (structInput && structInputIndex < inputIndex &&
structInput.fields.length >= inputIndex - structInputIndex) {
relIndex = inputIndex - structInputIndex - 1;
var defaultArg = structInput.setDefaultFieldArg(relIndex);
this.silentReplaceInput(arg, defaultArg);
this.cachedInputs = null;
} else {
fn.apply(this, arguments);
}
};
};
ReporterBlockMorph.prototype.revertToDefaultInput =
addStructReplaceSupport(ReporterBlockMorph.prototype.revertToDefaultInput);
CommandBlockMorph.prototype.revertToDefaultInput =
addStructReplaceSupport(CommandBlockMorph.prototype.revertToDefaultInput);