changed requestprocessor interface

This commit is contained in:
Peter Schojer 2008-05-29 11:32:51 +00:00
parent 4e774e71aa
commit bfddbdb20a
13 changed files with 113 additions and 198 deletions

View File

@ -47,6 +47,7 @@ namespace Poco {
namespace WebWidgets {
class ButtonCell;
class Button;
namespace ExtJS {
@ -55,6 +56,8 @@ class ExtJS_API ButtonCellRenderer: public CellRenderer
/// ButtonCellRenderer renders a button
{
public:
static const std::string EV_CLICK;
ButtonCellRenderer();
/// Creates the ButtonCellRenderer.
@ -71,6 +74,10 @@ public:
static void renderButton(const ButtonCell* pCell, const std::string& content, bool writeId, bool submitButton, std::ostream& ostr, bool showText = true);
/// Renders button properties
static void addClickServerCallback(Button* pCombo, const std::string& onSuccess, const std::string& onFailure);
/// Adds a server callback for the buttonClicked event. The JS method signature for click is
/// click : ( Button this, EventObject e )
protected:
virtual ~ButtonCellRenderer();

View File

@ -38,6 +38,10 @@
#include "Poco/WebWidgets/ExtJS/FormRenderer.h"
#include "Poco/WebWidgets/ExtJS/Utility.h"
#include "Poco/WebWidgets/ButtonCell.h"
#include "Poco/WebWidgets/Button.h"
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/WebWidgets/WebApplication.h"
#include "Poco/NumberFormatter.h"
namespace Poco {
@ -45,6 +49,9 @@ namespace WebWidgets {
namespace ExtJS {
const std::string ButtonCellRenderer::EV_CLICK("click");
ButtonCellRenderer::ButtonCellRenderer()
{
}
@ -89,7 +96,7 @@ void ButtonCellRenderer::renderProperties(const ButtonCell* pButtonCell, const s
Utility::writeRenderableProperties(pButtonCell, ostr);
if (!pButtonCell->isEnabled())
ostr << ",disabled:true";
View* pOwner = pButtonCell->getOwner();
Button* pOwner = dynamic_cast<Button*>(pButtonCell->getOwner());
if (pOwner)
{
if (!pOwner->getName().empty())
@ -98,11 +105,21 @@ void ButtonCellRenderer::renderProperties(const ButtonCell* pButtonCell, const s
ostr << ",minWidth:" << pOwner->getWidth();
if (!pOwner->isVisible())
ostr << ",hidden:true";
if (!pOwner->buttonClicked.jsDelegates().empty())
{
ostr << ",listeners:{";
Utility::writeJSEvent(ostr, EV_CLICK, pOwner->buttonClicked.jsDelegates());
ostr << "}";
}
}
std::string toolTip(pButtonCell->getToolTip());
if (!toolTip.empty())
ostr << ",tooltip:'" << Utility::safe(toolTip) << "'";
WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(pButtonCell->id()), const_cast<ButtonCell*>(pButtonCell));
}
@ -120,4 +137,15 @@ void ButtonCellRenderer::writeConfigData(const Cell* pCell, const RenderContext&
}
void ButtonCellRenderer::addClickServerCallback(Button* pButton, const std::string& onSuccess, const std::string& onFailure)
{
//click : ( Button this, EventObject e )
static const std::string signature("function(but,e)");
std::map<std::string, std::string> addParams;
addParams.insert(std::make_pair(RequestHandler::KEY_EVID, ButtonCell::EV_BUTTONCLICKED));
Utility::addServerCallback(pButton->buttonClicked, signature, addParams, pButton->getCell()->id(), onSuccess, onFailure);
}
} } } // namespace Poco::WebWidgets::ExtJS

View File

@ -68,7 +68,7 @@ ComboBoxCellRenderer::~ComboBoxCellRenderer()
void ComboBoxCellRenderer::addSelectedServerCallback(ComboBox* pCombo, const std::string& onSuccess, const std::string& onFailure)
{
//select : ( Ext.form.ComboBox combo, Ext.data.Record record, Number index )
static const std::string signature("function(combo,rec, idx)");
static const std::string signature("function(combo,rec,idx)");
std::map<std::string, std::string> addParams;
addParams.insert(std::make_pair(ComboBoxCell::FIELD_VAL, "+rec.get('d')"));
addParams.insert(std::make_pair(RequestHandler::KEY_EVID, ComboBoxCell::EV_SELECTED));

View File

@ -54,6 +54,8 @@ class WebWidgets_API ButtonCell: public Cell
public:
typedef Poco::AutoPtr<ButtonCell> Ptr;
static const std::string EV_BUTTONCLICKED;
Delegate buttonClicked;
ButtonCell(View* pOwner);

View File

@ -161,16 +161,10 @@ public:
///
/// The default implementation does nothing.
virtual void handleRequest(const Poco::Net::HTTPServerRequest& request);
/// Handles a complete HTTP request submitted by the client.
virtual void handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response);
/// Handles a complete AJAX request submitted by the client.
///
/// The default implementation does nothing.
virtual void handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
/// Handles a complete HTTP request submitted by the client. Also takes care of handing the response,
/// e.g. if one wants to send back data.
///
/// The default implementation does nothing (except calling handleRequest and response.send()).
/// The default implementation does nothing and sends an HTTP Status 200 OK
protected:
Cell(View* pOwner, const std::type_info& type);

View File

@ -102,20 +102,15 @@ public:
void handleForm(const std::string& field, const std::string& value);
void handleRequest(const Poco::Net::HTTPServerRequest& request);
/// Handles a complete HTTP request submitted by the client.
void handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response);
/// Handles a complete AJAX request submitted by the client.
void handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
/// Handles a complete HTTP request submitted by the client. Also takes care of handling the response,
/// e.g. if one wants to send back data.
protected:
~ComboBoxCell();
/// Destroys the ComboBoxCell.
private:
std::vector<Any> _elements;
std::string _ev;
};

View File

@ -45,7 +45,7 @@
namespace Poco {
namespace Net {
class HTTPServerRequest;
class NameValueCollection;
class HTTPServerResponse;
} }
@ -62,12 +62,8 @@ public:
virtual void handleForm(const std::string& field, const std::string& value) = 0;
/// Handles a form field submitted by the client.
virtual void handleRequest(const Poco::Net::HTTPServerRequest& request) = 0;
/// Handles a complete HTTP request submitted by the client.
virtual void handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) = 0;
/// Handles a complete HTTP request submitted by the client. Also takes care of handing the response,
/// e.g. if one wants to send back data.
virtual void handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response) = 0;
/// Handles a complete AJAX request submitted by the client.
protected:
RequestProcessor();

View File

@ -133,24 +133,9 @@ public:
void handleForm(const std::string& field, const std::string& value);
/// Handles a form field submitted by the client.
void handleRequest(const Poco::Net::HTTPServerRequest& request);
/// Handles a complete HTTP request submitted by the client.
void handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response);
/// Handles a complete HTTP request submitted by the client. Also takes care of handling the response,
/// e.g. if one wants to send back data.
void handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response);
/// Handles a complete AJAX request submitted by the client.
private:
void handleValueChanged();
///Applies the update to the table
void handleCellClicked();
///handles cell clicked
void handleCol(const std::string& val);
void handleRow(const std::string& val);
void handleVal(const std::string& val);
void handleCnt(const std::string& val);
protected:
Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel);
/// Creates a Table and assigns it the given name.
@ -167,11 +152,6 @@ protected:
private:
TableModel::Ptr _pModel;
TableColumns _columns;
int _col;
int _row;
int _cnt;
std::string _val;
std::string _ev;
};

View File

@ -42,6 +42,9 @@ namespace Poco {
namespace WebWidgets {
const std::string ButtonCell::EV_BUTTONCLICKED("click");;
ButtonCell::ButtonCell(View* pOwner, const std::type_info& type):
Cell(pOwner, type)
{

View File

@ -175,14 +175,8 @@ void Cell::handleForm(const std::string& field, const std::string& value)
}
void Cell::handleRequest(const Poco::Net::HTTPServerRequest& request)
void Cell::handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response)
{
}
void Cell::handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
handleRequest(request);
response.send();
}

View File

@ -77,37 +77,31 @@ void ComboBoxCell::erase(const Any& elem)
void ComboBoxCell::handleForm(const std::string& field, const std::string& value)
{
if (field == FIELD_VAL)
Formatter::Ptr pForm(getFormatter());
if (pForm)
{
Formatter::Ptr pForm(getFormatter());
if (pForm)
{
setSelected(pForm->parse(value));
}
setSelected(pForm->parse(value));
}
else if (field == RequestHandler::KEY_EVID)
_ev = value;
}
void ComboBoxCell::handleRequest(const Poco::Net::HTTPServerRequest& request)
{
//ev selected already handled in handleForm
}
void ComboBoxCell::handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
void ComboBoxCell::handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response)
{
// RequestHandler has already called all the handeForm stuff
if (_ev == EV_LOAD)
const std::string& ev = args[RequestHandler::KEY_EVID];
if (ev == EV_LOAD)
{
Poco::Net::HTTPServerResponse* pResponse = &response;
beforeLoad.notify(this, pResponse);
}
else
else if (ev == EV_SELECTED)
{
handleRequest(request);
Formatter::Ptr pForm(getFormatter());
if (pForm)
{
setSelected(pForm->parse(args[FIELD_VAL]));
}
response.send();
}
}

View File

@ -126,15 +126,9 @@ void RequestHandler::handleAjaxRequest(Poco::Net::HTTPServerRequest& request, Po
return;
}
for (;it != args.end(); ++it)
{
const std::string& key = it->first;
const std::string& val = it->second;
pProc->handleForm(key, val);
}
try
{
pProc->handleRequestAndResponse(request, response);
pProc->handleAjaxRequest(args, response);
}
catch(...)
{

View File

@ -56,12 +56,7 @@ const std::string Table::EV_LOADDATA("load");
Table::Table(const TableColumns& tc, TableModel::Ptr pModel):
View(typeid(Table)),
_pModel(pModel),
_columns(tc),
_col(-1),
_row(-1),
_cnt(-1),
_val(),
_ev()
_columns(tc)
{
checkValidConfig();
}
@ -70,12 +65,7 @@ Table::Table(const TableColumns& tc, TableModel::Ptr pModel):
Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel):
View(name, typeid(Table)),
_pModel(pModel),
_columns(tc),
_col(-1),
_row(-1),
_cnt(-1),
_val(),
_ev()
_columns(tc)
{
checkValidConfig();
}
@ -84,12 +74,7 @@ Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pM
Table::Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel):
View(name, type),
_pModel(pModel),
_columns(tc),
_col(-1),
_row(-1),
_cnt(-1),
_val(),
_ev()
_columns(tc)
{
checkValidConfig();
}
@ -98,12 +83,7 @@ Table::Table(const std::string& name, const std::type_info& type, const TableCol
Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel):
View(type),
_pModel(pModel),
_columns(tc),
_col(-1),
_row(-1),
_cnt(-1),
_val(),
_ev()
_columns(tc)
{
checkValidConfig();
}
@ -134,113 +114,61 @@ void Table::checkValidConfig()
void Table::handleForm(const std::string& field, const std::string& value)
{
if (field == FIELD_COL)
handleCol(value);
else if (field == FIELD_ROW)
handleRow(value);
else if (field == FIELD_VAL)
handleVal(value);
else if (field == FIELD_CNT)
handleCnt(value);
else if (field == RequestHandler::KEY_EVID)
_ev = value;
// Form fields from a table?
}
void Table::handleRequest(const Poco::Net::HTTPServerRequest& req)
void Table::handleAjaxRequest(const Poco::Net::NameValueCollection& args, Poco::Net::HTTPServerResponse& response)
{
if (_ev == EV_CELLVALUECHANGED)
handleValueChanged();
else if (_ev == EV_CELLCLICKED)
handleCellClicked();
_col = -1;
_row = -1;
_cnt = -1;
_val.clear();
_ev.clear();
}
void Table::handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
// RequestHandler has already called all the handeForm stuff
if (_ev == EV_LOADDATA)
static const std::string strZero("0");
static const std::string strMin1("-1");
const std::string& ev = args[RequestHandler::KEY_EVID];
const std::string& strRow = args.get(FIELD_ROW, strMin1);
const std::string& strCol = args.get(FIELD_COL, strMin1);
const std::string& strCnt = args.get(FIELD_CNT, strZero);
int row(-1);
int cnt(-1);
int col(-1);
Poco::NumberParser::tryParse(strRow, row);
Poco::NumberParser::tryParse(strCol, col);
Poco::NumberParser::tryParse(strCnt, cnt);
if (ev == EV_LOADDATA)
{
/// serialize the Table back
/// check for cnt and start if only a segment was requested
if (_row < 0)
_row = 0;
if (_cnt < 0)
_cnt = 0;
LoadData ld(&response, this, _row, _cnt);
_col = -1;
_row = -1;
_cnt = -1;
_val.clear();
_ev.clear();
/// check for cnt and start if only a segment was requested
if (row < 0)
row = 0;
if (cnt < 0)
cnt = 0;
LoadData ld(&response, this, row, cnt);
beforeLoad.notify(this, ld);
}
else
else if (ev == EV_CELLCLICKED)
{
handleRequest(request);
if (col < 0 || row < 0 || col >= getColumnCount())
throw InvalidArgumentException("col/row out of range");
CellClick ev(row, col);
cellClicked(this, ev);
response.send();
}
}
void Table::handleValueChanged()
{
if (_col < 0 || _row < 0 || _col >= getColumnCount())
throw InvalidArgumentException("col/row out of range");
Cell::Ptr pCell = getColumns()[_col]->getCell();
Formatter::Ptr pForm;
if (pCell)
pForm = pCell->getFormatter();
if (pForm)
else if (ev == EV_CELLVALUECHANGED)
{
Poco::Any any = pForm->parse(_val);
setValue(any, _row, _col);
if (col < 0 || row < 0 || col >= getColumnCount())
throw InvalidArgumentException("col/row out of range");
const std::string& val = args.get(FIELD_VAL);
Cell::Ptr pCell = getColumns()[col]->getCell();
Formatter::Ptr pForm;
if (pCell)
pForm = pCell->getFormatter();
if (pForm)
{
Poco::Any any = pForm->parse(val);
setValue(any, row, col);
}
else
setValue(Poco::Any(val), row, col);
response.send();
}
else
setValue(Poco::Any(_val), _row, _col);
}
void Table::handleCellClicked()
{
if (_col < 0 || _row < 0 || _col >= getColumnCount())
throw InvalidArgumentException("col/row out of range");
CellClick ev(_row, _col);
cellClicked(this, ev);
}
void Table::handleCol(const std::string& val)
{
_col = Poco::NumberParser::parse(val);
}
void Table::handleRow(const std::string& val)
{
_row = Poco::NumberParser::parse(val);
}
void Table::handleVal(const std::string& val)
{
// we have no guarantee that _col is set at this timepoint
// so we cannot get the formatter for the row
// we do the conversion later in apply
_val = val;
}
void Table::handleCnt(const std::string& val)
{
_cnt = Poco::NumberParser::parse(val);
}