forked from snapapps/edgy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byob.js
3440 lines (2931 loc) · 99.2 KB
/
byob.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
/*
byob.js
"build your own blocks" for SNAP!
based on morphic.js, widgets.js blocks.js, threads.js and objects.js
inspired by Scratch
written by Jens Mönig
Copyright (C) 2014 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 blocks.js, threads.js, objects.js, widgets.js and morphic.js
hierarchy
---------
the following tree lists all constructors hierarchically,
indentation indicating inheritance. Refer to this list to get a
contextual overview:
BlockLabelFragment
CustomBlockDefinition
CommandBlockMorph***
CustomCommandBlockMorph
HatBlockMorph***
PrototypeHatBlockMorph
DialogBoxMorph**
BlockDialogMorph
BlockEditorMorph
BlockExportDialogMorph
BlockImportDialogMorph
InputSlotDialogMorph
VariableDialogMorph
ReporterBlockMorph***
CustomReporterBlockMorph
JaggedBlockMorph
StringMorph*
BlockLabelFragmentMorph
BlockLabelPlaceHolderMorph
TemplateSlotMorph***
BlockInputFragmentMorph
* from morphic.js
** from widgets.js
*** from blocks.js
toc
---
the following list shows the order in which all constructors are
defined. Use this list to locate code in this document:
CustomBlockDefinition
CustomCommandBlockMorph
CustomReporterBlockMorph
JaggedBlockMorph
BlockDialogMorph
BlockEditorMorph
PrototypeHatBlockMorph
BlockLabelFragmentMorph
BlockLabelPlaceHolderMorph
BlockInputFragmentMorph
InputSlotDialogMorph
VariableDialogMorph
BlockExportDialogMorph
BlockImportDialogMorph
*/
/*global modules, CommandBlockMorph, SpriteMorph, TemplateSlotMorph,
StringMorph, Color, DialogBoxMorph, ScriptsMorph, ScrollFrameMorph,
Point, HandleMorph, HatBlockMorph, BlockMorph, detect, List, Process,
AlignmentMorph, ToggleMorph, InputFieldMorph, ReporterBlockMorph,
Context, StringMorph, nop, newCanvas, radians, BoxMorph,
ArrowMorph, PushButtonMorph, contains, InputSlotMorph, ShadowMorph,
ToggleButtonMorph, IDE_Morph, MenuMorph, copy, ToggleElementMorph,
Morph, fontHeight, StageMorph, SyntaxElementMorph, SnapSerializer,
CommentMorph, localize, CSlotMorph, SpeechBubbleMorph, MorphicPreferences,
SymbolMorph, isNil*/
// Global stuff ////////////////////////////////////////////////////////
modules.byob = '2014-Jun-06';
// Declarations
var CustomBlockDefinition;
var CustomCommandBlockMorph;
var CustomReporterBlockMorph;
var BlockDialogMorph;
var BlockEditorMorph;
var PrototypeHatBlockMorph;
var BlockLabelFragment;
var BlockLabelFragmentMorph;
var BlockInputFragmentMorph;
var BlockLabelPlaceHolderMorph;
var InputSlotDialogMorph;
var VariableDialogMorph;
var JaggedBlockMorph;
var BlockExportDialogMorph;
var BlockImportDialogMorph;
// CustomBlockDefinition ///////////////////////////////////////////////
// CustomBlockDefinition instance creation:
function CustomBlockDefinition(spec, receiver) {
this.body = null; // a Context (i.e. a reified top block)
this.scripts = [];
this.category = null;
this.isGlobal = false;
this.type = 'command';
this.spec = spec || '';
// format: {'inputName' : [type, default, options, readonly]}
this.declarations = {};
this.comment = null;
this.codeMapping = null; // experimental, generate text code
this.codeHeader = null; // experimental, generate text code
// don't serialize (not needed for functionality):
this.receiver = receiver || null; // for serialization only (pointer)
}
// CustomBlockDefinition instantiating blocks
CustomBlockDefinition.prototype.blockInstance = function () {
var block;
if (this.type === 'command') {
block = new CustomCommandBlockMorph(this);
} else {
block = new CustomReporterBlockMorph(
this,
this.type === 'predicate'
);
}
block.isDraggable = true;
return block;
};
CustomBlockDefinition.prototype.templateInstance = function () {
var block;
block = this.blockInstance();
block.refreshDefaults();
block.isDraggable = false;
block.isTemplate = true;
return block;
};
CustomBlockDefinition.prototype.prototypeInstance = function () {
var block, slot, myself = this;
// make a new block instance and mark it as prototype
if (this.type === 'command') {
block = new CustomCommandBlockMorph(this, true);
} else {
block = new CustomReporterBlockMorph(
this,
this.type === 'predicate',
true
);
}
// assign slot declarations to prototype inputs
block.parts().forEach(function (part) {
if (part instanceof BlockInputFragmentMorph) {
slot = myself.declarations[part.fragment.labelString];
if (slot) {
part.fragment.type = slot[0];
part.fragment.defaultValue = slot[1];
part.fragment.options = slot[2];
part.fragment.isReadOnly = slot[3] || false;
}
}
});
return block;
};
// CustomBlockDefinition duplicating
CustomBlockDefinition.prototype.copyAndBindTo = function (sprite) {
var c = copy(this);
c.receiver = sprite; // only for (kludgy) serialization
c.declarations = copy(this.declarations); // might have to go deeper
c.body = Process.prototype.reify.call(
null,
this.body.expression,
new List(this.inputNames())
);
c.body.outerContext = null;
return c;
};
// CustomBlockDefinition accessing
CustomBlockDefinition.prototype.blockSpec = function () {
var myself = this,
ans = [],
parts = this.parseSpec(this.spec),
spec;
parts.forEach(function (part) {
if (part[0] === '%') {
spec = myself.typeOf(part.slice(1));
} else {
spec = part;
}
ans.push(spec);
ans.push(' ');
});
return ''.concat.apply('', ans).trim();
};
CustomBlockDefinition.prototype.helpSpec = function () {
var ans = [],
parts = this.parseSpec(this.spec);
parts.forEach(function (part) {
if (part[0] !== '%') {
ans.push(part);
}
});
return ''.concat.apply('', ans).replace(/\?/g, '');
};
CustomBlockDefinition.prototype.typeOf = function (inputName) {
if (this.declarations[inputName]) {
return this.declarations[inputName][0];
}
return '%s';
};
CustomBlockDefinition.prototype.defaultValueOf = function (inputName) {
if (this.declarations[inputName]) {
return this.declarations[inputName][1];
}
return '';
};
CustomBlockDefinition.prototype.defaultValueOfInputIdx = function (idx) {
var inputName = this.inputNames()[idx];
return this.defaultValueOf(inputName);
};
CustomBlockDefinition.prototype.dropDownMenuOfInputIdx = function (idx) {
var inputName = this.inputNames()[idx];
return this.dropDownMenuOf(inputName);
};
CustomBlockDefinition.prototype.isReadOnlyInputIdx = function (idx) {
var inputName = this.inputNames()[idx];
return this.isReadOnlyInput(inputName);
};
CustomBlockDefinition.prototype.inputOptionsOfIdx = function (idx) {
var inputName = this.inputNames()[idx];
return this.inputOptionsOf(inputName);
};
CustomBlockDefinition.prototype.dropDownMenuOf = function (inputName) {
var dict = {};
if (this.declarations[inputName] && this.declarations[inputName][2]) {
this.declarations[inputName][2].split('\n').forEach(function (line) {
var pair = line.split('=');
dict[pair[0]] = isNil(pair[1]) ? pair[0] : pair[1];
});
return dict;
}
return null;
};
CustomBlockDefinition.prototype.isReadOnlyInput = function (inputName) {
return this.declarations[inputName] &&
this.declarations[inputName][3] === true;
};
CustomBlockDefinition.prototype.inputOptionsOf = function (inputName) {
return [
this.dropDownMenuOf(inputName),
this.isReadOnlyInput(inputName)
];
};
CustomBlockDefinition.prototype.inputNames = function () {
var vNames = [],
parts = this.parseSpec(this.spec);
parts.forEach(function (part) {
if (part[0] === '%') {
vNames.push(part.slice(1));
}
});
return vNames;
};
CustomBlockDefinition.prototype.parseSpec = function (spec) {
// private
var parts = [], word = '', i, quoted = false, c;
for (i = 0; i < spec.length; i += 1) {
c = spec[i];
if (c === "'") {
quoted = !quoted;
} else if (c === ' ' && !quoted) {
parts.push(word);
word = '';
} else {
word = word.concat(c);
}
}
parts.push(word);
return parts;
};
// CustomBlockDefinition picturing
CustomBlockDefinition.prototype.scriptsPicture = function () {
var scripts, proto, block, comment;
scripts = new ScriptsMorph();
scripts.cleanUpMargin = 10;
proto = new PrototypeHatBlockMorph(this);
proto.setPosition(scripts.position().add(10));
if (this.comment !== null) {
comment = this.comment.fullCopy();
proto.comment = comment;
comment.block = proto;
}
if (this.body !== null) {
proto.nextBlock(this.body.expression.fullCopy());
}
scripts.add(proto);
proto.fixBlockColor(null, true);
this.scripts.forEach(function (element) {
block = element.fullCopy();
block.setPosition(scripts.position().add(element.position()));
scripts.add(block);
if (block instanceof BlockMorph) {
block.allComments().forEach(function (comment) {
comment.align(block);
});
}
});
proto.allComments().forEach(function (comment) {
comment.align(proto);
});
proto.children[0].fixLayout();
scripts.fixMultiArgs();
return scripts.scriptsPicture();
};
// CustomCommandBlockMorph /////////////////////////////////////////////
// CustomCommandBlockMorph inherits from CommandBlockMorph:
CustomCommandBlockMorph.prototype = new CommandBlockMorph();
CustomCommandBlockMorph.prototype.constructor = CustomCommandBlockMorph;
CustomCommandBlockMorph.uber = CommandBlockMorph.prototype;
// CustomCommandBlockMorph instance creation:
function CustomCommandBlockMorph(definition, isProto) {
this.init(definition, isProto);
}
CustomCommandBlockMorph.prototype.init = function (definition, isProto) {
this.definition = definition; // mandatory
this.isPrototype = isProto || false; // optional
CustomCommandBlockMorph.uber.init.call(this);
this.category = definition.category;
this.selector = 'evaluateCustomBlock';
if (definition) { // needed for de-serializing
this.refresh();
}
};
CustomCommandBlockMorph.prototype.refresh = function () {
var def = this.definition,
newSpec = this.isPrototype ?
def.spec : def.blockSpec(),
oldInputs;
this.setCategory(def.category);
if (this.blockSpec !== newSpec) {
oldInputs = this.inputs();
if (!this.zebraContrast) {
this.forceNormalColoring();
} else {
this.fixBlockColor();
}
this.setSpec(newSpec);
this.fixLabelColor();
this.restoreInputs(oldInputs);
} else { // update all input slots' drop-downs
this.inputs().forEach(function (inp, i) {
if (inp instanceof InputSlotMorph) {
inp.setChoices.apply(inp, def.inputOptionsOfIdx(i));
}
});
}
// find unnahmed upvars and label them
// to their internal definition (default)
this.inputs().forEach(function (inp, idx) {
if (inp instanceof TemplateSlotMorph && inp.contents() === '\u2191') {
inp.setContents(def.inputNames()[idx]);
}
});
};
CustomCommandBlockMorph.prototype.restoreInputs = function (oldInputs) {
// try to restore my previous inputs when my spec has been changed
var i = 0,
old,
myself = this;
if (this.isPrototype) {return; }
this.inputs().forEach(function (inp) {
old = oldInputs[i];
if (old instanceof ReporterBlockMorph &&
(!(inp instanceof TemplateSlotMorph))) {
myself.silentReplaceInput(inp, old);
} else if (old instanceof InputSlotMorph
&& inp instanceof InputSlotMorph) {
inp.setContents(old.evaluate());
} else if (old instanceof TemplateSlotMorph
&& inp instanceof TemplateSlotMorph) {
inp.setContents(old.evaluate());
} else if (old instanceof CSlotMorph
&& inp instanceof CSlotMorph) {
inp.nestedBlock(old.evaluate());
}
i += 1;
});
};
CustomCommandBlockMorph.prototype.refreshDefaults = function () {
// fill my editable slots with the defaults specified in my definition
var inputs = this.inputs(), idx = 0, myself = this;
inputs.forEach(function (inp) {
if (inp instanceof InputSlotMorph) {
inp.setContents(myself.definition.defaultValueOfInputIdx(idx));
}
idx += 1;
});
};
CustomCommandBlockMorph.prototype.refreshPrototype = function () {
// create my label parts from my (edited) fragments only
var hat,
protoSpec,
frags = [],
myself = this,
words,
newFrag,
i = 0;
if (!this.isPrototype) {return null; }
hat = this.parentThatIsA(PrototypeHatBlockMorph);
// remember the edited fragments
this.parts().forEach(function (part) {
if (!part.fragment.isDeleted) {
// take into consideration that a fragment may spawn others
// if it isn't an input label consisting of several words
if (part.fragment.type) { // marked as input, take label as is
frags.push(part.fragment);
} else { // not an input, devide into several non-input fragments
words = myself.definition.parseSpec(
part.fragment.labelString
);
words.forEach(function (word) {
newFrag = part.fragment.copy();
newFrag.labelString = word;
frags.push(newFrag);
});
}
}
});
// remember the edited prototype spec
protoSpec = this.specFromFragments();
// update the prototype's type
// and possibly exchange 'this' for 'myself'
if (this instanceof CustomCommandBlockMorph
&& ((hat.type === 'reporter') || (hat.type === 'predicate'))) {
myself = new CustomReporterBlockMorph(
this.definition,
hat.type === 'predicate',
true
);
hat.silentReplaceInput(this, myself);
} else if (this instanceof CustomReporterBlockMorph) {
if (hat.type === 'command') {
myself = new CustomCommandBlockMorph(
this.definition,
true
);
hat.silentReplaceInput(this, myself);
} else {
this.isPredicate = (hat.type === 'predicate');
this.drawNew();
}
}
myself.setCategory(hat.blockCategory || 'other');
hat.fixBlockColor();
// update the (new) prototype's appearance
myself.setSpec(protoSpec);
// update the (new) prototype's (new) fragments
// with the previously edited ones
myself.parts().forEach(function (part) {
if (!(part instanceof BlockLabelPlaceHolderMorph)) {
if (frags[i]) { // don't delete the default fragment
part.fragment = frags[i];
}
i += 1;
}
});
// refresh slot type indicators
this.refreshPrototypeSlotTypes();
hat.fixLayout();
};
CustomCommandBlockMorph.prototype.refreshPrototypeSlotTypes = function () {
this.parts().forEach(function (part) {
if (part instanceof BlockInputFragmentMorph) {
part.template().instantiationSpec = part.contents();
part.setContents(part.fragment.defTemplateSpecFragment());
}
});
this.fixBlockColor(null, true); // enforce zebra coloring of templates
};
CustomCommandBlockMorph.prototype.inputFragmentNames = function () {
// for the variable name slot drop-down menu (in the block editor)
var ans = [];
this.parts().forEach(function (part) {
if (!part.fragment.isDeleted && (part.fragment.type)) {
ans.push(part.fragment.labelString);
}
});
return ans;
};
CustomCommandBlockMorph.prototype.upvarFragmentNames = function () {
// for the variable name slot drop-down menu (in the block editor)
var ans = [];
this.parts().forEach(function (part) {
if (!part.fragment.isDeleted && (part.fragment.type === '%upvar')) {
ans.push(part.fragment.labelString);
}
});
return ans;
};
CustomCommandBlockMorph.prototype.upvarFragmentName = function (idx) {
// for block prototypes while they are being edited
return this.upvarFragmentNames()[idx] || '\u2191';
};
CustomCommandBlockMorph.prototype.specFromFragments = function () {
// for block prototypes while they are being edited
var ans = '';
this.parts().forEach(function (part) {
if (!part.fragment.isDeleted) {
ans = ans + part.fragment.defSpecFragment() + ' ';
}
});
return ans.trim();
};
CustomCommandBlockMorph.prototype.blockSpecFromFragments = function () {
// for block instances while their prototype is being edited
var ans = '';
this.parts().forEach(function (part) {
if (!part.fragment.isDeleted) {
ans = ans + part.fragment.blockSpecFragment() + ' ';
}
});
return ans.trim();
};
CustomCommandBlockMorph.prototype.declarationsFromFragments = function () {
// format for type declarations: {inputName : [type, default]}
var ans = {};
this.parts().forEach(function (part) {
if (part instanceof BlockInputFragmentMorph) {
ans[part.fragment.labelString] = [
part.fragment.type,
part.fragment.defaultValue,
part.fragment.options,
part.fragment.isReadOnly
];
}
});
return ans;
};
CustomCommandBlockMorph.prototype.parseSpec = function (spec) {
if (!this.isPrototype) {
return CustomCommandBlockMorph.uber.parseSpec.call(this, spec);
}
return this.definition.parseSpec.call(this, spec);
};
CustomCommandBlockMorph.prototype.mouseClickLeft = function () {
if (!this.isPrototype) {
return CustomCommandBlockMorph.uber.mouseClickLeft.call(this);
}
this.edit();
};
function hashPassword(password, salt) {
// SHA-512 is *not* a good way of hashing passwords, but key
// derivation functions are painfully slow in JavaScript.
return hex_sha512(salt + password);
}
CustomCommandBlockMorph.prototype.edit = function () {
var myself = this, block, hat;
if (this.isPrototype) {
block = this.definition.blockInstance();
block.addShadow();
hat = this.parentThatIsA(PrototypeHatBlockMorph);
new BlockDialogMorph(
null,
function (definition) {
if (definition) { // temporarily update everything
hat.blockCategory = definition.category;
hat.type = definition.type;
myself.refreshPrototype();
}
},
myself
).openForChange(
'Change block',
hat.blockCategory,
hat.type,
myself.world(),
block.fullImage(),
myself.isInUse()
);
} else {
if (this.definition.password !== undefined) {
new DialogBoxMorph(null, function receivePassword(password) {
if(myself.definition.password === hashPassword(password,
myself.definition.salt)) {
new BlockEditorMorph(myself.definition, myself.receiver()).popUp();
} else {
new DialogBoxMorph(null, receivePassword).prompt(
'Invalid password', '',
myself.world(),
new TextMorph('Invalid password.\nEnter correct '
+ 'password to view how this block works.'));
}
}).prompt(
'Password protected block', '',
this.world(),
new TextMorph('Enter password to view how this block works.')
);
} else {
new BlockEditorMorph(this.definition, this.receiver()).popUp();
}
}
};
CustomCommandBlockMorph.prototype.labelPart = function (spec) {
var part;
if (!this.isPrototype) {
return CustomCommandBlockMorph.uber.labelPart.call(this, spec);
}
if ((spec[0] === '%') && (spec.length > 1)) {
part = new BlockInputFragmentMorph(spec.slice(1));
} else {
part = new BlockLabelFragmentMorph(spec);
part.fontSize = this.fontSize;
part.color = new Color(255, 255, 255);
part.isBold = true;
part.shadowColor = this.color.darker(this.labelContrast);
part.shadowOffset = this.embossing;
part.drawNew();
}
return part;
};
CustomCommandBlockMorph.prototype.placeHolder = function () {
var part;
part = new BlockLabelPlaceHolderMorph();
part.fontSize = this.fontSize * 1.4;
part.color = new Color(45, 45, 45);
part.drawNew();
return part;
};
CustomCommandBlockMorph.prototype.attachTargets = function () {
if (this.isPrototype) {
return [];
}
return CustomCommandBlockMorph.uber.attachTargets.call(this);
};
CustomCommandBlockMorph.prototype.isInUse = function () {
// anser true if an instance of my definition is found
// in any of my receiver's scripts or block definitions
return this.receiver().usesBlockInstance(this.definition);
};
// CustomCommandBlockMorph menu:
CustomCommandBlockMorph.prototype.userMenu = function () {
var menu;
if (this.isPrototype) {
menu = new MenuMorph(this);
menu.addItem(
"script pic...",
function () {
window.open(this.topBlock().fullImage().toDataURL());
},
'open a new window\nwith a picture of this script'
);
} else {
menu = this.constructor.uber.userMenu.call(this);
if (!menu) {
menu = new MenuMorph(this);
} else {
menu.addLine();
}
// menu.addItem("export definition...", 'exportBlockDefinition');
menu.addItem("delete block definition...", 'deleteBlockDefinition');
}
menu.addItem("edit...", 'edit'); // works also for prototypes
return menu;
};
CustomCommandBlockMorph.prototype.exportBlockDefinition = function () {
var xml = new SnapSerializer().serialize(this.definition);
window.open('data:text/xml,' + encodeURIComponent(xml));
};
CustomCommandBlockMorph.prototype.deleteBlockDefinition = function () {
var idx, rcvr, stage, ide, myself = this, block;
if (this.isPrototype) {
return null; // under construction...
}
block = myself.definition.blockInstance();
block.addShadow();
new DialogBoxMorph(
this,
function () {
rcvr = myself.receiver();
rcvr.deleteAllBlockInstances(myself.definition);
if (myself.definition.isGlobal) {
stage = rcvr.parentThatIsA(StageMorph);
idx = stage.globalBlocks.indexOf(myself.definition);
if (idx !== -1) {
stage.globalBlocks.splice(idx, 1);
}
} else {
idx = rcvr.customBlocks.indexOf(myself.definition);
if (idx !== -1) {
rcvr.customBlocks.splice(idx, 1);
}
}
ide = rcvr.parentThatIsA(IDE_Morph);
if (ide) {
ide.flushPaletteCache();
ide.refreshPalette();
}
},
this
).askYesNo(
'Delete Custom Block',
localize('block deletion dialog text'), // long string lookup
myself.world(),
block.fullImage()
);
};
// CustomCommandBlockMorph events:
CustomCommandBlockMorph.prototype.mouseEnter = function () {
var comment, help;
if (this.isTemplate && this.definition.comment) {
comment = this.definition.comment.fullCopy();
comment.contents.parse();
help = '';
comment.contents.lines.forEach(function (line) {
help = help + '\n' + line;
});
this.bubbleHelp(
help.substr(1),
this.definition.comment.color
);
}
};
CustomCommandBlockMorph.prototype.mouseLeave = function () {
if (this.isTemplate && this.definition.comment) {
this.world().hand.destroyTemporaries();
}
};
// CustomCommandBlockMorph bubble help:
CustomCommandBlockMorph.prototype.bubbleHelp = function (contents, color) {
var myself = this;
this.fps = 2;
this.step = function () {
if (this.bounds.containsPoint(this.world().hand.position())) {
myself.popUpbubbleHelp(contents, color);
}
myself.fps = 0;
delete myself.step;
};
};
CustomCommandBlockMorph.prototype.popUpbubbleHelp = function (
contents,
color
) {
new SpeechBubbleMorph(
contents,
color,
null,
1
).popUp(this.world(), this.rightCenter().add(new Point(-8, 0)));
};
// CustomCommandBlockMorph relabelling
CustomCommandBlockMorph.prototype.relabel = function (alternatives) {
var menu = new MenuMorph(this),
oldInputs = this.inputs().map(
function (each) {return each.fullCopy(); }
),
myself = this;
alternatives.forEach(function (def) {
var block = def.blockInstance();
block.restoreInputs(oldInputs);
block.fixBlockColor(null, true);
block.addShadow(new Point(3, 3));
menu.addItem(
block,
function () {
myself.definition = def;
myself.refresh();
}
);
});
menu.popup(this.world(), this.bottomLeft().subtract(new Point(
8,
this instanceof CommandBlockMorph ? this.corner : 0
)));
};
CustomCommandBlockMorph.prototype.alternatives = function () {
var rcvr = this.receiver(),
stage = rcvr.parentThatIsA(StageMorph),
allDefs = rcvr.customBlocks.concat(stage.globalBlocks),
myself = this;
return allDefs.filter(function (each) {
return each !== myself.definition &&
each.type === myself.definition.type;
});
};
// CustomReporterBlockMorph ////////////////////////////////////////////
// CustomReporterBlockMorph inherits from ReporterBlockMorph:
CustomReporterBlockMorph.prototype = new ReporterBlockMorph();
CustomReporterBlockMorph.prototype.constructor = CustomReporterBlockMorph;
CustomReporterBlockMorph.uber = ReporterBlockMorph.prototype;
// CustomReporterBlockMorph instance creation:
function CustomReporterBlockMorph(definition, isPredicate, isProto) {
this.init(definition, isPredicate, isProto);
}
CustomReporterBlockMorph.prototype.init = function (
definition,
isPredicate,
isProto
) {
this.definition = definition; // mandatory
this.isPrototype = isProto || false; // optional
CustomReporterBlockMorph.uber.init.call(this, isPredicate);
this.category = definition.category;
this.selector = 'evaluateCustomBlock';
if (definition) { // needed for de-serializing
this.refresh();
}
};
CustomReporterBlockMorph.prototype.refresh = function () {
CustomCommandBlockMorph.prototype.refresh.call(this);
if (!this.isPrototype) {
this.isPredicate = (this.definition.type === 'predicate');
}
this.drawNew();
};
CustomReporterBlockMorph.prototype.mouseClickLeft = function () {
if (!this.isPrototype) {
return CustomReporterBlockMorph.uber.mouseClickLeft.call(this);
}
this.edit();
};
CustomReporterBlockMorph.prototype.placeHolder
= CustomCommandBlockMorph.prototype.placeHolder;
CustomReporterBlockMorph.prototype.parseSpec
= CustomCommandBlockMorph.prototype.parseSpec;
CustomReporterBlockMorph.prototype.edit
= CustomCommandBlockMorph.prototype.edit;
CustomReporterBlockMorph.prototype.labelPart
= CustomCommandBlockMorph.prototype.labelPart;
CustomReporterBlockMorph.prototype.upvarFragmentNames
= CustomCommandBlockMorph.prototype.upvarFragmentNames;
CustomReporterBlockMorph.prototype.upvarFragmentName
= CustomCommandBlockMorph.prototype.upvarFragmentName;
CustomReporterBlockMorph.prototype.inputFragmentNames
= CustomCommandBlockMorph.prototype.inputFragmentNames;
CustomReporterBlockMorph.prototype.specFromFragments
= CustomCommandBlockMorph.prototype.specFromFragments;
CustomReporterBlockMorph.prototype.blockSpecFromFragments
= CustomCommandBlockMorph.prototype.blockSpecFromFragments;
CustomReporterBlockMorph.prototype.declarationsFromFragments
= CustomCommandBlockMorph.prototype.declarationsFromFragments;
CustomReporterBlockMorph.prototype.refreshPrototype
= CustomCommandBlockMorph.prototype.refreshPrototype;
CustomReporterBlockMorph.prototype.refreshPrototypeSlotTypes
= CustomCommandBlockMorph.prototype.refreshPrototypeSlotTypes;
CustomReporterBlockMorph.prototype.restoreInputs
= CustomCommandBlockMorph.prototype.restoreInputs;
CustomReporterBlockMorph.prototype.refreshDefaults
= CustomCommandBlockMorph.prototype.refreshDefaults;