Wt examples  4.10.4
Popup.C
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 
8 #include <Wt/WAny.h>
9 
10 #include "Popup.h"
11 
12 using namespace Wt;
13 
14 Popup::Popup(Type t, const WString& message, std::string defaultValue)
15  : WObject(),
16  okPressed_(this, "ok"),
17  cancelPressed_(this, "cancel"),
18  t_(t),
19  message_(message),
20  defaultValue_(defaultValue)
21 {
22  setJavaScript();
23 }
24 
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 }
60 
61 void Popup::setMessage(const WString& message)
62 {
63  message_ = message;
64  setJavaScript();
65 }
66 
67 void Popup::setDefaultValue(const std::string defaultValue)
68 {
70  setJavaScript();
71 }
72 
73 std::unique_ptr<Popup> Popup::createConfirm(const WString& message)
74 {
75  return std::make_unique<Popup>(Type::Confirm, message, std::string());
76 }
77 
78 std::unique_ptr<Popup> Popup::createAlert(const WString& message)
79 {
80  return std::make_unique<Popup>(Type::Alert, message, std::string());
81 }
82 
83 std::unique_ptr<Popup> Popup::createPrompt(const WString& message,
84  const std::string defaultValue)
85 {
86  return std::make_unique<Popup>(Type::Prompt, message, defaultValue);
87 }
void setMessage(const WString &message)
Change the message.
Definition: Popup.C:61
const WString & message() const
Get the current message.
Definition: Popup.h:66
JSignal cancelPressed_
Definition: Popup.h:89
static std::unique_ptr< Popup > createConfirm(const WString &message)
Create a confirm dialog.
Definition: Popup.C:73
JSlot show
Show the dialog.
Definition: Popup.h:77
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
Popup(Type t, const WString &message, const std::string defaultValue)
Popup constructor.
Definition: Popup.C:14
Type t_
Definition: Popup.h:91
static std::unique_ptr< Popup > createAlert(const WString &message)
Create an alert dialog.
Definition: Popup.C:78
WString message_
Definition: Popup.h:92
void setDefaultValue(const std::string defaultValue)
Change the default value for a prompt dialog.
Definition: Popup.C:67
static std::unique_ptr< Popup > createPrompt(const WString &message, const std::string defaultValue)
Create a prompt dialog with the given default value.
Definition: Popup.C:83
std::string defaultValue_
Definition: Popup.h:93
Type
Popup type.
Definition: Popup.h:37
@ Alert
Definition: Popup.h:37
@ Prompt
Definition: Popup.h:37
@ Confirm
Definition: Popup.h:37
JSignal< std::string > okPressed_
Definition: Popup.h:88
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25
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