Lazy load crmEditable scripts
authorColeman Watts <coleman@civicrm.org>
Wed, 7 Dec 2016 19:37:33 +0000 (14:37 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 25 Feb 2017 03:50:45 +0000 (20:50 -0700)
Enhances CRM.loadScript to support jQuery plugins

CRM/Core/Resources.php
js/Common.js
js/jquery/jquery.crmEditable.js [moved from js/jquery/jquery.crmeditable.js with 97% similarity]
templates/CRM/Campaign/Form/Search/Campaign.tpl
templates/CRM/Campaign/Form/Search/Petition.tpl
templates/CRM/Campaign/Form/Search/Survey.tpl
templates/CRM/Custom/Page/Option.tpl
templates/CRM/Financial/Form/Search.tpl
templates/CRM/common/enableDisableApi.tpl

index 52815a69f74cb558db1ef6812a511e2480ac2700..cb46b981bdcc2844600b6aad51ead071f0da68a1 100644 (file)
@@ -740,9 +740,7 @@ class CRM_Core_Resources {
       $items[] = "packages/jquery/plugins/jquery.tableHeader.js";
       $items[] = "packages/jquery/plugins/jquery.menu.min.js";
       $items[] = "css/civicrmNavigation.css";
-      $items[] = "packages/jquery/plugins/jquery.jeditable.min.js";
       $items[] = "packages/jquery/plugins/jquery.notify.min.js";
-      $items[] = "js/jquery/jquery.crmeditable.js";
     }
 
     // JS for multilingual installations
index 4600589652cff76d237258f34d2d781c1c9fe184..bab1634261136dc936630f891041c7853de1b83f 100644 (file)
@@ -243,8 +243,16 @@ if (!CRM.vars) CRM.vars = {};
       var script = document.createElement('script');
       scriptsLoaded[url] = $.Deferred();
       script.onload = function () {
+        if (window.jQuery === CRM.$ && CRM.CMSjQuery) {
+          window.jQuery = CRM.CMSjQuery;
+        }
         scriptsLoaded[url].resolve();
       };
+      // Make jQuery global available while script is loading
+      if (window.jQuery !== CRM.$) {
+        CRM.CMSjQuery = window.jQuery;
+        window.jQuery = CRM.$;
+      }
       script.src = url;
       document.getElementsByTagName("head")[0].appendChild(script);
     }
@@ -1056,6 +1064,12 @@ if (!CRM.vars) CRM.vars = {};
       $('.crm-form-text[data-crm-datepicker]', e.target).each(function() {
         $(this).crmDatepicker($(this).data('crmDatepicker'));
       });
+      $('.crm-editable', e.target).not('thead *').each(function() {
+        var $el = $(this);
+        CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmEditable.js').done(function() {
+          $el.crmEditable();
+        });
+      });
       // Cache Form Input initial values
       $('form[data-warn-changes] :input', e.target).each(function() {
         $(this).data('crm-initial-value', $(this).is(':checkbox, :radio') ? $(this).prop('checked') : $(this).val());
similarity index 97%
rename from js/jquery/jquery.crmeditable.js
rename to js/jquery/jquery.crmEditable.js
index 453c4969320ff3e64d3c9e7acefd13ba394795c7..624f51b971b1137666570c4e994faca42b273657 100644 (file)
       }
       $i.addClass('crm-editable-enabled');
 
-      $i.editable(function(value, settings) {
+      function callback(value, settings) {
         $i.addClass('crm-editable-saving');
         var
           info = $i.crmEditableEntity(),
           .fail(function(data) {
             editableSettings.error.call($el[0], info.entity, info.field, value, data);
           });
-      }, settings);
+      }
+
+      CRM.loadScript(CRM.config.resourceBase + 'packages/jquery/plugins/jquery.jeditable.min.js').done(function() {
+        $i.editable(callback, settings);
+      });
 
       // CRM-15759 - Workaround broken textarea handling in jeditable 1.7.1
       $i.click(function() {
     });
   };
 
-  $(document).on('crmLoad', function(e) {
-    $('.crm-editable', e.target).not('thead *').crmEditable();
-  });
-
 })(jQuery, CRM._);
index ad7d9a77ce70a7bb4e1ffa6b57e66546b105d3a5..067c6b2e9eb28bd223a4641b03ed120d18e626af 100644 (file)
       "oLanguage": {"sEmptyTable": noRecordFoundMsg,
         "sZeroRecords": noRecordFoundMsg },
       "fnDrawCallback": function () {
-        // FIXME: trigger crmLoad and crmEditable would happen automatically
-        $('.crm-editable').crmEditable();
+        $(this).trigger('crmLoad');
       },
       "fnRowCallback": function (nRow, aData, iDisplayIndex) {
         //insert the id for each row for enable/disable.
index 6f54ea14ac31f133a63c8f820ccde1b142231dad..08da9d8157ed4e7c8a1638c2d71645c4e011d9f9 100644 (file)
@@ -198,8 +198,7 @@ function loadPetitionList( )
              "oLanguage":{"sEmptyTable"  : noRecordFoundMsg,
                  "sZeroRecords" : noRecordFoundMsg },
              "fnDrawCallback": function() {
-               // FIXME: trigger crmLoad and crmEditable would happen automatically
-               CRM.$('.crm-editable').crmEditable();
+               $(this).trigger('crmLoad');
              },
              "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
 
index ceb44095f42879e54aae00070fb9b92d5f182f3a..2b72618d5853fdfe4f05e30927436343cd38c9ed 100644 (file)
@@ -212,8 +212,7 @@ function loadSurveyList( )
              "oLanguage":{"sEmptyTable"  : noRecordFoundMsg,
                  "sZeroRecords" : noRecordFoundMsg },
              "fnDrawCallback": function() {
-               // FIXME: trigger crmLoad and crmEditable would happen automatically
-               CRM.$('.crm-editable').crmEditable();
+               $(this).trigger('crmLoad');
              },
              "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                // Crm-editable
index cf9aa881c71a95db0f0a2dff610b48d691eabfff..7cd373f67dc09d3196c944eff3258746ee4055bd 100644 (file)
                 return nRow;
               },
               "fnDrawCallback": function() {
-                // FIXME: trigger crmLoad and crmEditable would happen automatically
-                $('.crm-editable').crmEditable();
+                $(this).trigger('crmLoad');
               },
 
               "fnServerData": function ( sSource, aoData, fnCallback ) {
index ec4695a14c17890e5e33787adf9ba77561286866..9e0c7543e493a2d38b70e3e6be2d30a4b1d314ab 100644 (file)
@@ -149,8 +149,7 @@ CRM.$(function($) {
         return nRow;
       },
       "fnDrawCallback": function(oSettings) {
-        // FIXME: trigger crmLoad and crmEditable would happen automatically
-        $('.crm-editable', '#crm-batch-selector-{/literal}{$batchStatus}{literal}').crmEditable();
+        $(this).trigger('crmLoad');
         $("#toggleSelect").prop('checked', false);
         if (checkedRows.length) {
           $(checkedRows.join(',')).prop('checked', true).change();
index 049471c3020c9bcc5532b7de17c2f4cf2552cb75..cb359af23c72feb949f1c9cf46d2c1fe5da374df 100644 (file)
@@ -61,7 +61,6 @@
     }
 
     function enableDisable() {
-      $a = $(this);
       $row = $a.closest('.crm-entity');
       info = $a.crmEditableEntity();
       fieldLabel = info.label || info.title || info.display_name || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal};
       } else {
         save();
       }
-      return false;
     }
 
     // Because this is an inline script it may get added to the document more than once, so remove handler before adding
     $('body')
       .off('.crmEnableDisable')
-      .on('click.crmEnableDisable', '.action-item.crm-enable-disable', enableDisable);
+      .on('click.crmEnableDisable', '.action-item.crm-enable-disable', function(e) {
+        e.preventDefault();
+        $a = $(this);
+        CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmEditable.js').done(enableDisable);
+      });
   });
 </script>
 {/literal}