-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathinput.coffee
198 lines (163 loc) · 6.19 KB
/
input.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
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
exports.keyboardLayer = new Layer
x:0, y:Screen.height, width:Screen.width, height:432
html:"<img style='width: 100%;' src='modules/keyboard.png'/>"
#screen width vs. size of image width
growthRatio = Screen.width / 732
imageHeight = growthRatio * 432
# Extends the LayerStyle class which does the pixel ratio calculations in framer
_inputStyle =
Object.assign({}, Framer.LayerStyle,
calculatePixelRatio = (layer, value) ->
(value * layer.context.pixelMultiplier) + "px"
fontSize: (layer) ->
calculatePixelRatio(layer, layer._properties.fontSize)
lineHeight: (layer) ->
(layer._properties.lineHeight) + "em"
padding: (layer) ->
{ pixelMultiplier } = layer.context
padding = []
paddingValue = layer._properties.padding
# Check if we have a single number as integer
if Number.isInteger(paddingValue)
return calculatePixelRatio(layer, paddingValue)
# If we have multiple values they come as string (e.g. "1 2 3 4")
paddingValues = layer._properties.padding.split(" ")
switch paddingValues.length
when 4
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[2])
padding.left = parseFloat(paddingValues[3])
when 3
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[2])
padding.left = parseFloat(paddingValues[1])
when 2
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[0])
padding.left = parseFloat(paddingValues[1])
else
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[0])
padding.bottom = parseFloat(paddingValues[0])
padding.left = parseFloat(paddingValues[0])
# Return as 4-value string (e.g "1px 2px 3px 4px")
"#{padding.top * pixelMultiplier}px #{padding.right * pixelMultiplier}px #{padding.bottom * pixelMultiplier}px #{padding.left * pixelMultiplier}px"
)
exports.keyboardLayer.states =
shown:
y: Screen.height - imageHeight
exports.keyboardLayer.states.animationOptions =
curve: "spring(500,50,15)"
class exports.Input extends Layer
@define "style",
get: -> @input.style
set: (value) ->
_.extend @input.style, value
@define "value",
get: -> @input.value
set: (value) ->
@input.value = value
constructor: (options = {}) ->
options.setup ?= false
options.width ?= Screen.width
options.clip ?= false
options.height ?= 60
options.backgroundColor ?= if options.setup then "rgba(255, 60, 47, .5)" else "rgba(255, 255, 255, .01)" # "transparent" seems to cause a bug in latest safari version
options.fontSize ?= 30
options.lineHeight ?= 1
options.padding ?= 10
options.text ?= ""
options.placeholder ?= ""
options.virtualKeyboard ?= if Utils.isMobile() then false else true
options.type ?= "text"
options.goButton ?= false
options.autoCorrect ?= "on"
options.autoComplete ?= "on"
options.autoCapitalize ?= "on"
options.spellCheck ?= "on"
options.autofocus ?= false
options.textColor ?= "#000"
options.fontFamily ?= "-apple-system"
options.fontWeight ?= "500"
options.submit ?= false
options.tabIndex ?= 0
options.textarea ?= false
options.disabled ?= false
options.letterSpacing ?= 0
super options
# Add additional properties
@_properties.fontSize = options.fontSize
@_properties.lineHeight = options.lineHeight
@_properties.padding = options.padding
@placeholderColor = options.placeholderColor if options.placeholderColor?
@input = document.createElement if options.textarea then 'textarea' else 'input'
@input.id = "input-#{_.now()}"
# Add styling to the input element
@input.style.width = _inputStyle["width"](@)
@input.style.height = _inputStyle["height"](@)
@input.style.fontSize = _inputStyle["fontSize"](@)
@input.style.lineHeight = _inputStyle["lineHeight"](@)
@input.style.outline = "none"
@input.style.border = "none"
@input.style.backgroundColor = options.backgroundColor
@input.style.padding = _inputStyle["padding"](@)
@input.style.fontFamily = options.fontFamily
@input.style.color = options.textColor
@input.style.fontWeight = options.fontWeight
@input.style.letterSpacing = options.letterSpacing
@input.value = options.text
@input.type = options.type
@input.placeholder = options.placeholder
@input.setAttribute "tabindex", options.tabindex
@input.setAttribute "autocorrect", options.autoCorrect
@input.setAttribute "autocomplete", options.autoComplete
@input.setAttribute "autocapitalize", options.autoCapitalize
if options.disabled == true
@input.setAttribute "disabled", true
if options.autofocus == true
@input.setAttribute "autofocus", true
@input.setAttribute "spellcheck", options.spellCheck
@form = document.createElement "form"
if (options.goButton && !options.submit) || !options.submit
@form.action = "#"
@form.addEventListener "submit", (event) ->
event.preventDefault()
@form.appendChild @input
@_element.appendChild @form
@backgroundColor = "transparent"
@updatePlaceholderColor options.placeholderColor if @placeholderColor
#only show honor virtual keyboard option when not on mobile,
#otherwise ignore
if !Utils.isMobile() && options.virtualKeyboard is true
@input.addEventListener "focus", ->
exports.keyboardLayer.bringToFront()
exports.keyboardLayer.stateCycle()
@input.addEventListener "blur", ->
exports.keyboardLayer.animate("default")
updatePlaceholderColor: (color) ->
@placeholderColor = color
if @pageStyle?
document.head.removeChild @pageStyle
@pageStyle = document.createElement "style"
@pageStyle.type = "text/css"
css = "##{@input.id}::-webkit-input-placeholder { color: #{@placeholderColor}; }"
@pageStyle.appendChild(document.createTextNode css)
document.head.appendChild @pageStyle
focus: () ->
@input.focus()
unfocus: () ->
@input.blur()
onFocus: (cb) ->
@input.addEventListener "focus", ->
cb.apply(@)
onBlur: (cb) ->
@input.addEventListener "blur", ->
cb.apply(@)
onUnfocus: this.onBlur
disable: () ->
@input.setAttribute "disabled", true
enable: () =>
@input.removeAttribute "disabled", true