From 59953785761902d7d8b622d2d2a2732f03204333 Mon Sep 17 00:00:00 2001
From: Robin Tissot <tissotrobin@gmail.com>
Date: Fri, 23 Apr 2021 11:37:05 +0200
Subject: [PATCH] Change read direction when changing the main script in the
 document description.

---
 .../templates/core/document_form.html           | 12 +++++++-----
 front/src/document_form.js                      | 17 ++++++++++++++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/app/escriptorium/templates/core/document_form.html b/app/escriptorium/templates/core/document_form.html
index c6cb28d6..baaf9abf 100644
--- a/app/escriptorium/templates/core/document_form.html
+++ b/app/escriptorium/templates/core/document_form.html
@@ -1,5 +1,5 @@
 {% extends 'core/document_nav.html' %}
-{% load i18n staticfiles bootstrap %}
+{% load i18n staticfiles bootstrap json %}
 {% block head_title %}{% if object %}{% trans "Update a Document" %}{% else %}{% trans "Create a new Document" %}{% endif %}{% endblock %}
 
 {% block nav-doc-active %}active{% endblock %}
@@ -173,9 +173,11 @@
 </script>
 {{ block.super }}
 <script type="text/javascript">
-    $(document).ready(function(){
-      bootDocumentForm();
-      bootHelp();
-    });
+ $(document).ready(function(){
+     var scripts = { {% for script in form.fields.main_script.queryset %}
+         '{{script.pk}}':'{{script.text_direction}}',{% endfor %} };
+     bootDocumentForm(scripts);
+     bootHelp();
+ });
 </script>
 {% endblock %}
diff --git a/front/src/document_form.js b/front/src/document_form.js
index 20d352c7..a4461987 100644
--- a/front/src/document_form.js
+++ b/front/src/document_form.js
@@ -1,6 +1,6 @@
 'use strict';
 /**** Metadata stuff ****/
-export function bootDocumentForm() {
+export function bootDocumentForm(scripts) {
     // delete a metadata row
     $('.js-metadata-delete').click(function(ev) {
         var btn = ev.target;
@@ -119,4 +119,19 @@ export function bootDocumentForm() {
             window.location.hash = this.hash;
         }
     });
+
+    // When selecting a rtl script, select rtl read direction
+    // flash shortly the read direction to tell the user when it changed
+    $('#id_main_script').on('change', function(ev) {
+        let dir = scripts[ev.target.value];
+        if (dir=='horizontal-rl') {
+            if ($('#id_read_direction').val() !== 'rtl') {
+                $('#id_read_direction').val('rtl').addClass('is-valid').removeClass('is-valid', 1000);
+            }
+        } else {
+            if ($('#id_read_direction').val() !== 'ltr') {
+                $('#id_read_direction').val('ltr').addClass('is-valid').removeClass('is-valid', 1000);
+            }
+        }
+    });
 }
-- 
GitLab