Wt examples  4.0.5
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 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 WString & message () 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...
 
JSignal & cancelPressed ()
 Signal emitted when cancel is pressed. More...
 

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_
 

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.

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 }
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
const WString & message() const
Get the current message.
Definition: Popup.h:66
Type t_
Definition: Popup.h:91
JSignal< std::string > okPressed_
Definition: Popup.h:88

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_; }
JSignal cancelPressed_
Definition: Popup.h:89

◆ 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 cpp14::make_unique<Popup>(Type::Alert, message, std::string());
81 }
const WString & message() const
Get the current message.
Definition: Popup.h:66

◆ 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 cpp14::make_unique<Popup>(Type::Confirm, message, std::string());
76 }
const WString & message() const
Get the current message.
Definition: Popup.h:66

◆ 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 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

◆ 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_; }
std::string defaultValue_
Definition: Popup.h:93

◆ message()

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

◆ okPressed()

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

◆ 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 }
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

◆ 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:
36  show.setJavaScript
37  ("function(){ if (confirm('" + message_.narrow() + "')) {"
38  + okPressed_.createCall({"''"}) +
39  "} else {"
40  + cancelPressed_.createCall({}) +
41  "}}");
42  break;
43  case Alert:
44  show.setJavaScript
45  ("function(){ alert('" + message_.narrow() + "');"
46  + okPressed_.createCall({"''"}) +
47  "}");
48  break;
49  case Prompt:
50  show.setJavaScript
51  ("function(){var n = prompt('" + message_.narrow() + "', '"
52  + defaultValue_ + "');"
53  "if (n != null) {"
54  + okPressed_.createCall({"n"}) +
55  "} else {"
56  + cancelPressed_.createCall({}) +
57  "}}");
58  }
59 }
JSlot show
Show the dialog.
Definition: Popup.h:77
WString message_
Definition: Popup.h:92
JSignal cancelPressed_
Definition: Popup.h:89
std::string defaultValue_
Definition: Popup.h:93
Type t_
Definition: Popup.h:91
JSignal< std::string > okPressed_
Definition: Popup.h:88

◆ 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 }
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

◆ 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:

Generated on Mon May 20 2019 for the C++ Web Toolkit (Wt) by doxygen 1.8.14