-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnum.client.coffee
58 lines (52 loc) · 1.43 KB
/
num.client.coffee
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
Obs = require 'obs'
Dom = require 'dom'
Form = require 'form'
Colors = require('plugin').colors()
exports.render = render = (opts) ->
opts = {} if typeof opts!='object'
if opts.onSave
return Form.editInModal(opts,render)
sanitize = opts.normalize = (v) ->
v = 0|v
if v<opts.min
v = opts.max
else if v>opts.max
v = opts.min
else
v
[handleChange,orgValue] = Form.makeInput opts
obsVal = Obs.create orgValue
Obs.observe !->
handleChange obsVal.get()
renderArrow = (dir) !->
Dom.div !->
Dom.style
width: 0
height: 0
borderStyle: "solid"
borderWidth: "#{if dir>0 then 0 else 20}px 20px #{if dir>0 then 20 else 0}px 20px"
borderColor: "#{if dir>0 then 'transparent' else Colors.highlight} transparent #{if dir>0 then Colors.highlight else Colors.highlight} transparent"
Dom.onTap !->
obsVal.set sanitize(obsVal.peek()+dir)
Dom.div !->
Dom.style Box: "vertical center"
renderArrow opts.step||1
Dom.input !->
inputE = Dom.get()
val = ''+obsVal.get()
while val.length < opts.minDigits
val = '0'+val
Dom.prop
size: opts.digits||opts.minDigits||3
value: val
Dom.style
fontFamily: 'monospace'
fontSize: '30px'
fontWeight: 'bold'
textAlign: 'center'
border: 'inherit'
backgroundColor: 'inherit'
color: 'inherit'
Dom.on 'input change', !-> obsVal.set sanitize(inputE.value())
Dom.on 'click', !-> inputE.select()
renderArrow -(opts.step||1)