table performance updates+JSEvent handling changes

This commit is contained in:
Peter Schojer
2008-05-20 13:58:01 +00:00
parent ab07e2cbba
commit 7a0aa6987c
21 changed files with 697 additions and 150 deletions

View File

@@ -0,0 +1,127 @@
//
// ArrayTableSerializer.cpp
//
// $Id: //poco/Main/WebWidgets/src/ArrayTableSerializer.cpp#3 $
//
// Library: ExtJS
// Package: Core
// Module: ArrayTableSerializer
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/WebWidgets/ExtJS/ArrayTableSerializer.h"
#include "Poco/WebWidgets/Table.h"
#include "Poco/DateTime.h"
namespace Poco {
namespace WebWidgets {
namespace ExtJS {
ArrayTableSerializer::ArrayTableSerializer()
{
}
ArrayTableSerializer::~ArrayTableSerializer()
{
}
void ArrayTableSerializer::serialize(std::ostream& ostr, const Table* pTable, std::size_t rowBegin, std::size_t rowCntUser)
{
//[
// ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
// ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am']
//]
const TableModel& tm = pTable->getModel();
const Table::TableColumns& tc = pTable->getColumns();
poco_assert_dbg (tc.size() == tm.getColumnCount());
std::size_t colCnt = tm.getColumnCount();
std::size_t rowCnt = tm.getRowCount();
if (rowBegin < 0)
rowBegin = 0;
if ((rowCntUser > 0) && (rowBegin + rowCntUser < rowCnt))
rowCnt = rowBegin + rowCntUser;
ostr << "[";
for (std::size_t row = rowBegin; row < rowCnt; ++row)
{
if (row != 0)
ostr << ",[";
else
ostr << "[";
for (std::size_t col = 0; col < colCnt; ++col)
{
if (col != 0)
ostr << ",";
// how do we distinguish if we want to write something as text or GUIElement?
// Example: Checkbutton can be written as text "true"/"false" or as a CheckButton
// we use the Cell: if we have a Cell set -> complex Type otherwise text
// -> already handled by the renderer!
const Poco::Any& aVal = tm.getValue(row, col);
if (aVal.empty())
ostr << "''";
else
{
//FIXME: we have no type nfo at all, assume string for everything
bool isString = (typeid(std::string) == aVal.type());
Cell::Ptr ptrCell = tc[col]->getCell();
if (isString)
ostr << "'" << RefAnyCast<std::string>(aVal) << "'";
else if (ptrCell)
{
//date must be written as string
if (typeid(Poco::DateTime) == aVal.type())
ostr << "'" << tc[col]->getCell()->getFormatter()->format(aVal) << "'";
else
ostr << tc[col]->getCell()->getFormatter()->format(aVal);
}
else
; //FIXME:
}
}
ostr << "]";
}
ostr << "]";
}
const std::string& ArrayTableSerializer::contentType() const
{
static const std::string ct("text/javascript");
return ct;
}
} } } // namespace Poco::WebWidgets::ExtJS

View File

@@ -50,6 +50,10 @@ namespace WebWidgets {
namespace ExtJS {
const std::string TableRenderer::EV_CELLCLICKED("cellclick");
const std::string TableRenderer::EV_AFTEREDIT("afteredit");
TableRenderer::TableRenderer()
{
}
@@ -79,33 +83,47 @@ void TableRenderer::renderBody(const Renderable* pRenderable, const RenderContex
}
void TableRenderer::renderProperties(const Table* pTable, const RenderContext& context, std::ostream& ostr)
void TableRenderer::addCellValueChangedServerCallback(Table* pTable, const std::string& onSuccess, const std::string& onFailure)
{
WebApplication& app = WebApplication::instance();
Renderable::ID id = app.getCurrentPage()->id();
Utility::writeRenderableProperties(pTable, ostr);
static const std::string afterEdit("afteredit");
static const std::string cellClicked("cellclick");
poco_check_ptr (pTable);
static const std::string signature("function(obj)");
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, "+obj.row"));
addParams.insert(std::make_pair(Table::FIELD_VAL, "+obj.value"));
addParams.insert(std::make_pair(RequestHandler::KEY_EVID, Table::EV_CELLVALUECHANGED));
JavaScriptEvent<int> ev;
ev.setJSDelegates(pTable->cellValueChanged.jsDelegates());
ev.add(jsDelegate("function(obj){obj.grid.getStore().commitChanges();}"));
ostr << ",listeners:{";
Utility::writeJSEventPlusServerCallback(ostr, afterEdit, ev.jsDelegates(), addParams, pTable->cellValueChanged.hasLocalHandlers());
//cellclick : ( Grid this, Number rowIndex, Number columnIndex, Ext.EventObject e )
//hm, more than one param in the eventhanlder of cellclick, writeJSEvent creates a fucntion(obj) wrapper
//FIXME: no support for custom javascript yet
addParams.clear();
addParams.insert(std::make_pair(Table::FIELD_COL, "+columnIndex"));
addParams.insert(std::make_pair(Table::FIELD_ROW, "+rowIndex"));
Utility::addServerCallback(pTable->cellValueChanged, signature, addParams, pTable->id(), onSuccess, onFailure);
pTable->cellValueChanged.add(jsDelegate("function(obj){obj.grid.getStore().commitChanges();}"));
}
void TableRenderer::addCellClickedServerCallback(Table* pTable, const std::string& onSuccess, const std::string& onFailure)
{
poco_check_ptr (pTable);
static const std::string signature("function(theGrid,row,col,e)");
std::map<std::string, std::string> addParams;
addParams.insert(std::make_pair(Table::FIELD_COL, "+col"));
addParams.insert(std::make_pair(Table::FIELD_ROW, "+row"));
addParams.insert(std::make_pair(RequestHandler::KEY_EVID, Table::EV_CELLCLICKED));
ostr << ",";
Utility::writeServerCallback(ostr,cellClicked, "function(aGrid, rowIndex,columnIndex,e)",addParams, pTable->cellClicked.hasLocalHandlers());
Utility::addServerCallback(pTable->cellClicked, signature, addParams, pTable->id(), onSuccess, onFailure);
}
void TableRenderer::renderProperties(const Table* pTable, const RenderContext& context, std::ostream& ostr)
{
WebApplication& app = WebApplication::instance();
Renderable::ID id = pTable->id();
Utility::writeRenderableProperties(pTable, ostr);
ostr << ",listeners:{";
bool written = Utility::writeJSEvent(ostr, EV_AFTEREDIT, pTable->cellValueChanged.jsDelegates());
if (written)
ostr << ",";
Utility::writeJSEvent(ostr, EV_CELLCLICKED, pTable->cellClicked.jsDelegates());
ostr << "},"; //close listeners
@@ -240,6 +258,7 @@ void TableRenderer::renderDataModel(const Table* pTable, std::ostream& ostr)
void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
{
//new Ext.data.SimpleStore({
// fields: [
// {name: 'company'},
@@ -248,13 +267,14 @@ void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
// {name: 'pctChange', type: 'float'},
// {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
// ],
// data: [...]
// proxy: new Ext.data.HttpProxy({url:'/myuri;...'}),
// reader: new Ext.data.ArrayReader()
//});
// we don't know the type, we just have a formatter, the name is always the idx!
// we use the formatter later to set a renderer for a different type than string
const Table::TableColumns& columns = pTable->getColumns();
ostr << "new Ext.data.SimpleStore({fields:[";
ostr << "new Ext.data.SimpleStore({autoLoad:true,fields:[";
Table::TableColumns::const_iterator it = columns.begin();
int i = 0;
for (; it != columns.end(); ++it, ++i)
@@ -264,9 +284,16 @@ void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
ostr << "{name:'" << i << "'}";
}
ostr << "],"; // close fields
ostr << "proxy: new Ext.data.HttpProxy({url:";
std::map<std::string, std::string> addParams;
addParams.insert(std::make_pair(RequestHandler::KEY_EVID,Table::EV_LOADDATA));
std::string url(Utility::createURI(addParams, pTable->id()));
ostr << url << "}),";
ostr << "reader: new Ext.data.ArrayReader()";
//Write data
ostr << "data:";
renderDataModel(pTable, ostr);
/*ostr << "data:";
renderDataModel(pTable, ostr);*/
ostr << "})";
}

View File

@@ -92,8 +92,10 @@
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/String.h"
#include "Poco/NumberFormatter.h"
#include "Poco/DateTimeFormat.h"
#include <sstream>
#include <cctype>
namespace Poco {
@@ -381,7 +383,7 @@ Form::Ptr Utility::insideForm(const Cell* pChild)
}
bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, const std::set<JSDelegate>& delegates)
bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, const std::list<JSDelegate>& delegates)
{
//'click' : {
// fn: this.onClick,
@@ -398,15 +400,30 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons
else
{
// rather simple way to support more than one delegate
// TODO: there sure is a more efficient way to do this
std::ostringstream invoke;
invoke << "invoke:function(" << JS_EVENTARGNAME << "){";
out << "{fn:function(" << JS_EVENTARGNAME << "){var all={";
writeCodeForDelegate(invoke, out, delegates);
int maxParams = detectMaxParamCount(delegates);
std::string fct(createFunctionSignature(maxParams));
out << "{fn:" << fct << "{var all={";
// the invoke function calls all the other functions sequentially
invoke << "invoke:" << fct <<"{";
std::list<JSDelegate>::const_iterator it = delegates.begin();
int cnt(0);
for (; it != delegates.end(); ++it)
{
std::string fctName("d");
fctName.append(Poco::NumberFormatter::format(cnt));
if (cnt > 0)
{
out << ",";
}
out << fctName << ":" << it->jsCode() << ","; //always write comma because invoke is written as the last method
invoke << "this." << createFunctionSignature(fctName, maxParams) << ";";
}
invoke << "}";
out << invoke.str() << "};"; //closes all
out << "all.invoke(" << JS_EVENTARGNAME << ");";
out << "all." << createFunctionSignature("invoke", maxParams) << ";";
out << "}"; //closes fn
out << "}"; //closes function
}
@@ -415,75 +432,11 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons
}
void Utility::writeCodeForDelegate(std::ostream& invoke, std::ostream& out, const std::set<JSDelegate>& jsDels)
{
int cnt(0);
std::set<JSDelegate>::const_iterator it = jsDels.begin();
for (; it != jsDels.end(); ++it)
{
writeCodeForDelegate(invoke, out, *it, cnt);
++cnt;
}
}
void Utility::writeCodeForDelegate(std::ostream& invoke, std::ostream& out, const JSDelegate& jsDel, int cnt)
{
std::string code(Poco::trim(jsDel.jsCode()));
if (code.find("function") == 0)
{
// inline function definition
out << "d" << cnt << ":" << code << ",";
invoke << "this.d" << cnt << "(" << JS_EVENTARGNAME << ");";
}
else
{
out << "d" << cnt << ":function(" << JS_EVENTARGNAME << "){" << code;
if (!code.empty() && code[code.size()-1] != ';')
out << ";";
out << "},";
invoke << "this.d" << cnt << "(" << JS_EVENTARGNAME << ");";
}
}
bool Utility::writeJSEventPlusServerCallback(std::ostream& out, const std::string& eventName, const std::set<JSDelegate>& delegates, const std::map<std::string, std::string>& addServerParams, bool reloadPage)
{
// rather simple way to support more than one delegate
// TODO: there sure is a more efficient way to do this
out << eventName << ":";
static const std::string defSignature("function("+JS_EVENTARGNAME+")");
std::ostringstream invoke;
invoke << "invoke:function(" << JS_EVENTARGNAME << "){";
out << "{fn:function(" << JS_EVENTARGNAME << "){var all={";
writeCodeForDelegate(invoke, out, delegates);
//write server callback
writeCodeForDelegate(invoke, out, jsDelegate(createCallbackFunctionCode(defSignature, addServerParams, reloadPage)), static_cast<int>(delegates.size()));
invoke << "}";
out << invoke.str() << "};"; //closes all
out << "all.invoke(" << JS_EVENTARGNAME << ");";
out << "}"; //closes fn
out << "}"; //closes function
return true;
}
bool Utility::writeServerCallback(std::ostream& out, const std::string& eventName, const std::string& signature, const std::map<std::string, std::string>& addServerParams, bool reloadPage)
{
std::string fctCode(createCallbackFunctionCode(signature, addServerParams, reloadPage));
out << eventName << ":";
out << fctCode;
return true;
}
std::string Utility::createURI(const std::map<std::string, std::string>& addParams)
std::string Utility::createURI(const std::map<std::string, std::string>& addParams, Renderable::ID id)
{
WebApplication& app = WebApplication::instance();
Renderable::ID id = app.getCurrentPage()->id();
std::ostringstream uri;
std::string theUri(app.getURI().toString());
if (theUri.empty())
@@ -529,26 +482,101 @@ std::string Utility::createURI(const std::map<std::string, std::string>& addPara
}
std::string Utility::createCallbackFunctionCode(const std::string& signature, const std::map<std::string, std::string>& addParams, bool reloadPage)
std::string Utility::createCallbackFunctionCode(const std::string& signature, const std::map<std::string, std::string>& addParams, Renderable::ID id, bool reloadPage)
{
static const std::string onSuccessReload("function(){window.location.reload();}");
static const std::string onFailureReload("function(){Ext.MessageBox.alert('Status','Failed to write changes back to server.');window.location.reload();}");
static const std::string onSuccess;
static const std::string onFailure("function(){Ext.MessageBox.alert('Status','Failed to write changes back to server.');}");
if (reloadPage)
return createCallbackFunctionCode(signature, addParams, id, onSuccessReload, onFailureReload);
return createCallbackFunctionCode(signature, addParams, id, onSuccess, onFailure);
}
std::string Utility::createCallbackFunctionCode(const std::string& signature, const std::map<std::string, std::string>& addParams, Renderable::ID id, const std::string& onSuccess, const std::string& onFailure)
{
poco_assert_dbg (!signature.empty());
poco_assert_dbg(signature.find("function") != std::string::npos);
poco_assert_dbg(signature.find("{") == std::string::npos);
std::string uri(createURI(addParams));
std::string uri(createURI(addParams, id));
// now add the callback code
std::ostringstream function;
function << signature;
function << "{var uri=" << uri << ";";
function << "Ext.Ajax.request({url:uri,";
if (reloadPage)
{
function << "success:function(){window.location.reload();},";
function << "failure:function(){Ext.MessageBox.alert('Status','Failed to write changes back to server.');window.location.reload();}});}";
}
else
function << "failure:function(){Ext.MessageBox.alert('Status','Failed to write changes back to server.');}});}";
function << "Ext.Ajax.request({url:uri";
if (!onSuccess.empty())
function << ",success:" << onSuccess;
if (!onFailure.empty())
function << ",failure:" << onFailure;
function << "});}";
return function.str();
}
int Utility::detectMaxParamCount(const std::list<JSDelegate>& delegates)
{
int cnt = 0;
std::list<JSDelegate>::const_iterator it = delegates.begin();
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;
}
}
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)
{
while (pos < code.size() && std::isspace(code[pos]))
++pos;
}
std::string Utility::createFunctionSignature(int paramCnt)
{
static const std::string fct("function");
return createFunctionSignature(fct, paramCnt);
}
std::string Utility::createFunctionSignature(const std::string& fctName, int paramCnt)
{
std::string result(fctName);
result.append("(");
while (paramCnt > 0)
{
result.append("p");
result.append(Poco::NumberFormatter::format(paramCnt));
--paramCnt;
if (paramCnt > 0)
result.append(",");
}
result.append(")");
return result;
}
} } } // namespace Poco::WebWidgets::ExtJS