Browse Source

support for different sections & separators in drop-down list

refactor-result-recursion
Lars Rohwedder 6 years ago
parent
commit
421a7e53a1
5 changed files with 41 additions and 7 deletions
  1. +2
    -2
      html/index.html
  2. +9
    -1
      html/interactive.js
  3. +17
    -2
      server/function_map.hh
  4. +11
    -2
      server/json_rpc.cc
  5. +2
    -0
      server/mt-server.cc

+ 2
- 2
html/index.html View File

@ -22,7 +22,7 @@
</head>
<body>
<h1>Send example calls via jQuery – v139</h1>
<h1>Send example calls via jQuery – v142</h1>
<form id="frm" name="frm">
@ -39,7 +39,7 @@
</select>
</td></tr>
<tr><td>Parameters:</td><td id="td_param"></td></tr>
<tr><th colspan="2"> <input type="button" value="Call function!" onClick="button_click()"> </td></tr>
<tr><th colspan="2"> <input type="button" value="Call function!" onClick="button_click()" id="call_btn"> </td></tr>
<tr><td>Debug...</td><td id="deb"><pre id="debug">...</pre></td></tr>
<tr><td>Result:</td><td id="result"><pre id="resultpre">Banana!</pre></td></tr>
</table>


+ 9
- 1
html/interactive.js View File

@ -288,6 +288,14 @@ function button_click()
function prepare_call(f)
{
func = f;
if(f.separator)
{
document.getElementById("td_param").innerHTML = "——";
document.getElementById("td_param").disabled = true;
return;
}
document.getElementById("td_param").disabled = false;
func_params = new Array(func.params.length);
var len = f.params.length;
@ -340,7 +348,7 @@ function init_pep_functions()
for(var i=0, len=pep_functions.length; i<len; ++i)
{
var f = pep_functions[i];
optionList += "<option>" + f.name + "</option>\n";
optionList += '<option' + (f.separator? ' disabled>':'>') + f.name + "</option>\n";
}
document.getElementById("fn_name").innerHTML = optionList;


+ 17
- 2
server/function_map.hh View File

@ -136,7 +136,6 @@ extern const Concat concat;
// helper functors for to_json(tuple<...>):
namespace
{
@ -266,6 +265,7 @@ class FuncBase
{
public:
virtual ~FuncBase() = default;
virtual bool isSeparator() const = 0;
virtual void setJavaScriptSignature(js::Object& o) const = 0;
virtual js::Value call(const js::Array& params) const = 0;
};
@ -279,6 +279,10 @@ public:
enum { Size = sizeof...(Args) };
virtual ~Func() = default;
virtual bool isSeparator() const override
{
return false;
}
Func() : fn() {}
@ -306,11 +310,22 @@ public:
o.emplace_back( "return", Type2String<R>::get() );
o.emplace_back( "params", params );
o.emplace_back( "separator", false );
}
};
typedef std::map< std::string, FuncBase* > FunctionMap;
class Separator : public FuncBase
{
public:
Separator() = default;
virtual bool isSeparator() const override { return true; }
virtual void setJavaScriptSignature(js::Object& o) const override { o.emplace_back("separator", true); }
virtual js::Value call(const js::Array& params) const override { return js::Value{}; }
};
//typedef std::map< std::string, FuncBase* > FunctionMap;
typedef std::vector< std::pair< std::string, FuncBase*> > FunctionMap;
typedef FunctionMap::value_type FP;
#endif // FUNCTION_MAP_HH

+ 11
- 2
server/json_rpc.cc View File

@ -32,6 +32,14 @@
}
namespace
{
FunctionMap::const_iterator find_in_vector(const FunctionMap& fm, const std::string& key)
{
return std::find_if(fm.begin(), fm.end(), [&key](const FunctionMap::value_type& v){ return v.first == key; });
}
}
using json_spirit::find_value;
@ -53,8 +61,9 @@ js::Object call(const FunctionMap& fm, const js::Object& request)
}
const std::string method_name = method.get_str();
const auto fn = fm.find(method_name);
if(fn == fm.end())
//const auto fn = fm.find(method_name);
const auto fn = find_in_vector(fm,method_name);
if(fn == fm.end() || fn->second->isSeparator())
{
return make_error(JSON_RPC::METHOD_NOT_FOUND, "Method \"" + method_name + "\" is unknown to me.", request, request_id);
}


+ 2
- 0
server/mt-server.cc View File

@ -120,6 +120,7 @@ std::string getVersion() { return "0.2"; }
const FunctionMap functions = {
// from message_api.h
FP( "—— Message API ——", new Separator ),
FP( "encrypt_message", new Func<PEP_STATUS, In<PEP_SESSION>, In<message*>, In<stringlist_t*>, Out<message*>, In<PEP_enc_format>>( &encrypt_message ) ),
FP( "decrypt_message", new Func<PEP_STATUS, In<PEP_SESSION>, In<message*>, Out<message*>, Out<stringlist_t*>, Out<PEP_color>>( &decrypt_message ) ),
FP( "outgoing_message_color", new Func<PEP_STATUS, In<PEP_SESSION>, In<message*>, Out<PEP_color>>( &outgoing_message_color ) ),
@ -128,6 +129,7 @@ const FunctionMap functions = {
FP( "get_gpg_path", new Func<PEP_STATUS, Out<const char*>>(&get_gpg_path) ),
// my own example function that does something useful. :-)
FP( "—— Other ——", new Separator ),
FP( "version", new Func<std::string>( &getVersion ) ),
FP( "releaseSession", new Func<PEP_STATUS, InRaw<PEP_SESSION>>(&releaseSession) ),
};


Loading…
Cancel
Save