Lazy load crmAjaxTable.js
authorColeman Watts <coleman@civicrm.org>
Sat, 25 Feb 2017 03:47:53 +0000 (20:47 -0700)
committerColeman Watts <coleman@civicrm.org>
Sat, 25 Feb 2017 03:51:13 +0000 (20:51 -0700)
Move script out of Common.js and only load when needed

js/Common.js
js/jquery/jquery.crmAjaxTable.js [new file with mode: 0644]

index bab1634261136dc936630f891041c7853de1b83f..499eb2eb73cd60c26d08deea0bd42ce680f4b4cd 100644 (file)
@@ -773,58 +773,6 @@ 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
-      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 = {
-        "processing": true,
-        "serverSide": true,
-        "order": [],
-        "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
-        "pageLength": 25,
-        "pagingType": "full_numbers",
-        "drawCallback": function(settings) {
-          //Add data attributes to cells
-          $('thead th', settings.nTable).each( function( index ) {
-            $.each(this.attributes, function() {
-              if(this.name.match("^cell-")) {
-                var cellAttr = this.name.substring(5);
-                var cellValue = this.value;
-                $('tbody tr', settings.nTable).each( function() {
-                  $('td:eq('+ index +')', this).attr( cellAttr, cellValue );
-                });
-              }
-            });
-          });
-          //Reload table after draw
-          $(settings.nTable).trigger('crmLoad');
-        }
-      };
-      //Include any table specific data
-      var settings = $.extend(true, defaults, $(this).data('table'));
-      // Remember pageLength
-      $(this).on('length.dt', function(e, settings, len) {
-        if (settings.ajax && window.localStorage) {
-          localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len;
-        }
-      });
-      //Make the DataTables call
-      $(this).DataTable(settings);
-    });
-  };
-
   CRM.utils.formatSelect2Result = function (row) {
     var markup = '<div class="crm-select2-row">';
     if (row.image !== undefined) {
@@ -1045,14 +993,19 @@ if (!CRM.vars) CRM.vars = {};
       $('table.crm-ajax-table', e.target).each(function() {
         var
           $table = $(this),
+          script = CRM.config.resourceBase + 'js/jquery/jquery.crmAjaxTable.js',
           $accordion = $table.closest('.crm-accordion-wrapper.collapsed, .crm-collapsible.collapsed');
         // For tables hidden by collapsed accordions, wait.
         if ($accordion.length) {
           $accordion.one('crmAccordion:open', function() {
-            $table.crmAjaxTable();
+            CRM.loadScript(script).done(function() {
+              $table.crmAjaxTable();
+            });
           });
         } else {
-          $table.crmAjaxTable();
+          CRM.loadScript(script).done(function() {
+            $table.crmAjaxTable();
+          });
         }
       });
       if ($("input:radio[name=radio_ts]").size() == 1) {
diff --git a/js/jquery/jquery.crmAjaxTable.js b/js/jquery/jquery.crmAjaxTable.js
new file mode 100644 (file)
index 0000000..66978e7
--- /dev/null
@@ -0,0 +1,59 @@
+// https://civicrm.org/licensing
+(function($, _) {
+  "use strict";
+  /* jshint validthis: true */
+
+  $.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
+      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 = {
+        "processing": true,
+        "serverSide": true,
+        "order": [],
+        "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
+        "pageLength": 25,
+        "pagingType": "full_numbers",
+        "drawCallback": function(settings) {
+          //Add data attributes to cells
+          $('thead th', settings.nTable).each( function( index ) {
+            $.each(this.attributes, function() {
+              if(this.name.match("^cell-")) {
+                var cellAttr = this.name.substring(5);
+                var cellValue = this.value;
+                $('tbody tr', settings.nTable).each( function() {
+                  $('td:eq('+ index +')', this).attr( cellAttr, cellValue );
+                });
+              }
+            });
+          });
+          //Reload table after draw
+          $(settings.nTable).trigger('crmLoad');
+        }
+      };
+      //Include any table specific data
+      var settings = $.extend(true, defaults, $(this).data('table'));
+      // Remember pageLength
+      $(this).on('length.dt', function(e, settings, len) {
+        if (settings.ajax && window.localStorage) {
+          localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len;
+        }
+      });
+      //Make the DataTables call
+      $(this).DataTable(settings);
+    });
+  };
+
+})(CRM.$, CRM._);