1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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 23 * SUCH DAMAGE. 24 */ 25 26 #ifndef DateTimeEditElement_h 27 #define DateTimeEditElement_h 28 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" 33 34 namespace blink { 35 36 class DateTimeFieldsState; 37 class KeyboardEvent; 38 class Locale; 39 class MouseEvent; 40 class StepRange; 41 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); 49 50 public: 51 // EditControlOwner implementer must call removeEditControlOwner when 52 // it doesn't handle event, e.g. at destruction. 53 class EditControlOwner : public WillBeGarbageCollectedMixin { 54 public: 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; 64 }; 65 66 struct LayoutParameters { 67 String dateTimeFormat; 68 String fallbackDateTimeFormat; 69 Locale& locale; 70 const StepRange stepRange; 71 DateComponents minimum; 72 DateComponents maximum; 73 String placeholderForDay; 74 String placeholderForMonth; 75 String placeholderForYear; 76 77 LayoutParameters(Locale& locale, const StepRange& stepRange) 78 : locale(locale) 79 , stepRange(stepRange) 80 { 81 } 82 }; 83 84 static PassRefPtrWillBeRawPtr<DateTimeEditElement> create(Document&, EditControlOwner&); 85 86 virtual ~DateTimeEditElement(); 87 virtual void trace(Visitor*) OVERRIDE; 88 89 void addField(PassRefPtrWillBeRawPtr<DateTimeFieldElement>); 90 bool anyEditableFieldsHaveValues() const; 91 void blurByOwner(); 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; } 102 void resetFields(); 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&); 107 void stepDown(); 108 void stepUp(); 109 String value() const; 110 DateTimeFieldsState valueAsDateTimeFieldsState() const; 111 112 private: 113 static const size_t invalidFieldIndex = static_cast<size_t>(-1); 114 115 // Datetime can be represent at most five fields, such as 116 // 1. year 117 // 2. month 118 // 3. day-of-month 119 // 4. hour 120 // 5. minute 121 // 6. second 122 // 7. millisecond 123 // 8. AM/PM 124 static const int maximumNumberOfFields = 8; 125 126 DateTimeEditElement(Document&, EditControlOwner&); 127 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(); 137 138 // Element function. 139 virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE; 140 virtual bool isDateTimeEditElement() const OVERRIDE; 141 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; 152 153 WillBeHeapVector<RawPtrWillBeMember<DateTimeFieldElement>, maximumNumberOfFields> m_fields; 154 RawPtrWillBeMember<EditControlOwner> m_editControlOwner; 155 }; 156 157 DEFINE_TYPE_CASTS(DateTimeEditElement, Element, element, element->isDateTimeEditElement(), element.isDateTimeEditElement()); 158 159 } // namespace blink 160 161 #endif 162 #endif 163