fixes to table, events, datefields

This commit is contained in:
Peter Schojer 2008-05-30 12:48:59 +00:00
parent 72a0f3c3ff
commit fafec95355
16 changed files with 329 additions and 66 deletions

View File

@ -144,12 +144,15 @@ private:
static LookAndFeel::Ptr createDefault();
static int detectMaxParamCount(const std::list<JSDelegate>& delegates);
static void skipWhiteSpace(const std::string& code, std::string::size_type& pos);
static void analyzeFunction(const JSDelegate& delegate, std::string& fctName, std::vector<std::string>& paramNames, std::string& code);
// Code contains { ... }
static std::string createFunctionSignature(int paramCnt);
/// Creates an anonmyous JS function with the given param count
static std::string createFunctionSignature(const std::string& fctName, int paramCnt);
static void writeFunction(std::ostream& out, const std::string& fctName, const std::vector<std::string> &params, const std::string& code);
private:
Utility();
~Utility();

View File

@ -81,7 +81,9 @@ void ComboBoxCellRenderer::renderHead(const Renderable* pRenderable, const Rende
poco_assert_dbg (pRenderable != 0);
poco_assert_dbg (pRenderable->type() == typeid(Poco::WebWidgets::ComboBoxCell));
ComboBoxCell* pCell = const_cast<ComboBoxCell*>(static_cast<const Poco::WebWidgets::ComboBoxCell*>(pRenderable));
ComboBox* pOwner = dynamic_cast<ComboBox*>(pCell->getOwner());
//two scenarios: owner is either a ComboBox or a TableColumn!
View* pOwner = pCell->getOwner();
ComboBox* pComboOwner = dynamic_cast<ComboBox*>(pOwner);
poco_check_ptr (pOwner);
ostr << "new Ext.form.ComboBox({";
@ -98,10 +100,10 @@ void ComboBoxCellRenderer::renderHead(const Renderable* pRenderable, const Rende
std::string tooltip (pCell->getToolTip());
if (!pOwner->selected.jsDelegates().empty())
if (pComboOwner && !pComboOwner->selected.jsDelegates().empty())
{
ostr << ",listeners:{";
Utility::writeJSEvent(ostr, EV_SELECTED, pOwner->selected.jsDelegates());
Utility::writeJSEvent(ostr, EV_SELECTED, pComboOwner->selected.jsDelegates());
if (!tooltip.empty())
ostr << ",render:function(c){Ext.QuickTips.register({target:c.getEl(),text:'" << Utility::safe(tooltip) << "'});}";
ostr << "}";

View File

@ -64,13 +64,15 @@ void PanelRenderer::renderHead(const Renderable* pRenderable, const RenderContex
Utility::writeRenderableProperties(pRenderable, ostr);
if (!pPanel->getName().empty())
ostr << ",title:'" << pPanel->getName() << "'";
ostr << ",title:'" << pPanel->getTitle() << "'";
if (pPanel->getWidth() > 0)
ostr << ",width:" << pPanel->getWidth();
if (pPanel->getHeight() > 0)
ostr << ",height:" << pPanel->getHeight();
if (pPanel->getModal())
ostr << ",modal:true";
if (!pPanel->hasCloseIcon())
ostr << ",closable:false";
// minimizable set to true only fires a minimize event, without an event handler attached
// it is pretty useless, instead use collapsible

View File

@ -86,7 +86,7 @@ TableCellHandlerFactory::TableCellHandlerFactory()
registerFactory(typeid(ImageButtonCell), pHandle);
}
{
TableCellHandler<DateFieldCell>::Ptr pHandle(new TableCellHandler<DateFieldCell>(true, false));
TableCellHandler<DateFieldCell>::Ptr pHandle(new TableCellHandler<DateFieldCell>(true, true));
registerFactory(typeid(DateFieldCell), pHandle);
}
}

View File

@ -97,7 +97,11 @@ void TableRenderer::addCellValueChangedServerCallback(Table* pTable, const std::
std::map<std::string, std::string> addParams;
addParams.insert(std::make_pair(Table::FIELD_COL, "+obj.column"));
addParams.insert(std::make_pair(Table::FIELD_ROW, origRow));
addParams.insert(std::make_pair(Table::FIELD_VAL, "+obj.value"));
//problem: I need the displayed string from teh renderer, not the value!
// date fields cause problems here, and I only habe one cellclick event per table not per column!
// from the tabel get the TableCOlumn, from thsi get the renderer for the given col and render obj.value
// {(var r=obj.grid.getColumnModel().getRenderer(obj.col))?r(obj.value);:obj.value;} hm, no working
addParams.insert(std::make_pair(Table::FIELD_VAL, "+obj.value"));
addParams.insert(std::make_pair(RequestHandler::KEY_EVID, Table::EV_CELLVALUECHANGED));
Utility::addServerCallback(pTable->cellValueChanged, signature, addParams, pTable->id(), onSuccess, onFailure);
pTable->cellValueChanged.add(jsDelegate("function(obj){obj.grid.getStore().commitChanges();}"));
@ -207,12 +211,12 @@ void TableRenderer::renderColumn(const Table* pTable, const TableColumn& tc, int
ostr << ",editor:";
tc.getCell()->renderHead(context, ostr);
}
if (pHandler->useRenderer())
{
ostr << ",renderer:";
pHandler->writeDynamicData(ostr);
}
}
ostr << "}";
@ -244,7 +248,14 @@ void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
{
if (i != 0)
ostr << ",";
ostr << "{name:'" << i << "'}";
if ((*it)->getCell()->type() == typeid(Poco::WebWidgets::DateFieldCell))
{
Poco::WebWidgets::DateFieldCell::Ptr pDf = (*it)->getCell().cast<Poco::WebWidgets::DateFieldCell>();
ostr << "{name:'" << i << "',type:'date',dateFormat:'" << Utility::convertPocoDateToPHPDate(pDf->getFormat()) << "'}";
}
else
ostr << "{name:'" << i << "'}";
}
ostr << ",{name:'" << i << "'}";
ostr << "],"; // close fields

View File

@ -92,6 +92,7 @@
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/String.h"
#include "Poco/StringTokenizer.h"
#include "Poco/NumberFormatter.h"
#include "Poco/DateTimeFormat.h"
#include <sstream>
@ -412,16 +413,40 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons
invoke << "invoke:" << fct <<"{";
std::list<JSDelegate>::const_iterator it = delegates.begin();
int cnt(0);
for (; it != delegates.end(); ++it)
for (; it != delegates.end(); ++it, ++cnt)
{
std::string fctName("d");
fctName.append(Poco::NumberFormatter::format(cnt));
if (cnt > 0)
std::string myFctName("d");
myFctName.append(Poco::NumberFormatter::format(cnt));
out << myFctName << ":";
std::string fctName;
std::vector<std::string> params;
std::string code;
analyzeFunction(*it, fctName, params, code);
if (fctName == "function")
{
out << ",";
// an inline definition, we must have code
if (!code.empty())
{
writeFunction(out, fctName, params, code);
}
}
out << fctName << ":" << it->jsCode() << ","; //always write comma because invoke is written as the last method
invoke << "this." << createFunctionSignature(fctName, maxParams) << ";";
else
{
if (code.empty())
{
// a reference to another js function
//maybe the user defined params too -> ignore them
out << fctName;
}
else
{
// we have code, so rename the function to function :-)
writeFunction(out, "function", params, code);
}
}
out << ","; //always write comma because invoke is written as the last method
invoke << "this." << createFunctionSignature(myFctName, maxParams) << ";";
}
invoke << "}";
@ -436,6 +461,27 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons
}
void Utility::writeFunction(std::ostream& out, const std::string& fctName, const std::vector<std::string> &params, const std::string& code)
{
out << fctName << "(";
std::vector<std::string>::const_iterator it = params.begin();
for (; it != params.end(); ++it)
{
if (it != params.begin())
out << ",";
out << *it;
}
out << ")";
bool openWritten = false;
if (code.find('{') != 0)
{
out << "{";
openWritten = true;
}
out << code;
if (openWritten)
out << "}";
}
std::string Utility::createURI(const std::map<std::string, std::string>& addParams, Renderable::ID id)
@ -526,39 +572,55 @@ int Utility::detectMaxParamCount(const std::list<JSDelegate>& delegates)
for (; it != delegates.end(); ++it)
{
//count all , between ()
int tmpCnt(0);
const std::string& code = it->jsCode();
std::string::size_type pos = code.find('(');
if (pos != std::string::npos)
{
++pos; //skip (
skipWhiteSpace(code, pos);
bool stop = false;
while(!stop && pos < code.size())
{
if (code[pos] == ')')
stop = true;
else if (code[pos] == ',')
++tmpCnt;
++pos;
}
}
std::string tmp;
std::string tmp2;
std::vector<std::string> params;
analyzeFunction(*it, tmp, params, tmp2);
tmpCnt = (int)params.size();
if (tmpCnt > cnt)
cnt = tmpCnt;
}
if (cnt > 0) // we counted ','
++cnt; // we want var count
return cnt;
}
void Utility::skipWhiteSpace(const std::string& code, std::string::size_type& pos)
void Utility::analyzeFunction(const JSDelegate& delegate, std::string& fctName, std::vector<std::string>& paramNames, std::string& code)
{
while (pos < code.size() && std::isspace(code[pos]))
// 3 cases:
// function(...){...}
// myname(....) {}
// myname //external call
const std::string& jsCode = delegate.jsCode();
fctName.clear();
paramNames.clear();
code.clear();
std::string::size_type pos = jsCode.find(')');
std::string fctHeader = jsCode.substr(0, pos);
if (pos != std::string::npos)
{
++pos;
code = jsCode.substr(pos);
Poco::trimInPlace(code);
}
Poco::StringTokenizer tok(fctHeader, "(,", Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
if (tok.count() > 0)
{
Poco::StringTokenizer::Iterator it = tok.begin();
fctName = *it;
++it;
for (; it != tok.end(); ++it)
paramNames.push_back(*it);
}
}
std::string Utility::createFunctionSignature(int paramCnt)
{
static const std::string fct("function");

View File

@ -1319,33 +1319,64 @@ void ExtJSTest::testTableImageButton()
void ExtJSTest::testJSEvent()
{
Button::Ptr pBut(new Button());
pBut->buttonClicked.add(jsDelegate("someFunction(obj)"));
pBut->buttonClicked.add(jsDelegate("function(obj){alert('Click');}"));
pBut->buttonClicked.add(jsDelegate("someFunction"));
pBut->buttonClicked.add(jsDelegate("someFunction2(obj)"));
pBut->buttonClicked.add(jsDelegate("hello(obj){alert('Click');}")); //hello must get renamed to function
std::ostringstream out;
Utility::writeJSEvent(out, "clicked", pBut->buttonClicked.jsDelegates());
std::string result(out.str());
static const std::string expected("'clicked':"
"{"
"fn:function(obj){"
"fn:function(p1){"
"var all={"
"d0:function(obj){"
"d0:someFunction,"
"d1:someFunction2,"
"d2:function(obj){"
"alert('Click');"
"},"
"d1:function(obj){"
"someFunction(obj);"
"},"
"invoke:function(obj){"
"this.d0(obj);"
"this.d1(obj);"
"invoke:function(p1){"
"this.d0(p1);"
"this.d1(p1);"
"this.d2(p1);"
"}"
"};"
"all.invoke(obj);"
"all.invoke(p1);"
"}"
"}");
assert (result == expected);
}
void ExtJSTest::testJSEvent2()
{
Button::Ptr pBut(new Button());
pBut->buttonClicked.add(jsDelegate("someFunction"));
pBut->buttonClicked.add(jsDelegate("someFunction2(obj,o2)"));
pBut->buttonClicked.add(jsDelegate("hello(obj){alert('Click');}")); //hello must get renamed to function
std::ostringstream out;
Utility::writeJSEvent(out, "clicked", pBut->buttonClicked.jsDelegates());
std::string result(out.str());
static const std::string expected("'clicked':"
"{"
"fn:function(p2,p1){"
"var all={"
"d0:someFunction,"
"d1:someFunction2,"
"d2:function(obj){"
"alert('Click');"
"},"
"invoke:function(p2,p1){"
"this.d0(p2,p1);"
"this.d1(p2,p1);"
"this.d2(p2,p1);"
"}"
"};"
"all.invoke(p2,p1);"
"}"
"}");
assert (result == expected);
}
void ExtJSTest::setUp()
{
}
@ -1397,6 +1428,7 @@ CppUnit::Test* ExtJSTest::suite()
CppUnit_addTest(pSuite, ExtJSTest, testTableButton);
CppUnit_addTest(pSuite, ExtJSTest, testTableImageButton);
CppUnit_addTest(pSuite, ExtJSTest, testJSEvent);
CppUnit_addTest(pSuite, ExtJSTest, testJSEvent2);
return pSuite;
}

View File

@ -82,6 +82,7 @@ public:
void testTableButton();
void testTableImageButton();
void testJSEvent();
void testJSEvent2();
void setUp();
void tearDown();

View File

@ -63,6 +63,8 @@ public:
// Cell
void handleForm(const std::string& field, const std::string& value);
void handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response);
protected:
~ButtonCell();

View File

@ -78,6 +78,17 @@ public:
std::vector<Any>::iterator end();
/// Iterator to all elements
template <typename T>
void setElements(const std::vector<T>& elems)
/// Initializes the combo box cell with the provided elements
{
std::vector<Any> result;
typename std::vector<T>::const_iterator it = elems.begin();
for (; it != elems.end(); ++it)
result.push_back(*it);
setElements(result);
}
void setElements(const std::vector<Any>& elems);
/// Initializes the combo box with the provided elements

View File

@ -59,12 +59,18 @@ public:
Frame(const std::string& name);
/// Creates the Frame.
Frame(const std::string& name, const std::string& title);
/// Creates the Frame.
Frame(View::Ptr pChild);
/// Creates an unnamed Frame.
Frame(const std::string& name, View::Ptr pChild);
/// Creates the Frame.
Frame(const std::string& name, const std::string& title, View::Ptr pChild);
/// Creates the Frame.
void setChild(View::Ptr pChild);
/// Sets the child of the panel

View File

@ -54,16 +54,22 @@ public:
typedef Poco::AutoPtr<Panel> Ptr;
Panel();
/// Creates an unnamed Panel.
/// Creates an unnamed Panel without a child
Panel(const std::string& name);
/// Creates the Panel.
/// Creates the Panel without a child and without a title
Panel(const std::string& name, const std::string& title);
/// Creates the Panel without a child
Panel(View::Ptr pChild);
/// Creates an unnamed Panel.
/// Creates an unnamed Panel with its child
Panel(const std::string& name, View::Ptr pChild);
/// Creates the Panel.
/// Creates the Panel with its child
Panel(const std::string& name, const std::string& title, View::Ptr pChild);
/// Creates the Panel with its child
void setChild(View::Ptr pChild);
/// Sets the child of the panel
@ -79,14 +85,36 @@ public:
/// Returns modal mode
View::Ptr findChild(const std::string& name) const;
void showCloseIcon(bool val);
/// Per default the panel has a close icon in the upper right corner.
/// Set val to false to hide it
bool hasCloseIcon() const;
/// Returns if the closeicon is shown
void setText(const std::string& text);
/// Sets the title of this View.
std::string getText() const;
/// Returns the title of this View.
void setTitle(const std::string& text);
/// Sets the title of this View.
const std::string& getTitle() const;
/// Returns the title of this View.
protected:
~Panel();
/// Destroys the Panel.
private:
View::Ptr _pChild;
bool _modal;
View::Ptr _pChild;
std::string _title;
bool _modal;
bool _showCloseIcon;
};
@ -112,6 +140,42 @@ inline bool Panel::getModal() const
}
inline void Panel::showCloseIcon(bool val)
{
_showCloseIcon = val;
}
inline bool Panel::hasCloseIcon() const
{
return _showCloseIcon;
}
inline void Panel::setText(const std::string& text)
{
setTitle(text);
}
inline std::string Panel::getText() const
{
return getTitle();
}
inline void Panel::setTitle(const std::string& text)
{
_title = text;
}
inline const std::string& Panel::getTitle() const
{
return _title;
}
} } // namespace Poco::WebWidgets

View File

@ -36,6 +36,8 @@
#include "Poco/WebWidgets/ButtonCell.h"
#include "Poco/WebWidgets/Button.h"
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/Net/HTTPServerResponse.h"
namespace Poco {
@ -68,4 +70,16 @@ void ButtonCell::handleForm(const std::string& field, const std::string& value)
}
void ButtonCell::handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response)
{
const std::string& ev = args[RequestHandler::KEY_EVID];
if (ev == EV_BUTTONCLICKED)
{
buttonClicked(this);
}
response.send();
}
} } // namespace Poco::WebWidgets

View File

@ -87,7 +87,6 @@ void ComboBoxCell::handleForm(const std::string& field, const std::string& value
void ComboBoxCell::handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response)
{
// RequestHandler has already called all the handeForm stuff
const std::string& ev = args[RequestHandler::KEY_EVID];
if (ev == EV_LOAD)
{

View File

@ -43,21 +43,32 @@ namespace WebWidgets {
Frame::Frame():
View(typeid(Frame)),
_pChild()
_pChild(),
_title()
{
}
Frame::Frame(const std::string& name):
View(name, typeid(Frame)),
_pChild()
_pChild(),
_title()
{
}
Frame::Frame(const std::string& name, const std::string& title):
View(name, typeid(Frame)),
_pChild(),
_title(title)
{
}
Frame::Frame(View::Ptr pChild):
View(typeid(Frame)),
_pChild()
_pChild(),
_title()
{
setChild(pChild);
}
@ -65,7 +76,17 @@ Frame::Frame(View::Ptr pChild):
Frame::Frame(const std::string& name, View::Ptr pChild):
View(name, typeid(Frame)),
_pChild()
_pChild(),
_title()
{
setChild(pChild);
}
Frame::Frame(const std::string& name, const std::string& title, View::Ptr pChild):
View(name, typeid(Frame)),
_pChild(),
_title(title)
{
setChild(pChild);
}
@ -73,21 +94,24 @@ Frame::Frame(const std::string& name, View::Ptr pChild):
Frame::Frame(const std::type_info& type):
View(type),
_pChild()
_pChild(),
_title()
{
}
Frame::Frame(const std::string& name, const std::type_info& type):
View(name, type),
_pChild()
_pChild(),
_title()
{
}
Frame::Frame(View::Ptr pChild, const std::type_info& type):
View(type),
_pChild()
_pChild(),
_title()
{
setChild(pChild);
}
@ -95,7 +119,8 @@ Frame::Frame(View::Ptr pChild, const std::type_info& type):
Frame::Frame(const std::string& name, View::Ptr pChild, const std::type_info& type):
View(name, typeid(Frame)),
_pChild()
_pChild(),
_title()
{
setChild(pChild);
}

View File

@ -44,7 +44,9 @@ namespace WebWidgets {
Panel::Panel():
View(typeid(Panel)),
_pChild(),
_modal(false)
_title(),
_modal(false),
_showCloseIcon(true)
{
}
@ -52,7 +54,19 @@ Panel::Panel():
Panel::Panel(const std::string& name):
View(name, typeid(Panel)),
_pChild(),
_modal(false)
_title(),
_modal(false),
_showCloseIcon(true)
{
}
Panel::Panel(const std::string& name, const std::string& title):
View(name, typeid(Panel)),
_pChild(),
_title(title),
_modal(false),
_showCloseIcon(true)
{
}
@ -60,7 +74,9 @@ Panel::Panel(const std::string& name):
Panel::Panel(View::Ptr pChild):
View(typeid(Panel)),
_pChild(),
_modal(false)
_title(),
_modal(false),
_showCloseIcon(true)
{
setChild(pChild);
}
@ -69,7 +85,20 @@ Panel::Panel(View::Ptr pChild):
Panel::Panel(const std::string& name, View::Ptr pChild):
View(name, typeid(Panel)),
_pChild(),
_modal(false)
_title(),
_modal(false),
_showCloseIcon(true)
{
setChild(pChild);
}
Panel::Panel(const std::string& name, const std::string& title, View::Ptr pChild):
View(name, typeid(Panel)),
_pChild(),
_title(title),
_modal(false),
_showCloseIcon(true)
{
setChild(pChild);
}