File: select.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (141 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download | duplicates (2)
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
/*
Copyright (©) 2021 Aranggi Toar.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include <dialog/select.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wsuggest-override"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#ifndef HAVE_PUGIXML
#include <pugixml/pugixml.hpp>
#endif
#ifdef HAVE_PUGIXML
#include <pugixml.hpp>
#endif
#pragma GCC diagnostic pop
#include <filter/string.h>
#include <filter/url.h>


namespace dialog::select {


// Create the html <select> element and fill them with the values.
static void create_select(pugi::xml_node parent, const Settings& settings)
{
  pugi::xml_node select_node = parent.append_child("select");
  select_node.append_attribute("id") = settings.identification;
  select_node.append_attribute("name") = settings.identification;
  if (settings.disabled)
    select_node.append_attribute("disabled") = "";
  for (size_t v {0}; v < settings.values.size(); v++) {
    pugi::xml_node option_node = select_node.append_child("option");
    option_node.append_attribute("value") = settings.values.at(v).c_str();
    if (settings.selected and settings.selected.value() == settings.values.at(v))
      option_node.append_attribute("selected");
    const std::string display = (v >= settings.displayed.size()) ? settings.values.at(v) : settings.displayed.at(v);
    option_node.text().set(display.c_str());
  }
  if (settings.tooltip)
    select_node.append_attribute("title") = settings.tooltip.value().c_str();
}


static std::string remove_comment_from_code (std::string code)
{
  // Code with a comment may look like this:
  /* Code */
  filter::strings::replace_between (code, "/*", "*/", std::string());
  return code;
}


std::string ajax(const Settings& settings)
{
  pugi::xml_document document {};

  create_select (document, settings);

  // The Javascript to POST the selected value if it changes.
  // The script should be a module because that defers execution till the document has been loaded.
  std::string javascript = filter_url_file_get_contents(filter_url_create_root_path({"dialog/selectajax.js"}));
  javascript = remove_comment_from_code (std::move(javascript));
  javascript = filter::strings::replace("identification", settings.identification, std::move(javascript));
  
  pugi::xml_node script_node = document.append_child("script");
  script_node.append_attribute("type") = "module";
  script_node.text().set(javascript.c_str());
  
  // Convert it to html including Javascript.
  std::stringstream html_ss {};
  document.print (html_ss, "", pugi::format_raw);
  
  // Update the html with the parameters to append to the POST request.
  std::string html = html_ss.str();
  const std::string url = filter_url_build_http_query(settings.url, settings.parameters);
  html = filter::strings::replace("URL", url, std::move(html));

  return html;
}


std::string form(const Settings& settings, const Form& form)
{
  pugi::xml_document document {};

  // The parameters, if any, to append to the POST action.
  std::stringstream ss{};
  for (const std::pair<std::string, std::string>& parameter : settings.parameters) {
    ss << (ss.str().empty()?"?":"&");
    ss << parameter.first << "=" << parameter.second;
  }
  const std::string action {settings.url + ss.str()};

  // Create the form.
  pugi::xml_node form_node = document.append_child("form");
  form_node.append_attribute("action") = action.c_str();
  form_node.append_attribute("method") = "post";
  form_node.append_attribute("style") = "display:inline!important;";
  create_select (form_node, settings);
  if (!form.auto_submit) {
    pugi::xml_node input_node = form_node.append_child("input");
    input_node.append_attribute("type") = "submit";
    if (settings.submit)
      input_node.append_attribute("value") = settings.submit.value().c_str();
  }
  
  // If automatic submit, add a script that does the job.
  // The script should be a module because that defers execution till the document has been loaded.
  if (form.auto_submit) {
    std::string javascript = filter_url_file_get_contents(filter_url_create_root_path({"dialog/selectform.js"}));
    javascript = remove_comment_from_code (std::move(javascript));
    javascript = filter::strings::replace("identification", settings.identification, std::move(javascript));
    pugi::xml_node script_node = document.append_child("script");
    script_node.append_attribute("type") = "module";
    script_node.text().set(javascript.c_str());
  }

  // Convert it to html including Javascript.
  std::stringstream html_ss {};
  document.print (html_ss, "", pugi::format_raw);
  return html_ss.str();
}


}