X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2FCommon.js;h=abc5cae3919372b766bb09156a5cef8341ce8d66;hb=5559162a97d504b2f8c0541d6ce5ee78f3cfaadc;hp=42d4cafd69b608038ab4b7a888e84e2ee0415ffa;hpb=e4f4dc2251ca019c33ced4494eeae97da420a9b0;p=civicrm-core.git diff --git a/js/Common.js b/js/Common.js index 42d4cafd69..abc5cae391 100644 --- a/js/Common.js +++ b/js/Common.js @@ -238,6 +238,23 @@ CRM.validate = CRM.validate || { }); }; +/** + * Compare Form Input values against cached initial value. + * + * @return {Boolean} true if changes have been made. + */ + CRM.utils.initialValueChanged = function(el) { + var isDirty = false; + $(':input:visible, .select2-container:visible+:input.select2-offscreen', el).not('[type=submit], [type=button], .crm-action-menu').each(function () { + var initialValue = $(this).data('crm-initial-value'); + // skip change of value for submit buttons + if (initialValue !== undefined && !_.isEqual(initialValue, $(this).val())) { + isDirty = true; + } + }); + return isDirty; + }; + /** * Wrapper for select2 initialization function; supplies defaults * @param options object @@ -453,6 +470,10 @@ CRM.validate = CRM.validate || { .find('input.select-row:checked').parents('tr').addClass('crm-row-selected'); $('.crm-select2:not(.select2-offscreen, .select2-container)', e.target).crmSelect2(); $('.crm-form-entityref:not(.select2-offscreen, .select2-container)', e.target).crmEntityRef(); + // Cache Form Input initial values + $('form[data-warn-changes] :input', e.target).each(function() { + $(this).data('crm-initial-value', $(this).val()); + }); }) .on('dialogopen', function(e) { var $el = $(e.target); @@ -484,10 +505,22 @@ CRM.validate = CRM.validate || { }) .on('dialogclose', function(e) { // Restore scrollbars when closing modal - if ($('.ui-dialog .modal-dialog').not(e.target).length < 1) { + if ($('.ui-dialog .modal-dialog:visible').not(e.target).length < 1) { $('body').css({overflow: ''}); } - }); + }) + .on('submit', function(e) { + // CRM-14353 - disable changes warn when submitting a form + $('[data-warn-changes]').attr('data-warn-changes', 'false'); + }) + ; + + // CRM-14353 - Warn of unsaved changes for forms which have opted in + window.onbeforeunload = function() { + if (CRM.utils.initialValueChanged($('form[data-warn-changes=true]:visible'))) { + return ts('You have unsaved changes.'); + } + }; /** * Function to make multiselect boxes behave as fields in small screens @@ -731,6 +764,16 @@ CRM.validate = CRM.validate || { return dialog.dialog(settings).trigger('crmLoad'); }; + /** provides a local copy of ts for a domain */ + CRM.ts = function(domain) { + return function(message, options) { + if (domain) { + options = $.extend(options || {}, {domain: domain}); + } + return ts(message, options); + }; + }; + /** * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Notification+Reference */ @@ -819,12 +862,6 @@ CRM.validate = CRM.validate || { catch (e) {} }); - /** - * Temporary stub to get around name conflict with legacy jQuery.autocomplete plugin - * FIXME: Remove this before 4.5 release - */ - $.widget('civi.crmAutocomplete', $.ui.autocomplete, {}); - $(function () { // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. $('.crm-container').trigger('crmLoad');