-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateButtons.js
138 lines (129 loc) · 4.02 KB
/
CreateButtons.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
/**
* @flow
*/
'use strict';
import React from 'react';
import {
ToastAndroid,
View,
} from 'react-native';
import DialogAndroid from 'react-native-dialogs';
import FloatingActionButton from './FloatingActionButton';
import StatelessComponent from './StatelessComponent';
import store from './store';
import * as t from './types';
type CreateButtonsProps = {
}
export default class CreateButtons extends StatelessComponent<CreateButtonsProps> {
shouldComponentUpdate() {
return false;
}
handleCreateLambda() {
const dialog = new DialogAndroid();
dialog.set({
title: 'Choose a variable name',
positiveText: 'OK',
negativeText: 'Cancel',
input: {
allowEmptyInput: false,
callback: (varName) => {
const error = checkDefNameErrors(varName);
if (error != null) {
ToastAndroid.show(error, ToastAndroid.SHORT);
} else {
store.dispatch(t.AddExpression.make(
t.CanvasExpression.make(
t.UserLambda.make(varName, null),
t.CanvasPoint.make(100, 100))
));
}
},
}
});
dialog.show();
}
handleCreateDefinition() {
const dialog = new DialogAndroid();
dialog.set({
title: 'Create or show definition',
positiveText: 'OK',
negativeText: 'Cancel',
input: {
allowEmptyInput: false,
type: 0x00001000, // InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
callback: (defName) => {
const error = checkDefNameErrors(defName);
if (error != null) {
ToastAndroid.show(error, ToastAndroid.SHORT)
} else {
store.dispatch(t.PlaceDefinition.make(
defName,
t.ScreenPoint.make(100, 100),
));
}
},
}
});
dialog.show();
}
render() {
return <View
style={{
flexDirection: 'row',
position: 'absolute',
right: 0,
bottom: 0,
}}
>
<FloatingActionButton
onPress={this.handleCreateLambda.bind(this)}
source={require('./img/lambda.png')}
style={{
marginRight: 24,
marginBottom: 24,
}}
/>
<FloatingActionButton
onPress={this.handleCreateDefinition.bind(this)}
source={require('./img/definition.png')}
style={{
marginRight: 24,
marginBottom: 24,
}}
/>
</View>;
}
};
const isLowerCase = (letter: string): boolean => {
return letter !== letter.toUpperCase();
};
/**
* Returns an error message if the variable name is invalid, or null if the name
* is valid.
*/
const checkVarNameErrors = (varName: string): ?string => {
if (varName.length > 8) {
return 'Variable names can only be up to 8 letters long.';
}
for (let i = 0; i < varName.length; i++) {
if (!isLowerCase(varName[i])) {
return 'Variable names can only contain lower-case letters.';
}
}
return null;
};
/**
* Returns an error message if the definition name is invalid, or null if the
* name is valid.
*/
const checkDefNameErrors = (defName: string): ?string => {
if (defName.length > 8) {
return 'Definition names can only be up to 8 letters long.';
}
for (let i = 0; i < defName.length; i++) {
if (isLowerCase(defName[i])) {
return 'Definition names can only contain capital letters and symbols.';
}
}
return null;
};