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/BaseChooserOnlyDateAndTimeInputType.h" 29 30 #include "bindings/v8/ExceptionStatePlaceholder.h" 31 #include "bindings/v8/ScriptController.h" 32 #include "core/dom/shadow/ShadowRoot.h" 33 #include "core/html/HTMLDivElement.h" 34 #include "core/html/HTMLInputElement.h" 35 #include "core/page/Chrome.h" 36 #include "core/page/Page.h" 37 38 namespace WebCore { 39 40 BaseChooserOnlyDateAndTimeInputType::~BaseChooserOnlyDateAndTimeInputType() 41 { 42 closeDateTimeChooser(); 43 } 44 45 void BaseChooserOnlyDateAndTimeInputType::handleDOMActivateEvent(Event*) 46 { 47 if (element()->isDisabledOrReadOnly() || !element()->renderer() || !ScriptController::processingUserGesture()) 48 return; 49 50 if (m_dateTimeChooser) 51 return; 52 if (!element()->document()->page()) 53 return; 54 DateTimeChooserParameters parameters; 55 if (!element()->setupDateTimeChooserParameters(parameters)) 56 return; 57 m_dateTimeChooser = element()->document()->page()->chrome().openDateTimeChooser(this, parameters); 58 } 59 60 void BaseChooserOnlyDateAndTimeInputType::createShadowSubtree() 61 { 62 DEFINE_STATIC_LOCAL(AtomicString, valueContainerPseudo, ("-webkit-date-and-time-value", AtomicString::ConstructFromLiteral)); 63 64 RefPtr<HTMLDivElement> valueContainer = HTMLDivElement::create(element()->document()); 65 valueContainer->setPart(valueContainerPseudo); 66 element()->userAgentShadowRoot()->appendChild(valueContainer.get()); 67 updateAppearance(); 68 } 69 70 void BaseChooserOnlyDateAndTimeInputType::updateAppearance() 71 { 72 Node* node = element()->userAgentShadowRoot()->firstChild(); 73 if (!node || !node->isHTMLElement()) 74 return; 75 String displayValue = visibleValue(); 76 if (displayValue.isEmpty()) { 77 // Need to put something to keep text baseline. 78 displayValue = " "; 79 } 80 toHTMLElement(node)->setInnerText(displayValue, ASSERT_NO_EXCEPTION); 81 } 82 83 void BaseChooserOnlyDateAndTimeInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior) 84 { 85 BaseDateAndTimeInputType::setValue(value, valueChanged, eventBehavior); 86 if (valueChanged) 87 updateAppearance(); 88 } 89 90 void BaseChooserOnlyDateAndTimeInputType::detach() 91 { 92 closeDateTimeChooser(); 93 } 94 95 void BaseChooserOnlyDateAndTimeInputType::didChooseValue(const String& value) 96 { 97 element()->setValue(value, DispatchInputAndChangeEvent); 98 } 99 100 void BaseChooserOnlyDateAndTimeInputType::didEndChooser() 101 { 102 m_dateTimeChooser.clear(); 103 } 104 105 void BaseChooserOnlyDateAndTimeInputType::closeDateTimeChooser() 106 { 107 if (m_dateTimeChooser) 108 m_dateTimeChooser->endChooser(); 109 } 110 111 void BaseChooserOnlyDateAndTimeInputType::handleKeydownEvent(KeyboardEvent* event) 112 { 113 BaseClickableWithKeyInputType::handleKeydownEvent(element(), event); 114 } 115 116 void BaseChooserOnlyDateAndTimeInputType::handleKeypressEvent(KeyboardEvent* event) 117 { 118 BaseClickableWithKeyInputType::handleKeypressEvent(element(), event); 119 } 120 121 void BaseChooserOnlyDateAndTimeInputType::handleKeyupEvent(KeyboardEvent* event) 122 { 123 BaseClickableWithKeyInputType::handleKeyupEvent(*this, event); 124 } 125 126 void BaseChooserOnlyDateAndTimeInputType::accessKeyAction(bool sendMouseEvents) 127 { 128 BaseDateAndTimeInputType::accessKeyAction(sendMouseEvents); 129 BaseClickableWithKeyInputType::accessKeyAction(element(), sendMouseEvents); 130 } 131 132 } 133 #endif 134