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 #include "config.h" 27 #if !ENABLE(INPUT_MULTIPLE_FIELDS_UI) 28 #include "core/html/forms/BaseChooserOnlyDateAndTimeInputType.h" 29 30 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 31 #include "core/dom/Document.h" 32 #include "core/dom/shadow/ShadowRoot.h" 33 #include "core/frame/FrameHost.h" 34 #include "core/html/HTMLDivElement.h" 35 #include "core/html/HTMLInputElement.h" 36 #include "core/page/Chrome.h" 37 #include "platform/UserGestureIndicator.h" 38 39 namespace blink { 40 41 BaseChooserOnlyDateAndTimeInputType::~BaseChooserOnlyDateAndTimeInputType() 42 { 43 closeDateTimeChooser(); 44 } 45 46 void BaseChooserOnlyDateAndTimeInputType::handleDOMActivateEvent(Event*) 47 { 48 if (element().isDisabledOrReadOnly() || !element().renderer() || !UserGestureIndicator::processingUserGesture() || element().hasAuthorShadowRoot()) 49 return; 50 51 if (m_dateTimeChooser) 52 return; 53 if (!element().document().isActive()) 54 return; 55 DateTimeChooserParameters parameters; 56 if (!element().setupDateTimeChooserParameters(parameters)) 57 return; 58 m_dateTimeChooser = element().document().frameHost()->chrome().openDateTimeChooser(this, parameters); 59 } 60 61 void BaseChooserOnlyDateAndTimeInputType::createShadowSubtree() 62 { 63 DEFINE_STATIC_LOCAL(AtomicString, valueContainerPseudo, ("-webkit-date-and-time-value", AtomicString::ConstructFromLiteral)); 64 65 RefPtrWillBeRawPtr<HTMLDivElement> valueContainer = HTMLDivElement::create(element().document()); 66 valueContainer->setShadowPseudoId(valueContainerPseudo); 67 element().userAgentShadowRoot()->appendChild(valueContainer.get()); 68 updateView(); 69 } 70 71 void BaseChooserOnlyDateAndTimeInputType::updateView() 72 { 73 Node* node = element().userAgentShadowRoot()->firstChild(); 74 if (!node || !node->isHTMLElement()) 75 return; 76 String displayValue; 77 if (!element().suggestedValue().isNull()) 78 displayValue = element().suggestedValue(); 79 else 80 displayValue = visibleValue(); 81 if (displayValue.isEmpty()) { 82 // Need to put something to keep text baseline. 83 displayValue = " "; 84 } 85 toHTMLElement(node)->setInnerText(displayValue, ASSERT_NO_EXCEPTION); 86 } 87 88 void BaseChooserOnlyDateAndTimeInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior) 89 { 90 BaseDateAndTimeInputType::setValue(value, valueChanged, eventBehavior); 91 if (valueChanged) 92 updateView(); 93 } 94 95 void BaseChooserOnlyDateAndTimeInputType::closePopupView() 96 { 97 closeDateTimeChooser(); 98 } 99 100 Element& BaseChooserOnlyDateAndTimeInputType::ownerElement() const 101 { 102 return element(); 103 } 104 105 void BaseChooserOnlyDateAndTimeInputType::didChooseValue(const String& value) 106 { 107 element().setValue(value, DispatchInputAndChangeEvent); 108 } 109 110 void BaseChooserOnlyDateAndTimeInputType::didChooseValue(double value) 111 { 112 ASSERT(std::isfinite(value) || std::isnan(value)); 113 if (std::isnan(value)) 114 element().setValue(emptyString(), DispatchInputAndChangeEvent); 115 else 116 element().setValueAsNumber(value, ASSERT_NO_EXCEPTION, DispatchInputAndChangeEvent); 117 } 118 119 void BaseChooserOnlyDateAndTimeInputType::didEndChooser() 120 { 121 m_dateTimeChooser.clear(); 122 } 123 124 void BaseChooserOnlyDateAndTimeInputType::closeDateTimeChooser() 125 { 126 if (m_dateTimeChooser) 127 m_dateTimeChooser->endChooser(); 128 } 129 130 void BaseChooserOnlyDateAndTimeInputType::handleKeydownEvent(KeyboardEvent* event) 131 { 132 BaseClickableWithKeyInputType::handleKeydownEvent(element(), event); 133 } 134 135 void BaseChooserOnlyDateAndTimeInputType::handleKeypressEvent(KeyboardEvent* event) 136 { 137 BaseClickableWithKeyInputType::handleKeypressEvent(element(), event); 138 } 139 140 void BaseChooserOnlyDateAndTimeInputType::handleKeyupEvent(KeyboardEvent* event) 141 { 142 BaseClickableWithKeyInputType::handleKeyupEvent(*this, event); 143 } 144 145 void BaseChooserOnlyDateAndTimeInputType::accessKeyAction(bool sendMouseEvents) 146 { 147 BaseDateAndTimeInputType::accessKeyAction(sendMouseEvents); 148 BaseClickableWithKeyInputType::accessKeyAction(element(), sendMouseEvents); 149 } 150 151 } 152 #endif 153