This repository was archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlists_getIndex.js
More file actions
executable file
·175 lines (156 loc) · 5.99 KB
/
lists_getIndex.js
File metadata and controls
executable file
·175 lines (156 loc) · 5.99 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
import { translate } from '../../../../utils/lang/i18n';
Blockly.Blocks.lists_getIndex = {
init() {
this.MODE_OPTIONS = [
[translate('get'), 'GET'],
[translate('get and remove'), 'GET_REMOVE'],
[translate('remove'), 'REMOVE'],
];
this.WHERE_OPTIONS = [
['#', 'FROM_START'],
[translate('# from end'), 'FROM_END'],
[translate('first'), 'FIRST'],
[translate('last'), 'LAST'],
[translate('random'), 'RANDOM'],
];
const modeMenu = new Blockly.FieldDropdown(this.MODE_OPTIONS, value => {
const isStatement = value === 'REMOVE';
this.updateStatement(isStatement);
});
this.appendValueInput('VALUE')
.setCheck('Array')
.appendField(translate('in list'));
this.appendDummyInput().appendField(modeMenu, 'MODE');
this.appendDummyInput('AT');
// eslint-disable-next-line no-underscore-dangle
this.setColourFromRawValues_(
Blockly.Colours.Binary.colour,
Blockly.Colours.Binary.colourSecondary,
Blockly.Colours.Binary.colourTertiary
);
this.setOutput(true, null);
this.updateAt(true);
},
meta(){
return {
'display_name': translate('List Get Index'),
'description' : translate('List Get Index Description'),
};
},
mutationToDom() {
const container = document.createElement('mutation');
const isStatement = !this.outputConnection;
const isAt = this.getInput('AT').type === Blockly.INPUT_VALUE;
container.setAttribute('statement', isStatement);
container.setAttribute('at', isAt);
return container;
},
domToMutation(xmlElement) {
const isStatement = xmlElement.getAttribute('statement') === 'true';
this.updateStatement(isStatement);
const isAt = xmlElement.getAttribute('at') !== 'false';
this.updateAt(isAt);
},
updateStatement(newStatement) {
const oldStatement = !this.outputConnection;
if (newStatement !== oldStatement) {
this.unplug(true, true);
this.setOutput(!newStatement);
this.setPreviousStatement(newStatement);
this.setNextStatement(newStatement);
this.initSvg();
this.render(false);
}
},
updateAt(isAt) {
this.removeInput('AT', true);
if (isAt) {
this.appendValueInput('AT').setCheck('Number');
} else {
this.appendDummyInput('AT');
}
const menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, value => {
const newAt = ['FROM_START', 'FROM_END'].includes(value);
if (newAt !== isAt) {
this.updateAt(newAt);
this.setFieldValue(value, 'WHERE');
return null;
}
return undefined;
});
this.getInput('AT').appendField(menu, 'WHERE');
this.initSvg();
this.render(false);
},
};
Blockly.JavaScript.lists_getIndex = block => {
const mode = block.getFieldValue('MODE') || 'GET';
const where = block.getFieldValue('WHERE') || 'FIRST';
const listOrder = where === 'RANDOM' ? Blockly.JavaScript.ORDER_COMMA : Blockly.JavaScript.ORDER_MEMBER;
const list = Blockly.JavaScript.valueToCode(block, 'VALUE', listOrder) || '[]';
let code,
order;
if (where === 'FIRST') {
if (mode === 'GET') {
code = `${list}[0]`;
order = Blockly.JavaScript.ORDER_MEMBER;
} else if (mode === 'GET_REMOVE') {
code = `${list}.shift()`;
order = Blockly.JavaScript.ORDER_MEMBER;
} else if (mode === 'REMOVE') {
return `${list}.shift();\n`;
}
} else if (where === 'LAST') {
if (mode === 'GET') {
code = `${list}.slice(-1)[0]`;
order = Blockly.JavaScript.ORDER_MEMBER;
} else if (mode === 'GET_REMOVE') {
code = `${list}.pop()`;
order = Blockly.JavaScript.ORDER_MEMBER;
} else if (mode === 'REMOVE') {
return `${list}.pop();\n`;
}
} else if (where === 'FROM_START') {
const at = Blockly.JavaScript.getAdjusted(block, 'AT');
if (mode === 'GET') {
code = `${list}[${at}]`;
order = Blockly.JavaScript.ORDER_MEMBER;
} else if (mode === 'GET_REMOVE') {
code = `${list}.splice(${at}, 1)[0]`;
order = Blockly.JavaScript.ORDER_FUNCTION_CALL;
} else if (mode === 'REMOVE') {
return `${list}.splice(${at}, 1);\n`;
}
} else if (where === 'FROM_END') {
const at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true);
if (mode === 'GET') {
code = `${list}.slice(${at})[0]`;
order = Blockly.JavaScript.ORDER_FUNCTION_CALL;
} else if (mode === 'GET_REMOVE') {
code = `${list}.splice(${at}, 1)[0]`;
order = Blockly.JavaScript.ORDER_FUNCTION_CALL;
} else if (mode === 'REMOVE') {
return `${list}.splice(${at}, 1);\n`;
}
} else if (where === 'RANDOM') {
// eslint-disable-next-line no-underscore-dangle
const functionName = Blockly.JavaScript.provideFunction_('listsGetRandomItem', [
// eslint-disable-next-line no-underscore-dangle
`function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(list, remove) {
var x = Math.floor(Math.random() * list.length);
if (remove) {
return list.splice(x, 1)[0];
} else {
return list[x];
}
}`,
]);
code = `${functionName}(${list}, ${mode !== 'GET'})`;
if (mode === 'GET' || mode === 'GET_REMOVE') {
order = Blockly.JavaScript.ORDER_FUNCTION_CALL;
} else if (mode === 'REMOVE') {
return `${code};\n`;
}
}
return [code, order];
};