void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEvent)
{
    ASSERT(!isDisabled());
    if (keyboardEvent->type() != EventTypeNames::keypress)
        return;

    UChar charCode = static_cast<UChar>(keyboardEvent->charCode());
    String number = localeForOwner().convertFromLocalizedNumber(String(&charCode, 1));
    const int digit = number[0] - '0';
    if (digit < 0 || digit > 9)
        return;

    DOMTimeStamp delta = keyboardEvent->timeStamp() - m_lastDigitCharTime;
    m_lastDigitCharTime = keyboardEvent->timeStamp();

    if (delta > typeAheadTimeout)
        m_typeAheadBuffer.clear();
    m_typeAheadBuffer.append(number);

    int newValue = typeAheadValue();
    if (newValue >= m_hardLimits.minimum)
        setValueAsInteger(newValue, DispatchEvent);
    else {
        m_hasValue = false;
        updateVisibleValue(DispatchEvent);
    }

    if (m_typeAheadBuffer.length() >= DateTimeNumericFieldElement::formatValue(m_range.maximum).length() || newValue * 10 > m_range.maximum)
        focusOnNextField();

    keyboardEvent->setDefaultHandled();
}
DateTimeNumericFieldElement::DateTimeNumericFieldElement(
    Document& document,
    FieldOwner& fieldOwner,
    const Range& range,
    const Range& hardLimits,
    const String& placeholder,
    const DateTimeNumericFieldElement::Step& step)
    : DateTimeFieldElement(document, fieldOwner),
      m_placeholder(placeholder),
      m_range(range),
      m_hardLimits(hardLimits),
      m_step(step),
      m_value(0),
      m_hasValue(false) {
  DCHECK_NE(m_step.step, 0);
  DCHECK_LE(m_range.minimum, m_range.maximum);
  DCHECK_LE(m_hardLimits.minimum, m_hardLimits.maximum);

  // We show a direction-neutral string such as "--" as a placeholder. It
  // should follow the direction of numeric values.
  if (localeForOwner().isRTL()) {
    CharDirection dir = direction(formatValue(this->maximum())[0]);
    if (dir == LeftToRight || dir == EuropeanNumber || dir == ArabicNumber) {
      setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueBidiOverride);
      setInlineStyleProperty(CSSPropertyDirection, CSSValueLtr);
    }
  }
}
示例#3
0
void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEvent)
{
    if (isReadOnly())
        return;

    if (keyboardEvent->type() != eventNames().keypressEvent)
        return;

    UChar charCode = static_cast<UChar>(keyboardEvent->charCode());
    if (charCode < ' ')
        return;

    DOMTimeStamp delta = keyboardEvent->timeStamp() - m_lastDigitCharTime;
    m_lastDigitCharTime = 0;

    String number = localeForOwner().convertFromLocalizedNumber(String(&charCode, 1));
    const int digit = number[0] - '0';
    if (digit < 0 || digit > 9)
        return;

    keyboardEvent->setDefaultHandled();
    setValueAsInteger(m_hasValue && delta < typeAheadTimeout ? m_value * 10 + digit : digit, DispatchEvent);
    if (m_value * 10 > m_range.maximum)
        focusOnNextField();
    else
        m_lastDigitCharTime = keyboardEvent->timeStamp();
}
void DateTimeNumericFieldElement::handleKeyboardEvent(KeyboardEvent* keyboardEvent)
{
    ASSERT(!isDisabled());
    if (keyboardEvent->type() != EventTypeNames::keypress)
        return;

    UChar charCode = static_cast<UChar>(keyboardEvent->charCode());
    String number = localeForOwner().convertFromLocalizedNumber(String(&charCode, 1));
    const int digit = number[0] - '0';
    if (digit < 0 || digit > 9)
        return;

    unsigned maximumLength = DateTimeNumericFieldElement::formatValue(m_range.maximum).length();
    if (m_typeAheadBuffer.length() >= maximumLength) {
        String current = m_typeAheadBuffer.toString();
        m_typeAheadBuffer.clear();
        unsigned desiredLength = maximumLength - 1;
        m_typeAheadBuffer.append(current, current.length() - desiredLength, desiredLength);
    }
    m_typeAheadBuffer.append(number);
    int newValue = typeAheadValue();
    if (newValue >= m_hardLimits.minimum)
        setValueAsInteger(newValue, DispatchEvent);
    else {
        m_hasValue = false;
        updateVisibleValue(DispatchEvent);
    }

    if (m_typeAheadBuffer.length() >= maximumLength || newValue * 10 > m_range.maximum)
        focusOnNextField();

    keyboardEvent->setDefaultHandled();
}
String DateTimeNumericFieldElement::formatValue(int value) const {
  Locale& locale = localeForOwner();
  if (m_hardLimits.maximum > 999)
    return locale.convertToLocalizedNumber(String::format("%04d", value));
  if (m_hardLimits.maximum > 99)
    return locale.convertToLocalizedNumber(String::format("%03d", value));
  return locale.convertToLocalizedNumber(String::format("%02d", value));
}
示例#6
0
DateTimeNumericFieldElement::DateTimeNumericFieldElement(Document* document, FieldOwner& fieldOwner, int minimum, int maximum, const String& placeholder)
    : DateTimeFieldElement(document, fieldOwner)
    , m_lastDigitCharTime(0)
    , m_placeholder(placeholder)
    , m_range(minimum, maximum)
    , m_value(0)
    , m_hasValue(false)
{
    // We show a direction-neutral string such as "--" as a placeholder. It
    // should follow the direction of numeric values.
    if (localeForOwner().isRTL()) {
        Direction dir = direction(formatValue(this->maximum())[0]);
        if (dir == LeftToRight || dir == EuropeanNumber || dir == ArabicNumber) {
            setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueBidiOverride);
            setInlineStyleProperty(CSSPropertyDirection, CSSValueLtr);
        }
    }
}