more arrow-function refactorings for byob.js

pull/95/head
jmoenig 2020-04-28 17:16:13 +02:00
rodzic b3c8aa0d8f
commit 2826f9e617
1 zmienionych plików z 45 dodań i 60 usunięć

Wyświetl plik

@ -273,7 +273,7 @@ CustomBlockDefinition.prototype.blockSpec = function () {
CustomBlockDefinition.prototype.helpSpec = function () { CustomBlockDefinition.prototype.helpSpec = function () {
var ans = [], var ans = [],
parts = this.parseSpec(this.spec); parts = this.parseSpec(this.spec);
parts.forEach(function (part) { parts.forEach(part => {
if (part[0] !== '%') { if (part[0] !== '%') {
ans.push(part); ans.push(part);
} }
@ -345,7 +345,7 @@ CustomBlockDefinition.prototype.dropDownMenuOf = function (inputName) {
CustomBlockDefinition.prototype.parseChoices = function (string) { CustomBlockDefinition.prototype.parseChoices = function (string) {
var dict = {}, var dict = {},
stack = [dict]; stack = [dict];
string.split('\n').forEach(function (line) { string.split('\n').forEach(line => {
var pair = line.split('='); var pair = line.split('=');
if (pair[0] === '}') { if (pair[0] === '}') {
stack.pop(); stack.pop();
@ -376,7 +376,7 @@ CustomBlockDefinition.prototype.inputOptionsOf = function (inputName) {
CustomBlockDefinition.prototype.inputNames = function () { CustomBlockDefinition.prototype.inputNames = function () {
var vNames = [], var vNames = [],
parts = this.parseSpec(this.spec); parts = this.parseSpec(this.spec);
parts.forEach(function (part) { parts.forEach(part => {
if (part[0] === '%' && part.length > 1) { if (part[0] === '%' && part.length > 1) {
vNames.push(part.slice(1)); vNames.push(part.slice(1));
} }
@ -437,20 +437,17 @@ CustomBlockDefinition.prototype.localizedSpec = function () {
} }
if (isNil(loc)) {return sem; } if (isNil(loc)) {return sem; }
inputs = BlockMorph.prototype.parseSpec(sem).filter(function (str) { inputs = BlockMorph.prototype.parseSpec(sem).filter(str => isInput(str));
return (isInput(str));
});
locParts = BlockMorph.prototype.parseSpec(loc); locParts = BlockMorph.prototype.parseSpec(loc);
// perform a bunch of sanity checks on the localized spec // perform a bunch of sanity checks on the localized spec
if (locParts.some(function (str) {return isInput(str); }) || if (locParts.some(str => isInput(str)) ||
(locParts.filter(function (str) {return str === '_'; }).length !== (locParts.filter(str => str === '_').length !== inputs.length)
inputs.length)
) { ) {
this.cachedTranslation = sem; this.cachedTranslation = sem;
} else { } else {
// substitute each input place holder with its semantic spec part // substitute each input place holder with its semantic spec part
locParts = locParts.map(function (str) { locParts = locParts.map(str => {
if (str === '_') { if (str === '_') {
i += 1; i += 1;
return inputs[i]; return inputs[i];
@ -522,19 +519,19 @@ CustomBlockDefinition.prototype.scriptsModel = function () {
} }
scripts.add(proto); scripts.add(proto);
proto.fixBlockColor(null, true); proto.fixBlockColor(null, true);
this.scripts.forEach(function (element) { this.scripts.forEach(element => {
block = element.fullCopy(); block = element.fullCopy();
block.setPosition(scripts.position().add(element.position())); block.setPosition(scripts.position().add(element.position()));
scripts.add(block); scripts.add(block);
if (block instanceof BlockMorph) { if (block instanceof BlockMorph) {
block.allComments().forEach(function (comment) { block.allComments().forEach(comment =>
comment.align(block); comment.align(block)
}); );
} }
}); });
proto.allComments().forEach(function (comment) { proto.allComments().forEach(comment =>
comment.align(proto); comment.align(proto)
}); );
template = proto.parts()[0]; template = proto.parts()[0];
template.fixLayout(); template.fixLayout();
template.forceNormalColoring(); template.forceNormalColoring();
@ -550,9 +547,9 @@ CustomBlockDefinition.prototype.purgeCorpses = function () {
if (this.body && this.body.expression.isCorpse) { if (this.body && this.body.expression.isCorpse) {
this.body = null; this.body = null;
} }
this.scripts = this.scripts.filter(function (topBlock) { this.scripts = this.scripts.filter(topBlock =>
return !topBlock.isCorpse; !topBlock.isCorpse
}); );
}; };
// CustomCommandBlockMorph ///////////////////////////////////////////// // CustomCommandBlockMorph /////////////////////////////////////////////
@ -631,7 +628,7 @@ CustomCommandBlockMorph.prototype.refresh = function (aDefinition) {
this.fixLabelColor(); this.fixLabelColor();
this.restoreInputs(oldInputs); this.restoreInputs(oldInputs);
} else { // update all input slots' drop-downs } else { // update all input slots' drop-downs
this.inputs().forEach(function (inp, i) { this.inputs().forEach((inp, i) => {
if (inp instanceof InputSlotMorph) { if (inp instanceof InputSlotMorph) {
inp.setChoices.apply(inp, def.inputOptionsOfIdx(i)); inp.setChoices.apply(inp, def.inputOptionsOfIdx(i));
} }
@ -641,7 +638,7 @@ CustomCommandBlockMorph.prototype.refresh = function (aDefinition) {
// find unnamed upvars and label them // find unnamed upvars and label them
// to their internal definition (default) // to their internal definition (default)
this.cachedInputs = null; this.cachedInputs = null;
this.inputs().forEach(function (inp, idx) { this.inputs().forEach((inp, idx) => {
if (inp instanceof TemplateSlotMorph && inp.contents() === '\u2191') { if (inp instanceof TemplateSlotMorph && inp.contents() === '\u2191') {
inp.setContents(def.inputNames()[idx]); inp.setContents(def.inputNames()[idx]);
} }
@ -793,7 +790,7 @@ CustomCommandBlockMorph.prototype.refreshPrototype = function () {
}; };
CustomCommandBlockMorph.prototype.refreshPrototypeSlotTypes = function () { CustomCommandBlockMorph.prototype.refreshPrototypeSlotTypes = function () {
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (part instanceof BlockInputFragmentMorph) { if (part instanceof BlockInputFragmentMorph) {
part.template().instantiationSpec = part.contents(); part.template().instantiationSpec = part.contents();
part.setContents(part.fragment.defTemplateSpecFragment()); part.setContents(part.fragment.defTemplateSpecFragment());
@ -807,7 +804,7 @@ CustomCommandBlockMorph.prototype.inputFragmentNames = function () {
// for the variable name slot drop-down menu (in the block editor) // for the variable name slot drop-down menu (in the block editor)
var ans = []; var ans = [];
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (!part.fragment.isDeleted && (part.fragment.type)) { if (!part.fragment.isDeleted && (part.fragment.type)) {
ans.push(part.fragment.labelString); ans.push(part.fragment.labelString);
} }
@ -819,7 +816,7 @@ CustomCommandBlockMorph.prototype.upvarFragmentNames = function () {
// for the variable name slot drop-down menu (in the block editor) // for the variable name slot drop-down menu (in the block editor)
var ans = []; var ans = [];
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (!part.fragment.isDeleted && (part.fragment.type === '%upvar')) { if (!part.fragment.isDeleted && (part.fragment.type === '%upvar')) {
ans.push(part.fragment.labelString); ans.push(part.fragment.labelString);
} }
@ -836,7 +833,7 @@ CustomCommandBlockMorph.prototype.specFromFragments = function () {
// for block prototypes while they are being edited // for block prototypes while they are being edited
var ans = ''; var ans = '';
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (!part.fragment.isDeleted) { if (!part.fragment.isDeleted) {
ans = ans + part.fragment.defSpecFragment() + ' '; ans = ans + part.fragment.defSpecFragment() + ' ';
} }
@ -848,7 +845,7 @@ CustomCommandBlockMorph.prototype.blockSpecFromFragments = function () {
// for block instances while their prototype is being edited // for block instances while their prototype is being edited
var ans = ''; var ans = '';
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (!part.fragment.isDeleted) { if (!part.fragment.isDeleted) {
ans = ans + part.fragment.blockSpecFragment() + ' '; ans = ans + part.fragment.blockSpecFragment() + ' ';
} }
@ -862,7 +859,7 @@ CustomCommandBlockMorph.prototype.declarationsFromFragments = function () {
// value: [type, default, options, isReadOnly] // value: [type, default, options, isReadOnly]
var ans = new Map(); var ans = new Map();
this.parts().forEach(function (part) { this.parts().forEach(part => {
if (part instanceof BlockInputFragmentMorph) { if (part instanceof BlockInputFragmentMorph) {
ans.set( ans.set(
part.fragment.labelString, part.fragment.labelString,
@ -1175,9 +1172,9 @@ CustomCommandBlockMorph.prototype.userMenu = function () {
} }
} }
this.variables.names().forEach(function (vName) { this.variables.names().forEach(vName =>
monitor(vName); monitor(vName)
}); );
} }
menu.addItem("edit...", 'edit'); // works also for prototypes menu.addItem("edit...", 'edit'); // works also for prototypes
return menu; return menu;
@ -1638,10 +1635,10 @@ BlockDialogMorph.prototype.openForChange = function (
this.category = category; this.category = category;
this.blockType = type; this.blockType = type;
this.categories.children.forEach(function (each) { this.categories.children.forEach(each =>
each.refresh(); each.refresh()
}); );
this.types.children.forEach(function (each) { this.types.children.forEach(each => {
each.setColor(clr); each.setColor(clr);
each.refresh(); each.refresh();
}); });
@ -1727,7 +1724,7 @@ BlockDialogMorph.prototype.fixCategoriesLayout = function () {
row, row,
col; col;
this.categories.children.forEach(function (button) { this.categories.children.forEach(button => {
i += 1; i += 1;
row = Math.ceil(i / 2); row = Math.ceil(i / 2);
col = 2 - (i % 2); col = 2 - (i % 2);
@ -2303,7 +2300,7 @@ BlockEditorMorph.prototype.context = function (prototypeHat) {
if (topBlock === null) { if (topBlock === null) {
return null; return null;
} }
topBlock.allChildren().forEach(function (c) { topBlock.allChildren().forEach(c => {
if (c instanceof BlockMorph) {c.cachedInputs = null; } if (c instanceof BlockMorph) {c.cachedInputs = null; }
}); });
stackFrame = Process.prototype.reify.call( stackFrame = Process.prototype.reify.call(
@ -2451,9 +2448,9 @@ PrototypeHatBlockMorph.prototype.init = function (definition) {
vars = this.labelPart('%blockVars'); vars = this.labelPart('%blockVars');
this.add(this.labelPart('%br')); this.add(this.labelPart('%br'));
this.add(vars); this.add(vars);
definition.variableNames.forEach(function (name) { definition.variableNames.forEach(name =>
vars.addInput(name); vars.addInput(name)
}); );
} }
proto.refreshPrototypeSlotTypes(); // show slot type indicators proto.refreshPrototypeSlotTypes(); // show slot type indicators
this.fixLayout(); this.fixLayout();
@ -3109,12 +3106,8 @@ InputSlotDialogMorph.prototype.setType = function (fragmentType) {
this.textfield.choices = fragmentType ? null : this.symbolMenu; this.textfield.choices = fragmentType ? null : this.symbolMenu;
this.textfield.fixLayout(); this.textfield.fixLayout();
this.fragment.type = fragmentType || null; this.fragment.type = fragmentType || null;
this.types.children.forEach(function (c) { this.types.children.forEach(c => c.refresh());
c.refresh(); this.slots.children.forEach(c => c.refresh());
});
this.slots.children.forEach(function (c) {
c.refresh();
});
this.edit(); this.edit();
}; };
@ -3179,9 +3172,7 @@ InputSlotDialogMorph.prototype.fixLayout = function () {
this.left() + this.padding, this.left() + this.padding,
this.types.bottom() + this.padding this.types.bottom() + this.padding
)); ));
this.slots.children.forEach(function (c) { this.slots.children.forEach(c => c.refresh());
c.refresh();
});
// buttons // buttons
this.buttons.fixLayout(); this.buttons.fixLayout();
@ -3382,9 +3373,7 @@ InputSlotDialogMorph.prototype.createSlotTypeButtons = function () {
InputSlotDialogMorph.prototype.setSlotType = function (type) { InputSlotDialogMorph.prototype.setSlotType = function (type) {
this.fragment.setSingleInputType(type); this.fragment.setSingleInputType(type);
this.slots.children.forEach(function (c) { this.slots.children.forEach(c => c.refresh());
c.refresh();
});
this.edit(); this.edit();
}; };
@ -3397,9 +3386,7 @@ InputSlotDialogMorph.prototype.setSlotArity = function (arity) {
this.fragment.setToUpvar(); this.fragment.setToUpvar();
// hide other options - under construction // hide other options - under construction
} }
this.slots.children.forEach(function (c) { this.slots.children.forEach(c => c.refresh());
c.refresh();
});
this.edit(); this.edit();
}; };
@ -3738,9 +3725,7 @@ VariableDialogMorph.prototype.addTypeButton
VariableDialogMorph.prototype.setType = function (varType) { VariableDialogMorph.prototype.setType = function (varType) {
this.isGlobal = (varType === 'global'); this.isGlobal = (varType === 'global');
this.types.children.forEach(function (c) { this.types.children.forEach(c => c.refresh());
c.refresh();
});
this.edit(); this.edit();
}; };
@ -3935,7 +3920,7 @@ BlockExportDialogMorph.prototype.userMenu = function () {
}; };
BlockExportDialogMorph.prototype.selectAll = function () { BlockExportDialogMorph.prototype.selectAll = function () {
this.body.contents.children.forEach(function (checkBox) { this.body.contents.children.forEach(checkBox => {
if (!checkBox.state) { if (!checkBox.state) {
checkBox.trigger(); checkBox.trigger();
} }
@ -3944,7 +3929,7 @@ BlockExportDialogMorph.prototype.selectAll = function () {
BlockExportDialogMorph.prototype.selectNone = function () { BlockExportDialogMorph.prototype.selectNone = function () {
this.blocks = []; this.blocks = [];
this.body.contents.children.forEach(function (checkBox) { this.body.contents.children.forEach(checkBox => {
checkBox.refresh(); checkBox.refresh();
}); });
}; };