Wt examples  3.7.1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
JavascriptExample Class Reference

An example showing how to interact custom JavaScript with Wt stuff. More...

#include <JavascriptExample.h>

Inheritance diagram for JavascriptExample:
Inheritance graph
[legend]

Public Member Functions

 JavascriptExample (const WEnvironment &env)
 Create the example application. More...
 
- Public Member Functions inherited from Wt::WApplication
 WApplication (const WEnvironment &environment)
 
 ~WApplication ()
 
const WEnvironmentenvironment () const
 
WContainerWidgetroot () const
 
WWidgetfindWidget (const std::string &name)
 
void setTitle (const WString &title)
 
const WStringtitle () const
 
const WStringcloseMessage () const
 
std::shared_ptr< WLocalizedStringslocalizedStrings ()
 
void setLocalizedStrings (const std::shared_ptr< WLocalizedStrings > &stringResolver)
 
WMessageResourceBundlemessageResourceBundle ()
 
void setLocale (const WLocale &locale)
 
const WLocalelocale () const
 
virtual void refresh ()
 
void bindWidget (std::unique_ptr< WWidget > widget, const std::string &domId)
 
std::string docRoot () const
 
void setConnectionMonitor (const std::string &jsObject)
 
std::string sessionId () const
 
void changeSessionId ()
 
void processEvents ()
 
virtual void waitForEvent ()
 
virtual void initialize ()
 
virtual void finalize ()
 
void setTwoPhaseRenderingThreshold (int size)
 
void setCookie (const std::string &name, const std::string &value, int maxAge, const std::string &domain="", const std::string &path="", bool secure=false)
 
void removeCookie (const std::string &name, const std::string &domain="", const std::string &path="")
 
void addMetaLink (const std::string &href, const std::string &rel, const std::string &media, const std::string &hreflang, const std::string &type, const std::string &sizes, bool disabled)
 
void removeMetaLink (const std::string &href)
 
void addMetaHeader (const std::string &name, const WString &content, const std::string &lang="")
 
void addMetaHeader (MetaHeaderType type, const std::string &name, const WString &content, const std::string &lang="")
 
WString metaHeader (MetaHeaderType type, const std::string &name) const
 
void removeMetaHeader (MetaHeaderType type, const std::string &name="")
 
WLogEntry log (const std::string &type) const
 
void setLoadingIndicator (std::unique_ptr< WLoadingIndicator > indicator)
 
WLoadingIndicatorloadingIndicator () const
 
void quit ()
 
void quit (const WString &restartMessage)
 
bool hasQuit () const
 
::int64_t maximumRequestSize () const
 
Signal< ::int64_t > & requestTooLarge ()
 
void setConfirmCloseMessage (const WString &message)
 
void deferRendering ()
 
void resumeRendering ()
 
std::string encodeUntrustedUrl (const std::string &url) const
 
void pushExposedConstraint (WWidget *w)
 
WCssStyleSheetstyleSheet ()
 
void useStyleSheet (const WLink &link, const std::string &media="all")
 
void useStyleSheet (const WLink &link, const std::string &condition, const std::string &media)
 
void useStyleSheet (const WLinkedCssStyleSheet &styleSheet, const std::string &condition="")
 
void removeStyleSheet (const WLink &link)
 
void setTheme (const std::shared_ptr< WTheme > &theme)
 
std::shared_ptr< WThemetheme () const
 
void setCssTheme (const std::string &name)
 
void setLayoutDirection (LayoutDirection direction)
 
LayoutDirection layoutDirection () const
 
void setBodyClass (const std::string &styleClass)
 
std::string bodyClass () const
 
void setHtmlClass (const std::string &styleClass)
 
std::string htmlClass () const
 
std::string url (const std::string &internalPath=std::string()) const
 
virtual std::string makeAbsoluteUrl (const std::string &url) const
 
std::string resolveRelativeUrl (const std::string &url) const
 
std::string bookmarkUrl () const
 
std::string bookmarkUrl (const std::string &internalPath) const
 
void setInternalPath (const std::string &path, bool emitChange=false)
 
void setInternalPathDefaultValid (bool valid)
 
bool internalPathDefaultValid () const
 
void setInternalPathValid (bool valid)
 
bool internalPathValid () const
 
std::string internalPath () const
 
std::string internalPathNextPart (const std::string &path) const
 
bool internalPathMatches (const std::string &path) const
 
Signal< std::string > & internalPathChanged ()
 
Signal< std::string > & internalPathInvalid ()
 
void redirect (const std::string &url)
 
void enableUpdates (bool enabled=true)
 
bool updatesEnabled () const
 
void triggerUpdate ()
 
void attachThread (bool attach=true)
 
void doJavaScript (const std::string &javascript, bool afterLoaded=true)
 
void addAutoJavaScript (const std::string &javascript)
 
void declareJavaScriptFunction (const std::string &name, const std::string &function)
 
bool require (const std::string &url, const std::string &symbol=std::string())
 
bool requireJQuery (const std::string &url)
 
bool customJQuery () const
 
void setJavaScriptClass (const std::string &className)
 
std::string javaScriptClass ()
 
EventSignal< WKeyEvent > & globalKeyWentDown ()
 
EventSignal< WKeyEvent > & globalKeyPressed ()
 
EventSignal< WKeyEvent > & globalKeyWentUp ()
 
EventSignalglobalEnterPressed ()
 
EventSignalglobalEscapePressed ()
 
WCssStyleSheetstyleSheet ()
 
void useStyleSheet (const WLink &link, const std::string &media="all")
 
void useStyleSheet (const WLink &link, const std::string &condition, const std::string &media)
 
void useStyleSheet (const WLinkedCssStyleSheet &styleSheet, const std::string &condition="")
 
void removeStyleSheet (const WLink &link)
 
void setTheme (const std::shared_ptr< WTheme > &theme)
 
std::shared_ptr< WThemetheme () const
 
void setCssTheme (const std::string &name)
 
void setLayoutDirection (LayoutDirection direction)
 
LayoutDirection layoutDirection () const
 
void setBodyClass (const std::string &styleClass)
 
std::string bodyClass () const
 
void setHtmlClass (const std::string &styleClass)
 
std::string htmlClass () const
 
std::string url (const std::string &internalPath=std::string()) const
 
virtual std::string makeAbsoluteUrl (const std::string &url) const
 
std::string resolveRelativeUrl (const std::string &url) const
 
std::string bookmarkUrl () const
 
std::string bookmarkUrl (const std::string &internalPath) const
 
void setInternalPath (const std::string &path, bool emitChange=false)
 
void setInternalPathDefaultValid (bool valid)
 
bool internalPathDefaultValid () const
 
void setInternalPathValid (bool valid)
 
bool internalPathValid () const
 
std::string internalPath () const
 
std::string internalPathNextPart (const std::string &path) const
 
bool internalPathMatches (const std::string &path) const
 
Signal< std::string > & internalPathChanged ()
 
Signal< std::string > & internalPathInvalid ()
 
void redirect (const std::string &url)
 
void enableUpdates (bool enabled=true)
 
bool updatesEnabled () const
 
void triggerUpdate ()
 
void attachThread (bool attach=true)
 
void doJavaScript (const std::string &javascript, bool afterLoaded=true)
 
void addAutoJavaScript (const std::string &javascript)
 
void declareJavaScriptFunction (const std::string &name, const std::string &function)
 
bool require (const std::string &url, const std::string &symbol=std::string())
 
bool requireJQuery (const std::string &url)
 
bool customJQuery () const
 
void setJavaScriptClass (const std::string &className)
 
std::string javaScriptClass ()
 
EventSignal< WKeyEvent > & globalKeyWentDown ()
 
EventSignal< WKeyEvent > & globalKeyPressed ()
 
EventSignal< WKeyEvent > & globalKeyWentUp ()
 
EventSignalglobalEnterPressed ()
 
EventSignalglobalEscapePressed ()
 
- Public Member Functions inherited from Wt::WObject
void addChild (std::unique_ptr< WObject > child)
 
Child * addChild (std::unique_ptr< Child > child)
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 
std::unique_ptr< Child > removeChild (Child *child)
 
virtual const std::string id () const
 
virtual void setObjectName (const std::string &name)
 
virtual std::string objectName () const
 
void resetLearnedSlots ()
 
void resetLearnedSlot (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 
void isNotStateless ()
 
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 
virtual ~observable ()
 
auto bindSafe (void(C::*method)(Args...)) noexcept
 
auto bindSafe (void(C::*method)(Args...) const) const noexcept
 
auto bindSafe (const Function &function) noexcept
 

Private Member Functions

void confirmed ()
 The user has confirmed the payment. More...
 
void setAmount (std::string amount)
 Set the amount to be payed. More...
 

Private Attributes

PopuppromptAmount_
 Popup for changing the amount. More...
 
PopupconfirmPay_
 Popup for paying. More...
 
WTextcurrentAmount_
 WText for showing the current amount. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WApplication
typedef Wt::ApplicationCreator ApplicationCreator
 
typedef std::function< std::unique_ptr< WApplication >const WEnvironment &)> ApplicationCreator
 
- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 
- Static Public Member Functions inherited from Wt::WApplication
static WApplicationinstance ()
 
static std::string resourcesUrl ()
 
static std::string relativeResourcesUrl ()
 
static std::string appRoot ()
 
static bool readConfigurationProperty (const std::string &name, std::string &value)
 
- Protected Member Functions inherited from Wt::WApplication
virtual void notify (const WEvent &e)
 
virtual bool isExposed (WWidget *w) const
 
virtual void enableAjax ()
 
virtual void unload ()
 
virtual void idleTimeout ()
 
virtual void handleJavaScriptError (const std::string &errorText)
 
- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 

Detailed Description

An example showing how to interact custom JavaScript with Wt stuff.

Definition at line 24 of file JavascriptExample.h.

Constructor & Destructor Documentation

◆ JavascriptExample()

JavascriptExample::JavascriptExample ( const WEnvironment env)

Create the example application.

Definition at line 19 of file JavascriptExample.C.

20  : WApplication(env)
21 {
22  setTitle("Javascript example");
23 
24  // Create a popup for prompting the amount of money, and connect the
25  // okPressed button to the slot for setting the amount of money.
26  //
27  // Note that the input provided by the user in the prompt box is passed as
28  // an argument to the slot.
29  promptAmount_ = Popup::createPrompt("How much do you want to pay?", "",
30  this);
32 
33  // Create a popup for confirming the payment.
34  //
35  // Since a confirm popup does not allow input, we ignore the
36  // argument carrying the input (which will be empty anyway).
39 
40  new WText("<h2>Wt Javascript example</h2>"
41  "<p>Wt makes abstraction of Javascript, and therefore allows you"
42  " to develop web applications without any knowledge of Javascript,"
43  " and which are not dependent on Javascript."
44  " However, Wt does allow you to add custom Javascript code:</p>"
45  " <ul>"
46  " <li>To call custom JavaScript code from an event handler, "
47  "connect the Wt::EventSignal to a Wt::JSlot.</li>"
48  " <li>To call C++ code from custom JavaScript, use "
49  "Wt.emit() to emit a Wt::JSignal.</li>"
50  " <li>To call custom JavaScript code from C++, use "
51  "WApplication::doJavascript() or Wt::JSlot::exec().</li>"
52  " </ul>"
53  "<p>This simple application shows how to interact between C++ and"
54  " JavaScript using the JSlot and JSignal classes.</p>", root());
55 
57  = new WText("Current amount: $" + promptAmount_->defaultValue(), root());
58 
59  WPushButton *amountButton = new WPushButton("Change ...", root());
60  amountButton->setMargin(10, Left | Right);
61 
62  new WBreak(root());
63 
64  WPushButton *confirmButton = new WPushButton("Pay now.", root());
65  confirmButton->setMargin(10, Top | Bottom);
66 
67  // Connect the event handlers to a JSlot: this will execute the JavaScript
68  // immediately, without a server round trip.
69  amountButton->clicked().connect(promptAmount_->show);
70  confirmButton->clicked().connect(confirmPay_->show);
71 
72  // Set the initial amount
73  setAmount("1000");
74 }
Popup * promptAmount_
Popup for changing the amount.
static Popup * createConfirm(const WString &message, WObject *parent=0)
Create a confirm dialog.
Definition: Popup.C:72
void confirmed()
The user has confirmed the payment.
JSlot show
Show the dialog.
Definition: Popup.h:70
WText * currentAmount_
WText for showing the current amount.
EventSignal< WMouseEvent > & clicked()
Wt::Signals::connection connect(F function)
void setTitle(const WString &title)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
void setAmount(std::string amount)
Set the amount to be payed.
WContainerWidget * root() const
static Popup * createPrompt(const WString &message, const std::string defaultValue, WObject *parent=0)
Create a prompt dialog with the given default value.
Definition: Popup.C:82
JSignal< std::string > & okPressed()
Signal emitted when ok pressed.
Definition: Popup.h:74
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:63
Popup * confirmPay_
Popup for paying.
WApplication(const WEnvironment &environment)

Member Function Documentation

◆ confirmed()

void JavascriptExample::confirmed ( )
private

The user has confirmed the payment.

Definition at line 88 of file JavascriptExample.C.

89 {
90  new WText("<br/>Just payed $" + promptAmount_->defaultValue() + ".", root());
91 }
Popup * promptAmount_
Popup for changing the amount.
WContainerWidget * root() const
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:63

◆ setAmount()

void JavascriptExample::setAmount ( std::string  amount)
private

Set the amount to be payed.

Definition at line 76 of file JavascriptExample.C.

77 {
78  // Change the confirmation message to include the amount.
79  confirmPay_->setMessage("Are you sure you want to pay $" + amount + " ?");
80 
81  // Change the default value for the prompt.
83 
84  // Change the text that shows the current amount.
85  currentAmount_->setText("Current amount: $" + promptAmount_->defaultValue());
86 }
Popup * promptAmount_
Popup for changing the amount.
WText * currentAmount_
WText for showing the current amount.
bool setText(const WString &text)
void setDefaultValue(const std::string defaultValue)
Change the default value for a prompt dialog.
Definition: Popup.C:66
void setMessage(const WString &message)
Change the message.
Definition: Popup.C:60
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:63
Popup * confirmPay_
Popup for paying.

Member Data Documentation

◆ confirmPay_

Popup* JavascriptExample::confirmPay_
private

Popup for paying.

Definition at line 46 of file JavascriptExample.h.

◆ currentAmount_

WText* JavascriptExample::currentAmount_
private

WText for showing the current amount.

Definition at line 50 of file JavascriptExample.h.

◆ promptAmount_

Popup* JavascriptExample::promptAmount_
private

Popup for changing the amount.

Definition at line 42 of file JavascriptExample.h.


The documentation for this class was generated from the following files:

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13