X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2Fcrm.ajax.js;h=8d785e82c03d35eecbf1e2420f40e71788805961;hb=5559162a97d504b2f8c0541d6ce5ee78f3cfaadc;hp=78205449cfc85b96b79009bf22a4ccaaacc66897;hpb=0d0919c531fa34f2e66c0e8c48ebeb733dfa43df;p=civicrm-core.git diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 78205449cf..8d785e82c0 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -3,7 +3,7 @@ * @see https://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Ajax+Pages+and+Forms */ -(function($, CRM) { +(function($, CRM, undefined) { /** * Almost like {crmURL} but on the client side * eg: var url = CRM.url('civicrm/contact/view', {reset:1,cid:42}); @@ -292,7 +292,9 @@ } $('
' + ts('Loading') + '...
').dialog(settings.dialog); $(settings.target).on('dialogclose', function() { - $(this).crmSnippet('destroy').dialog('destroy').remove(); + if ($(this).attr('data-unsaved-changes') !== 'true') { + $(this).crmSnippet('destroy').dialog('destroy').remove(); + } }); } if (settings.dialog && !settings.dialog.title) { @@ -314,16 +316,7 @@ refreshAction: ['next_new', 'submit_savenext', 'upload_new'], cancelButton: '.cancel', openInline: 'a.open-inline, a.button, a.action-item', - onCancel: function(event) {}, - onError: function(data) { - var $el = $(this); - $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data).trigger('crmFormError', data); - if (typeof(data.errors) == 'object') { - $.each(data.errors, function(formElement, msg) { - $('[name="'+formElement+'"]', $el).crmError(msg); - }); - } - } + onCancel: function(event) {} } }; // Move options that belong to crmForm. Others will be passed through to crmSnippet @@ -338,21 +331,43 @@ var widget = CRM.loadPage(url, settings).off('.crmForm'); + // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out + function cancelAction() { + var dirty = CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', widget)); + widget + .attr('data-unsaved-changes', dirty ? 'true' : 'false') + .dialog('close'); + if (dirty) { + var id = widget.attr('id') + '-unsaved-alert', + title = widget.dialog('option', 'title'), + alert = CRM.alert('

' + ts('%1 has not been saved.', {1: title}) + '

' + ts('Restore') + '

', ts('Unsaved Changes'), 'alert unsaved-dialog', {expires: 60000}); + $('#' + id).button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}}).click(function(e) { + widget.attr('data-unsaved-changes', 'false').dialog('open'); + e.preventDefault(); + }); + } + } + if (widget.data('uiDialog')) { + // CRM-14353 - This is a bit harsh but we are removing jQuery UI's event handler from the close button and adding our own + widget.parent().find('.ui-dialog-titlebar-close').first().off().click(cancelAction); + } + widget.on('crmFormLoad.crmForm', function(event, data) { - var $el = $(this); + var $el = $(this) + .attr('data-unsaved-changes', 'false'); var settings = $el.crmSnippet('option', 'crmForm'); - settings.cancelButton && $(settings.cancelButton, this).click(function(event) { - var returnVal = settings.onCancel.call($el, event); + settings.cancelButton && $(settings.cancelButton, this).click(function(e) { + e.preventDefault(); + var returnVal = settings.onCancel.call($el, e); if (returnVal !== false) { - $el.trigger('crmFormCancel', event); + $el.trigger('crmFormCancel', e); if ($el.data('uiDialog') && settings.autoClose) { - $el.dialog('close'); + cancelAction(); } else if (!settings.autoClose) { $el.crmSnippet('resetUrl').crmSnippet('refresh'); } } - return returnVal === false; }); if (settings.validate) { $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); @@ -361,7 +376,7 @@ url: data.url.replace(/reset=1[&]?/, ''), dataType: 'json', success: function(response) { - if (response.status !== 'form_error') { + if (response.content === undefined) { $el.crmSnippet('option', 'block') && $el.unblock(); $el.trigger('crmFormSuccess', response); // Reset form for e.g. "save and new" @@ -379,7 +394,13 @@ } else { response.url = data.url; - settings.onError.call($el, response); + $el.html(response.content).trigger('crmLoad', response).trigger('crmFormLoad', response); + if (response.status === 'form_error') { + $el.trigger('crmFormError', response); + $.each(response.errors || [], function(formElement, msg) { + $('[name="'+formElement+'"]', $el).crmError(msg); + }); + } } }, beforeSerialize: function(form, options) { @@ -401,6 +422,10 @@ return false; }); } + // Alow a button to prevent ajax submit + $('input[data-no-ajax-submit=true]').click(function() { + $(this).closest('form').ajaxFormUnbind(); + }); // For convenience, focus the first field $('input[type=text], textarea, select', this).filter(':visible').first().not('.dateplugin').focus(); }); @@ -408,7 +433,6 @@ }; /** * Handler for jQuery click event e.g. $('a').click(CRM.popup) - * @returns {boolean} */ CRM.popup = function(e) { var $el = $(this).first(), @@ -447,7 +471,7 @@ e.preventDefault(); }; /** - * An event callback for CRM.popup or a standalone function to refresh the content around a popup link + * An event callback for CRM.popup or a standalone function to refresh the content around a given element * @param e event|selector */ CRM.refreshParent = function(e) { @@ -468,7 +492,16 @@ }; $(function($) { - $('body').on('click', 'a.crm-popup', CRM.popup); + $('body') + .on('click', 'a.crm-popup', CRM.popup) + // Close unsaved dialog messages + .on('dialogopen', function(e) { + $('.alert.unsaved-dialog .ui-notify-cross', '#crm-notification-container').click(); + }) + // Destroy old unsaved dialog + .on('dialogcreate', function(e) { + $('.ui-dialog-content.crm-ajax-container:hidden[data-unsaved-changes=true]').crmSnippet('destroy').dialog('destroy').remove(); + }); }); }(jQuery, CRM));