-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberEntry.coffee
More file actions
180 lines (128 loc) · 4.54 KB
/
numberEntry.coffee
File metadata and controls
180 lines (128 loc) · 4.54 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
################
# jquery Number Entry Plugin
# @author Jason Raede <jason@torchedm.com>
# @license MIT
################
(
($) ->
$.numberEntry =
defaults:
decimalsAllowed:2
format:'usa'
allowNegative:false
mask: (el, retainSelection = true) ->
options = el.data('options')
decimalKey = options.decimalKey
thousandsKey = options.thousandsKey
cursorPos = el.getSelection().start
cursorDiff = el.getSelection().end - el.getSelection().start
val = el.val()
# Are we negative?
isNegative = false
if options.allowNegative and val.substr(0, 1) is '-'
isNegative = true
# They could theoretically have a hyphen anywhere but this will strip it out. we just
# re-add it to the front at the end.
val = val.replace(/\-/g, '')
# Strip out commas so we can re-add them
pos = val.indexOf(thousandsKey)
while pos >= 0
if pos < cursorPos
cursorPos--
val = val.substr(0, pos) + val.substr(pos + 1)
pos = val.indexOf(thousandsKey)
numberOfCharactersAddedBeforeCursor = 0
# Add commas and remove extraneous numbers
decimalPosition = val.indexOf(decimalKey)
newVal = val
if decimalPosition >= 0
decimalPosition
if newVal.length - options.decimalsAllowed >= decimalPosition
# Cut off the last decimal(s), there are too many
pos = decimalPosition + 1 + options.decimalsAllowed
newVal = newVal.substr(0, pos)
# Now add commas
split = newVal.split(decimalKey)
lengthBeforeDecimal = split[0].length
whole = split[0].split('').reverse().join('')
commaVal = ''
for i in [1..whole.length] by 1
key = i-1
commaVal += whole[key]
if !(i%3) and i isnt whole.length
commaVal += thousandsKey
if lengthBeforeDecimal - i < cursorPos
numberOfCharactersAddedBeforeCursor++
commaVal = commaVal.split('').reverse().join('')
final = commaVal
if split[1]?
final += decimalKey + split[1]
newVal = final
if isNegative
newVal = '-' + newVal
el.val(newVal)
if retainSelection
el.setSelection(cursorPos + numberOfCharactersAddedBeforeCursor, cursorPos + numberOfCharactersAddedBeforeCursor + cursorDiff)
$.fn.numberEntry = (options) ->
return @each ->
options = $.extend({}, $.numberEntry.defaults, options)
# keys to allow: digits, decimal point, arrow keys, tab key, return key,backspace. and HYPHEN
validKeys = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,110,190, 37,38,39,40,9,8]
if options.allowNegative
validKeys.push(189)
# Add the valid decimal separator from options
if options.format is 'usa'
validKeys.push(110)
validKeys.push(190)
if options.allowNegative
options.regex = /[^0-9\.\-]/g
else
options.regex = /[^0-9\.]/g
options.decimalKey = '.'
options.thousandsKey = ','
else
validKeys.push(188)
if options.allowNegative
options.regex = /[^0-9,\-]/g
else
options.regex = /[^0-9,]/g
options.decimalKey = ','
options.thousandsKey = '.'
if $(@).val()
$(@).val($.numberFormat.mask($(@), options))
$(@).data('options', options)
$(@).keydown (e) ->
key = e.charCode or e.keyCode or 0
val = $(@).val()
cursorPos = $(@).getSelection().start
decimalPosition = val.indexOf(options.decimalKey)
if validKeys.indexOf(key) < 0
# also allow copy, cut, and paste
if e.ctrlKey and ([67,86,88].indexOf(key) >= 0)
if key is 86 # we need to parse after pasting
@removeBadKeys = true
else if e.ctrlKey # control key means command, so let them do it
return true
else
return false
if decimalPosition >= 0 and ((key >= 48 and key <= 57) or (key >=96 and key <=105)) and decimalPosition is (val.length - (options.decimalsAllowed + 1))
if cursorPos is val.length
return false
if key is 190 or key is 110
if decimalPosition >= 0
return false
return true
$(@).keyup (e) ->
if @removeBadKeys
$(@).val($(@).val().replace(options.regex, ''))
@removeBadKeys = false
$.numberEntry.mask($(@))
$(@).blur (e) ->
# Worst case also do it on blur since they can click, then paste via the edit menu
$.numberEntry.mask($(@), false)
# It could be changed by another process, so if they're not actively entering anything,
# apply the formatting.
$(@).change (e) ->
if !$(@).is(':focus')
$.numberEntry.mask($(@))
)(window.jQuery)