From 0155e4994a14b238d7eee479b2fb6e44006d30be Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 3 May 2022 18:05:22 +0000
Subject: [PATCH] fix url handling in resources.js, fixes #998

Closes #998

Merge request studip/studip!576
---
 .../assets/javascripts/bootstrap/resources.js | 82 +++++++------------
 1 file changed, 30 insertions(+), 52 deletions(-)

diff --git a/resources/assets/javascripts/bootstrap/resources.js b/resources/assets/javascripts/bootstrap/resources.js
index da266af0676..1f6ac87ca4a 100644
--- a/resources/assets/javascripts/bootstrap/resources.js
+++ b/resources/assets/javascripts/bootstrap/resources.js
@@ -634,32 +634,19 @@ STUDIP.ready(function () {
     }
 
     function updateViewURL(defaultView) {
-        var sURLVariables = window.location.href.split(/[?&]/);
-        var changed = false;
-        for (var i = 0; i < sURLVariables.length; i++) {
-            var sParameterName = sURLVariables[i].split('=');
-            if (sParameterName[0] == "defaultView") {
-                sParameterName[1] = defaultView;
-                sURLVariables[i] = sParameterName.join('=');
-                changed = true;
-            }
-        }
-        if (!changed) {
-            sURLVariables.push('defaultView=' + defaultView);
-        }
-        if (sURLVariables.length > 2) {
-            var newurl = sURLVariables[0] + '?' + sURLVariables[1] + '&';
-            sURLVariables.shift();
-            sURLVariables.shift();
-            newurl += sURLVariables.join('&');
-        } else {
-            var newurl = sURLVariables.join('?');
-        }
-        history.pushState({}, null, newurl);
-        var std_day = newurl.replace(/&?allday=\d+/, '');
-        $('.booking-plan-std_view').attr('href', std_day);
-        $('.booking-plan-allday_view').attr('href', std_day + '&allday=1');
-    };
+        const url = new URL(window.location.href);
+        url.searchParams.set('defaultView', defaultView);
+
+        // Push current view url to history
+        history.pushState({}, null, url.toString());
+
+        // Set links accordingly
+        url.searchParams.delete('allday');
+        $('.booking-plan-std_view').attr('href', url.toString());
+
+        url.searchParams.set('allday', 1);
+        $('.booking-plan-allday_view').attr('href', url.toString());
+    }
 
     function submitDatePicker() {
         var picked = $('#booking-plan-jmpdate').val();
@@ -712,34 +699,25 @@ STUDIP.ready(function () {
                 jQuery(export_action).attr('href', export_url);
             }
 
-            //Now change the URL of the window.
+            // Now change the URL of the window.
             var changeddate = STUDIP.Fullcalendar.toRFC3339String(changedmoment).split('T')[0];
-            var sURLVariables = window.location.href.split(/[?&]/);
-            var changed = false;
-            for (var i = 0; i < sURLVariables.length; i++) {
-                var sParameterName = sURLVariables[i].split('=');
-                if (sParameterName[0] == "defaultDate") {
-                    sParameterName[1] = changeddate;
-                    sURLVariables[i] = sParameterName.join('=');
-                    changed = true;
-                }
-            }
-            if (!changed) {
-                sURLVariables.push('defaultDate=' + changeddate);
-            }
-            if (sURLVariables.length > 2) {
-                var newurl = sURLVariables[0] + '?' + sURLVariables[1] + '&';
-                sURLVariables.shift();
-                sURLVariables.shift();
-                newurl += sURLVariables.join('&');
-            } else {
-                var newurl = sURLVariables.join('?');
-            }
-            history.pushState({}, null, newurl);
-            var std_day = newurl.replace(/&?allday=\d+/, '');
-            $('.booking-plan-std_view').attr('href', std_day);
-            $('.booking-plan-allday_view').attr('href', std_day + '&allday=1');
+
+            const url = new URL(window.location.href);
+            url.searchParams.set('defaultDate', changeddate);
+
+            // Update url in history
+            history.pushState({}, null, url.toString());
+
+            // Adjust links accordingly
+            url.searchParams.delete('allday');
+            $('.booking-plan-std_view').attr('href', url.toString());
+
+            url.searchParams.set('allday', 1);
+            $('.booking-plan-allday_view').attr('href', url.toString());
+
+            // Update sidebar value
             $('#booking-plan-jmpdate').val(changedmoment.toLocaleDateString('de-DE'));
+
             //Store the date in the sessionStorage:
             sessionStorage.setItem('booking_plan_date', changeddate)
         }
-- 
GitLab