1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* Copyright (C) 2010 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WPushButton>
#include <Wt/WSuggestionPopup>
#include <Wt/WLineEdit>
#include <Wt/WStringListModel>
#include <Wt/WText>
/*
* See also: http://www.webtoolkit.eu/wt/blog/2010/03/02/javascript_that_is_c__
*/
#define INLINE_JAVASCRIPT(...) #__VA_ARGS__
class SuggestionPopups : public Wt::WApplication
{
public:
SuggestionPopups(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("WSuggestionPopup example");
setCssTheme("polished");
messageResourceBundle().use(appRoot() + "text");
styleSheet().addRule(".Wt-suggest b", "color: black;");
mostSimplePopup(root());
simplePopup(root());
serverSideFilteringPopups(root());
}
private:
Wt::WStringListModel *fourCharModel_;
Wt::WAbstractItemModel *createSimpleDrugsModel()
{
const char *hivDrugs[] = {
"Delavirdine",
"Efavirenz",
"Etravirine",
"Nevirapine",
"Abacavir",
"Didanosine",
"Emtricitabine",
"Lamivudine",
"Stavudine",
"Tenofovir DF",
"Zidovudine",
"Amprenavir",
"Atazanavir",
"Darunavir",
"Fosamprenavir",
"Indinavir",
"Lopinavir, Ritonavir",
"Nelfinavir",
"Ritonavir",
"Saquinavir",
"Tipranavir",
0
};
Wt::WStringListModel *model = new Wt::WStringListModel();
for (const char **i = hivDrugs; *i; ++i)
model->addString(Wt::WString::fromUTF8(*i));
return model;
}
/*
* Creates a one-column model with data on HIV drugs.
* - the DisplayRole contains a semi-colon separated list of all names,
* starting with the leading name
* - the UserRole contains the leading name
*
* The DisplayRole will be interpreted by the special aliasing match
* function
*/
Wt::WAbstractItemModel *createDrugsModel()
{
const char *hivDrugs[] = {
"Delavirdine;Rescriptor;DLV",
"Efavirenz;Sustiva;EFV",
"Etravirine;Intelence;TMC125;ETR",
"Nevirapine;Viramune;NVP",
"Abacavir;Ziagen;ABC",
"Didanosine;Videx;ddI;Videx EC",
"Emtricitabine;Emtriva;FTC",
"Lamivudine;Epivir;3TC",
"Stavudine;Zerit;d4T",
"Tenofovir DF;Viread,TDF",
"Zidovudine;Retrovir;AZT;ZDV",
"Amprenavir;Agenerase;APV",
"Atazanavir;Reyataz;ATV",
"Darunavir;Prezista;TMC114;DRV",
"Fosamprenavir;Lexiva;FPV",
"Indinavir;Crixivan;IDV",
"Lopinavir, Ritonavir;Kaletra;LPV/r",
"Nelfinavir;Viracept;NFV",
"Ritonavir;Norvir;RTV",
"Saquinavir;Invirase;SQV",
"Tipranavir;Aptivus;TPV",
0
};
Wt::WStringListModel *model = new Wt::WStringListModel(this);
for (const char **i = hivDrugs; *i; ++i) {
int row = model->rowCount();
std::string names = *i;
model->addString(names);
std::string value = names;
std::size_t sc = value.find(';');
if (sc != std::string::npos)
value = value.substr(0, sc);
model->setData(row, 0, value, Wt::UserRole);
}
model->sort(0);
return model;
}
void mostSimplePopup(Wt::WContainerWidget *parent)
{
Wt::WSuggestionPopup::Options simpleOptions;
simpleOptions.highlightBeginTag = "<b>";
simpleOptions.highlightEndTag = "</b>";
simpleOptions.listSeparator = 0;
Wt::WSuggestionPopup *popup = new Wt::WSuggestionPopup(simpleOptions,
parent);
popup->setModel(createSimpleDrugsModel());
new Wt::WText(Wt::WString::tr("simplest-popup"), parent);
Wt::WLineEdit *edit = new Wt::WLineEdit(parent);
edit->resize(150, Wt::WLength::Auto);
popup->forEdit(edit);
}
void simplePopup(Wt::WContainerWidget *parent)
{
Wt::WSuggestionPopup *popup = createAliasesMatchingPopup(parent);
popup->setModel(createDrugsModel());
popup->setMinimumSize(150, Wt::WLength::Auto);
popup->setMaximumSize(Wt::WLength::Auto, 300);
new Wt::WText(Wt::WString::tr("simple-popup-editing"), parent);
Wt::WLineEdit *edit = new Wt::WLineEdit(parent);
edit->resize(150, Wt::WLength::Auto);
popup->forEdit(edit);
new Wt::WText(Wt::WString::tr("simple-popup-dropdown"), parent);
edit = new Wt::WLineEdit(parent);
edit->resize(150, Wt::WLength::Auto);
popup->forEdit(edit, Wt::WSuggestionPopup::DropDownIcon);
/*
showAt() shows the suggestion popup
Wt::WPushButton *show = new Wt::WPushButton("show", parent);
show->clicked().connect(boost::bind(&Wt::WSuggestionPopup::showAt,
popup, edit));
*/
}
void serverSideFilteringPopups(Wt::WContainerWidget *parent)
{
fourCharModel_ = new Wt::WStringListModel(this);
Wt::WSuggestionPopup *popup = createAliasesMatchingPopup(parent);
popup->setModel(fourCharModel_);
popup->setFilterLength(3);
popup->filterModel().connect(this, &SuggestionPopups::filter);
popup->setMinimumSize(150, Wt::WLength::Auto);
popup->setMaximumSize(Wt::WLength::Auto, 300);
new Wt::WText(Wt::WString::tr("serverside-popup-editing"), parent);
Wt::WLineEdit *edit = new Wt::WLineEdit(parent);
edit->resize(150, Wt::WLength::Auto);
popup->forEdit(edit, Wt::WSuggestionPopup::Editing);
new Wt::WText(Wt::WString::tr("serverside-popup-dropdown"), parent);
edit = new Wt::WLineEdit(parent);
edit->resize(150, Wt::WLength::Auto);
popup->forEdit(edit, Wt::WSuggestionPopup::DropDownIcon);
}
void filter(const Wt::WString& input)
{
/*
* We implement a virtual model contains all items that start with
* any arbitrary 3 characters, followed by "a-z"
*/
fourCharModel_->removeRows(0, fourCharModel_->rowCount());
for (int i = 0; i < 26; ++i) {
int row = fourCharModel_->rowCount();
/*
* If the input is shorter than the server-side filter length,
* then limit the number of matches and end with a '...'
*/
if (input.value().length() < 3 && i > 10) {
fourCharModel_->addString("...");
fourCharModel_->setData(row, 0, std::string(""), Wt::UserRole);
fourCharModel_->setData(row, 0, std::string("Wt-more-data"),
Wt::StyleClassRole);
break;
}
std::wstring v = input;
while (v.length() < 3)
v += L'a';
v += (L'a' + i);
fourCharModel_->addString(v);
}
}
Wt::WSuggestionPopup *createAliasesMatchingPopup(Wt::WContainerWidget *parent)
{
/*
* This matcher JavaScript function matches the input against the
* name of a product, or one or more aliases.
*
* A match is indicated by product name and optionally matching aliases
* between brackets.
*/
/*
* Note!
*
* INLINE_JAVASCRIPT is a macro which allows entry of JavaScript
* directly in a C++ file.
*/
std::string matcherJS = INLINE_JAVASCRIPT
(
function (edit) {
var value = edit.value;
return function(suggestion) {
if (!suggestion)
return value;
var i, il,
names = suggestion.split(';'),
val = value.toUpperCase(),
matchedAliases = [],
matched = null;
if (val.length) {
for (i = 0, il = names.length; i < il; ++i) {
var name = names[i];
if (name.length >= val.length
&& name.toUpperCase().substr(0, val.length) == val) {
// This name matches
name = '<b>' + name.substr(0, val.length) + '</b>'
+ name.substr(val.length);
if (i == 0) // it's the product name
matched = name;
else // it's an alias
matchedAliases.push(name);
}
}
}
// Let '...' always match
if (names[0] == '...')
matched = names[0];
if (matched || matchedAliases.length) {
if (!matched)
matched = names[0];
if (matchedAliases.length)
matched += " (" + matchedAliases.join(", ") + ")";
return { match : true,
suggestion : matched };
} else {
return { match : false,
suggestion : names[0] };
}
}
}
);
std::string replacerJS = INLINE_JAVASCRIPT
(
function (edit, suggestionText, suggestionValue) {
edit.value = suggestionValue;
if (edit.selectionStart)
edit.selectionStart = edit.selectionEnd = suggestionValue.length;
}
);
return new Wt::WSuggestionPopup(matcherJS, replacerJS, parent);
}
};
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new SuggestionPopups(env);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, &createApplication);
}
|