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_getSublist.js
More file actions
executable file
·134 lines (116 loc) · 4.65 KB
/
lists_getSublist.js
File metadata and controls
executable file
·134 lines (116 loc) · 4.65 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
import { translate } from '../../../../utils/lang/i18n';
Blockly.Blocks.lists_getSublist = {
init() {
this.WHERE_OPTIONS_1 = [
[translate('get sub-list from #'), 'FROM_START'],
[translate('get sub-list from # from end'), 'FROM_END'],
[translate('get sub-list from first'), 'FIRST'],
];
this.WHERE_OPTIONS_2 = [
[translate('#'), 'FROM_START'],
[translate('# from end'), 'FROM_END'],
[translate('last'), 'LAST'],
];
this.appendValueInput('LIST').appendField(translate('in list'));
this.appendDummyInput('AT1');
this.appendDummyInput('AT2');
// 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.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
this.updateAt(1, true);
this.updateAt(2, true);
},
meta() {
return {
'display_name': translate('List get sublit'),
'description' : translate('List get sublist description'),
};
},
mutationToDom() {
const container = document.createElement('mutation');
const isAt1 = this.getInput('AT1').type === Blockly.INPUT_VALUE;
const isAt2 = this.getInput('AT2').type === Blockly.INPUT_VALUE;
container.setAttribute('at1', isAt1);
container.setAttribute('at2', isAt2);
return container;
},
domToMutation(xmlElement) {
const isAt1 = xmlElement.getAttribute('at1') === 'true';
const isAt2 = xmlElement.getAttribute('at2') === 'true';
this.updateAt(1, isAt1);
this.updateAt(2, isAt2);
},
updateAt(n, isAt) {
this.removeInput(`AT${n}`);
if (isAt) {
this.appendValueInput(`AT${n}`).setCheck('Number');
} else {
this.appendDummyInput(`AT${n}`);
}
const menu = new Blockly.FieldDropdown(this[`WHERE_OPTIONS_${n}`], value => {
const newAt = ['FROM_START', 'FROM_END'].includes(value);
if (newAt !== isAt) {
this.updateAt(n, newAt);
this.setFieldValue(value, `WHERE${n}`);
return null;
}
return undefined;
});
this.getInput(`AT${n}`).appendField(menu, `WHERE${n}`);
if (n === 1) {
this.moveInputBefore('AT1', 'AT2');
}
this.initSvg();
this.render(false);
},
};
Blockly.JavaScript.lists_getSublist = block => {
const list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_MEMBER) || '[]';
const where1 = block.getFieldValue('WHERE1');
const where2 = block.getFieldValue('WHERE2');
let at1,
at2,
code;
if (list.match(/^\w+$/)) {
if (where1 === 'FROM_START') {
at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
} else if (where1 === 'FROM_END') {
at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false, Blockly.JavaScript.ORDER_SUBTRACTION);
at1 = `${list}.length - ${at1}`;
}
if (where2 === 'FROM_START') {
at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1);
} else if (where2 === 'FROM_END') {
at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false, Blockly.JavaScript.ORDER_SUBTRACTION);
at2 = `${list}.length - ${at2}`;
}
code = `${list}.slice(${at1}, ${at2})`;
} else {
at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
at2 = Blockly.JavaScript.getAdjusted(block, 'AT2');
const wherePascalCase = {
FROM_START: 'FromStart',
FROM_END : 'FromEnd',
};
const getIndex = (listName, where, at) => (where === 'FROM_END' ? `${listName}.length - 1 - ${at}` : at);
// eslint-disable-next-line no-underscore-dangle
const functionName = Blockly.JavaScript.provideFunction_(
`subsequence${wherePascalCase[where1]}${wherePascalCase[where2]}`,
[
// eslint-disable-next-line no-underscore-dangle
`function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(sequence, at1, at2) {
var start = ${getIndex('sequence', where1, 'at1')};
var end = ${getIndex('sequence', where2, 'at2')};
return sequence.slice(start, end);
}`,
]
);
code = `${functionName}(${list}, ${at1}, ${at2})`;
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};