-
Notifications
You must be signed in to change notification settings - Fork 14
/
bindModel.js
182 lines (152 loc) · 4.97 KB
/
bindModel.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
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
var listener = require('./listener')
var binding = require('./binding')
var RefreshHook = require('./render').RefreshHook
module.exports = function (tag, attributes, children) {
var type = inputType(tag, attributes)
var bind = inputTypeBindings[type] || bindTextInput
bind(attributes, children, binding(attributes.binding))
}
var inputTypeBindings = {
text: bindTextInput,
textarea: bindTextInput,
checkbox: function (attributes, children, binding) {
attributes.checked = binding.get()
attachEventHandler(attributes, 'onclick', function (ev) {
attributes.checked = ev.target.checked
return binding.set(ev.target.checked)
}, binding)
},
radio: function (attributes, children, binding) {
var value = attributes.value
attributes.checked = binding.get() === attributes.value
attributes.on_hyperdomsyncchecked = listener(function (event) {
attributes.checked = event.target.checked
})
attachEventHandler(attributes, 'onclick', function (event) {
var name = event.target.name
if (name) {
var inputs = document.getElementsByName(name)
for (var i = 0, l = inputs.length; i < l; i++) {
inputs[i].dispatchEvent(customEvent('_hyperdomsyncchecked'))
}
}
return binding.set(value)
}, binding)
},
select: function (attributes, children, binding) {
var currentValue = binding.get()
var options = []
var tagName
children.forEach(function (child) {
tagName = child.tagName && child.tagName.toLowerCase()
switch (tagName) {
case 'optgroup':
child.children.forEach(function (optChild) {
if (optChild.tagName && optChild.tagName.toLowerCase() === 'option') {
options.push(optChild)
}
})
break
case 'option':
options.push(child)
break
}
})
var values = []
var valueSelected = attributes.multiple
? function (value) { return currentValue instanceof Array && currentValue.indexOf(value) >= 0 }
: function (value) { return currentValue === value }
for (var n = 0; n < options.length; n++) {
var option = options[n]
var hasValue = option.properties.hasOwnProperty('value')
var value = option.properties.value
var text = option.children.map(function (x) { return x.text }).join('')
values.push(hasValue ? value : text)
var selected = valueSelected(hasValue ? value : text)
option.properties.selected = selected
}
attachEventHandler(attributes, 'onchange', function (ev) {
if (ev.target.multiple) {
var options = ev.target.options
var selectedValues = []
for (var n = 0; n < options.length; n++) {
var op = options[n]
if (op.selected) {
selectedValues.push(values[n])
}
}
return binding.set(selectedValues)
} else {
attributes.selectedIndex = ev.target.selectedIndex
return binding.set(values[ev.target.selectedIndex])
}
}, binding)
},
file: function (attributes, children, binding) {
var multiple = attributes.multiple
attachEventHandler(attributes, 'onchange', function (ev) {
if (multiple) {
return binding.set(ev.target.files)
} else {
return binding.set(ev.target.files[0])
}
}, binding)
}
}
function inputType (selector, attributes) {
if (/^textarea\b/i.test(selector)) {
return 'textarea'
} else if (/^select\b/i.test(selector)) {
return 'select'
} else {
return attributes.type || 'text'
}
}
function bindTextInput (attributes, children, binding) {
var textEventNames = ['onkeyup', 'oninput', 'onpaste', 'textInput']
var bindingValue = binding.get()
if (!(bindingValue instanceof Error)) {
attributes.value = bindingValue !== undefined ? bindingValue : ''
}
attachEventHandler(attributes, textEventNames, function (ev) {
if (binding.get() !== ev.target.value) {
return binding.set(ev.target.value)
}
}, binding)
}
function attachEventHandler (attributes, eventNames, handler) {
if (eventNames instanceof Array) {
for (var n = 0; n < eventNames.length; n++) {
insertEventHandler(attributes, eventNames[n], handler)
}
} else {
insertEventHandler(attributes, eventNames, handler)
}
}
function insertEventHandler (attributes, eventName, handler) {
var previousHandler = attributes[eventName]
if (previousHandler) {
attributes[eventName] = sequenceFunctions(handler, previousHandler)
} else {
attributes[eventName] = handler
}
}
function sequenceFunctions (handler1, handler2) {
return function (ev) {
handler1(ev)
if (handler2 instanceof RefreshHook) {
return handler2.handler(ev)
} else {
return handler2(ev)
}
}
}
function customEvent (name) {
if (typeof Event === 'function') {
return new window.Event(name)
} else {
var event = document.createEvent('Event')
event.initEvent(name, false, false)
return event
}
}