From 7d1764c97c7970130e8842fe3c68d3f7c976257b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 19 May 2022 14:53:23 +0200
Subject: [PATCH] pricing: add js & css for ordering & formset (#65440)

---
 .gitignore                                    |  1 +
 lingo/manager/static/css/style.scss           | 35 +++++++++++++++++++
 lingo/manager/static/js/lingo.manager.js      | 34 ++++++++++++++++++
 .../manager/templates/lingo/manager_base.html |  6 ++++
 setup.py                                      |  3 +-
 5 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 lingo/manager/static/css/style.scss
 create mode 100644 lingo/manager/static/js/lingo.manager.js

diff --git a/.gitignore b/.gitignore
index 1d3398c..37303ae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,3 +14,4 @@ lingo.egg-info/
 junit*xml
 pylint.out
 *.swp
+lingo/manager/static/css/style.css
diff --git a/lingo/manager/static/css/style.scss b/lingo/manager/static/css/style.scss
new file mode 100644
index 0000000..de28645
--- /dev/null
+++ b/lingo/manager/static/css/style.scss
@@ -0,0 +1,35 @@
+div.paragraph {
+	background: white;
+	box-sizing: border-box;
+	border: 1px solid #386ede;
+	border-radius: 3px;
+	max-width: 100%;
+	padding: 5px 15px;
+	margin-bottom: 1rem;
+	h4 {
+		margin-top: 5px;
+		border-bottom: none;
+	}
+}
+
+.sortable {
+	span.handle {
+		cursor: move;
+		display: inline-block;
+		padding: 0.5ex;
+		text-align: center;
+		width: 2em;
+		height: 100%;
+		box-sizing: border-box;
+        font-weight: normal;
+	}
+}
+
+ul.objects-list.sortable {
+	li {
+		position: relative;
+		& > a {
+			display: inline-block;
+		}
+	}
+}
diff --git a/lingo/manager/static/js/lingo.manager.js b/lingo/manager/static/js/lingo.manager.js
new file mode 100644
index 0000000..bc33b30
--- /dev/null
+++ b/lingo/manager/static/js/lingo.manager.js
@@ -0,0 +1,34 @@
+$(function() {
+    $(document).on('click', '#add-pricing-variable-form', function() {
+    if (typeof property_forms === "undefined") {var property_forms = $('.pricing-variable-form');}
+    if (typeof total_forms === "undefined") {var total_form = $('#id_form-TOTAL_FORMS');}
+    if (typeof form_num === "undefined") {var form_num = property_forms.length - 1;}
+    var new_form = $(property_forms[0]).clone();
+    var form_regex = RegExp(`form-(\\d){1}-`,'g');
+    form_num++;
+    new_form.html(new_form.html().replace(form_regex, `form-${form_num}-`));
+    new_form.appendTo('#pricing-variable-forms tbody');
+    $('#id_form-' + form_num + '-key').val('');
+    $('#id_form-' + form_num + '-value').val('');
+    total_form.val(form_num + 1);
+  })
+
+  $('.sortable').sortable({
+    handle: '.handle',
+    items: '.sortable-item',
+    update : function(event, ui) {
+      var new_order = '';
+      $(this).find('.sortable-item').each(function(i, x) {
+        var item_id = $(x).data('item-id');
+        if (new_order) {
+          new_order += ',';
+        }
+        new_order += item_id;
+      });
+      $.ajax({
+        url: $(this).data('order-url'),
+        data: {'new-order': new_order}
+      });
+    }
+  });
+});
diff --git a/lingo/manager/templates/lingo/manager_base.html b/lingo/manager/templates/lingo/manager_base.html
index 985ec62..c28d9be 100644
--- a/lingo/manager/templates/lingo/manager_base.html
+++ b/lingo/manager/templates/lingo/manager_base.html
@@ -1 +1,7 @@
 {% extends "lingo/base.html" %}
+{% load static %}
+
+{% block extrascripts %}
+{{ block.super }}
+<script src="{% static 'js/lingo.manager.js' %}"></script>
+{% endblock %}
diff --git a/setup.py b/setup.py
index 0ee0275..860413b 100644
--- a/setup.py
+++ b/setup.py
@@ -130,7 +130,7 @@ class compile_scss(Command):
 
 
 class build(_build):
-    sub_commands = [('compile_translations', None)] + _build.sub_commands
+    sub_commands = [('compile_translations', None), ('compile_scss', None)] + _build.sub_commands
 
 
 class install_lib(_install_lib):
@@ -169,6 +169,7 @@ setup(
     zip_safe=False,
     cmdclass={
         'build': build,
+        'compile_scss': compile_scss,
         'compile_translations': compile_translations,
         'install_lib': install_lib,
         'sdist': eo_sdist,
-- 
2.35.1

