From 92594d6858c3067c17b0707514aabe21533450d5 Mon Sep 17 00:00:00 2001
From: Gabriel Monnerat <gabriel@nexedi.com>
Date: Tue, 27 Apr 2021 04:02:22 +0200
Subject: [PATCH] erp5_web_renderjs_ui: Add action in ERP5JS UI to access
 update action script on backend

---
 .../ERP5Document_getHateoas.py                |  4 ++
 .../rjs_gadget_erp5_form_js.js                | 37 ++++++----
 .../rjs_gadget_erp5_form_js.xml               |  4 +-
 .../testDeveloperEditFormUpdateAction.xml     | 58 +++++++++++++++
 .../testDeveloperEditFormUpdateAction.zpt     | 72 +++++++++++++++++++
 5 files changed, 161 insertions(+), 14 deletions(-)
 create mode 100644 bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.xml
 create mode 100644 bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.zpt

diff --git a/bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py b/bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
index b9cbfe51e4..e18220c053 100644
--- a/bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
+++ b/bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
@@ -1248,6 +1248,10 @@ def renderFormDefinition(form, response_dict):
       response_dict["edit_form_action_href"] = '%s/%s/manage_main' % (
         site_root.getId(),
         form.action)
+    if form.update_action:
+      response_dict["edit_form_update_href"] = '%s/%s/manage_main' % (
+        site_root.getId(),
+        form.update_action)
 
 
 def statusLevelToString(level):
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.js b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.js
index f0deea7a8d..a324479f74 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.js
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.js
@@ -183,6 +183,10 @@
       if (options.form_definition.hasOwnProperty("edit_form_action_href")) {
         hash += "edit_form_action";
       }
+      if (options.form_definition.hasOwnProperty("edit_form_update_href")) {
+        hash += "edit_form_update_action";
+      }
+
       return this.changeState({
         erp5_document: options.erp5_document,
         form_definition: options.form_definition,
@@ -233,6 +237,7 @@
             dev_element_list,
             parent_element,
             field_href,
+            developer_action_mapping,
             j;
 
           if (modification_dict.hasOwnProperty('hash')) {
@@ -252,22 +257,30 @@
             for (j = 0; j < dev_element_list.length; j += 1) {
               form_gadget.element.removeChild(dev_element_list[j]);
             }
-            if (form_definition.hasOwnProperty("edit_form_href")) {
-              field_href = addDeveloperAction(
+            developer_action_mapping = {
+              "edit_form_href": [
                 "edit-form ui-icon-edit ui-btn-icon-left",
-                form_definition.edit_form_href,
                 "Edit this form"
-              );
-              form_gadget.element.insertBefore(field_href, dom_element);
-            }
-
-            if (form_definition.hasOwnProperty("edit_form_action_href")) {
-              field_href = addDeveloperAction(
+              ],
+              "edit_form_action_href": [
                 "edit-form-action ui-icon-external-link ui-btn-icon-left",
-                form_definition.edit_form_action_href,
                 "Edit this form's action"
-              );
-              form_gadget.element.insertBefore(field_href, dom_element);
+              ],
+              "edit_form_update_href": [
+                "edit-form-action ui-icon-external-link-square ui-btn-icon-left",
+                "Edit this form's update action"
+              ]
+            };
+            for (j in developer_action_mapping) {
+              if (developer_action_mapping.hasOwnProperty(j) &&
+                    form_definition.hasOwnProperty(j)) {
+                field_href = addDeveloperAction(
+                  developer_action_mapping[j][0],
+                  form_definition[j],
+                  developer_action_mapping[j][1]
+                );
+                form_gadget.element.insertBefore(field_href, dom_element);
+              }
             }
           }
         });
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.xml b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.xml
index 9c2df19590..6758403766 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.xml
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_form_js.xml
@@ -234,7 +234,7 @@
                   </item>
                   <item>
                       <key> <string>serial</string> </key>
-                      <value> <string>990.57432.47622.47940</string> </value>
+                      <value> <string>991.26405.47.57924</string> </value>
                   </item>
                   <item>
                       <key> <string>state</string> </key>
@@ -252,7 +252,7 @@
                           </tuple>
                           <state>
                             <tuple>
-                              <float>1616447651.73</float>
+                              <float>1618519262.64</float>
                               <string>UTC</string>
                             </tuple>
                           </state>
diff --git a/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.xml b/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.xml
new file mode 100644
index 0000000000..558ece678d
--- /dev/null
+++ b/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ZopePageTemplate" module="Products.PageTemplates.ZopePageTemplate"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>content_type</string> </key>
+            <value> <string>text/html</string> </value>
+        </item>
+        <item>
+            <key> <string>expand</string> </key>
+            <value> <int>0</int> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>testDeveloperEditFormUpdateAction</string> </value>
+        </item>
+        <item>
+            <key> <string>output_encoding</string> </key>
+            <value> <string>utf-8</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <unicode>Test Developer Edit Form Update Action</unicode> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.zpt b/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.zpt
new file mode 100644
index 0000000000..ad6fe7a944
--- /dev/null
+++ b/bt5/erp5_web_renderjs_ui_test/PathTemplateItem/portal_tests/renderjs_ui_developer_mode_zuite/testDeveloperEditFormUpdateAction.zpt
@@ -0,0 +1,72 @@
+<html xmlns:tal="http://xml.zope.org/namespaces/tal"
+      xmlns:metal="http://xml.zope.org/namespaces/metal">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>Test Fields without label</title>
+</head>
+<body>
+  <table cellpadding="1" cellspacing="1" border="1">
+    <thead>
+      <tr>
+        <td rowspan="1" colspan="3">Test Parallel Field
+          <tal:block metal:use-macro="here/Zuite_CommonTemplate/macros/expected_failure_for_anonymous_selection" />
+        </td>
+      </tr>
+    </thead>
+    <tbody>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplate/macros/init" />
+      <tr>
+        <td>open</td>
+        <td>${base_url}/web_site_module/renderjs_runner/#/?page=preference</td>
+        <td></td>
+      </tr>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/enable_developer_mode" />
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/go_to_module_list" />
+      <tr>
+        <td>click</td>
+        <td>//a[text()="Foos"]</td>
+        <td></td>
+      </tr>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_listbox_loaded" />
+      <tal:block tal:define="click_configuration python: {'text': 'Add'}">
+        <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/click_on_header_link" />
+      </tal:block>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
+      <tal:block tal:define="notification_configuration python: {'class': 'success',
+                                                           'text': 'Object created.'}">
+        <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
+      </tal:block>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
+      <tal:block tal:define="click_configuration python: {'text': 'Dialog with only Update'}">
+        <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/click_on_panel_link" />
+      </tal:block>
+      <tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
+      <tr>
+        <td>assertElementPresent</td>
+        <td>//div[contains(@data-gadget-url, 'gadget_erp5_form.html')]/a[@href="portal_skins/erp5_ui_test/Foo_viewDialogWithOnlyUpdateDialog/manage_main"]</td>
+        <td></td>
+      </tr>
+      <tr>
+        <td>assertElementPresent</td>
+        <td>//div[contains(@data-gadget-url, 'gadget_erp5_form.html')]/a[@href="hateoas/Foo_viewDialogWithOnlyUpdateDialog/manage_main"]</td>
+        <td></td>
+      </tr>
+      <tr>
+        <td>click</td>
+        <td>//div[contains(@data-gadget-url, 'gadget_erp5_form.html')]/a[@href="hateoas/Foo_viewDialogWithOnlyUpdateDialog/manage_main"]</td>
+        <td></td>
+      </tr>
+      <tr>
+        <td>waitForElementPresent</td>
+        <td>//a[@href="my_title/manage_workspace"]</td>
+        <td></td>
+      </tr>
+      <tr>
+        <td>assertElementPresent</td>
+        <td>//a[contains(@href, "/web_site_module/renderjs_runner/hateoas/Foo_viewDialogWithOnlyUpdateDialog/manage_workspace")]</td>
+        <td></td>
+      </tr>
+    </tbody>
+  </table>
+</body>
+</html>
\ No newline at end of file
-- 
2.30.9