forked from jmoenig/Snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.js
12176 lines (10755 loc) · 335 KB
/
blocks.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
blocks.js
a programming construction kit
based on morphic.js
inspired by Scratch
written by Jens Mönig
Copyright (C) 2016 by Jens Mönig
This file is part of Snap!.
Snap! is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
prerequisites:
--------------
needs morphic.js
hierarchy
---------
the following tree lists all constructors hierarchically,
indentation indicating inheritance. Refer to this list to get a
contextual overview:
Morph*
ArrowMorph
BlockHighlightMorph
ScriptsMorph
SymbolMorph
SyntaxElementMorph
ArgMorph
ArgLabelMorph
BooleanSlotMorph
ColorSlotMorph
CommandSlotMorph
CSlotMorph
RingCommandSlotMorph
FunctionSlotMorph
ReporterSlotMorph
RingReporterSlotMorph
InputSlotMorph
TextSlotMorph
MultiArgMorph
TemplateSlotMorph
BlockMorph
CommandBlockMorph
HatBlockMorph
ReporterBlockMorph
RingMorph
BoxMorph*
CommentMorph
ScriptFocusMorph
* from morphic.js
toc
---
the following list shows the order in which all constructors are
defined. Use this list to locate code in this document:
SyntaxElementMorph
BlockMorph
CommandBlockMorph
HatBlockMorph
ReporterBlockMorph
RingMorph
ScriptsMorph
ArgMorph
CommandSlotMorph
RingCommandSlotMorph
CSlotMorph
InputSlotMorph
BooleanSlotMorph
ArrowMorph
TextSlotMorph
SymbolMorph
ColorSlotMorph
TemplateSlotMorph
BlockHighlightMorph
MultiArgMorph
ArgLabelMorph
FunctionSlotMorph
ReporterSlotMorph
RingReporterSlotMorph
CommentMorph
structure of syntax elements
----------------------------
the structure of syntax elements is identical with their morphic
tree. There are, however, accessor methods to get (only) the
parts which are relevant for evaluation wherever appropriate.
In Scratch/BYOB every sprite and the stage has its own "blocks bin",
an instance of ScriptsMorph (we're going to name it differently in
Snap, probably just "scripts").
At the top most level blocks are assembled into stacks in ScriptsMorph
instances. A ScriptsMorph contains nothing but blocks, therefore
every child of a ScriptsMorph is expected to be a block.
Each block contains:
selector - indicating the name of the function it triggers,
Its arguments are first evaluated and then passed along as the
selector is called. Arguments can be either instances of ArgMorph
or ReporterBlockMorph. The getter method for a block's arguments is
inputs() - gets an array of arg morphs and/or reporter blocks
in addition to inputs, command blocks also know their
nextBlock() - gets the block attached to the receiver's bottom
and the block they're attached to - if any: Their parent.
please also refer to the high-level comment at the beginning of each
constructor for further details.
*/
/*global Array, BoxMorph,
Color, ColorPaletteMorph, FrameMorph, Function, HandleMorph, Math, MenuMorph,
Morph, MorphicPreferences, Object, Point, ScrollFrameMorph, ShadowMorph,
String, StringMorph, TextMorph, WorldMorph, contains, degrees, detect,
document, getDocumentPositionOf, isNaN, isString, newCanvas, nop, parseFloat,
radians, useBlurredShadows, SpeechBubbleMorph, modules, StageMorph,
fontHeight, TableFrameMorph, SpriteMorph, Context, ListWatcherMorph,
CellMorph, DialogBoxMorph, BlockInputFragmentMorph, PrototypeHatBlockMorph,
Costume, IDE_Morph, BlockDialogMorph, BlockEditorMorph, localize, isNil,
isSnapObject, copy*/
// Global stuff ////////////////////////////////////////////////////////
modules.blocks = '2016-May-10';
var SyntaxElementMorph;
var BlockMorph;
var CommandBlockMorph;
var ReporterBlockMorph;
var ScriptsMorph;
var ArgMorph;
var CommandSlotMorph;
var CSlotMorph;
var InputSlotMorph;
var BooleanSlotMorph;
var ArrowMorph;
var ColorSlotMorph;
var HatBlockMorph;
var BlockHighlightMorph;
var MultiArgMorph;
var TemplateSlotMorph;
var FunctionSlotMorph;
var ReporterSlotMorph;
var RingMorph;
var RingCommandSlotMorph;
var RingReporterSlotMorph;
var SymbolMorph;
var CommentMorph;
var ArgLabelMorph;
var TextSlotMorph;
var ScriptFocusMorph;
WorldMorph.prototype.customMorphs = function () {
// add examples to the world's demo menu
return [];
/*
return [
new SymbolMorph(
'pipette',
50,
new Color(250, 250, 250),
new Point(-1, -1),
new Color(20, 20, 20)
)
];
*/
/*
var sm = new ScriptsMorph();
sm.setExtent(new Point(800, 600));
return [
new SymbolMorph(),
new HatBlockMorph(),
new CommandBlockMorph(),
sm,
new CommandSlotMorph(),
new CSlotMorph(),
new InputSlotMorph(),
new InputSlotMorph(null, true),
new BooleanSlotMorph(),
new ColorSlotMorph(),
new TemplateSlotMorph('foo'),
new ReporterBlockMorph(),
new ReporterBlockMorph(true),
new ArrowMorph(),
new MultiArgMorph(),
new FunctionSlotMorph(),
new ReporterSlotMorph(),
new ReporterSlotMorph(true),
// new DialogBoxMorph('Dialog Box'),
// new InputFieldMorph('Input Field')
new RingMorph(),
new RingCommandSlotMorph(),
new RingReporterSlotMorph(),
new RingReporterSlotMorph(true)
];
*/
};
// SyntaxElementMorph //////////////////////////////////////////////////
// I am the ancestor of all blocks and input slots
// SyntaxElementMorph inherits from Morph:
SyntaxElementMorph.prototype = new Morph();
SyntaxElementMorph.prototype.constructor = SyntaxElementMorph;
SyntaxElementMorph.uber = Morph.prototype;
// SyntaxElementMorph preferences settings:
/*
the following settings govern the appearance of all syntax elements
(blocks and slots) where applicable:
outline:
corner - radius of command block rounding
rounding - radius of reporter block rounding
edge - width of 3D-ish shading box
hatHeight - additional top space for hat blocks
hatWidth - minimum width for hat blocks
rfBorder - pixel width of reification border (grey outline)
minWidth - minimum width for any syntax element's contents
jigsaw shape:
inset - distance from indentation to left edge
dent - width of indentation bottom
paddings:
bottomPadding - adds to the width of the bottom most c-slot
cSlotPadding - adds to the width of the open "C" in c-slots
typeInPadding - adds pixels between text and edge in input slots
labelPadding - adds left/right pixels to block labels
label:
labelFontName - <string> specific font family name
labelFontStyle - <string> generic font family name, cascaded
fontSize - duh
embossing - <Point> offset for embossing effect
labelWidth - column width, used for word wrapping
labelWordWrap - <bool> if true labels can break after each word
dynamicInputLabels - <bool> if true inputs can have dynamic labels
snapping:
feedbackColor - <Color> for displaying drop feedbacks
feedbackMinHeight - height of white line for command block snaps
minSnapDistance - threshold when commands start snapping
reporterDropFeedbackPadding - increases reporter drop feedback
color gradients:
contrast - <percent int> 3D-ish shading gradient contrast
labelContrast - <percent int> 3D-ish label shading contrast
activeHighlight - <Color> for stack highlighting when active
errorHighlight - <Color> for error highlighting
activeBlur - <pixels int> shadow for blurred activeHighlight
activeBorder - <pixels int> unblurred activeHighlight
rfColor - <Color> for reified outlines and slot backgrounds
*/
SyntaxElementMorph.prototype.setScale = function (num) {
var scale = Math.min(Math.max(num, 1), 25);
this.scale = scale;
this.corner = 3 * scale;
this.rounding = 9 * scale;
this.edge = 1.000001 * scale;
this.inset = 6 * scale;
this.hatHeight = 12 * scale;
this.hatWidth = 70 * scale;
this.rfBorder = 3 * scale;
this.minWidth = 0;
this.dent = 8 * scale;
this.bottomPadding = 3 * scale;
this.cSlotPadding = 4 * scale;
this.typeInPadding = scale;
this.labelPadding = 4 * scale;
this.labelFontName = 'Verdana';
this.labelFontStyle = 'sans-serif';
this.fontSize = 10 * scale;
this.embossing = new Point(
-1 * Math.max(scale / 2, 1),
-1 * Math.max(scale / 2, 1)
);
this.labelWidth = 450 * scale;
this.labelWordWrap = true;
this.dynamicInputLabels = true;
this.feedbackColor = new Color(255, 255, 255);
this.feedbackMinHeight = 5;
this.minSnapDistance = 20;
this.reporterDropFeedbackPadding = 10 * scale;
this.contrast = 65;
this.labelContrast = 25;
this.activeHighlight = new Color(153, 255, 213);
this.errorHighlight = new Color(173, 15, 0);
this.activeBlur = 20;
this.activeBorder = 4;
this.rfColor = new Color(120, 120, 120);
};
SyntaxElementMorph.prototype.setScale(1);
SyntaxElementMorph.prototype.isCachingInputs = false;
// SyntaxElementMorph instance creation:
function SyntaxElementMorph() {
this.init();
}
SyntaxElementMorph.prototype.init = function (silently) {
this.cachedClr = null;
this.cachedClrBright = null;
this.cachedClrDark = null;
this.isStatic = false; // if true, I cannot be exchanged
SyntaxElementMorph.uber.init.call(this, silently);
this.defaults = [];
this.cachedInputs = null;
};
// SyntaxElementMorph accessing:
SyntaxElementMorph.prototype.parts = function () {
// answer my non-crontrol submorphs
var nb = null;
if (this.nextBlock) { // if I am a CommandBlock or a HatBlock
nb = this.nextBlock();
}
return this.children.filter(function (child) {
return (child !== nb)
&& !(child instanceof ShadowMorph)
&& !(child instanceof BlockHighlightMorph);
});
};
SyntaxElementMorph.prototype.inputs = function () {
// answer my arguments and nested reporters
if (isNil(this.cachedInputs) || !this.isCachingInputs) {
this.cachedInputs = this.parts().filter(function (part) {
return part instanceof SyntaxElementMorph;
});
}
// this.debugCachedInputs();
return this.cachedInputs;
};
SyntaxElementMorph.prototype.debugCachedInputs = function () {
// private - only used for manually debugging inputs caching
var realInputs, i;
if (!isNil(this.cachedInputs)) {
realInputs = this.parts().filter(function (part) {
return part instanceof SyntaxElementMorph;
});
}
if (this.cachedInputs.length !== realInputs.length) {
throw new Error('cached inputs size do not match: ' +
this.constructor.name);
}
for (i = 0; i < realInputs.length; i += 1) {
if (this.cachedInputs[i] !== realInputs[i]) {
throw new Error('cached input does not match: ' +
this.constructor.name +
' #' +
i +
' ' +
this.cachedInputs[i].constructor.name +
' != ' +
realInputs[i].constructor.name);
}
}
};
SyntaxElementMorph.prototype.allInputs = function () {
// answer arguments and nested reporters of all children
var myself = this;
return this.allChildren().slice(0).reverse().filter(
function (child) {
return (child instanceof ArgMorph) ||
(child instanceof ReporterBlockMorph &&
child !== myself);
}
);
};
SyntaxElementMorph.prototype.allEmptySlots = function () {
// answer empty input slots of all children excluding myself,
// but omit those in nested rings (lambdas) and JS-Function primitives.
// Used by the evaluator when binding implicit formal parameters
// to empty input slots
var empty = [];
if (!(this instanceof RingMorph) &&
(this.selector !== 'reportJSFunction')) {
this.children.forEach(function (morph) {
if (morph.isEmptySlot && morph.isEmptySlot()) {
empty.push(morph);
} else if (morph.allEmptySlots) {
empty = empty.concat(morph.allEmptySlots());
}
});
}
return empty;
};
SyntaxElementMorph.prototype.tagExitBlocks = function (stopTag, isCommand) {
// tag 'report' and 'stop this block' blocks of all children including
// myself, with either a stopTag (for "stop" blocks) or an indicator of
// being inside a command block definition, but omit those in nested
// rings (lambdas. Used by the evaluator when entering a procedure
if (this.selector === 'doReport') {
this.partOfCustomCommand = isCommand;
} else if (this.selector === 'doStopThis') {
this.exitTag = stopTag;
} else {
if (!(this instanceof RingMorph)) {
this.children.forEach(function (morph) {
if (morph.tagExitBlocks) {
morph.tagExitBlocks(stopTag, isCommand);
}
});
}
}
};
SyntaxElementMorph.prototype.replaceInput = function (oldArg, newArg) {
var scripts = this.parentThatIsA(ScriptsMorph),
replacement = newArg,
idx = this.children.indexOf(oldArg),
i = 0;
// try to find the ArgLabel embedding the newArg,
// used for the undrop() feature
if (idx === -1 && newArg instanceof MultiArgMorph) {
this.children.forEach(function (morph) {
if (morph instanceof ArgLabelMorph &&
morph.argMorph() === oldArg) {
idx = i;
}
i += 1;
});
}
if ((idx === -1) || (scripts === null)) {
return null;
}
if (oldArg.cachedSlotSpec) {oldArg.cachedSlotSpec = null; }
if (newArg.cachedSlotSpec) {newArg.cachedSlotSpec = null; }
this.startLayout();
if (newArg.parent) {
newArg.parent.removeChild(newArg);
}
if (oldArg instanceof MultiArgMorph) {
oldArg.inputs().forEach(function (inp) { // preserve nested reporters
oldArg.replaceInput(inp, new InputSlotMorph());
});
if (this.dynamicInputLabels) {
replacement = new ArgLabelMorph(newArg);
}
}
replacement.parent = this;
this.children[idx] = replacement;
if (oldArg instanceof ReporterBlockMorph) {
if (!(oldArg instanceof RingMorph)
|| (oldArg instanceof RingMorph && oldArg.contents())) {
scripts.add(oldArg);
oldArg.moveBy(replacement.extent());
oldArg.fixBlockColor();
}
}
if (replacement instanceof MultiArgMorph
|| replacement instanceof ArgLabelMorph
|| replacement.constructor === CommandSlotMorph) {
replacement.fixLayout();
if (this.fixLabelColor) { // special case for variadic continuations
this.fixLabelColor();
}
} else {
replacement.drawNew();
this.fixLayout();
}
this.cachedInputs = null;
this.endLayout();
};
SyntaxElementMorph.prototype.silentReplaceInput = function (oldArg, newArg) {
// used by the Serializer or when programatically
// changing blocks
var i = this.children.indexOf(oldArg),
replacement;
if (i === -1) {
return;
}
if (oldArg.cachedSlotSpec) {oldArg.cachedSlotSpec = null; }
if (newArg.cachedSlotSpec) {newArg.cachedSlotSpec = null; }
if (newArg.parent) {
newArg.parent.removeChild(newArg);
}
if (oldArg instanceof MultiArgMorph && this.dynamicInputLabels) {
replacement = new ArgLabelMorph(newArg);
} else {
replacement = newArg;
}
replacement.parent = this;
this.children[i] = replacement;
if (replacement instanceof MultiArgMorph
|| replacement instanceof ArgLabelMorph
|| replacement.constructor === CommandSlotMorph) {
replacement.fixLayout();
if (this.fixLabelColor) { // special case for variadic continuations
this.fixLabelColor();
}
} else {
replacement.drawNew();
this.fixLayout();
}
this.cachedInputs = null;
};
SyntaxElementMorph.prototype.revertToDefaultInput = function (arg, noValues) {
var idx = this.parts().indexOf(arg),
inp = this.inputs().indexOf(arg),
deflt = new InputSlotMorph();
if (idx !== -1) {
if (this instanceof BlockMorph) {
deflt = this.labelPart(this.parseSpec(this.blockSpec)[idx]);
if (deflt instanceof InputSlotMorph && this.definition) {
deflt.setChoices.apply(
deflt,
this.definition.inputOptionsOfIdx(inp)
);
deflt.setContents(
this.definition.defaultValueOfInputIdx(inp)
);
}
} else if (this instanceof MultiArgMorph) {
deflt = this.labelPart(this.slotSpec);
} else if (this instanceof ReporterSlotMorph) {
deflt = this.emptySlot();
}
}
// set default value
if (!noValues) {
if (inp !== -1) {
if (deflt instanceof MultiArgMorph) {
deflt.setContents(this.defaults);
deflt.defaults = this.defaults;
} else if (!isNil(this.defaults[inp])) {
deflt.setContents(this.defaults[inp]);
}
}
}
this.silentReplaceInput(arg, deflt);
if (deflt instanceof MultiArgMorph) {
deflt.refresh();
} else if (deflt instanceof RingMorph) {
deflt.fixBlockColor();
}
this.cachedInputs = null;
};
SyntaxElementMorph.prototype.isLocked = function () {
// answer true if I can be exchanged by a dropped reporter
return this.isStatic;
};
// SyntaxElementMorph enumerating:
SyntaxElementMorph.prototype.topBlock = function () {
if (this.parent && this.parent.topBlock) {
return this.parent.topBlock();
}
return this;
};
// SyntaxElementMorph reachable variables
SyntaxElementMorph.prototype.getVarNamesDict = function () {
var block = this.parentThatIsA(BlockMorph),
rcvr,
tempVars = [],
dict;
if (!block) {
return {};
}
rcvr = block.receiver();
block.allParents().forEach(function (morph) {
if (morph instanceof PrototypeHatBlockMorph) {
tempVars.push.apply(
tempVars,
morph.variableNames()
);
tempVars.push.apply(
tempVars,
morph.inputs()[0].inputFragmentNames()
);
} else if (morph instanceof BlockMorph) {
morph.inputs().forEach(function (inp) {
if (inp instanceof TemplateSlotMorph) {
tempVars.push(inp.contents());
} else if (inp instanceof MultiArgMorph) {
inp.children.forEach(function (m) {
if (m instanceof TemplateSlotMorph) {
tempVars.push(m.contents());
}
});
}
});
}
});
if (rcvr) {
dict = rcvr.variables.allNamesDict();
tempVars.forEach(function (name) {
dict[name] = name;
});
return dict;
}
return {};
};
// SyntaxElementMorph drag & drop:
SyntaxElementMorph.prototype.reactToGrabOf = function (grabbedMorph) {
var topBlock = this.topBlock(),
affected;
if (grabbedMorph instanceof CommandBlockMorph) {
affected = this.parentThatIsA(CommandSlotMorph);
if (affected) {
this.startLayout();
affected.fixLayout();
this.endLayout();
}
}
if (topBlock) {
topBlock.allComments().forEach(function (comment) {
comment.align(topBlock);
});
if (topBlock.getHighlight()) {
topBlock.addHighlight(topBlock.removeHighlight());
}
}
};
// SyntaxElementMorph 3D - border color rendering:
SyntaxElementMorph.prototype.bright = function () {
return this.color.lighter(this.contrast).toString();
};
SyntaxElementMorph.prototype.dark = function () {
return this.color.darker(this.contrast).toString();
};
// SyntaxElementMorph color changing:
SyntaxElementMorph.prototype.setColor = function (aColor, silently) {
if (aColor) {
if (!this.color.eq(aColor)) {
this.color = aColor;
if (!silently) {this.drawNew(); }
this.children.forEach(function (child) {
if (!silently || child instanceof TemplateSlotMorph) {
child.drawNew();
child.changed();
}
});
this.changed();
}
}
};
SyntaxElementMorph.prototype.setLabelColor = function (
textColor,
shadowColor,
shadowOffset
) {
this.children.forEach(function (morph) {
if (morph instanceof StringMorph && !morph.isProtectedLabel) {
morph.shadowOffset = shadowOffset || morph.shadowOffset;
morph.shadowColor = shadowColor || morph.shadowColor;
morph.setColor(textColor);
} else if (morph instanceof MultiArgMorph
|| morph instanceof ArgLabelMorph
|| (morph instanceof SymbolMorph && !morph.isProtectedLabel)
|| (morph instanceof InputSlotMorph
&& morph.isReadOnly)) {
morph.setLabelColor(textColor, shadowColor, shadowOffset);
}
});
};
// SyntaxElementMorph zebra coloring
SyntaxElementMorph.prototype.fixBlockColor = function (
nearestBlock,
isForced
) {
this.children.forEach(function (morph) {
if (morph instanceof SyntaxElementMorph) {
morph.fixBlockColor(nearestBlock, isForced);
}
});
};
// SyntaxElementMorph label parts:
SyntaxElementMorph.prototype.labelPart = function (spec) {
var part, tokens;
if (spec[0] === '%' &&
spec.length > 1 &&
(this.selector !== 'reportGetVar' ||
(spec === '%turtleOutline' && this.isObjInputFragment()))) {
// check for variable multi-arg-slot:
if ((spec.length > 5) && (spec.slice(0, 5) === '%mult')) {
part = new MultiArgMorph(spec.slice(5));
part.addInput();
return part;
}
// single-arg and specialized multi-arg slots:
switch (spec) {
case '%imgsource':
part = new InputSlotMorph(
null, // text
false, // non-numeric
{
'pen trails': ['pen trails'],
'stage image': ['stage image']
},
true
);
part.setContents(['pen trails']);
break;
case '%inputs':
part = new MultiArgMorph('%s', 'with inputs');
part.isStatic = false;
part.canBeEmpty = false;
break;
case '%scriptVars':
part = new MultiArgMorph('%t', null, 1, spec);
part.canBeEmpty = false;
break;
case '%blockVars':
part = new MultiArgMorph('%t', 'block variables', 0, spec);
part.canBeEmpty = false;
break;
case '%parms':
part = new MultiArgMorph('%t', 'Input Names:', 0, spec);
part.canBeEmpty = false;
break;
case '%ringparms':
part = new MultiArgMorph(
'%t',
'input names:',
0,
spec
);
break;
case '%cmdRing':
part = new RingMorph();
part.color = SpriteMorph.prototype.blockColor.other;
part.selector = 'reifyScript';
part.setSpec('%rc %ringparms');
part.isDraggable = true;
break;
case '%repRing':
part = new RingMorph();
part.color = SpriteMorph.prototype.blockColor.other;
part.selector = 'reifyReporter';
part.setSpec('%rr %ringparms');
part.isDraggable = true;
part.isStatic = true;
break;
case '%predRing':
part = new RingMorph(true);
part.color = SpriteMorph.prototype.blockColor.other;
part.selector = 'reifyPredicate';
part.setSpec('%rp %ringparms');
part.isDraggable = true;
part.isStatic = true;
break;
case '%words':
part = new MultiArgMorph('%s', null, 0);
part.addInput(); // allow for default value setting
part.addInput(); // allow for default value setting
part.isStatic = false;
break;
case '%exp':
part = new MultiArgMorph('%s', null, 0);
part.addInput();
part.isStatic = true;
part.canBeEmpty = false;
break;
case '%br':
part = new Morph();
part.setExtent(new Point(0, 0));
part.isBlockLabelBreak = true;
part.getSpec = function () {
return '%br';
};
break;
case '%inputName':
part = new ReporterBlockMorph();
part.category = 'variables';
part.color = SpriteMorph.prototype.blockColor.variables;
part.setSpec(localize('Input name'));
break;
case '%s':
part = new InputSlotMorph();
break;
case '%anyUE':
part = new InputSlotMorph();
part.isUnevaluated = true;
break;
case '%txt':
part = new InputSlotMorph();
part.minWidth = part.height() * 1.7; // "landscape"
part.fixLayout();
break;
case '%mlt':
part = new TextSlotMorph();
part.fixLayout();
break;
case '%code':
part = new TextSlotMorph();
part.contents().fontName = 'monospace';
part.contents().fontStyle = 'monospace';
part.fixLayout();
break;
case '%obj':
part = new ArgMorph('object');
break;
case '%n':
part = new InputSlotMorph(null, true);
break;
case '%dir':
part = new InputSlotMorph(
null,
true,
{
'(90) right' : 90,
'(-90) left' : -90,
'(0) up' : '0',
'(180) down' : 180
}
);
part.setContents(90);
break;
case '%inst':
part = new InputSlotMorph(
null,
true,
{
'(1) Acoustic Grand' : 1,
'(2) Bright Acoustic' : 2,
'(3) Electric Grand' : 3,
'(4) Honky Tonk' : 4,
'(5) Electric Piano 1' : 5,
'(6) Electric Piano 2' : 6,
'(7) Harpsichord' : 7
}
);
part.setContents(1);
break;
case '%month':
part = new InputSlotMorph(
null, // text
false, // numeric?
{
'January' : ['January'],
'February' : ['February'],
'March' : ['March'],
'April' : ['April'],
'May' : ['May'],
'June' : ['June'],
'July' : ['July'],
'August' : ['August'],
'September' : ['September'],
'October' : ['October'],
'November' : ['November'],
'December' : ['December']
},
true // read-only
);
break;
case '%interaction':
part = new InputSlotMorph(
null, // text
false, // numeric?
{
'clicked' : ['clicked'],
'pressed' : ['pressed'],
'dropped' : ['dropped'],
'mouse-entered' : ['mouse-entered'],
'mouse-departed' : ['mouse-departed']
},
true // read-only
);
part.isStatic = true;
break;
case '%dates':
part = new InputSlotMorph(
null, // text
false, // non-numeric
{
'year' : ['year'],
'month' : ['month'],
'date' : ['date'],
'day of week' : ['day of week'],
'hour' : ['hour'],
'minute' : ['minute'],
'second' : ['second'],
'time in milliseconds' : ['time in milliseconds']
},
true // read-only
);
part.setContents(['date']);
break;
case '%delim':
part = new InputSlotMorph(
null, // text
false, // numeric?
{
'letter' : ['letter'],
'whitespace' : ['whitespace'],
'line' : ['line'],
'tab' : ['tab'],
'cr' : ['cr']
},
false // read-only
);
break;
case '%ida':
part = new InputSlotMorph(
null,
true,
{
'1' : 1,
last : ['last'],
'~' : null,
all : ['all']
}
);
part.setContents(1);
break;
case '%idx':
part = new InputSlotMorph(
null,
true,
{
'1' : 1,
last : ['last'],
any : ['any']
}
);
part.setContents(1);