diff --git a/src/Wt/WFormWidget.C b/src/Wt/WFormWidget.C index 54fd1e583..16a3ed58f 100644 --- a/src/Wt/WFormWidget.C +++ b/src/Wt/WFormWidget.C @@ -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"; @@ -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 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() @@ -155,39 +96,14 @@ void WFormWidget::render(WFlags 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() @@ -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); } @@ -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); } diff --git a/src/Wt/WFormWidget.h b/src/Wt/WFormWidget.h index 7f7b03ecb..ddf6b8f09 100644 --- a/src/Wt/WFormWidget.h +++ b/src/Wt/WFormWidget.h @@ -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 @@ -154,15 +142,23 @@ class WT_API WFormWidget : public WInteractWidget protected: WLabel *label_; std::shared_ptr validator_; - std::unique_ptr validateJs_, filterInput_, removeEmptyText_; - WString emptyText_; + std::unique_ptr 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 @@ -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 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; diff --git a/src/Wt/WInPlaceEdit.C b/src/Wt/WInPlaceEdit.C index d1fabf1db..6e95b7cce 100644 --- a/src/Wt/WInPlaceEdit.C +++ b/src/Wt/WInPlaceEdit.C @@ -103,8 +103,6 @@ void WInPlaceEdit::setText(const WString& text) void WInPlaceEdit::setPlaceholderText(const WString& text) { - placeholderText_ = text; - edit_->setPlaceholderText(text); if (empty_) text_->setText(text); @@ -112,7 +110,7 @@ void WInPlaceEdit::setPlaceholderText(const WString& text) const WString& WInPlaceEdit::placeholderText() const { - return placeholderText_; + return edit_->placeholderText(); } void WInPlaceEdit::save() diff --git a/src/Wt/WInPlaceEdit.h b/src/Wt/WInPlaceEdit.h index 7553a0476..8f2229351 100644 --- a/src/Wt/WInPlaceEdit.h +++ b/src/Wt/WInPlaceEdit.h @@ -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_; }; diff --git a/src/Wt/WLineEdit.C b/src/Wt/WLineEdit.C index ed71c1d08..03fb2c0e7 100644 --- a/src/Wt/WLineEdit.C +++ b/src/Wt/WLineEdit.C @@ -70,8 +70,6 @@ void WLineEdit::setText(const WT_USTRING& text) repaint(); validate(); - - applyEmptyText(); } } diff --git a/src/Wt/WLineEdit.h b/src/Wt/WLineEdit.h index 52add8476..adda94698 100644 --- a/src/Wt/WLineEdit.h +++ b/src/Wt/WLineEdit.h @@ -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 diff --git a/src/Wt/WTextArea.C b/src/Wt/WTextArea.C index f31ac1e01..43427ec79 100644 --- a/src/Wt/WTextArea.C +++ b/src/Wt/WTextArea.C @@ -43,8 +43,6 @@ void WTextArea::setText(const WT_USTRING& text) repaint(); validate(); - - applyEmptyText(); } void WTextArea::setColumns(int columns) diff --git a/src/Wt/WTextArea.h b/src/Wt/WTextArea.h index 5c9646ccf..3ca1d78dc 100644 --- a/src/Wt/WTextArea.h +++ b/src/Wt/WTextArea.h @@ -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. diff --git a/src/js/WFormWidget.js b/src/js/WFormWidget.js deleted file mode 100644 index 33d909d1a..000000000 --- a/src/js/WFormWidget.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2010 Emweb bv, Herent, Belgium. - * - * See the LICENSE file for terms of use. - */ - -/* Note: this is at the same time valid JavaScript and C++. */ - -WT_DECLARE_WT_MEMBER(1, JavaScriptConstructor, "WFormWidget", function(APP, el, emptyText) { - el.wtObj = this; - - const WT = APP.WT, emptyTextStyle = "Wt-edit-emptyText"; - - this.applyEmptyText = function() { - if (WT.hasFocus(el)) { - if (el.classList.contains(emptyTextStyle)) { - if (!WT.isIE && el.oldtype) { - el.type = el.oldtype; - } - el.classList.remove(emptyTextStyle); - el.value = ""; - } - } else { - if (el.value === "") { - if (el.type === "password") { - if (!WT.isIE) { - el.oldtype = "password"; - el.type = "text"; - } else { - return; - } - } - el.classList.add(emptyTextStyle); - el.value = emptyText; - } else { - el.classList.remove(emptyTextStyle); - } - } - }; - - this.setEmptyText = function(newEmptyText) { - emptyText = newEmptyText; - - if (el.classList.contains(emptyTextStyle)) { - el.value = emptyText; - } - }; - - this.applyEmptyText(); -}); diff --git a/src/js/WFormWidget.min.js b/src/js/WFormWidget.min.js deleted file mode 100644 index 2c88a297d..000000000 --- a/src/js/WFormWidget.min.js +++ /dev/null @@ -1 +0,0 @@ -WT_DECLARE_WT_MEMBER(1,JavaScriptConstructor,"WFormWidget",(function(t,s,e){s.wtObj=this;const i=t.WT,a="Wt-edit-emptyText";this.applyEmptyText=function(){if(i.hasFocus(s)){if(s.classList.contains(a)){!i.isIE&&s.oldtype&&(s.type=s.oldtype);s.classList.remove(a);s.value=""}}else if(""===s.value){if("password"===s.type){if(i.isIE)return;s.oldtype="password";s.type="text"}s.classList.add(a);s.value=e}else s.classList.remove(a)};this.setEmptyText=function(t){e=t;s.classList.contains(a)&&(s.value=e)};this.applyEmptyText()})); \ No newline at end of file