ultimatepp/uppsrc/Skylark/StdLib.icpp
cxl 39f4e84527 Skylark: Fixed indentity issue
git-svn-id: svn://ultimatepp.org/upp/trunk@5730 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2013-01-26 11:23:44 +00:00

77 lines
1.9 KiB
Text

#include "Skylark.h"
namespace Upp {
Value Cycle(const Vector<Value>& arg, const Renderer *)
{
if(arg.GetCount() < 3 && !IsNumber(arg[0]))
return String();
return arg[1 + int(arg[0]) % (arg.GetCount() - 1)];
}
Value CountFn(const Vector<Value>& arg, const Renderer *)
{
return arg.GetCount() && IsValueArray(arg[0]) ? ValueArray(arg[0]).GetCount() : 0;
}
Value RawFn(const Vector<Value>& arg, const Renderer *)
{
RawHtmlText r;
for(int i = 0; i < arg.GetCount(); i++)
r.text.Cat(AsString(arg[i]));
return RawToValue(r);
}
String GetIdentity(const Renderer *r)
{
// This ugly hack expects that __identity__ is always present in r->var
Http *http = const_cast<Http *>(dynamic_cast<const Http *>(r));
if(!http)
throw Exc("invalid POST identity call");
String s = http->var[0];
if(s.GetCount())
return s;
s = AsString(Uuid::Create());
http->SessionSet0("__identity__", s);
http->var[0] = s;
return s;
}
Value PostIdentity(const Vector<Value>&, const Renderer *r)
{
return Raw("<input type=\"hidden\" name=\"__post_identity__\" value=\"" + GetIdentity(r) + "\">");
}
Value JsIdentity(const Vector<Value>&, const Renderer *r)
{
return Raw("<script type=\"text/javascript\">var __js_identity__ = \"" + GetIdentity(r) + "\"</script>");
}
Value VariablesSet(const Vector<Value>&, const Renderer *r)
{
String html;
if(r) {
const VectorMap<String, Value>& set = r->Variables();
html << "<table border='1'><tr><th>ID</th><th>VALUE</th></tr>\n";
for(int i = 0; i < set.GetCount(); i++)
html << "<tr><td>"
<< EscapeHtml(set.GetKey(i))
<< "</td><td>"
<< EscapeHtml(AsString(set[i]))
<< "</td></tr>"
;
html << "</table>";
}
return Raw(html);
}
INITBLOCK {
Compiler::Register("cycle", Cycle);
Compiler::Register("raw", RawFn);
Compiler::Register("count", CountFn);
Compiler::Register("post_identity", PostIdentity);
Compiler::Register("js_identity", JsIdentity);
Compiler::Register("set", VariablesSet);
};
};