Change the serialization format of form control state to make the code simple
[WebKit-https.git] / Source / WebCore / html / shadow / CalendarPickerElement.cpp
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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "CalendarPickerElement.h"
33
34 #if ENABLE(CALENDAR_PICKER)
35
36 #include "CalendarPicker.h"
37 #include "Chrome.h"
38 #include "ChromeClient.h"
39 #include "DateComponents.h"
40 #include "DocumentWriter.h"
41 #include "Event.h"
42 #include "FrameView.h"
43 #include "HTMLInputElement.h"
44 #include "HTMLNames.h"
45 #include "Language.h"
46 #include "LocalizedDate.h"
47 #include "LocalizedStrings.h"
48 #include "Page.h"
49 #include "RenderDetailsMarker.h"
50 #include "RenderTheme.h"
51 #include <wtf/text/StringBuilder.h>
52
53 using namespace WTF::Unicode;
54
55 namespace WebCore {
56
57 using namespace HTMLNames;
58
59 inline CalendarPickerElement::CalendarPickerElement(Document* document)
60     : HTMLDivElement(divTag, document)
61     , m_popup(0)
62 {
63     setShadowPseudoId("-webkit-calendar-picker-indicator");
64 }
65
66 PassRefPtr<CalendarPickerElement> CalendarPickerElement::create(Document* document)
67 {
68     return adoptRef(new CalendarPickerElement(document));
69 }
70
71 CalendarPickerElement::~CalendarPickerElement()
72 {
73     closePopup();
74     ASSERT(!m_popup);
75 }
76
77 RenderObject* CalendarPickerElement::createRenderer(RenderArena* arena, RenderStyle*)
78 {
79     return new (arena) RenderDetailsMarker(this);
80 }
81
82 inline HTMLInputElement* CalendarPickerElement::hostInput()
83 {
84     ASSERT(shadowAncestorNode());
85     ASSERT(shadowAncestorNode()->hasTagName(inputTag));
86     return static_cast<HTMLInputElement*>(shadowAncestorNode());
87 }
88
89 void CalendarPickerElement::defaultEventHandler(Event* event)
90 {
91     HTMLInputElement* input = hostInput();
92     if (input->readOnly() || input->disabled())
93         return;
94
95     if (event->type() == eventNames().clickEvent) {
96         openPopup();
97         event->setDefaultHandled();
98     }
99
100     if (!event->defaultHandled())
101         HTMLDivElement::defaultEventHandler(event);
102 }
103
104 void CalendarPickerElement::openPopup()
105 {
106     if (m_popup)
107         return;
108     if (!document()->page())
109         return;
110     Chrome* chrome = document()->page()->chrome();
111     if (!chrome)
112         return;
113     if (!document()->view())
114         return;
115     IntRect elementRectInRootView = document()->view()->contentsToRootView(hostInput()->getPixelSnappedRect());
116     m_popup = chrome->client()->openPagePopup(this, elementRectInRootView);
117 }
118
119 void CalendarPickerElement::closePopup()
120 {
121     if (!m_popup)
122         return;
123     if (!document()->page())
124         return;
125     Chrome* chrome = document()->page()->chrome();
126     if (!chrome)
127         return;
128     chrome->client()->closePagePopup(m_popup);
129 }
130
131 void CalendarPickerElement::detach()
132 {
133     closePopup();
134     HTMLDivElement::detach();
135 }
136
137 IntSize CalendarPickerElement::contentSize()
138 {
139     return IntSize(100, 100);
140 }
141
142 #define addLiteral(literal, writer)    writer.addData(literal, sizeof(literal) - 1)
143
144 static inline void addString(const String& str, DocumentWriter& writer)
145 {
146     CString str8 = str.utf8();
147     writer.addData(str8.data(), str8.length());
148 }
149
150 static void addJavaScriptString(const String& str, DocumentWriter& writer)
151 {
152     addLiteral("\"", writer);
153     StringBuilder builder;
154     builder.appendEscaped(str, '\\', '"');
155     addString(builder.toString(), writer);
156     addLiteral("\"", writer);
157 }
158
159 static void addProperty(const char* name, const String& value, DocumentWriter& writer)
160 {
161     writer.addData(name, strlen(name));
162     addLiteral(": ", writer);
163     addJavaScriptString(value, writer);
164     addLiteral(",\n", writer);
165 }
166
167 static void addProperty(const char* name, unsigned value, DocumentWriter& writer)
168 {
169     writer.addData(name, strlen(name));
170     addLiteral(": ", writer);
171     addString(String::number(value), writer);
172     addLiteral(",\n", writer);
173 }
174
175 static void addProperty(const char* name, bool value, DocumentWriter& writer)
176 {
177     writer.addData(name, strlen(name));
178     addLiteral(": ", writer);
179     if (value)
180         addLiteral("true", writer);
181     else
182         addLiteral("false", writer);
183     addLiteral(",\n", writer);
184 }
185
186 static void addProperty(const char* name, const Vector<String>& values, DocumentWriter& writer)
187 {
188     writer.addData(name, strlen(name));
189     addLiteral(": [", writer);
190     for (unsigned i = 0; i < values.size(); ++i) {
191         if (i)
192             addLiteral(",", writer);
193         addJavaScriptString(values[i], writer);
194     }
195     addLiteral("],\n", writer);
196 }
197
198 void CalendarPickerElement::writeDocument(DocumentWriter& writer)
199 {
200     HTMLInputElement* input = hostInput();
201     DateComponents date;
202     date.setMillisecondsSinceEpochForDate(input->minimum());
203     String minString = date.toString();
204     date.setMillisecondsSinceEpochForDate(input->maximum());
205     String maxString = date.toString();
206     Decimal step;
207     String stepString = input->fastGetAttribute(stepAttr);
208     if (stepString.isEmpty() || !input->getAllowedValueStep(&step))
209         stepString = "1";
210
211     addLiteral("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", writer);
212     writer.addData(calendarPickerCss, sizeof(calendarPickerCss));
213     if (document()->page()) {
214         CString extraStyle = document()->page()->theme()->extraCalendarPickerStyleSheet();
215         if (extraStyle.length())
216             writer.addData(extraStyle.data(), extraStyle.length());
217     }
218     addLiteral("</style></head><body><div id=main>Loading...</div><script>\n"
219                "window.dialogArguments = {\n", writer);
220     addProperty("min", minString, writer);
221     addProperty("max", maxString, writer);
222     addProperty("step", stepString, writer);
223     addProperty("required", input->required(), writer);
224     addProperty("currentValue", input->value(), writer);
225     addProperty("locale", defaultLanguage(), writer);
226     addProperty("todayLabel", calendarTodayText(), writer);
227     addProperty("clearLabel", calendarClearText(), writer);
228     addProperty("weekStartDay", firstDayOfWeek(), writer);
229     addProperty("monthLabels", monthLabels(), writer);
230     addProperty("dayLabels", weekDayShortLabels(), writer);
231     Direction dir = direction(monthLabels()[0][0]);
232     addProperty("isRTL", dir == RightToLeft || dir == RightToLeftArabic, writer);
233     addLiteral("}\n", writer);
234
235     writer.addData(calendarPickerJs, sizeof(calendarPickerJs));
236     addLiteral("</script></body>\n", writer);
237 }
238
239 void CalendarPickerElement::setValueAndClosePopup(int numValue, const String& stringValue)
240 {
241     ASSERT(m_popup);
242     closePopup();
243     if (numValue >= 0)
244         hostInput()->setValue(stringValue, DispatchChangeEvent);
245 }
246
247 void CalendarPickerElement::didClosePopup()
248 {
249     m_popup = 0;
250 }
251
252 }
253
254 #endif