From 91ff428e848ebdb1b922496cc97e9c48065d881c Mon Sep 17 00:00:00 2001
From: Tomas Peterka <tomas.peterka@nexedi.com>
Date: Fri, 28 Jul 2017 11:11:09 +0200
Subject: [PATCH] [renderjs_ui] Solve blurring/emptying of the panel search
 field in clearer way

---
 .../rjs_gadget_erp5_panel_js.js               | 38 +++++++++----------
 .../rjs_gadget_erp5_panel_js.xml              |  4 +-
 .../rjs_gadget_erp5_searchfield_js.js         |  2 +-
 .../rjs_gadget_erp5_searchfield_js.xml        |  4 +-
 .../rjs_gadget_html5_input_js.js              |  8 +++-
 .../rjs_gadget_html5_input_js.xml             |  4 +-
 6 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.js b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.js
index d331c6ea8f..78038430d6 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.js
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.js
@@ -322,38 +322,36 @@
 
     .onEvent('submit', function (event) {
       var gadget = this,
+        search_gadget,
         redirect_options = {
           page: "search"
         };
 
-      return gadget.getDeclaredGadget("erp5_searchfield")
-        .push(function (search_gadget) {
+      return gadget
+        .getDeclaredGadget("erp5_searchfield")
+        .push(function (declared_gadget) {
+          search_gadget = declared_gadget;
           return search_gadget.getContent();
         })
         .push(function (data) {
           if (data.search) {
             redirect_options.extended_search = data.search;
           }
-          // don't redirect yet even when we have all necessary arguments
-          return gadget.getDeclaredGadget("erp5_searchfield");
+          // let the search gadget know its current state (value and focus)
+          // in order to be able to zero it out in the next Promise
+          // input gadget's state does not reflect immediate reality
+          // so we need to manage its state from the parent
+          return search_gadget.render({
+            extended_search: data.search,
+            focus: true
+          });
         })
-        .push(function (search_gadget) {
+        .push(function () {
           // we want the search field in side panel to be empty and blured
-          // but the state of the search gadget is still empty and blurred
-          // because the search gadget does not catch onSubmit
-          // thus we need to modify its state with the submitted values
-          // and then modify it back to nothing so the "nothing" gets rendered
-          return new RSVP.Queue()
-            .push(function () {
-              return search_gadget.render({
-                extended_search: redirect_options.extended_search,
-              });
-            })
-            .push(function () {
-              return search_gadget.render({
-                extended_search: ''
-              });
-            });
+          return search_gadget.render({
+            extended_search: '',
+            focus: false  // we don't want focus on the empty field for sure
+          });
         })
         .push(function () {
           return gadget.redirect({command: 'display', options: redirect_options});
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.xml b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.xml
index b7f720a2e2..0fef1abbd4 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.xml
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_panel_js.xml
@@ -230,7 +230,7 @@
             </item>
             <item>
                 <key> <string>serial</string> </key>
-                <value> <string>960.63261.10188.49271</string> </value>
+                <value> <string>961.1708.1551.12032</string> </value>
             </item>
             <item>
                 <key> <string>state</string> </key>
@@ -248,7 +248,7 @@
                     </tuple>
                     <state>
                       <tuple>
-                        <float>1501172620.25</float>
+                        <float>1501231473.84</float>
                         <string>UTC</string>
                       </tuple>
                     </state>
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.js b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.js
index 0df35e9685..893da8fe79 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.js
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.js
@@ -20,7 +20,7 @@
       var gadget = this;
       return gadget.getDeclaredGadget('input')
         .push(function (input_gadget) {
-          var focus = gadget.state.focus || false;
+          var focus = gadget.state.focus; // undefined focus is fine as well
 
           if (gadget.state.focus === undefined && !gadget.state.extended_search) {
             focus = true;
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.xml b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.xml
index 87cd9f04ba..ef923ef14e 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.xml
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_searchfield_js.xml
@@ -230,7 +230,7 @@
             </item>
             <item>
                 <key> <string>serial</string> </key>
-                <value> <string>960.63097.14876.50483</string> </value>
+                <value> <string>961.1709.1577.19712</string> </value>
             </item>
             <item>
                 <key> <string>state</string> </key>
@@ -248,7 +248,7 @@
                     </tuple>
                     <state>
                       <tuple>
-                        <float>1501172560.38</float>
+                        <float>1501231535.25</float>
                         <string>UTC</string>
                       </tuple>
                     </state>
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.js b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.js
index 63d1af3284..6ee4bea963 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.js
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.js
@@ -11,7 +11,8 @@
       title: '',
       type: 'text',
       required: false,
-      trim: false
+      trim: false,
+      focus: undefined
     })
 
     .declareMethod('render', function (options) {
@@ -75,6 +76,11 @@
         textarea.autofocus = true;
         textarea.focus();
       }
+
+      if (this.state.focus === false) {
+        textarea.autofocus = false;
+        textarea.blur();
+      }
     })
 
     .declareService(function () {
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.xml b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.xml
index b1c725bb7e..424e3ef6a2 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.xml
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_html5_input_js.xml
@@ -230,7 +230,7 @@
             </item>
             <item>
                 <key> <string>serial</string> </key>
-                <value> <string>956.58742.58866.48708</string> </value>
+                <value> <string>961.1644.62757.27801</string> </value>
             </item>
             <item>
                 <key> <string>state</string> </key>
@@ -248,7 +248,7 @@
                     </tuple>
                     <state>
                       <tuple>
-                        <float>1490622626.22</float>
+                        <float>1501232763.54</float>
                         <string>UTC</string>
                       </tuple>
                     </state>
-- 
2.30.9