Commit b04ee44a authored by Ivan Tyagov's avatar Ivan Tyagov

Make ti possible that 2+ gadgets having same preference fields form work in...

Make ti possible that 2+ gadgets having same preference fields form work in ERP5 like one HTML form name space without conflicting each other (i.e. if two fields for 'field_my_title' exists they are transmitted as a list of two values, now no longer). Trick is to use unique form field prefixes for every gadget preference form.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33683 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ff956a8f
...@@ -53,36 +53,43 @@ ...@@ -53,36 +53,43 @@
</item> </item>
<item> <item>
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string># XXX: How validation\n <value> <string>"""\n
This script edits a Knowledge Box instance used for saving a Gadget preferences.\n
"""\n
# XXX: How validation\n
kw = {}\n kw = {}\n
request = context.REQUEST\n request = context.REQUEST\n
form = request.form\n form = request.form\n
fields = filter(lambda x: x.startswith(\'field_\'), form.keys())\n fields = filter(lambda x: x.startswith(form_fields_main_prefix), form.keys())\n
box_relative_url = request[\'box_relative_url\']\n
box = context.restrictedTraverse(box_relative_url)\n box = context.restrictedTraverse(box_relative_url)\n
\n
# get interesting for us fields and save\n # get interesting for us fields and save\n
listbox_selection_field_prefix = \'%s_my_listbox_selection_\' %form_fields_main_prefix\n
for field in fields:\n for field in fields:\n
#if it\'s a fied in a lisbox gadget it modifies directly the selection\n #if it\'s a fied in a lisbox gadget it modifies directly the selection\n
if field.startswith(\'field_my_listbox_selection_\'):\n if field.startswith(listbox_selection_field_prefix):\n
portal_selection = getattr(context,\'portal_selections\')\n portal_selection = getattr(context, \'portal_selections\')\n
selection_name = context.Base_getListboxGadgetSelectionName(box_relative_url)\n selection_name = context.Base_getListboxGadgetSelectionName(box_relative_url)\n
selection = portal_selection.getSelectionFor(selection_name)\n selection = portal_selection.getSelectionFor(selection_name)\n
if selection is not None:\n if selection is not None:\n
params = selection.getParams()\n params = selection.getParams()\n
params[field.replace(\'field_my_listbox_selection_\',\'\')] = str(form[field])\n params[field.replace(listbox_selection_field_prefix, \'\')] = str(form[field])\n
portal_selection.setSelectionParamsFor(selection_name, params)\n portal_selection.setSelectionParamsFor(selection_name, params)\n
kw[field.replace(\'field_my_\', \'\')] = form[field]\n kw[field.replace(\'%s_my_\' %form_fields_main_prefix, \'\')] = form[field]\n
box.edit(**kw)\n \n
dynamic_field_name_for_cancel_url = \'%s_cancel_url\' %box_relative_url.replace(\'/\', \'_\')\n # determine redirect URL as passed from gadget preference form\n
if request.get(dynamic_field_name_for_cancel_url, None) is not None:\n redirect_url = kw.pop(\'%s_cancel_url\' %form_fields_main_prefix, None)\n
# cancel_url is passed in REQUEST\n if redirect_url is None:\n
redirect_url = request[dynamic_field_name_for_cancel_url]\n
else:\n
# taking URL1 as the base of the original URL. \n # taking URL1 as the base of the original URL. \n
# it works for both synchronous and asynchronous gadgets\n # it works for both synchronous and asynchronous gadgets\n
redirect_url = request[\'URL1\']\n redirect_url = request[\'URL1\']\n
\n
# edit\n
box.edit(**kw)\n
\n
request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
%(redirect_url, context.Base_translateString(\'Preference updated.\')))\n %(redirect_url, \n
context.Base_translateString(\'Preference updated.\')))\n
</string> </value> </string> </value>
</item> </item>
<item> <item>
...@@ -93,7 +100,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -93,7 +100,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>cancel_url=None</string> </value> <value> <string>form_fields_main_prefix, box_relative_url</string> </value>
</item> </item>
<item> <item>
<key> <string>errors</string> </key> <key> <string>errors</string> </key>
...@@ -113,13 +120,14 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -113,13 +120,14 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
<dictionary> <dictionary>
<item> <item>
<key> <string>co_argcount</string> </key> <key> <string>co_argcount</string> </key>
<value> <int>1</int> </value> <value> <int>2</int> </value>
</item> </item>
<item> <item>
<key> <string>co_varnames</string> </key> <key> <string>co_varnames</string> </key>
<value> <value>
<tuple> <tuple>
<string>cancel_url</string> <string>form_fields_main_prefix</string>
<string>box_relative_url</string>
<string>kw</string> <string>kw</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>context</string> <string>context</string>
...@@ -127,9 +135,8 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -127,9 +135,8 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
<string>form</string> <string>form</string>
<string>filter</string> <string>filter</string>
<string>fields</string> <string>fields</string>
<string>_getitem_</string>
<string>box_relative_url</string>
<string>box</string> <string>box</string>
<string>listbox_selection_field_prefix</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>field</string> <string>field</string>
<string>getattr</string> <string>getattr</string>
...@@ -139,10 +146,10 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -139,10 +146,10 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
<string>None</string> <string>None</string>
<string>params</string> <string>params</string>
<string>str</string> <string>str</string>
<string>_getitem_</string>
<string>_write_</string> <string>_write_</string>
<string>_apply_</string>
<string>dynamic_field_name_for_cancel_url</string>
<string>redirect_url</string> <string>redirect_url</string>
<string>_apply_</string>
</tuple> </tuple>
</value> </value>
</item> </item>
...@@ -154,9 +161,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -154,9 +161,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
<item> <item>
<key> <string>func_defaults</string> </key> <key> <string>func_defaults</string> </key>
<value> <value>
<tuple> <none/>
<none/>
</tuple>
</value> </value>
</item> </item>
<item> <item>
......
...@@ -115,38 +115,42 @@ ...@@ -115,38 +115,42 @@
i18n:domain="ui">Server side error.</span>\n i18n:domain="ui">Server side error.</span>\n
</tal:block>\n </tal:block>\n
\n \n
<tal:block tal:condition="python: edit_form_object is not None">\n <tal:block tal:condition="python: edit_form_object is not None"\n
<form action="KnowledgeBox_baseEdit"\n tal:define="box_relative_url python: box.getRelativeUrl();\n
tal:condition="not: is_asynchronous_gadget"\n form_fields_main_prefix python: \'gadget_preference_%s_field\' %box_relative_url.replace(\'/\', \'_\');">\n
tal:define="box_relative_url python: box.getRelativeUrl();">\n \n
<div>\n <!-- render edit gadget preferences form -->\n
<span tal:replace="structure python: edit_form_object()"/>\n <span tal:replace="structure python: edit_form_object(key_prefix=form_fields_main_prefix)"/>\n
<input type="hidden" \n \n
name="box_relative_url" \n <tal:block tal:condition="not: is_asynchronous_gadget">\n
tal:attributes="value box_relative_url"/>\n \n
<input type="hidden" \n <input type="hidden" \n
name="cancel_url" \n name="cancel_url" \n
tal:attributes="value cancel_url;\n tal:attributes="value cancel_url;\n
name python: \'%s_cancel_url\' \n name python: \'%s_cancel_url\' %form_fields_main_prefix"/>\n
%box_relative_url.replace(\'/\', \'_\')"/> \n <button type="submit"\n
<button type="submit"\n i18n:translate="" \n
i18n:translate="" \n i18n:domain="ui"\n
i18n:domain="ui" \n tal:attributes="onclick python: \n
name="KnowledgeBox_baseEdit:method">Save</button>\n \'submitSynchronousGadgetPreferenceForm(\\\'%s\\\',\\\'%s\\\')\' \n
</div>\n %(form_fields_main_prefix, box_relative_url)">Save</button>\n
</form>\n \n
</tal:block>\n
\n \n
<tal:block tal:condition="is_asynchronous_gadget">\n <tal:block tal:condition="is_asynchronous_gadget">\n
<span tal:replace="structure python: edit_form_object()"/>\n \n
<button type="button" \n <button type="button" \n
i18n:translate="" \n i18n:translate="" \n
i18n:domain="ui"\n i18n:domain="ui"\n
tal:attributes="onclick python: \n tal:attributes="onclick python: \n
\'sendPreferencesToServer(\\\'%s\\\', \\\'%s\\\', \\\'%s\\\', \\\'%s\\\')\'\n \'submitAsynchronousGadgetPreferenceForm(\n
%(edit_form_dom_id, \n \\\'%s\\\', \\\'%s\\\', \\\'%s\\\', \\\'%s\\\', \\\'%s\\\')\'\n
view_form_id, \n %(edit_form_dom_id, \n
box.getRelativeUrl(), \n view_form_id, \n
view_form_dom_id)">Save</button>\n box_relative_url, \n
view_form_dom_id,\n
form_fields_main_prefix)">Save</button>\n
\n
</tal:block>\n </tal:block>\n
</tal:block>\n </tal:block>\n
</tal:block>\n </tal:block>\n
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
</item> </item>
<item> <item>
<key> <string>_EtagSupport__etag</string> </key> <key> <string>_EtagSupport__etag</string> </key>
<value> <string>ts66498134.37</string> </value> <value> <string>ts68379872.09</string> </value>
</item> </item>
<item> <item>
<key> <string>__name__</string> </key> <key> <string>__name__</string> </key>
...@@ -157,12 +157,32 @@ function createDefaultPadOnServer(timeout, create_url,\n ...@@ -157,12 +157,32 @@ function createDefaultPadOnServer(timeout, create_url,\n
checkForActivitiesOnServer(timeout, return_url);};\n checkForActivitiesOnServer(timeout, return_url);};\n
};\n };\n
\n \n
function submitSynchronousGadgetPreferenceForm(\n
form_fields_main_prefix, \n
box_relative_url){\n
/* this will add respective gadget knowledge box relative url and\n
gadget ERP5 preference form field_prefix (so multiple gadgets can \n
safely coexist in one HTML page with one HTML form */\n
appendChildNodes(document.forms[0], \n
INPUT({\'value\':box_relative_url, \n
\'name\': \'box_relative_url\', \n
\'type\':\'hidden\'}));\n
appendChildNodes(document.forms[0], \n
INPUT({\'value\':form_fields_main_prefix, \n
\'name\': \'form_fields_main_prefix\', \n
\'type\':\'hidden\'}));\n
clickSaveButton(\'KnowledgeBox_baseEdit\');\n
};\n
\n \n
function sendPreferencesToServer(form_dom_id, view_form_url, \n function submitAsynchronousGadgetPreferenceForm(\n
box_relative_url, visual_block_dom_id){\n form_dom_id, \n
view_form_url, \n
box_relative_url, \n
visual_block_dom_id, \n
form_fields_main_prefix){\n
// iterate over all possible form elements within edit form,\n // iterate over all possible form elements within edit form,\n
// collect them and send to server\n // collect them and send to server\n
var request_str = "?box_relative_url=" + box_relative_url+ \'&\';\n var request_str = "?box_relative_url=" + box_relative_url+ "&form_fields_main_prefix=" + form_fields_main_prefix + "&";\n
var url="KnowledgeBox_baseEdit";\n var url="KnowledgeBox_baseEdit";\n
forEach(getElementsByTagAndClassName(\'input\', null, form_dom_id) , function (item) {\n forEach(getElementsByTagAndClassName(\'input\', null, form_dom_id) , function (item) {\n
if (item.type == "checkbox"){\n if (item.type == "checkbox"){\n
...@@ -379,7 +399,7 @@ MochiKit.DOM.addLoadEvent(initialize);\n ...@@ -379,7 +399,7 @@ MochiKit.DOM.addLoadEvent(initialize);\n
</item> </item>
<item> <item>
<key> <string>size</string> </key> <key> <string>size</string> </key>
<value> <long>13542</long> </value> <value> <long>14577</long> </value>
</item> </item>
<item> <item>
<key> <string>title</string> </key> <key> <string>title</string> </key>
......
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
global preferred_html_style_translator_mode \n global preferred_html_style_translator_mode \n
preferred_html_style_developper_mode;\n preferred_html_style_developper_mode;\n
original_editable_mode python: request.get(\'editable_mode\', 0);\n original_editable_mode python: request.get(\'editable_mode\', 0);\n
dummy python: request.set(\'editable_mode\',1);">\n dummy python: request.set(\'editable_mode\', 1);">\n
<!-- Set unconditionally editable_mode to True and return back to original after\n <!-- Set unconditionally editable_mode to True and return back to original after\n
form is rendered. -->\n form is rendered. -->\n
<tal:block metal:use-macro="here/form_render/macros/master" />\n <tal:block metal:use-macro="here/form_render/macros/master" />\n
......
485 488
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment