From fbf55b9a0433da555ce9620a31471f0bfe57cce8 Mon Sep 17 00:00:00 2001
From: dcz <dcz@ipipan.waw.pl>
Date: Thu, 30 Jun 2022 23:41:02 +0200
Subject: [PATCH] Removeing preference.

---
 .../entries/js/components/LexicalUnitEdit.js  | 55 ++++++++++++++++++-
 unifier/urls.py                               |  1 +
 unifier/views.py                              | 16 ++++++
 3 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/entries/static/entries/js/components/LexicalUnitEdit.js b/entries/static/entries/js/components/LexicalUnitEdit.js
index 93dc339..1cb53c0 100644
--- a/entries/static/entries/js/components/LexicalUnitEdit.js
+++ b/entries/static/entries/js/components/LexicalUnitEdit.js
@@ -192,7 +192,7 @@ Object.assign(LexicalUnitEdit, {
         show_info(gettext('Kliknij, aby wybrać kolumnę do edycji.'));
       }
     },
-    addSelectivePreference() {
+    addSelectivePreference () {
       if (!this.active_unified_frame_argument) {
         alert(gettext("Zaznacz argument, do którego chcesz dodać preferencję."));
       } else {
@@ -201,6 +201,58 @@ Object.assign(LexicalUnitEdit, {
         }.bind(this));
       }
     },
+    removeSelectionalPreference () {
+      if (!this.active_unified_frame_argument) {
+        alert(gettext("Zaznacz argument, do którego chcesz dodać preferencję."));
+      } else {
+        var existingPreferencies = function () {
+          return this.active_unified_frame_argument.preferences.map(preference => {
+            return `<label><input type="checkbox" name="preference" value="${preference.id}" /> ${preference.str}</label><br />`;
+          }).join("");
+        }.bind(this);
+
+        var remove_preference_popup = {
+          state0: {
+            title: 'Wybierz preferencje do usunięcia',
+            html: existingPreferencies,
+            buttons: {Anuluj: 0, Usuń: 1},
+            focus: -1,
+            submit: function (e, v, m, f) {
+              if (v == 0) {
+                e.preventDefault();
+                $.prompt.close();
+              }
+              if (v === 1) {
+                e.preventDefault();
+                let preference_ids = normalizeFormData(f.preference);
+                var data = {
+                  'unified_frame_id': this.unified_frame.id,
+                  'argument_id': this.active_unified_frame_argument.id,
+                  'preference_ids': JSON.stringify(preference_ids)
+                };
+                $.ajax({
+                  type: 'post',
+                  url: '/' + lang + '/unifier/remove_selectional_preference/',
+                  dataType: 'json',
+                  data: data,
+                  timeout: 60000,
+                  success: function (response) {
+                    show_info('Wybrane preferencje zostału usunięte.');
+                    this.loadFrame();
+                    $.prompt.close();
+                  }.bind(this),
+                  error: function (request, errorType, errorMessage) {
+                    show_error(errorType + ' (' + errorMessage + ')');
+                    $.prompt.close();
+                  }
+                });
+              }
+            }.bind(this)
+          }
+        }
+        $.prompt(remove_preference_popup);
+      }
+    },
     changeTitle() {
       let title = this.unified_frame.title != null ? this.unified_frame.title : '';
       var change_title_popup = {
@@ -755,6 +807,7 @@ Object.assign(LexicalUnitEdit, {
         <tr style="background-color: white;">
           <td id="change-role" @click="changeRole" style="padding: 10px 15px 10px 15px; color: #000000;">Zmień rolę</td>
           <td id="remove-arg" @click="removeArgument" style="padding: 10px 15px 10px 15px; color: #000000;">Usuń argum.</td>
+          <td id="change-windows" style="padding: 10px 15px 10px 15px; color: #000000;" @click="removeSelectionalPreference">Usuń prefer.</td>
           <td id="change-windows" style="padding: 10px 15px 10px 15px; color: #000000;" @click="swapUnifiedFrames">Zamień okna</td>
           <td id="duplicates" @click="duplicate" style="padding: 10px 15px 10px 15px; color: #000000;">Duplikuj</td>
         </tr>
diff --git a/unifier/urls.py b/unifier/urls.py
index 21fc47c..3ea83f5 100644
--- a/unifier/urls.py
+++ b/unifier/urls.py
@@ -20,5 +20,6 @@ urlpatterns = [
     path('remove_argument/', views.remove_argument, name='remove_argument'),
     path('duplicate_unified_frame/', views.duplicate_unified_frame, name='duplicate_unified_frame'),
     path('change_unified_frame_status_to_ready/', views.change_unified_frame_status_to_ready, name='change_unified_frame_status_to_ready'),
+    path('remove_selectional_preference/', views.remove_selectional_preference, name='remove_selectional_preference'),
 
 ]
diff --git a/unifier/views.py b/unifier/views.py
index 015e301..0b3fbd1 100644
--- a/unifier/views.py
+++ b/unifier/views.py
@@ -227,6 +227,22 @@ def extract_frames_to_new_frame(request):
         return JsonResponse(get_unified_frame_json(new_frame_fullfiled_and_saved, request))
     return JsonResponse({})
 
+@ajax_required
+def remove_selectional_preference(request):
+    if request.method == 'POST':
+        unified_frame_id = request.POST['unified_frame_id']
+        argument_id = request.POST['argument_id']
+        preference_ids = json.loads(request.POST['preference_ids'])
+
+        unified_frame_argument = UnifiedFrameArgument.objects.get(id=argument_id, unified_frame=unified_frame_id)
+        unified_frame_argument.predefined.set(unified_frame_argument.predefined.exclude(id__in=preference_ids))
+        unified_frame_argument.synsets.set(unified_frame_argument.synsets.exclude(id__in=preference_ids))
+        unified_frame_argument.relations.set(unified_frame_argument.relations.exclude(id__in=preference_ids))
+        unified_frame_argument.save()
+
+        return JsonResponse({})
+    return JsonResponse({})
+
 @ajax_required
 def duplicate_unified_frame(request):
     if request.method == 'POST':
-- 
GitLab