Wt examples  4.10.4
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
Popup Class Reference

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

#include <Popup.h>

Inheritance diagram for Popup:
[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)
 
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
 

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

◆ Type

Popup type.

Enumerator
Confirm 
Alert 
Prompt 

Definition at line 37 of file Popup.h.

37 { Confirm, Alert, Prompt };
@ Alert
Definition: Popup.h:37
@ Prompt
Definition: Popup.h:37
@ Confirm
Definition: Popup.h:37

Constructor & Destructor Documentation

◆ Popup()

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),
21 {
22  setJavaScript();
23 }
const WString & message() const
Get the current message.
Definition: Popup.h:66
JSignal cancelPressed_
Definition: Popup.h:89
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
Type t_
Definition: Popup.h:91
WString message_
Definition: Popup.h:92
std::string defaultValue_
Definition: Popup.h:93
JSignal< std::string > okPressed_
Definition: Popup.h:88
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25

Member Function Documentation

◆ cancelPressed()

JSignal& Popup::cancelPressed ( )
inline

Signal emitted when cancel is pressed.

Definition at line 85 of file Popup.h.

85 { return cancelPressed_; }

◆ createAlert()

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 std::make_unique<Popup>(Type::Alert, message, std::string());
81 }

◆ createConfirm()

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 std::make_unique<Popup>(Type::Confirm, message, std::string());
76 }

◆ createPrompt()

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 std::make_unique<Popup>(Type::Prompt, message, defaultValue);
87 }

◆ defaultValue()

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_; }

◆ message()

const WString& Popup::message ( ) const
inline

Get the current message.

Definition at line 66 of file Popup.h.

66 { return message_; }

◆ okPressed()

JSignal<std::string>& Popup::okPressed ( )
inline

Signal emitted when ok pressed.

Definition at line 81 of file Popup.h.

81 { return okPressed_; }

◆ setDefaultValue()

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 }

◆ setJavaScript()

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
const std::string createCall(std::initializer_list< std::string > args) const
void setJavaScript(const std::string &javaScript, int nbArgs=0)
std::string narrow(const std::locale &loc=std::locale()) const

◆ setMessage()

void Popup::setMessage ( const WString message)

Change the message.

Definition at line 61 of file Popup.C.

62 {
63  message_ = message;
64  setJavaScript();
65 }

Member Data Documentation

◆ cancelPressed_

JSignal Popup::cancelPressed_
private

Definition at line 89 of file Popup.h.

◆ defaultValue_

std::string Popup::defaultValue_
private

Definition at line 93 of file Popup.h.

◆ message_

WString Popup::message_
private

Definition at line 92 of file Popup.h.

◆ okPressed_

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

Definition at line 88 of file Popup.h.

◆ show

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.

◆ t_

Type Popup::t_
private

Definition at line 91 of file Popup.h.


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