Wt examples  4.0.0
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members

A JavaScript based popup window, encapsulating the Javascript functions alert(), confirm(), and prompt(). More...

#include <Popup.h>

Inheritance diagram for Popup:
Inheritance graph
[legend]

Public Types

enum  Type { Confirm, Alert, Prompt }
 Popup type. More...
 
- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 

Public Member Functions

 Popup (Type t, const WString &message, const std::string defaultValue)
 Popup constructor. More...
 
void setMessage (const WString &message)
 Change the message. More...
 
void setDefaultValue (const std::string defaultValue)
 Change the default value for a prompt dialog. More...
 
const WStringmessage () const
 Get the current message. More...
 
const std::string & defaultValue () const
 Get the default value for a prompt dialog. More...
 
JSignal< std::string > & okPressed ()
 Signal emitted when ok pressed. More...
 
JSignalcancelPressed ()
 Signal emitted when cancel is pressed. More...
 
- 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)
 
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 ()
 
virtual ~observable ()
 
auto bindSafe (void(C::*method)(Args...))
 
auto bindSafe (void(C::*method)(Args...) const) const
 
auto bindSafe (const Function &function)
 

Static Public Member Functions

static std::unique_ptr< PopupcreateConfirm (const WString &message)
 Create a confirm dialog. More...
 
static std::unique_ptr< PopupcreatePrompt (const WString &message, const std::string defaultValue)
 Create a prompt dialog with the given default value. More...
 
static std::unique_ptr< PopupcreateAlert (const WString &message)
 Create an alert dialog. More...
 

Public Attributes

JSlot show
 Show the dialog. More...
 

Private Member Functions

void setJavaScript ()
 Update the javascript code. More...
 

Private Attributes

JSignal< std::string > okPressed_
 
JSignal cancelPressed_
 
Type t_
 
WString message_
 
std::string defaultValue_
 

Additional Inherited Members

- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 

Detailed Description

A JavaScript based popup window, encapsulating the Javascript functions alert(), confirm(), and prompt().

Use one of the create static methods to create a popup. This will not display the popup, until either the show slot is triggered from an event handler, or is executed using it's exec() method.

When the user closes the popup, either the okPressed or cancelPressed signal is emitted. For a prompt dialog, the value is passed as a parameter to the okPressed signal.

Definition at line 32 of file Popup.h.

Member Enumeration Documentation

Popup type.

Enumerator
Confirm 
Alert 
Prompt 

Definition at line 37 of file Popup.h.

Constructor & Destructor Documentation

Popup::Popup ( Type  t,
const WString message,
const std::string  defaultValue 
)

Popup constructor.

Definition at line 14 of file Popup.C.

15  : WObject(),
16  okPressed_(this, "ok"),
17  cancelPressed_(this, "cancel"),
18  t_(t),
19  message_(message),
21 {
22  setJavaScript();
23 }
WString message_
Definition: Popup.h:92
JSignal cancelPressed_
Definition: Popup.h:89
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25
std::string defaultValue_
Definition: Popup.h:93
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
Type t_
Definition: Popup.h:91
JSignal< std::string > okPressed_
Definition: Popup.h:88

Member Function Documentation

JSignal& Popup::cancelPressed ( )
inline

Signal emitted when cancel is pressed.

Definition at line 85 of file Popup.h.

85 { return cancelPressed_; }
JSignal cancelPressed_
Definition: Popup.h:89
std::unique_ptr< Popup > Popup::createAlert ( const WString message)
static

Create an alert dialog.

Definition at line 78 of file Popup.C.

79 {
80  return cpp14::make_unique<Popup>(Type::Alert, message, std::string());
81 }
const WString & message() const
Get the current message.
Definition: Popup.h:66
std::unique_ptr< Popup > Popup::createConfirm ( const WString message)
static

Create a confirm dialog.

Definition at line 73 of file Popup.C.

74 {
75  return cpp14::make_unique<Popup>(Type::Confirm, message, std::string());
76 }
const WString & message() const
Get the current message.
Definition: Popup.h:66
std::unique_ptr< Popup > Popup::createPrompt ( const WString message,
const std::string  defaultValue 
)
static

Create a prompt dialog with the given default value.

Definition at line 83 of file Popup.C.

85 {
86  return cpp14::make_unique<Popup>(Type::Prompt, message, defaultValue);
87 }
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
const WString & message() const
Get the current message.
Definition: Popup.h:66
const std::string& Popup::defaultValue ( ) const
inline

Get the default value for a prompt dialog.

Definition at line 70 of file Popup.h.

70 { return defaultValue_; }
std::string defaultValue_
Definition: Popup.h:93
const WString& Popup::message ( ) const
inline

Get the current message.

Definition at line 66 of file Popup.h.

66 { return message_; }
WString message_
Definition: Popup.h:92
JSignal<std::string>& Popup::okPressed ( )
inline

Signal emitted when ok pressed.

Definition at line 81 of file Popup.h.

81 { return okPressed_; }
JSignal< std::string > okPressed_
Definition: Popup.h:88
void Popup::setDefaultValue ( const std::string  defaultValue)

Change the default value for a prompt dialog.

Definition at line 67 of file Popup.C.

68 {
70  setJavaScript();
71 }
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25
std::string defaultValue_
Definition: Popup.h:93
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
void Popup::setJavaScript ( )
private

Update the javascript code.

Definition at line 25 of file Popup.C.

26 {
27  /*
28  * Sets the JavaScript code.
29  *
30  * Notice how Wt.emit() is used to emit the okPressed or cancelPressed
31  * signal, and how arguments may be passed to it, matching the number and
32  * type of arguments in the JSignal definition.
33  */
34  switch (t_) {
35  case Confirm:
37  ("function(){ if (confirm('" + message_.narrow() + "')) {"
38  + okPressed_.createCall({"''"}) +
39  "} else {"
41  "}}");
42  break;
43  case Alert:
45  ("function(){ alert('" + message_.narrow() + "');"
46  + okPressed_.createCall({"''"}) +
47  "}");
48  break;
49  case Prompt:
51  ("function(){var n = prompt('" + message_.narrow() + "', '"
52  + defaultValue_ + "');"
53  "if (n != null) {"
54  + okPressed_.createCall({"n"}) +
55  "} else {"
57  "}}");
58  }
59 }
JSlot show
Show the dialog.
Definition: Popup.h:77
std::string narrow(const std::locale &loc=std::locale()) const
WString message_
Definition: Popup.h:92
JSignal cancelPressed_
Definition: Popup.h:89
std::string defaultValue_
Definition: Popup.h:93
const std::string createCall(std::initializer_list< std::string > args) const
Type t_
Definition: Popup.h:91
JSignal< std::string > okPressed_
Definition: Popup.h:88
void setJavaScript(const std::string &javaScript, int nbArgs=0)
void Popup::setMessage ( const WString message)

Change the message.

Definition at line 61 of file Popup.C.

62 {
63  message_ = message;
64  setJavaScript();
65 }
WString message_
Definition: Popup.h:92
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25
const WString & message() const
Get the current message.
Definition: Popup.h:66

Member Data Documentation

JSignal Popup::cancelPressed_
private

Definition at line 89 of file Popup.h.

std::string Popup::defaultValue_
private

Definition at line 93 of file Popup.h.

WString Popup::message_
private

Definition at line 92 of file Popup.h.

JSignal<std::string> Popup::okPressed_
private

Definition at line 88 of file Popup.h.

JSlot Popup::show

Show the dialog.

Use show.exec() to show the dialog, or connect the slot to an EventSignal to directly show the dialog without a server round trip.

Definition at line 77 of file Popup.h.

Type Popup::t_
private

Definition at line 91 of file Popup.h.


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

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11