From 554b17683c67968994011a9b3d189201bb0c6b3a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 2 Apr 2014 14:36:12 -0400 Subject: [PATCH] CRM-13863 - Add CRM.refreshParent helper --- js/crm.ajax.js | 20 +++++++++++++++++++ js/crm.livePage.js | 5 +---- templates/CRM/Activity/Form/ActivityLinks.tpl | 2 +- .../CRM/Contact/Page/View/CustomData.tpl | 2 +- .../CRM/Contact/Page/View/GroupContact.tpl | 2 +- templates/CRM/common/TabHeader.js | 5 +---- templates/CRM/common/enableDisableApi.tpl | 16 +++------------ 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 4639550204..597e77bfc0 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -446,6 +446,26 @@ }); e.preventDefault(); }; + /** + * An event callback for CRM.popup or a standalone function to refresh the content around a popup link + * @param e event|selector + */ + CRM.refreshParent = function(e) { + // Use e.target if input smells like an event, otherwise assume it's a jQuery selector + var $el = (e.stopPropagation && e.target) ? $(e.target) : $(e), + $table = $el.closest('.dataTable'); + // Call native refresh method on ajax datatables + if ($table && $.fn.DataTable.fnIsDataTable($table[0]) && $table.dataTable().fnSettings().sAjaxSource) { + // Refresh ALL datatables - needed for contact relationship tab + $.each($.fn.dataTable.fnTables(), function() { + $(this).dataTable().fnSettings().sAjaxSource && $(this).unblock().dataTable().fnDraw(); + }); + } + // Otherwise refresh the nearest crmSnippet + else { + $el.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh'); + } + }; $(function($) { $('body').on('click', 'a.crm-popup', CRM.popup); diff --git a/js/crm.livePage.js b/js/crm.livePage.js index 819b58f804..5133f5d371 100644 --- a/js/crm.livePage.js +++ b/js/crm.livePage.js @@ -8,8 +8,5 @@ CRM.$(function($) { // Open action links in a popup .off('.crmLivePage') .on('click.crmLivePage', active, CRM.popup) - .on('crmPopupFormSuccess.crmLivePage', active, function() { - // Refresh page when form completes - $('#crm-main-content-wrapper').crmSnippet('refresh'); - }); + .on('crmPopupFormSuccess.crmLivePage', active, CRM.refreshParent); }); diff --git a/templates/CRM/Activity/Form/ActivityLinks.tpl b/templates/CRM/Activity/Form/ActivityLinks.tpl index eda8494f5b..fa86d88cbe 100644 --- a/templates/CRM/Activity/Form/ActivityLinks.tpl +++ b/templates/CRM/Activity/Form/ActivityLinks.tpl @@ -46,7 +46,7 @@ if (url) { $el.select2('val', ''); CRM.loadForm(url).on('crmFormSuccess', function() { - $el.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh'); + CRM.refreshParent($el); }); } }); diff --git a/templates/CRM/Contact/Page/View/CustomData.tpl b/templates/CRM/Contact/Page/View/CustomData.tpl index 345a656d45..c2caa0a3c8 100644 --- a/templates/CRM/Contact/Page/View/CustomData.tpl +++ b/templates/CRM/Contact/Page/View/CustomData.tpl @@ -42,7 +42,7 @@ var request = $.post(postUrl, $el.data('delete_params')); CRM.status({/literal}"{ts escape='js'}Record Deleted{/ts}"{literal}, request); request.done(function() { - $table.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh'); + CRM.refreshParent($el); }); }) e.preventDefault(); diff --git a/templates/CRM/Contact/Page/View/GroupContact.tpl b/templates/CRM/Contact/Page/View/GroupContact.tpl index 9cc96d3f9c..e4659dde32 100644 --- a/templates/CRM/Contact/Page/View/GroupContact.tpl +++ b/templates/CRM/Contact/Page/View/GroupContact.tpl @@ -185,7 +185,7 @@ // Handle enable/delete links var that; function refresh() { - $(that).closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh'); + CRM.refreshParent(that); } function enableDisableGroup() { var params = { diff --git a/templates/CRM/common/TabHeader.js b/templates/CRM/common/TabHeader.js index 4eb99a5a15..75b0532482 100644 --- a/templates/CRM/common/TabHeader.js +++ b/templates/CRM/common/TabHeader.js @@ -33,10 +33,7 @@ CRM.$(function($) { ui.panel .off('click.crmLivePage') .on('click.crmLivePage', 'a.button, a.action-item', CRM.popup) - .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', function() { - // Refresh panel when form completes - ui.panel.crmSnippet('refresh'); - }); + .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', CRM.refreshParent); } ui.panel .off('.tabInfo') diff --git a/templates/CRM/common/enableDisableApi.tpl b/templates/CRM/common/enableDisableApi.tpl index 838e39c4e9..83a9964c39 100644 --- a/templates/CRM/common/enableDisableApi.tpl +++ b/templates/CRM/common/enableDisableApi.tpl @@ -27,7 +27,7 @@ {literal}