From 5bd23e62b36d1badfb973b6d07b14317f3d6dfc6 Mon Sep 17 00:00:00 2001 From: Peter Schojer Date: Mon, 25 Aug 2008 11:09:04 +0000 Subject: [PATCH] fixed Imagebutton click event --- .../include/Poco/WebWidgets/ExtJS/Utility.h | 25 +++ .../ExtJS/src/ImageButtonCellRenderer.cpp | 14 +- WebWidgets/ExtJS/src/Utility.cpp | 143 ++++++++++++------ 3 files changed, 133 insertions(+), 49 deletions(-) diff --git a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/Utility.h b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/Utility.h index b780f3bd9..d7ba51b11 100644 --- a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/Utility.h +++ b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/Utility.h @@ -168,7 +168,32 @@ public: const std::string& onSuccessJS, const std::string& onFailureJS); + template + static bool writeFunctionCode(std::ostream& out, const std::string& varName, const JavaScriptEvent& ev, CreateServerCallbackFct fct, const Param* p) + { + if (!ev.hasJavaScriptCode()) + return false; + if (ev.willDoServerCallback()) + return writeFunctionCode(out, varName, ev.jsDelegates(), (*fct)(p), ev.getServerCallbackPos()); + return writeFunctionCode(out, varName, ev.jsDelegates()); + } + private: + static bool writeFunctionCode(std::ostream& out, + const std::string& varName, + const std::list& delegates, + const Poco::WebWidgets::JSDelegate& serverCallback, + std::size_t serverCallPos); + + static bool writeFunctionCode(std::ostream& out, + const std::string& varName, + const std::list& delegates); + + static std::list Utility::insertServerCallback(const std::list& delegates, + const Poco::WebWidgets::JSDelegate& serverCallback, + std::size_t serverCallPos); + + static void convertPocoDateToPHPDate(char in, std::string& result); static void convertPHPDateToPocoDate(char in, std::string& result); static void escapeCharForPHP(char in, std::string& result); diff --git a/WebWidgets/ExtJS/src/ImageButtonCellRenderer.cpp b/WebWidgets/ExtJS/src/ImageButtonCellRenderer.cpp index b0f561004..97252572f 100644 --- a/WebWidgets/ExtJS/src/ImageButtonCellRenderer.cpp +++ b/WebWidgets/ExtJS/src/ImageButtonCellRenderer.cpp @@ -38,6 +38,7 @@ #include "Poco/WebWidgets/ExtJS/FormRenderer.h" #include "Poco/WebWidgets/ExtJS/Utility.h" #include "Poco/WebWidgets/ImageButtonCell.h" +#include "Poco/WebWidgets/ImageButton.h" namespace Poco { @@ -95,11 +96,20 @@ void ImageButtonCellRenderer::writeHTML(const ImageButtonCell* pButtonCell, bool std::string tooltip = ptrImg->getToolTip(); if (!tooltip.empty()) ostr << " alt=\"" << Utility::safe(tooltip) << "\""; + const Poco::WebWidgets::ImageButton* pOwner = static_cast(pButtonCell->getOwner()); + poco_check_ptr (pOwner); + if (pOwner->buttonClicked.hasJavaScriptCode()) + { + ostr << " onclick=\""; + //FIXME: this will only work without params! + Utility::writeFunctionCode(ostr, "fct", pOwner->buttonClicked, &ButtonCellRenderer::createClickServerCallback, pOwner); + ostr << "\""; + } ostr << " type=\"image\"/>"; ostr << ""; - std::string txt(pButtonCell->getOwner()->getText()); + std::string txt(pOwner->getText()); if (showTxt && !txt.empty()) - ostr << "
" << Utility::safe(pButtonCell->getOwner()->getText()) << "
"; + ostr << "
" << Utility::safe(pButtonCell->getOwner()->getText()) << "
"; ostr << "'"; } diff --git a/WebWidgets/ExtJS/src/Utility.cpp b/WebWidgets/ExtJS/src/Utility.cpp index 7582325b2..a58093ad8 100644 --- a/WebWidgets/ExtJS/src/Utility.cpp +++ b/WebWidgets/ExtJS/src/Utility.cpp @@ -428,58 +428,15 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons out << "'" << eventName << "':{"; if (delegates.size() == 1) - out << "fn:" << delegates.begin()->jsCode() << ""; + out << "fn:" << delegates.begin()->jsCode(); else { // rather simple way to support more than one delegate std::ostringstream invoke; 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::const_iterator it = delegates.begin(); - int cnt(0); - for (; it != delegates.end(); ++it, ++cnt) - { - std::string myFctName("d"); - myFctName.append(Poco::NumberFormatter::format(cnt)); - out << myFctName << ":"; - - std::string fctName; - std::vector params; - std::string code; - analyzeFunction(*it, fctName, params, code); - if (fctName == "function") - { - // an inline definition, we must have code - if (!code.empty()) - { - writeFunction(out, fctName, params, code); - } - } - 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 << "}"; - out << invoke.str() << "};"; //closes all - - out << "all." << createFunctionSignature("invoke", maxParams) << ";"; + out << "fn:" << fct << "{"; + writeFunctionCode(out, "all", delegates); out << "}"; //closes fn } @@ -495,6 +452,89 @@ bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, cons } +bool Utility::writeFunctionCode(std::ostream& out, + const std::string& varName, + const std::list& delegates, + const Poco::WebWidgets::JSDelegate& serverCallback, + std::size_t serverCallPos) +{ + return writeFunctionCode(out, varName, insertServerCallback(delegates, serverCallback, serverCallPos)); +} + + +bool Utility::writeFunctionCode(std::ostream& out, + const std::string& varName, + const std::list& delegates) +{ + if (delegates.empty()) + return false; + + if (delegates.size() == 1) + { + out << delegates.begin()->jsCode(); + return true; + } + + // rather simple way to support more than one delegate + std::ostringstream invoke; + int maxParams = detectMaxParamCount(delegates); + out << "var " << varName << "={"; + // the invoke function calls all the other functions sequentially + std::string fct(createFunctionSignature(maxParams)); + invoke << "invoke:" << fct <<"{"; + std::list::const_iterator it = delegates.begin(); + int cnt(0); + for (; it != delegates.end(); ++it, ++cnt) + { + std::string myFctName("d"); + myFctName.append(Poco::NumberFormatter::format(cnt)); + out << myFctName << ":"; + + std::string fctName; + std::vector params; + std::string code; + analyzeFunction(*it, fctName, params, code); + if (fctName == "function") + { + // an inline definition, we must have code + if (!code.empty()) + { + writeFunction(out, fctName, params, code); + } + } + else + { + if (code.empty()) + { + // a reference to another js function + //maybe the user defined params too -> ignore them + // more likely the user defined constant values! + if (params.empty()) + out << fctName; + else + { + params.clear(); + writeFunction(out, "function", params, it->jsCode()); + } + } + 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 << "}"; + out << invoke.str() << "};"; //closes varName + //FIXME: this will only work with no args when not called from writeJSEvent + out << varName << "." << createFunctionSignature("invoke", maxParams) << ";"; + return true; +} + + bool Utility::writeJSEvent(std::ostream& out, const std::string& eventName, const std::list& delegates, @@ -502,6 +542,14 @@ bool Utility::writeJSEvent(std::ostream& out, std::size_t serverCallPos, int delayTime, bool group) +{ + return writeJSEvent(out, eventName, insertServerCallback(delegates, serverCallback, serverCallPos), delayTime, group); +} + + +std::list Utility::insertServerCallback(const std::list& delegates, + const Poco::WebWidgets::JSDelegate& serverCallback, + std::size_t serverCallPos) { // TODO: we can optimize here a bit by avoiding the copy std::list dels; @@ -518,7 +566,8 @@ bool Utility::writeJSEvent(std::ostream& out, } if (!written) dels.push_back(serverCallback); - return writeJSEvent(out, eventName, dels, delayTime, group); + + return dels; }