From: Coleman Watts Date: Wed, 7 Dec 2016 19:37:33 +0000 (-0500) Subject: Lazy load crmEditable scripts X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=30b7642cb5d97c189f873b148e132a6910a2743c;p=civicrm-core.git Lazy load crmEditable scripts Enhances CRM.loadScript to support jQuery plugins --- diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 52815a69f7..cb46b981bd 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -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 diff --git a/js/Common.js b/js/Common.js index 4600589652..bab1634261 100644 --- a/js/Common.js +++ b/js/Common.js @@ -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()); diff --git a/js/jquery/jquery.crmeditable.js b/js/jquery/jquery.crmEditable.js similarity index 97% rename from js/jquery/jquery.crmeditable.js rename to js/jquery/jquery.crmEditable.js index 453c496932..624f51b971 100644 --- a/js/jquery/jquery.crmeditable.js +++ b/js/jquery/jquery.crmEditable.js @@ -127,7 +127,7 @@ } $i.addClass('crm-editable-enabled'); - $i.editable(function(value, settings) { + function callback(value, settings) { $i.addClass('crm-editable-saving'); var info = $i.crmEditableEntity(), @@ -165,7 +165,11 @@ .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() { @@ -237,8 +241,4 @@ }); }; - $(document).on('crmLoad', function(e) { - $('.crm-editable', e.target).not('thead *').crmEditable(); - }); - })(jQuery, CRM._); diff --git a/templates/CRM/Campaign/Form/Search/Campaign.tpl b/templates/CRM/Campaign/Form/Search/Campaign.tpl index ad7d9a77ce..067c6b2e9e 100644 --- a/templates/CRM/Campaign/Form/Search/Campaign.tpl +++ b/templates/CRM/Campaign/Form/Search/Campaign.tpl @@ -214,8 +214,7 @@ "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. diff --git a/templates/CRM/Campaign/Form/Search/Petition.tpl b/templates/CRM/Campaign/Form/Search/Petition.tpl index 6f54ea14ac..08da9d8157 100644 --- a/templates/CRM/Campaign/Form/Search/Petition.tpl +++ b/templates/CRM/Campaign/Form/Search/Petition.tpl @@ -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 ) { diff --git a/templates/CRM/Campaign/Form/Search/Survey.tpl b/templates/CRM/Campaign/Form/Search/Survey.tpl index ceb44095f4..2b72618d58 100644 --- a/templates/CRM/Campaign/Form/Search/Survey.tpl +++ b/templates/CRM/Campaign/Form/Search/Survey.tpl @@ -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 diff --git a/templates/CRM/Custom/Page/Option.tpl b/templates/CRM/Custom/Page/Option.tpl index cf9aa881c7..7cd373f67d 100644 --- a/templates/CRM/Custom/Page/Option.tpl +++ b/templates/CRM/Custom/Page/Option.tpl @@ -105,8 +105,7 @@ return nRow; }, "fnDrawCallback": function() { - // FIXME: trigger crmLoad and crmEditable would happen automatically - $('.crm-editable').crmEditable(); + $(this).trigger('crmLoad'); }, "fnServerData": function ( sSource, aoData, fnCallback ) { diff --git a/templates/CRM/Financial/Form/Search.tpl b/templates/CRM/Financial/Form/Search.tpl index ec4695a14c..9e0c7543e4 100644 --- a/templates/CRM/Financial/Form/Search.tpl +++ b/templates/CRM/Financial/Form/Search.tpl @@ -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(); diff --git a/templates/CRM/common/enableDisableApi.tpl b/templates/CRM/common/enableDisableApi.tpl index 049471c302..cb359af23c 100644 --- a/templates/CRM/common/enableDisableApi.tpl +++ b/templates/CRM/common/enableDisableApi.tpl @@ -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}; @@ -79,13 +78,16 @@ } 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); + }); }); {/literal}