From accffe9af073fa58c1236253673aab0ddebdff15 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 18 May 2019 14:28:51 +0200
Subject: [PATCH] manager: keep cell open after edit actions (#33103)

---
 combo/apps/gallery/views.py                  |  6 ++++--
 combo/manager/static/js/combo.manager.js     |  7 +++++--
 combo/manager/templates/combo/page_view.html |  2 +-
 combo/manager/views.py                       |  5 +++--
 tests/test_manager.py                        | 10 +++++-----
 5 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/combo/apps/gallery/views.py b/combo/apps/gallery/views.py
index 76ca3517..06a17767 100644
--- a/combo/apps/gallery/views.py
+++ b/combo/apps/gallery/views.py
@@ -37,7 +37,8 @@ class ImageAddView(CreateView):
         return super(ImageAddView, self).form_valid(form)
 
     def get_success_url(self):
-        return reverse('combo-manager-page-view', kwargs={'pk': self.object.gallery.page.id})
+        return reverse('combo-manager-page-view',
+                kwargs={'pk': self.object.gallery.page.id}) + '#cell-' + self.object.gallery.get_reference()
 
 image_add = ImageAddView.as_view()
 
@@ -48,7 +49,8 @@ class ImageEditView(UpdateView):
     form_class = ImageEditForm
 
     def get_success_url(self):
-        return reverse('combo-manager-page-view', kwargs={'pk': self.object.gallery.page.id})
+        return reverse('combo-manager-page-view',
+                kwargs={'pk': self.object.gallery.page.id}) + '#cell-' + self.object.gallery.get_reference()
 
 image_edit = ImageEditView.as_view()
 
diff --git a/combo/manager/static/js/combo.manager.js b/combo/manager/static/js/combo.manager.js
index cdaaeb95..f074e8d1 100644
--- a/combo/manager/static/js/combo.manager.js
+++ b/combo/manager/static/js/combo.manager.js
@@ -269,9 +269,12 @@ $(function() {
   });
 
   $('div.cell').each(function(i, x) {
-    $(this).attr('id', 'div-cell-'+i);
     compute_max_height($(this));
-    $(this).addClass('untoggled');
+    if (window.location.hash == '#' + $(this).attr('id')) {
+      $(this).addClass('toggled');
+    } else {
+      $(this).addClass('untoggled');
+    }
   });
   $('#assets-browser table tr').on('hover mouseenter', function() {
     var $img = $(this).find('img');
diff --git a/combo/manager/templates/combo/page_view.html b/combo/manager/templates/combo/page_view.html
index 7f430eb9..ec3585fe 100644
--- a/combo/manager/templates/combo/page_view.html
+++ b/combo/manager/templates/combo/page_view.html
@@ -95,7 +95,7 @@
     <h2>{{ placeholder.name }}</h2>
     <div class="cell-list">
       {% for cell in placeholder.cells %}
-      <div class="cell {{cell.class_name}}" data-cell-reference="{{ cell.get_reference }}">
+      <div id="cell-{{cell.get_reference}}" class="cell {{cell.class_name}}" data-cell-reference="{{ cell.get_reference }}">
         <h3><span class="handle">⣿</span>
                 <span class="group1">
                         {{ cell.get_label }}
diff --git a/combo/manager/views.py b/combo/manager/views.py
index cca2a79a..04dfae4c 100644
--- a/combo/manager/views.py
+++ b/combo/manager/views.py
@@ -346,7 +346,7 @@ class PageAddCellView(RedirectView):
             cell.order = 1
         cell.save()
         PageSnapshot.take(cell.page, request=self.request, comment=_('added cell "%s"') % cell)
-        return reverse('combo-manager-page-view', kwargs={'pk': page_pk})
+        return reverse('combo-manager-page-view', kwargs={'pk': page_pk}) + '#cell-' + cell.get_reference()
 
 page_add_cell = PageAddCellView.as_view()
 
@@ -375,7 +375,8 @@ class PageEditCellView(UpdateView):
         return self.object.get_default_form_class()
 
     def get_success_url(self):
-        return reverse('combo-manager-page-view', kwargs={'pk': self.kwargs.get('page_pk')})
+        return reverse('combo-manager-page-view', kwargs={'pk': self.kwargs.get('page_pk')}
+                ) + '#cell-' + self.object.get_reference()
 
     def form_valid(self, form):
         if self.request.is_ajax():
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 8392f421..8aafd611 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -342,18 +342,18 @@ def test_add_edit_cell(app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.id)
     # click on first option link, this should add a text cell
     resp = app.get(resp.html.find('option').get('data-add-url'))
-    assert resp.location.endswith('/manage/pages/1/')
 
     cells = CellBase.get_cells(page_id=page.id)
     assert len(cells) == 1
     assert isinstance(cells[0], TextCell)
+    assert resp.location.endswith('/manage/pages/1/#cell-%s' % cells[0].get_reference())
 
     resp = app.get('/manage/pages/%s/' % page.id)
     assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
     resp.forms[0]['c%s-text' % cells[0].get_reference()].value = 'Hello world'
     resp = resp.forms[0].submit()
     assert resp.status_int == 302
-    assert resp.location.endswith('/manage/pages/1/')
+    assert resp.location.endswith('/manage/pages/1/#cell-%s' % cells[0].get_reference())
 
     resp = app.get('/manage/pages/%s/' % page.id)
     assert resp.forms[0]['c%s-text' % cells[0].get_reference()].value == 'Hello world'
@@ -557,11 +557,11 @@ def test_edit_text_cell(app, admin_user):
     resp = app.get('/manage/pages/%s/' % page.id)
     data_add_url = [x for x in resp.html.find_all('option') if x.text == 'Text'][0].get('data-add-url')
     resp = app.get(data_add_url)
-    assert resp.location.endswith('/manage/pages/%s/' % page.id)
 
     cells = CellBase.get_cells(page_id=page.id)
     assert len(cells) == 1
     assert isinstance(cells[0], TextCell)
+    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))
 
     resp = app.get('/manage/pages/%s/' % page.id)
     resp.form['cdata_textcell-%s-text' % cells[0].id].value = 'Hello : World'
@@ -594,12 +594,12 @@ def test_edit_config_json_cell(app, admin_user):
         assert 'Foobar' in options
         data_add_url = [x for x in resp.html.find_all('option') if x.text == 'Foobar'][0].get('data-add-url')
         resp = app.get(data_add_url)
-        assert resp.location.endswith('/manage/pages/%s/' % page.id)
 
         cells = CellBase.get_cells(page_id=page.id)
         assert len(cells) == 1
         assert isinstance(cells[0], ConfigJsonCell)
         assert cells[0].key == 'test-config-json-cell'
+        assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))
 
         resp = app.get('/manage/pages/%s/' % page.id)
         assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
@@ -641,7 +641,7 @@ def test_edit_config_json_cell(app, admin_user):
         resp.form['c%s-test3' % cells[0].get_reference()].value = 'Hello again'
         resp = resp.form.submit()
         assert resp.status_int == 302
-        assert resp.location.endswith('/manage/pages/%s/' % page.id)
+        assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))
 
         resp = app.get('/manage/pages/%s/' % page.id)
         assert resp.form['c%s-test' % cells[0].get_reference()].value == 'Hello world'
-- 
2.20.1

