MDL-66372 forumreport_summary: Improved generate with filters override
authorMichael Hawkins <michaelh@moodle.com>
Tue, 15 Oct 2019 07:56:15 +0000 (15:56 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Thu, 7 Nov 2019 04:58:02 +0000 (12:58 +0800)
Previously this override simply appended any GET params from the
action it was overriding to the final action URL, now it can handle
overriding param values, so that they are updated or added as needed

mod/forum/report/summary/amd/build/filters.min.js
mod/forum/report/summary/amd/build/filters.min.js.map
mod/forum/report/summary/amd/src/filters.js

index 25eb61a..dd42dab 100644 (file)
Binary files a/mod/forum/report/summary/amd/build/filters.min.js and b/mod/forum/report/summary/amd/build/filters.min.js differ
index 54c31d6..86c45a9 100644 (file)
Binary files a/mod/forum/report/summary/amd/build/filters.min.js.map and b/mod/forum/report/summary/amd/build/filters.min.js.map differ
index 55fbf4a..30fc20d 100644 (file)
@@ -42,33 +42,67 @@ export const init = (root) => {
     // Generic filter handlers.
 
     // Called to override click event to trigger a proper generate request with filtering.
-    const generateWithFilters = (event) => {
-        let newLink = $('#filtersform').attr('action');
+    const generateWithFilters = (event, getparams) => {
+        let currentLink = document.forms.filtersform.action,
+            newLink;
 
         if (event) {
             event.preventDefault();
 
-            let filterParams = event.target.search.substr(1);
-            newLink += '&' + filterParams;
+           let currentSplit = currentLink.split('?'),
+               currentstring = currentSplit[1],
+               newparamsarray = getparams.split('&'),
+               paramsstring = '',
+               paramkeys = [],
+               paramvalues = [];
+
+            // Separate out the existing action GET param string.
+            currentstring.split('&').forEach(function(param) {
+                let splitparam = param.split('=');
+                paramkeys.push(splitparam[0]);
+                paramvalues.push(splitparam[1]);
+            });
+
+            newparamsarray.forEach(function(paramstring) {
+                let newparam = paramstring.split('='),
+                    existingkey = paramkeys.indexOf(newparam[0]);
+
+                // Overwrite value if existing, otherwise add new param.
+                if (existingkey > -1) {
+                    paramvalues[existingkey] = newparam[1];
+                } else {
+                    paramkeys.push(newparam[0]);
+                    paramvalues.push(newparam[1]);
+                }
+            });
+
+            // Build URL.
+            paramkeys.forEach(function(name, key) {
+                paramsstring += `&${name}=${paramvalues[key]}`;
+            });
+
+            newLink = currentSplit[0] + '?' + paramsstring.substr(1);
+        } else {
+            newLink = currentLink;
         }
 
-        $('#filtersform').attr('action', newLink);
-        $('#filtersform').submit();
+        document.forms.filtersform.action = newLink;
+        document.forms.filtersform.submit();
     };
 
     // Override 'reset table preferences' so it generates with filters.
     $('.resettable').on("click", "a", function(event) {
-        generateWithFilters(event);
+        generateWithFilters(event, event.target.search.substr(1));
     });
 
     // Override table heading sort links so they generate with filters.
     $('thead').on("click", "a", function(event) {
-        generateWithFilters(event);
+        generateWithFilters(event, event.target.search.substr(1));
     });
 
     // Override pagination page links so they generate with filters.
     $('.pagination').on("click", "a", function(event) {
-        generateWithFilters(event);
+        generateWithFilters(event, event.target.search.substr(1));
     });
 
     // Submit report via filter