mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-18 11:39:00 +02:00
added WebWidgets
This commit is contained in:
256
WebWidgets/ExtJS/src/TableRenderer.cpp
Normal file
256
WebWidgets/ExtJS/src/TableRenderer.cpp
Normal file
@@ -0,0 +1,256 @@
|
||||
//
|
||||
// TableRenderer.cpp
|
||||
//
|
||||
// $Id: //poco/Main/WebWidgets/ExtJS/src/TableRenderer.cpp#4 $
|
||||
//
|
||||
// Library: ExtJS
|
||||
// Package: Core
|
||||
// Module: TableRenderer
|
||||
//
|
||||
// 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/TableRenderer.h"
|
||||
#include "Poco/WebWidgets/ExtJS/FormRenderer.h"
|
||||
#include "Poco/WebWidgets/ExtJS/Utility.h"
|
||||
#include "Poco/WebWidgets/ExtJS/TableCellHandlerFactory.h"
|
||||
#include "Poco/WebWidgets/Table.h"
|
||||
#include "Poco/WebWidgets/WebApplication.h"
|
||||
#include "Poco/WebWidgets/RequestHandler.h"
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace WebWidgets {
|
||||
namespace ExtJS {
|
||||
|
||||
|
||||
TableRenderer::TableRenderer()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TableRenderer::~TableRenderer()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderHead(const Renderable* pRenderable, const RenderContext& context, std::ostream& ostr)
|
||||
{
|
||||
poco_assert_dbg (pRenderable != 0);
|
||||
poco_assert_dbg (pRenderable->type() == typeid(Poco::WebWidgets::Table));
|
||||
const Table* pTable = static_cast<const Poco::WebWidgets::Table*>(pRenderable);
|
||||
|
||||
ostr << "new Ext.grid.EditorGridPanel({";
|
||||
|
||||
TableRenderer::renderProperties(pTable, context, ostr);
|
||||
|
||||
ostr << "})";
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderBody(const Renderable* pRenderable, const RenderContext& context, std::ostream& ostr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderProperties(const Table* pTable, const RenderContext& context, std::ostream& ostr)
|
||||
{
|
||||
Utility::writeRenderableProperties(pTable, ostr);
|
||||
WebApplication& app = WebApplication::instance();
|
||||
Renderable::ID id = app.getCurrentPage()->id();
|
||||
// add an afterEdit handler
|
||||
// event handlers can be defined in the constructor after the listeners member
|
||||
//{
|
||||
// 'afterEdit' : {
|
||||
// fn: function(obj){obj.row, obj.column, obj.value, tableId}
|
||||
//},
|
||||
|
||||
std::ostringstream uri;
|
||||
uri << "'" <<app.getURI().toString();
|
||||
uri << ";"; //mark as AJAX request
|
||||
uri << RequestHandler::KEY_ID << "=" << id << "&";
|
||||
uri << Table::FIELD_COL << "='+obj.column+'&";
|
||||
uri << Table::FIELD_ROW << "='+obj.row+'&" << Table::FIELD_VAL << "='+obj.value";
|
||||
//obj.commitChanges... hides the red triangle
|
||||
ostr << ", listeners: {'afteredit':{fn:function(obj){var uri=" << uri.str() << ";obj.grid.getStore().commitChanges();";
|
||||
ostr << "Ext.Ajax.request({url:uri,";
|
||||
ostr << "failure:function(){Ext.MessageBox.alert('Status','Failed to write changes back to server.');}});}}},";
|
||||
|
||||
renderColumns(pTable, context, ostr);
|
||||
ostr << ",clicksToEdit:1,store:";
|
||||
renderStore(pTable, ostr);
|
||||
app.registerAjaxProcessor(Poco::NumberFormatter::format(id), const_cast<Table*>(pTable));
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderColumns(const Table* pTable, const RenderContext& context, std::ostream& ostr)
|
||||
{
|
||||
|
||||
//columns: [...]
|
||||
ostr << "columns:[";
|
||||
const Table::TableColumns& columns = pTable->getColumns();
|
||||
Table::TableColumns::const_iterator it = columns.begin();
|
||||
int i = 0;
|
||||
for (; it != columns.end(); ++it, ++i)
|
||||
{
|
||||
if (i != 0)
|
||||
ostr << ",";
|
||||
|
||||
renderColumn(pTable, *(*it), i, context, ostr);
|
||||
}
|
||||
ostr << "]";
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderColumn(const Table* pTable, const TableColumn& tc, int idx, const RenderContext& context, std::ostream& ostr)
|
||||
{
|
||||
static LookAndFeel& laf = Utility::getDefaultRenderers();
|
||||
|
||||
// {id:'company', header: "Company", width: 200, sortable: true, dataIndex: 'company'}
|
||||
// {header: "Last Updated", width: 135, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
|
||||
ostr << "{";
|
||||
std::string hdr(Utility::safe(tc.getHeader()));
|
||||
|
||||
ostr << "header:'" << hdr << "',dataIndex:'" << idx << "'";
|
||||
|
||||
if (tc.getWidth() > 0)
|
||||
ostr << ",width:" << tc.getWidth();
|
||||
if (tc.isSortable())
|
||||
ostr << ",sortable:true";
|
||||
|
||||
static TableCellHandlerFactory& fty = TableCellHandlerFactory::instance();
|
||||
|
||||
if (tc.getCell())
|
||||
{
|
||||
AbstractTableCellHandler::Ptr pHandler = fty.factory(tc.getCell());
|
||||
if (tc.getCell()->isEditable())
|
||||
ostr << ",editable:true";
|
||||
if (tc.getCell()->isEditable() && pHandler->useEditor())
|
||||
{
|
||||
ostr << ",editor:";
|
||||
tc.getCell()->renderHead(context, ostr);
|
||||
}
|
||||
if (pHandler->useRenderer())
|
||||
{
|
||||
ostr << ",renderer:";
|
||||
pHandler->writeDynamicData(ostr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ostr << "}";
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderDataModel(const Table* pTable, std::ostream& ostr)
|
||||
{
|
||||
//[
|
||||
//[ ['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();
|
||||
ostr << "[";
|
||||
for (std::size_t row = 0; row < rowCnt; ++row)
|
||||
{
|
||||
if (row != 0)
|
||||
ostr << ",[";
|
||||
else
|
||||
ostr << "[";
|
||||
for (std::size_t col = 0; col < colCnt; ++col)
|
||||
{
|
||||
if (col != 0)
|
||||
ostr << ",";
|
||||
|
||||
//FIXME: 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)
|
||||
ostr << tc[col]->getCell()->getFormatter()->format(aVal);
|
||||
else
|
||||
; //FIXME:
|
||||
}
|
||||
}
|
||||
ostr << "]";
|
||||
}
|
||||
ostr << "]";
|
||||
}
|
||||
|
||||
|
||||
void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
|
||||
{
|
||||
//new Ext.data.SimpleStore({
|
||||
// fields: [
|
||||
// {name: 'company'},
|
||||
// {name: 'price', type: 'float'},
|
||||
// {name: 'change', type: 'float'},
|
||||
// {name: 'pctChange', type: 'float'},
|
||||
// {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
|
||||
// ],
|
||||
// data: [...]
|
||||
//});
|
||||
|
||||
// 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:[";
|
||||
Table::TableColumns::const_iterator it = columns.begin();
|
||||
int i = 0;
|
||||
for (; it != columns.end(); ++it, ++i)
|
||||
{
|
||||
if (i != 0)
|
||||
ostr << ",";
|
||||
ostr << "{name:'" << i << "'}";
|
||||
}
|
||||
ostr << "],"; // close fields
|
||||
//Write data
|
||||
ostr << "data:";
|
||||
renderDataModel(pTable, ostr);
|
||||
ostr << "})";
|
||||
}
|
||||
|
||||
|
||||
|
||||
} } } // namespace Poco::WebWidgets::ExtJS
|
Reference in New Issue
Block a user