Wt examples: Popup Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
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 Member Functions
| Popup (Type t, const WString &message, const std::string defaultValue)
| Popup constructor.
void | setMessage (const WString &message)
| Change the message.
void | setDefaultValue (const std::string defaultValue)
| Change the default value for a prompt dialog.
const WString & | message () const
| Get the current message.
const std::string & | defaultValue () const
| Get the default value for a prompt dialog.
JSignal< std::string > & | okPressed ()
| Signal emitted when ok pressed.
JSignal & | cancelPressed ()
| Signal emitted when cancel is pressed.

## Static Public Member Functions
static std::unique_ptr< Popup > | createConfirm (const WString &message)
| Create a confirm dialog.
static std::unique_ptr< Popup > | createPrompt (const WString &message, const std::string defaultValue)
| Create a prompt dialog with the given default value.
static std::unique_ptr< Popup > | createAlert (const WString &message)
| Create an alert dialog.

## Public Attributes
JSlot | show
| Show the dialog.

## Private Member Functions
void | setJavaScript ()
| Update the javascript code.

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

enum Popup::Type

Popup type.

Enumerator
Confirm |
Alert |
Prompt |

Definition at line 37 of file Popup.h.

37{ Confirm, Alert, Prompt };
Popup::Alert
@ Alert
Definition Popup.h:37
Popup::Prompt
@ Prompt
Definition Popup.h:37
Popup::Confirm
@ 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),
19 message_(message),
20 defaultValue_(defaultValue)
21{
22 setJavaScript();
23}
Popup::cancelPressed_
JSignal cancelPressed_
Definition Popup.h:89
Popup::defaultValue
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition Popup.h:70
Popup::t_
Type t_
Definition Popup.h:91
Popup::message_
WString message_
Definition Popup.h:92
Popup::message
const WString & message() const
Get the current message.
Definition Popup.h:66
Popup::defaultValue_
std::string defaultValue_
Definition Popup.h:93
Popup::okPressed_
JSignal< std::string > okPressed_
Definition Popup.h:88
Popup::setJavaScript
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{
69 defaultValue_ = defaultValue;
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:
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}
Popup::show
JSlot show
Show the dialog.
Definition Popup.h:77

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

- javascript/Popup.h
- javascript/Popup.C