DatePickerClient.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "DatePickerClient.h"
  20. #include "Document.h"
  21. #include "Frame.h"
  22. #include "HTMLInputElement.h"
  23. #include "PopupPicker.h"
  24. #include "WebPage_p.h"
  25. #include <BlackBerryPlatformString.h>
  26. #include <LocaleHandler.h>
  27. #include <LocalizeResource.h>
  28. #include <unicode/dtfmtsym.h>
  29. #include <wtf/text/StringBuilder.h>
  30. using namespace WebCore;
  31. using namespace icu;
  32. namespace BlackBerry {
  33. namespace WebKit {
  34. DEFINE_STATIC_LOCAL(BlackBerry::Platform::LocalizeResource, s_resource, ());
  35. DatePickerClient::DatePickerClient(BlackBerry::Platform::BlackBerryInputType type, const BlackBerry::Platform::String& value, const BlackBerry::Platform::String& min, const BlackBerry::Platform::String& max, double step, WebPagePrivate* webPagePrivate, WebCore::HTMLInputElement* element)
  36. : PagePopupClient(webPagePrivate)
  37. , m_type(type)
  38. , m_element(element)
  39. {
  40. generateHTML(type, value, min, max, step);
  41. }
  42. DatePickerClient::~DatePickerClient()
  43. {
  44. }
  45. void DatePickerClient::generateHTML(BlackBerry::Platform::BlackBerryInputType type, const BlackBerry::Platform::String& value, const BlackBerry::Platform::String& min, const BlackBerry::Platform::String& max, double step)
  46. {
  47. StringBuilder source;
  48. String title = "";
  49. source.appendLiteral("<style>\n");
  50. // Include CSS file.
  51. source.append(popupControlBlackBerryCss, sizeof(popupControlBlackBerryCss));
  52. source.appendLiteral("</style>\n<style>");
  53. source.append(timeControlBlackBerryCss, sizeof(timeControlBlackBerryCss));
  54. source.appendLiteral("</style></head><body>\n");
  55. source.appendLiteral("<script>\n");
  56. source.appendLiteral("window.addEventListener('load', function showIt() {");
  57. source.appendLiteral("window.popupcontrol.show({");
  58. // Add DatePicker type
  59. source.appendLiteral("type:");
  60. switch (type) {
  61. case BlackBerry::Platform::InputTypeDate:
  62. source.appendLiteral("'Date', ");
  63. title = String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_DATE_TITLE));
  64. break;
  65. case BlackBerry::Platform::InputTypeTime:
  66. source.appendLiteral("'Time', ");
  67. title = String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_TIME_TITLE));
  68. break;
  69. case BlackBerry::Platform::InputTypeDateTime:
  70. source.appendLiteral("'DateTime', ");
  71. title = String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_DATE_TIME_TITLE));
  72. break;
  73. case BlackBerry::Platform::InputTypeDateTimeLocal:
  74. source.appendLiteral("'DateTimeLocal', ");
  75. title = String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_DATE_TIME_LOCAL_TITLE));
  76. break;
  77. case BlackBerry::Platform::InputTypeMonth:
  78. source.appendLiteral("'Month', ");
  79. title = String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_MONTH_TITLE));
  80. break;
  81. case BlackBerry::Platform::InputTypeWeek:
  82. default:
  83. break;
  84. }
  85. // Add datetime value
  86. source.appendLiteral("initialValue:");
  87. if (!value.empty())
  88. source.append("'" + String(value) + "', ");
  89. else
  90. source.appendLiteral("null, ");
  91. // Add lower and upper bounds
  92. source.appendLiteral("min:");
  93. if (!min.empty())
  94. source.append("'" + String(min) + "', ");
  95. else
  96. source.appendLiteral("null, ");
  97. source.appendLiteral("max:");
  98. if (!max.empty())
  99. source.append("'" + String(max) + "', ");
  100. else
  101. source.appendLiteral("null, ");
  102. // Add step size
  103. source.append("step:" + String::number(step) + ", ");
  104. // Add UI text
  105. source.appendLiteral("uiText: {");
  106. source.append("title:'" + title + "',");
  107. source.append("doneButtonLabel:'" + String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_DONE_BUTTON_LABEL)) + "',");
  108. source.append("cancelButtonLabel:'" + String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_CANCEL_BUTTON_LABEL)) + "',");
  109. source.append("monthLabels:" + DatePickerClient::generateDateLabels(UDAT_STANDALONE_MONTHS) + ",");
  110. source.append("shortMonthLabels:" + DatePickerClient::generateDateLabels(UDAT_SHORT_MONTHS) + ",");
  111. source.append("daysOfWeekLabels:" + DatePickerClient::generateDateLabels(UDAT_STANDALONE_WEEKDAYS) + ",");
  112. source.append("amPmLabels:" + DatePickerClient::generateDateLabels(UDAT_AM_PMS) + ",");
  113. source.appendLiteral("},");
  114. // Add directionality
  115. bool isRtl = BlackBerry::Platform::LocaleHandler::instance()->isRtlLocale();
  116. source.append("direction:'" + String(isRtl ? "rtl" : "ltr") + "',");
  117. source.appendLiteral("});\n");
  118. source.appendLiteral(" window.removeEventListener('load', showIt); }); \n");
  119. source.append(timeControlBlackBerryJs, sizeof(timeControlBlackBerryJs));
  120. source.appendLiteral("</script>\n</body> </html>\n");
  121. m_source = source.toString();
  122. }
  123. void DatePickerClient::setValueAndClosePopup(const String& value)
  124. {
  125. // Popup closed.
  126. if (!m_element)
  127. return;
  128. // We hide caret when we select date input field, restore it when we close date picker.
  129. m_element->document()->frame()->selection()->setCaretVisible(true);
  130. // Return -1 if user cancel the selection.
  131. if (value != "-1")
  132. m_element->setValue(value, DispatchChangeEvent);
  133. closePopup();
  134. }
  135. void DatePickerClient::didClosePopup()
  136. {
  137. PagePopupClient::didClosePopup();
  138. m_element = 0;
  139. }
  140. // UDAT_foo are for labels that are meant to be formatted as part of a date.
  141. // UDAT_STANDALONE_foo are for labels that are displayed separately from other date components.
  142. // For example, UDAT_SHORT_MONTHS in Catalan puts a preposition in front of the month but UDAT_STANDALONE_SHORT_MONTHS does not.
  143. const String DatePickerClient::generateDateLabels(UDateFormatSymbolType symbolType)
  144. {
  145. UErrorCode uerrStatus = U_ZERO_ERROR;
  146. DateFormatSymbols dateSymbols = DateFormatSymbols(uerrStatus); // constructor will never fail
  147. const UnicodeString* labels = 0;
  148. int32_t labelCount = 0;
  149. switch (symbolType) {
  150. // dateSymbols retain ownership of return values from getFoo calls
  151. case UDAT_STANDALONE_MONTHS:
  152. labelCount = 12;
  153. labels = dateSymbols.getMonths(labelCount, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
  154. break;
  155. case UDAT_STANDALONE_SHORT_MONTHS:
  156. labelCount = 12;
  157. labels = dateSymbols.getMonths(labelCount, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
  158. break;
  159. case UDAT_SHORT_MONTHS:
  160. labelCount = 12;
  161. labels = dateSymbols.getMonths(labelCount, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
  162. break;
  163. case UDAT_STANDALONE_WEEKDAYS:
  164. labelCount = 8;
  165. // getWeekdays returns an array where the zeroeth element is empty; the first label is placed in index 1
  166. labels = &(dateSymbols.getWeekdays(labelCount, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE))[1]; // skip zeroeth element
  167. --labelCount;
  168. break;
  169. case UDAT_AM_PMS:
  170. labelCount = 2;
  171. labels = dateSymbols.getAmPmStrings(labelCount);
  172. break;
  173. default:
  174. ASSERT(0);
  175. break;
  176. }
  177. StringBuilder printedLabels;
  178. printedLabels.appendLiteral("[");
  179. for (int32_t i = 0; i < labelCount; ++i) {
  180. String escapedLabel = String(labels[i].getBuffer(), labels[i].length()).replace('\\', "\\\\").replace('\'', "\\'"); // TODO PR 243547: refactor escaping of strings for DatePickerClient and SelectPopupClient
  181. printedLabels.append("'" + escapedLabel + "'");
  182. if (i < labelCount - 1)
  183. printedLabels.appendLiteral(",");
  184. }
  185. printedLabels.appendLiteral("]");
  186. return printedLabels.toString();
  187. }
  188. }
  189. }