Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed WFormWidget placeholder not updating on locale change #198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 9 additions & 100 deletions src/Wt/WFormWidget.C
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#include "DomElement.h"
#include "WebUtils.h"

#ifndef WT_DEBUG_JS
#include "js/WFormWidget.min.js"
#endif

namespace Wt {

const char *WFormWidget::CHANGE_SIGNAL = "M_change";
Expand Down Expand Up @@ -81,69 +77,14 @@ bool WFormWidget::isReadOnly() const

void WFormWidget::setPlaceholderText(const WString& placeholderText)
{
emptyText_ = placeholderText;

WApplication* app = WApplication::instance();
const WEnvironment& env = app->environment();
if (!env.agentIsIElt(10) &&
(domElementType() == DomElementType::INPUT || domElementType() == DomElementType::TEXTAREA)) {
flags_.set(BIT_PLACEHOLDER_CHANGED);
repaint();
} else {
if (env.ajax()) {
if (!emptyText_.empty()) {
if (!flags_.test(BIT_JS_OBJECT))
defineJavaScript();
else
updateEmptyText();

if (!removeEmptyText_) {
removeEmptyText_.reset(new JSlot(this));

focussed().connect(*removeEmptyText_);
blurred().connect(*removeEmptyText_);
keyWentDown().connect(*removeEmptyText_);

std::string jsFunction =
"function(obj, event) {"
"""" + jsRef() + ".wtObj.applyEmptyText();"
"}";
removeEmptyText_->setJavaScript(jsFunction);
}
} else {
removeEmptyText_.reset();
}
} else {
setToolTip(placeholderText);
}
}
}

void WFormWidget::defineJavaScript(bool force)
{
if (force || !flags_.test(BIT_JS_OBJECT)) {
flags_.set(BIT_JS_OBJECT);

if (!isRendered())
return;

WApplication *app = WApplication::instance();

LOAD_JAVASCRIPT(app, "js/WFormWidget.js", "WFormWidget", wtjs1);

setJavaScriptMember(" WFormWidget", "new " WT_CLASS ".WFormWidget("
+ app->javaScriptClass() + ","
+ jsRef() + ","
+ emptyText_.jsStringLiteral() + ");");
}
placeholderText_ = placeholderText;
flags_.set(BIT_PLACEHOLDER_CHANGED);
repaint();
}

void WFormWidget::render(WFlags<RenderFlag> flags)
{
if (flags.test(RenderFlag::Full)) {
if (flags_.test(BIT_JS_OBJECT))
defineJavaScript(true);

if (validator()) {
WValidator::Result result = validator()->validate(valueText());
WApplication::instance()->theme()
Expand All @@ -155,39 +96,14 @@ void WFormWidget::render(WFlags<RenderFlag> flags)
WInteractWidget::render(flags);
}

void WFormWidget::updateEmptyText()
{
WApplication *app = WApplication::instance();
const WEnvironment &env = app->environment();
if (env.agentIsIElt(10) && isRendered())
doJavaScript(jsRef() + ".wtObj"
".setEmptyText(" + emptyText_.jsStringLiteral() + ");");
}

void WFormWidget::applyEmptyText()
{
WApplication *app = WApplication::instance();
const WEnvironment &env = app->environment();
if (env.agentIsIElt(10) && isRendered() && !emptyText_.empty())
doJavaScript(jsRef() + ".wtObj.applyEmptyText();");
}

void WFormWidget::refresh()
{
if (emptyText_.refresh())
updateEmptyText();

WInteractWidget::refresh();
}

void WFormWidget::enableAjax()
{
if (!emptyText_.empty() && toolTip() == emptyText_) {
setToolTip("");
setPlaceholderText(emptyText_);
if (placeholderText_.refresh()) {
flags_.set(BIT_PLACEHOLDER_CHANGED);
repaint();
}

WInteractWidget::enableAjax();
WInteractWidget::refresh();
}

void WFormWidget::validatorChanged()
Expand Down Expand Up @@ -249,13 +165,6 @@ void WFormWidget::updateDom(DomElement& element, bool all)
if (!all || !isEnabled())
element.setProperty(Wt::Property::Disabled,
isEnabled() ? "false" : "true");

if (!all && isEnabled() && env.agentIsIE()) {
/*
* FIXME: implement a workaround for IE, reenabling a checkbox makes
* the input box loose interactivity.
*/
}
flags_.reset(BIT_ENABLED_CHANGED);
}

Expand All @@ -267,8 +176,8 @@ void WFormWidget::updateDom(DomElement& element, bool all)
}

if (flags_.test(BIT_PLACEHOLDER_CHANGED) || all) {
if (!all || !emptyText_.empty())
element.setProperty(Wt::Property::Placeholder, emptyText_.toUTF8());
if (!all || !placeholderText_.empty())
element.setProperty(Wt::Property::Placeholder, placeholderText_.toUTF8());
flags_.reset(BIT_PLACEHOLDER_CHANGED);
}

Expand Down
38 changes: 15 additions & 23 deletions src/Wt/WFormWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,6 @@ class WT_API WFormWidget : public WInteractWidget
*/
bool isReadOnly() const;

/*! \brief Sets the placeholder text.
*
* This sets the text that is shown when the field is empty.
*/
virtual void setPlaceholderText(const WString& placeholder);

/*! \brief Returns the placeholder text.
*
* \sa setPlaceholderText()
*/
const WString& placeholderText() const { return emptyText_; }

/*! \brief %Signal emitted when the value was changed.
*
* For a keyboard input, the signal is only emitted when the focus is lost
Expand All @@ -154,15 +142,23 @@ class WT_API WFormWidget : public WInteractWidget
protected:
WLabel *label_;
std::shared_ptr<WValidator> validator_;
std::unique_ptr<JSlot> validateJs_, filterInput_, removeEmptyText_;
WString emptyText_;
std::unique_ptr<JSlot> validateJs_, filterInput_;
WString placeholderText_;

// also used in WAbstractToggleButton
static const char *CHANGE_SIGNAL;

void applyEmptyText();
/*! \brief Sets the placeholder text.
*
* This sets the text that is shown when the field is empty.
*/
virtual void setPlaceholderText(const WString& placeholder);

virtual void enableAjax() override;
/*! \brief Returns the placeholder text.
*
* \sa setPlaceholderText()
*/
const WString& placeholderText() const { return placeholderText_; }

/*! \internal
* \brief Called whenever the validator is changed
Expand All @@ -181,19 +177,15 @@ class WT_API WFormWidget : public WInteractWidget
static const int BIT_ENABLED_CHANGED = 0;
static const int BIT_READONLY = 1;
static const int BIT_READONLY_CHANGED = 2;
static const int BIT_JS_OBJECT = 3;
static const int BIT_VALIDATION_CHANGED = 4;
static const int BIT_PLACEHOLDER_CHANGED = 5;
static const int BIT_VALIDATION_CHANGED = 3;
static const int BIT_PLACEHOLDER_CHANGED = 4;

std::bitset<6> flags_;
std::bitset<5> flags_;
Signal<WValidator::Result> validated_;
WString validationToolTip_;

void setLabel(WLabel *label);

void defineJavaScript(bool force = false);
void updateEmptyText();

protected:
virtual void updateDom(DomElement& element, bool all) override;
virtual void propagateRenderOk(bool deep) override;
Expand Down
4 changes: 1 addition & 3 deletions src/Wt/WInPlaceEdit.C
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,14 @@ void WInPlaceEdit::setText(const WString& text)

void WInPlaceEdit::setPlaceholderText(const WString& text)
{
placeholderText_ = text;

edit_->setPlaceholderText(text);
if (empty_)
text_->setText(text);
}

const WString& WInPlaceEdit::placeholderText() const
{
return placeholderText_;
return edit_->placeholderText();
}

void WInPlaceEdit::save()
Expand Down
1 change: 0 additions & 1 deletion src/Wt/WInPlaceEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class WT_API WInPlaceEdit : public WCompositeWidget
WText *text_;
WLineEdit *edit_;
WPushButton *save_, *cancel_;
WString placeholderText_;
Wt::Signals::connection c2_;
bool empty_;
};
Expand Down
2 changes: 0 additions & 2 deletions src/Wt/WLineEdit.C
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ void WLineEdit::setText(const WT_USTRING& text)
repaint();

validate();

applyEmptyText();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Wt/WLineEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class WT_API WLineEdit : public WFormWidget
*/
const WT_USTRING& text() const { return content_; }

/*! \brief Sets the placeholder text.
*
* This sets the text that is shown when the field is empty.
*/
using WFormWidget::setPlaceholderText;

/*! \brief Returns the placeholder text.
*
* \sa setPlaceholderText()
*/
using WFormWidget::placeholderText;

/*! \brief Returns the displayed text.
*
* If echoMode() is set to Normal, and no input mask is defined, this returns the same as
Expand Down
2 changes: 0 additions & 2 deletions src/Wt/WTextArea.C
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ void WTextArea::setText(const WT_USTRING& text)
repaint();

validate();

applyEmptyText();
}

void WTextArea::setColumns(int columns)
Expand Down
12 changes: 12 additions & 0 deletions src/Wt/WTextArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ class WT_API WTextArea : public WFormWidget
*/
virtual void setText(const WT_USTRING& text);

/*! \brief Sets the placeholder text.
*
* This sets the text that is shown when the field is empty.
*/
using WFormWidget::setPlaceholderText;

/*! \brief Returns the placeholder text.
*
* \sa setPlaceholderText()
*/
using WFormWidget::placeholderText;

/*! \brief Returns the current selection start.
*
* Returns -1 if there is no selected text.
Expand Down
50 changes: 0 additions & 50 deletions src/js/WFormWidget.js

This file was deleted.

1 change: 0 additions & 1 deletion src/js/WFormWidget.min.js

This file was deleted.