OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return m_range.maximum; | 97 return m_range.maximum; |
98 } | 98 } |
99 | 99 |
100 int DateTimeNumericFieldElement::defaultValueForStepUp() const | 100 int DateTimeNumericFieldElement::defaultValueForStepUp() const |
101 { | 101 { |
102 return m_range.minimum; | 102 return m_range.minimum; |
103 } | 103 } |
104 | 104 |
105 void DateTimeNumericFieldElement::didBlur() | 105 void DateTimeNumericFieldElement::didBlur() |
106 { | 106 { |
107 m_lastDigitCharTime = 0; | 107 int value = typeAheadValue(); |
| 108 m_typeAheadBuffer.clear(); |
| 109 if (value >= 0) |
| 110 setValueAsInteger(value, DispatchEvent); |
108 DateTimeFieldElement::didBlur(); | 111 DateTimeFieldElement::didBlur(); |
109 } | 112 } |
110 | 113 |
111 String DateTimeNumericFieldElement::formatValue(int value) const | 114 String DateTimeNumericFieldElement::formatValue(int value) const |
112 { | 115 { |
113 Locale& locale = localeForOwner(); | 116 Locale& locale = localeForOwner(); |
114 if (m_range.maximum > 999) | 117 if (m_range.maximum > 999) |
115 return locale.convertToLocalizedNumber(String::format("%04d", value)); | 118 return locale.convertToLocalizedNumber(String::format("%04d", value)); |
116 if (m_range.maximum > 99) | 119 if (m_range.maximum > 99) |
117 return locale.convertToLocalizedNumber(String::format("%03d", value)); | 120 return locale.convertToLocalizedNumber(String::format("%03d", value)); |
118 return locale.convertToLocalizedNumber(String::format("%02d", value)); | 121 return locale.convertToLocalizedNumber(String::format("%02d", value)); |
119 } | 122 } |
120 | 123 |
121 void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEve
nt) | 124 void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEve
nt) |
122 { | 125 { |
123 if (isReadOnly()) | 126 if (isReadOnly()) |
124 return; | 127 return; |
125 | 128 |
126 if (keyboardEvent->type() != eventNames().keypressEvent) | 129 if (keyboardEvent->type() != eventNames().keypressEvent) |
127 return; | 130 return; |
128 | 131 |
129 UChar charCode = static_cast<UChar>(keyboardEvent->charCode()); | 132 UChar charCode = static_cast<UChar>(keyboardEvent->charCode()); |
130 if (charCode < ' ') | |
131 return; | |
132 | |
133 DOMTimeStamp delta = keyboardEvent->timeStamp() - m_lastDigitCharTime; | |
134 m_lastDigitCharTime = 0; | |
135 | |
136 String number = localeForOwner().convertFromLocalizedNumber(String(&charCode
, 1)); | 133 String number = localeForOwner().convertFromLocalizedNumber(String(&charCode
, 1)); |
137 const int digit = number[0] - '0'; | 134 const int digit = number[0] - '0'; |
138 if (digit < 0 || digit > 9) | 135 if (digit < 0 || digit > 9) |
139 return; | 136 return; |
140 | 137 |
| 138 DOMTimeStamp delta = keyboardEvent->timeStamp() - m_lastDigitCharTime; |
| 139 m_lastDigitCharTime = keyboardEvent->timeStamp(); |
| 140 |
| 141 if (delta > typeAheadTimeout) |
| 142 m_typeAheadBuffer.clear(); |
| 143 m_typeAheadBuffer.append(number); |
| 144 |
| 145 int newValue = typeAheadValue(); |
| 146 if (m_range.isInRange(newValue)) |
| 147 setValueAsInteger(newValue, DispatchEvent); |
| 148 else |
| 149 updateVisibleValue(DispatchEvent); |
| 150 |
| 151 if (m_typeAheadBuffer.length() >= DateTimeNumericFieldElement::formatValue(m
_range.maximum).length() || newValue * 10 > m_range.maximum) |
| 152 focusOnNextField(); |
| 153 |
141 keyboardEvent->setDefaultHandled(); | 154 keyboardEvent->setDefaultHandled(); |
142 setValueAsInteger(m_hasValue && delta < typeAheadTimeout ? m_value * 10 + di
git : digit, DispatchEvent); | |
143 if (m_value * 10 > m_range.maximum) | |
144 focusOnNextField(); | |
145 else | |
146 m_lastDigitCharTime = keyboardEvent->timeStamp(); | |
147 } | 155 } |
148 | 156 |
149 bool DateTimeNumericFieldElement::hasValue() const | 157 bool DateTimeNumericFieldElement::hasValue() const |
150 { | 158 { |
151 return m_hasValue; | 159 return m_hasValue; |
152 } | 160 } |
153 | 161 |
154 int DateTimeNumericFieldElement::maximum() const | 162 int DateTimeNumericFieldElement::maximum() const |
155 { | 163 { |
156 return m_range.maximum; | 164 return m_range.maximum; |
157 } | 165 } |
158 | 166 |
159 int DateTimeNumericFieldElement::minimum() const | 167 int DateTimeNumericFieldElement::minimum() const |
160 { | 168 { |
161 return m_range.minimum; | 169 return m_range.minimum; |
162 } | 170 } |
163 | 171 |
164 void DateTimeNumericFieldElement::setEmptyValue(EventBehavior eventBehavior) | 172 void DateTimeNumericFieldElement::setEmptyValue(EventBehavior eventBehavior) |
165 { | 173 { |
166 m_lastDigitCharTime = 0; | |
167 | |
168 if (isReadOnly()) | 174 if (isReadOnly()) |
169 return; | 175 return; |
170 | 176 |
171 m_hasValue = false; | 177 m_hasValue = false; |
172 m_value = 0; | 178 m_value = 0; |
| 179 m_typeAheadBuffer.clear(); |
173 updateVisibleValue(eventBehavior); | 180 updateVisibleValue(eventBehavior); |
174 } | 181 } |
175 | 182 |
176 void DateTimeNumericFieldElement::setValueAsInteger(int value, EventBehavior eve
ntBehavior) | 183 void DateTimeNumericFieldElement::setValueAsInteger(int value, EventBehavior eve
ntBehavior) |
177 { | 184 { |
178 m_value = clampValueForHardLimits(value); | 185 m_value = clampValueForHardLimits(value); |
179 m_hasValue = true; | 186 m_hasValue = true; |
180 updateVisibleValue(eventBehavior); | 187 updateVisibleValue(eventBehavior); |
181 m_lastDigitCharTime = 0; | |
182 } | 188 } |
183 | 189 |
184 void DateTimeNumericFieldElement::stepDown() | 190 void DateTimeNumericFieldElement::stepDown() |
185 { | 191 { |
| 192 m_typeAheadBuffer.clear(); |
186 if (m_hasValue) | 193 if (m_hasValue) |
187 setValueAsInteger(m_value == m_range.minimum ? m_range.maximum : clampVa
lue(m_value - 1), DispatchEvent); | 194 setValueAsInteger(m_value == m_range.minimum ? m_range.maximum : clampVa
lue(m_value - 1), DispatchEvent); |
188 else | 195 else |
189 setValueAsInteger(defaultValueForStepDown(), DispatchEvent); | 196 setValueAsInteger(defaultValueForStepDown(), DispatchEvent); |
190 } | 197 } |
191 | 198 |
192 void DateTimeNumericFieldElement::stepUp() | 199 void DateTimeNumericFieldElement::stepUp() |
193 { | 200 { |
| 201 m_typeAheadBuffer.clear(); |
194 if (m_hasValue) | 202 if (m_hasValue) |
195 setValueAsInteger(m_value == m_range.maximum ? m_range.minimum : clampVa
lue(m_value + 1), DispatchEvent); | 203 setValueAsInteger(m_value == m_range.maximum ? m_range.minimum : clampVa
lue(m_value + 1), DispatchEvent); |
196 else | 204 else |
197 setValueAsInteger(defaultValueForStepUp(), DispatchEvent); | 205 setValueAsInteger(defaultValueForStepUp(), DispatchEvent); |
198 } | 206 } |
199 | 207 |
200 String DateTimeNumericFieldElement::value() const | 208 String DateTimeNumericFieldElement::value() const |
201 { | 209 { |
202 return m_hasValue ? formatValue(m_value) : emptyString(); | 210 return m_hasValue ? formatValue(m_value) : emptyString(); |
203 } | 211 } |
204 | 212 |
205 int DateTimeNumericFieldElement::valueAsInteger() const | 213 int DateTimeNumericFieldElement::valueAsInteger() const |
206 { | 214 { |
207 return m_hasValue ? m_value : -1; | 215 return m_hasValue ? m_value : -1; |
208 } | 216 } |
209 | 217 |
| 218 int DateTimeNumericFieldElement::typeAheadValue() const |
| 219 { |
| 220 if (m_typeAheadBuffer.length()) |
| 221 return m_typeAheadBuffer.toString().toInt(); |
| 222 return -1; |
| 223 } |
| 224 |
210 String DateTimeNumericFieldElement::visibleValue() const | 225 String DateTimeNumericFieldElement::visibleValue() const |
211 { | 226 { |
| 227 if (m_typeAheadBuffer.length()) |
| 228 return formatValue(typeAheadValue()); |
212 return m_hasValue ? value() : m_placeholder; | 229 return m_hasValue ? value() : m_placeholder; |
213 } | 230 } |
214 | 231 |
215 } // namespace WebCore | 232 } // namespace WebCore |
216 | 233 |
217 #endif | 234 #endif |
OLD | NEW |