CRM-18286 - Strip the ids out of ajax urls to make them more generic
authorColeman Watts <coleman@civicrm.org>
Thu, 5 May 2016 18:46:51 +0000 (14:46 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 6 May 2016 01:02:31 +0000 (21:02 -0400)
js/Common.js

index 60c156b9da68b0694415f5e6d8dfd1a6ca980368..d9ec1d7a1d12caded37f8b959d3e323857dab5ac 100644 (file)
@@ -728,10 +728,18 @@ if (!CRM.vars) CRM.vars = {};
   };
 
   $.fn.crmAjaxTable = function() {
+    // Strip the ids from ajax urls to make pageLength storage more generic
+    function simplifyUrl(ajax) {
+      // Datatables ajax prop could be a url string or an object containing the url
+      var url = typeof ajax === 'object' ? ajax.url : ajax;
+      return typeof url === 'string' ? url.replace(/[&?]\w*id=\d+/g, '') : null;
+    }
+
     return $(this).each(function() {
       // Recall pageLength for this table
-      if ($(this).data('ajax') && window.localStorage && localStorage['dataTablePageLength:' + $(this).data('ajax')]) {
-        $(this).data('pageLength', localStorage['dataTablePageLength:' + $(this).data('ajax')]);
+      var url = simplifyUrl($(this).data('ajax'));
+      if (url && window.localStorage && localStorage['dataTablePageLength:' + url]) {
+        $(this).data('pageLength', localStorage['dataTablePageLength:' + url]);
       }
       // Declare the defaults for DataTables
       var defaults = {
@@ -762,7 +770,7 @@ if (!CRM.vars) CRM.vars = {};
       // Remember pageLength
       $(this).on('length.dt', function(e, settings, len) {
         if (settings.ajax && window.localStorage) {
-          localStorage['dataTablePageLength:' + settings.ajax] = len;
+          localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len;
         }
       });
       //Make the DataTables call