SelectPopupClient.cpp [plain text]
#include "config.h"
#include "SelectPopupClient.h"
#include "HTMLOptionElement.h"
#include "HTMLSelectElement.h"
#include "PopupPicker.h"
#include "RenderObject.h"
#include "WebPage_p.h"
#include <BlackBerryPlatformString.h>
#include <LocaleHandler.h>
#include <LocalizeResource.h>
#include <wtf/text/StringBuilder.h>
using namespace WebCore;
namespace BlackBerry {
namespace WebKit {
DEFINE_STATIC_LOCAL(BlackBerry::Platform::LocalizeResource, s_resource, ());
SelectPopupClient::SelectPopupClient(bool multiple, int size, const ScopeArray<BlackBerry::Platform::String>& labels, bool* enableds, const int* itemType, bool* selecteds, WebPagePrivate* webPagePrivate, HTMLSelectElement* element)
: PagePopupClient(webPagePrivate)
, m_multiple(multiple)
, m_size(size)
, m_element(element)
, m_notifyChangeTimer(this, &SelectPopupClient::notifySelectionChange)
{
generateHTML(multiple, size, labels, enableds, itemType, selecteds);
}
SelectPopupClient::~SelectPopupClient()
{
}
void SelectPopupClient::generateHTML(bool, int size, const ScopeArray<BlackBerry::Platform::String>& labels, bool* enableds,
const int* itemType, bool* selecteds)
{
StringBuilder source;
source.appendLiteral("<style>\n");
source.append(popupControlBlackBerryCss, sizeof(popupControlBlackBerryCss));
source.appendLiteral("</style>\n<style>");
source.append(selectControlBlackBerryCss, sizeof(selectControlBlackBerryCss));
source.appendLiteral("</style></head><body>\n");
source.appendLiteral("<script>\n");
source.appendLiteral("window.addEventListener('load', function showIt() {");
source.appendLiteral("window.select.show({");
source.appendLiteral("isMultiSelect:");
if (m_multiple)
source.appendLiteral("true,");
else
source.appendLiteral("false,");
source.appendLiteral("labels: [");
for (int i = 0; i < size; i++) {
source.append("'" + String(labels[i]).replaceWithLiteral('\\', "\\\\").replaceWithLiteral('\'', "\\'") + "'");
if (i != size - 1)
source.appendLiteral(", ");
}
source.appendLiteral("], ");
source.append("enableds: [");
for (int i = 0; i < size; i++) {
if (enableds[i])
source.appendLiteral("true");
else
source.appendLiteral("false");
if (i != size - 1)
source.appendLiteral(", ");
}
source.appendLiteral("], ");
source.appendLiteral("itemTypes: [");
for (int i = 0; i < size; i++) {
source.appendNumber(itemType[i]);
if (i != size - 1)
source.appendLiteral(", ");
}
source.appendLiteral("], ");
source.appendLiteral("selecteds: [");
for (int i = 0; i < size; i++) {
if (selecteds[i])
source.appendLiteral("true");
else
source.appendLiteral("false");
if (i != size - 1)
source.appendLiteral(", ");
}
source.appendLiteral("], ");
source.appendLiteral("uiText: {");
source.append("title:'" + String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_SELECT_TITLE)) + "',");
source.append("doneButtonLabel:'" + String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_DONE_BUTTON_LABEL)) + "',");
source.append("cancelButtonLabel:'" + String::fromUTF8(s_resource.getString(BlackBerry::Platform::PICKER_CANCEL_BUTTON_LABEL)) + "',");
source.appendLiteral("},");
bool isRtl = BlackBerry::Platform::LocaleHandler::instance()->isRtlLocale();
source.append("direction:'" + String(isRtl ? "rtl" : "ltr") + "',");
source.appendLiteral("});\n");
source.appendLiteral(" window.removeEventListener('load', showIt); }); \n");
source.append(selectControlBlackBerryJs, sizeof(selectControlBlackBerryJs));
source.appendLiteral("</script>\n</body> </html>\n");
m_source = source.toString();
}
void SelectPopupClient::setValueAndClosePopup(const String& stringValue)
{
if (!m_element)
return;
static const char* cancelValue = "-1";
if (stringValue == cancelValue) {
closePopup();
return;
}
if (m_size > 0) {
bool selecteds[m_size];
for (unsigned i = 0; i < m_size; i++)
selecteds[i] = stringValue[i] - '0';
const Vector<HTMLElement*>& items = m_element->listItems();
if (items.size() != static_cast<unsigned>(m_size)) {
closePopup();
return;
}
HTMLOptionElement* option;
for (unsigned i = 0; i < m_size; i++) {
if (items[i]->hasTagName(HTMLNames::optionTag)) {
option = static_cast<HTMLOptionElement*>(items[i]);
option->setSelectedState(selecteds[i]);
}
}
}
if (m_element->renderer())
m_element->renderer()->repaint();
m_notifyChangeTimer.startOneShot(0);
}
void SelectPopupClient::didClosePopup()
{
PagePopupClient::didClosePopup();
m_element = 0;
}
void SelectPopupClient::notifySelectionChange(WebCore::Timer<SelectPopupClient>*)
{
if (m_element)
m_element->dispatchFormControlChangeEvent();
closePopup();
}
}
}