2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #ifndef DateTimeEditElement_h
27 #define DateTimeEditElement_h
29 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
30 #include "core/html/forms/StepRange.h"
31 #include "core/html/shadow/DateTimeFieldElement.h"
32 #include "platform/DateComponents.h"
36 class DateTimeFieldsState;
42 // DateTimeEditElement class contains numberic field and symbolc field for
43 // representing date and time, such as
44 // - Year, Month, Day Of Month
45 // - Hour, Minute, Second, Millisecond, AM/PM
46 class DateTimeEditElement FINAL : public HTMLDivElement, public DateTimeFieldElement::FieldOwner {
47 WTF_MAKE_NONCOPYABLE(DateTimeEditElement);
48 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(DateTimeEditElement);
51 // EditControlOwner implementer must call removeEditControlOwner when
52 // it doesn't handle event, e.g. at destruction.
53 class EditControlOwner : public WillBeGarbageCollectedMixin {
55 virtual ~EditControlOwner();
56 virtual void didBlurFromControl() = 0;
57 virtual void didFocusOnControl() = 0;
58 virtual void editControlValueChanged() = 0;
59 virtual String formatDateTimeFieldsState(const DateTimeFieldsState&) const = 0;
60 virtual bool isEditControlOwnerDisabled() const = 0;
61 virtual bool isEditControlOwnerReadOnly() const = 0;
62 virtual AtomicString localeIdentifier() const = 0;
63 virtual void editControlDidChangeValueByKeyboard() = 0;
66 struct LayoutParameters {
67 String dateTimeFormat;
68 String fallbackDateTimeFormat;
70 const StepRange stepRange;
71 DateComponents minimum;
72 DateComponents maximum;
73 String placeholderForDay;
74 String placeholderForMonth;
75 String placeholderForYear;
77 LayoutParameters(Locale& locale, const StepRange& stepRange)
79 , stepRange(stepRange)
84 static PassRefPtrWillBeRawPtr<DateTimeEditElement> create(Document&, EditControlOwner&);
86 virtual ~DateTimeEditElement();
87 virtual void trace(Visitor*) OVERRIDE;
89 void addField(PassRefPtrWillBeRawPtr<DateTimeFieldElement>);
90 bool anyEditableFieldsHaveValues() const;
92 virtual void defaultEventHandler(Event*) OVERRIDE;
93 void disabledStateChanged();
94 Element* fieldsWrapperElement() const;
95 void focusIfNoFocus();
96 // If oldFocusedNode is one of sub-fields, focus on it. Otherwise focus on
97 // the first sub-field.
98 void focusByOwner(Element* oldFocusedElement = 0);
99 bool hasFocusedField();
100 void readOnlyStateChanged();
101 void removeEditControlOwner() { m_editControlOwner = nullptr; }
103 void setEmptyValue(const LayoutParameters&, const DateComponents& dateForReadOnlyField);
104 void setValueAsDate(const LayoutParameters&, const DateComponents&);
105 void setValueAsDateTimeFieldsState(const DateTimeFieldsState&);
106 void setOnlyYearMonthDay(const DateComponents&);
109 String value() const;
110 DateTimeFieldsState valueAsDateTimeFieldsState() const;
113 static const size_t invalidFieldIndex = static_cast<size_t>(-1);
115 // Datetime can be represent at most five fields, such as
124 static const int maximumNumberOfFields = 8;
126 DateTimeEditElement(Document&, EditControlOwner&);
128 DateTimeFieldElement* fieldAt(size_t) const;
129 size_t fieldIndexOf(const DateTimeFieldElement&) const;
130 DateTimeFieldElement* focusedField() const;
131 size_t focusedFieldIndex() const;
132 bool focusOnNextFocusableField(size_t startIndex);
133 bool isDisabled() const;
134 bool isReadOnly() const;
135 void layout(const LayoutParameters&, const DateComponents&);
136 void updateUIState();
139 virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
140 virtual bool isDateTimeEditElement() const OVERRIDE;
142 // DateTimeFieldElement::FieldOwner functions.
143 virtual void didBlurFromField() OVERRIDE;
144 virtual void didFocusOnField() OVERRIDE;
145 virtual void fieldValueChanged() OVERRIDE;
146 virtual bool focusOnNextField(const DateTimeFieldElement&) OVERRIDE;
147 virtual bool focusOnPreviousField(const DateTimeFieldElement&) OVERRIDE;
148 virtual bool isFieldOwnerDisabled() const OVERRIDE;
149 virtual bool isFieldOwnerReadOnly() const OVERRIDE;
150 virtual AtomicString localeIdentifier() const OVERRIDE;
151 virtual void fieldDidChangeValueByKeyboard() OVERRIDE;
153 WillBeHeapVector<RawPtrWillBeMember<DateTimeFieldElement>, maximumNumberOfFields> m_fields;
154 RawPtrWillBeMember<EditControlOwner> m_editControlOwner;
157 DEFINE_TYPE_CASTS(DateTimeEditElement, Element, element, element->isDateTimeEditElement(), element.isDateTimeEditElement());