-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberInput.coffee
More file actions
358 lines (279 loc) · 8.53 KB
/
NumberInput.coffee
File metadata and controls
358 lines (279 loc) · 8.53 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
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
###
* coffeescript-ui - Coffeescript User Interface System (CUI)
* Copyright (c) 2013 - 2016 Programmfabrik GmbH
* MIT Licence
* https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###
class CUI.NumberInput extends CUI.Input
initOpts: ->
super()
@addOpts
decimals:
default: 0
check: "Integer"
symbol:
check: (v) ->
CUI.util.isString(v) and v.length > 0
symbol_before:
default: false
check: Boolean
store_as_integer:
default: false
check: Boolean
json_number:
default: false
check: Boolean
decimalpoint:
mandatory: true
default: "."
check: (v) ->
CUI.util.isString(v) and v.length > 0
separator:
check: (v) ->
CUI.util.isString(v) and v.length > 0
min:
default: null
check: (v) ->
CUI.util.isNumber(v)
max:
default: null
check: (v) ->
CUI.util.isNumber(v)
prevent_invalid_input:
default: true
check: Boolean
@removeOpt("checkInput")
@removeOpt("getValueForDisplay")
@removeOpt("getValueForInput")
@removeOpt("correctValueForInput")
readOpts: ->
super()
if @_json_number
@_decimals = 13
@_checkInput = @__checkInput
@setMin(@_min)
@setMax(@_max)
setMin: (@__min) ->
setMax: (@__max) ->
formatValueForDisplay: (value=@getValue(), forInput = false) ->
CUI.util.assert(typeof(value) == "number" or value == null, "NumberInput.formatValueForDisplay", "value needs to be Number or null", value: value, type: typeof(value))
if CUI.util.isEmpty(value)
return ""
if @_store_as_integer
v = (value / Math.pow(10, @_decimals)).toFixed(@_decimals)
else
v = value+""
# decimal is a "."
v0 = v.split(".")
if v0.length > 1
number = v0[0]
decimals = v0[1]
else
number = v0[0]
decimals = ""
if @_decimals > 0 and not @_json_number
while decimals.length < @_decimals
decimals = decimals + "0"
if forInput
if @_decimals > 0 and decimals.length > 0
return number + @_decimalpoint + decimals
else
return number
if @_decimals > 0 and decimals.length > 0
v1 = @__addSeparator(number)+@_decimalpoint+decimals
else
v1 = @__addSeparator(number)
# console.debug "v: ", v, value, @__addSeparator(v), v1
@__addSymbol(v1)
getValue: ->
v = super()
if @hasData()
return v
# value is as string in our input
@getValueForStore(v)
getValueForDisplay: ->
@formatValueForDisplay(@getValue())
getValueForStore: (value) ->
if not CUI.util.isString(value)
value = value + ""
number = parseFloat(value.replace(/,/,"."))
if @_json_number and number == Infinity
number = Number.MAX_VALUE
if @_json_number and number == -Infinity
number = -Number.MAX_VALUE
if isNaN(number)
return null
if @_store_as_integer
return parseInt((number * Math.pow(10, @_decimals)).toFixed(0))
return number
getDefaultValue: ->
null
setValue: (v, flags = {}) ->
@checkValue(v)
super(v, flags)
checkValue: (v) ->
if v == null
true
else if (@_decimals > 0 or @_json_number) and CUI.util.isFloat(v)
true
else if CUI.util.isInteger(v)
true
else
throw new Error("#{@__cls}.setValue(value): Value needs to be Number or null.")
__addSymbol: (str) ->
if CUI.util.isEmpty(@_symbol)
return str
if @_symbol_before
@_symbol+" "+str
else
str+" "+@_symbol
__addSeparator: (str) ->
if CUI.util.isEmpty(@_separator)
return str
isNegative = str.startsWith("-")
if isNegative
str = str.substr(1)
nn = []
for n,idx in str.split("").reverse()
if idx%3 == 0 and idx > 0
nn.push(@_separator)
nn.push(n)
if isNegative
nn.push("-")
nn.reverse()
nn.join("")
correctValueForInput: (value) ->
return value.replace(/[,\.]/g, @_decimalpoint)
getValueForInput: ->
@formatValueForDisplay(null, true)
checkInput: (value) ->
if value == null
return true
else
return super(value)
__checkInput: (value) ->
if not @hasShadowFocus()
v = value.replace(@_symbol, "")
else
v = value
v = v.trim()
if v == ""
return true
if @_separator
re = new RegExp(RegExp.escape(@_separator), "g")
v = v.replace(re, "")
point_idx = v.lastIndexOf(@_decimalpoint)
if point_idx == -1
number = v
points = ""
else
if @_decimals == 0
return false
number = v.substring(0, point_idx)
points = v.substring(point_idx+1)
# console.debug "v:", v, "number:", number, "points:", points, "decimalpoint", @_decimalpoint, "separator", @_separator
if points.length > @_decimals and not @_json_number
return false
if number.length > 0 and not number.match(/^((0|[1-9]+[0-9]*)|(-|-[1-9]|-[1-9][0-9]*))$/) and not @_json_number
# console.debug "number not matched", number
return false
if not CUI.util.isNull(@__min)
if @__min >= 0 and number == "-"
return false
if number < @__min
return false
if not CUI.util.isNull(@__max)
if number > @__max
return false
if not points.match(/^([0-9]*)$/) and not @_json_number
# console.debug "points not matched", points
return false
if points.length > @_decimals and not @_json_number
return false
if @_json_number
v = v.replace(",",".")
if v.match(/([eE][+\-]?|\.)$/)
# If we receive a partial JSON number such as "2.32e", we return false to indicate an invalid input.
# However, we also allows the user to continue entering the rest of the number. In preventInvalidInput:
# This gives the user the flexibility to complete the number
return false
json_number_regexp = /^-?(0|[1-9]\d*)(\.\d+)?([eE][-+]?\d+)?$/
return json_number_regexp.test(v)
return true
preventInvalidInput: ->
if @_json_number
# Json Number require a dynamic value for preventInvalidInput for partial json Numbers.
shadowValue = @__shadow?.value?.trim()?.replace(/,/g,".")
# We allow the input of a first negative symbol.
if shadowValue == "-"
return false
# Check if we try to input more than a decimal point
if !!(shadowValue?.split(".")?.length > 2)
return true
# Check if we are trying to input only a decimal point or e as a first character
if shadowValue == "." or shadowValue?.toLowerCase() == "e"
return true
if shadowValue?.match(/([eE][+\-]?|\.)$/)
# Check if we have "1.e" or "3.E"
if /\.e|\.E/.test(shadowValue)
return true
# Check if we are trying to input more than one e
if !!(shadowValue?.match(/[eE]/gi)?.length > 1)
return true
return false
if @__min and @__min > 9 and @__shadow?.value < @__min
# if we have a min value greater than 9, we allow the user to continue entering the rest of the number, unless
# the number is bigger than the min value.
return false
return super()
@format: (v, opts={}) ->
if CUI.util.isEmpty(v)
v = null
# automatically set decimals
if CUI.util.isFloat(v) and not opts.hasOwnProperty("decimals") and not opts.hasOwnProperty("json_number")
_v = v+""
opts.decimals = _v.length - _v.indexOf(".") - 1
ni = new CUI.NumberInput(opts)
ni.start()
if not ni.checkInput(v+"")
null
else
ni.formatValueForDisplay(v)
@parse: (string, decimals) ->
if isNaN(string.replace(/[,\.]/g, "")) # Remove comma to check if the input is valid.
return null
if isNaN(decimals)
decimals = 0
commaIndex = string.indexOf(",")
dotIndex = string.indexOf(".")
# Comma or dot not found, nothing to do.
if commaIndex == -1 and dotIndex == -1
return parseInt(string)
# In case both are found, we assume that the first one will be the thousand and the second one the decimal separator.
if dotIndex > 0 and commaIndex > 0
if dotIndex > commaIndex
thousandSeparatorRegex = /,/g
else
thousandSeparatorRegex = /\./g
# When decimal defined, we can guess the decimal separator if the input has the correct amount of decimals.
if not thousandSeparatorRegex and decimals > 0
decimal = string[string.length - 1 - decimals]
if decimal == ","
thousandSeparatorRegex = /\./g
else if decimal == "."
thousandSeparatorRegex = /,/g
# When there is no decimals it is possible to check with a regexp whether the separator is a valid thousand separator.
if not thousandSeparatorRegex and decimals == 0
if string.match(/^\d{1,3}([\.,]\d{3})+/)?[0] == string
if dotIndex > 0
thousandSeparatorRegex = /\./g
else
thousandSeparatorRegex = /,/g
# If we have decimals -1 (Double) and we only have a dot or a comma then we use that as decimal separator
if thousandSeparatorRegex
string = string.replace(thousandSeparatorRegex, "")
if decimals == 0
return parseInt(string)
else
string = string.replace(/,/, ".")
return parseFloat(string)