X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2FCommon.js;h=a3e0b5a3437e23eb8eeff175ddf3e79b5c219561;hb=61c97605193ec9e4d9c6c7df41027e400d6799bf;hp=8770917b036466d8502b10a72772dee06021924f;hpb=a694874199cbef06b6421a647b3ee3d4e21e72e9;p=civicrm-core.git diff --git a/js/Common.js b/js/Common.js index 8770917b03..a3e0b5a343 100644 --- a/js/Common.js +++ b/js/Common.js @@ -396,13 +396,8 @@ function unselectRadio(fieldName, form) { * @return null */ var submitcount = 0; -/** - * @deprecated - * Changes button label on submit, and disables button after submit for newer browsers. - * Puts up alert for older browsers. - * @todo CRM-13397 replaces this with an alternate (jquery) mechanism - to use the jquery mechanism add - * parent::buildQuickForm to make buttons 'protected' - * */ +/* Changes button label on submit, and disables button after submit for newer browsers. + Puts up alert for older browsers. */ function submitOnce(obj, formId, procText) { // if named button clicked, change text if (obj.value != null) { @@ -825,7 +820,192 @@ CRM.validate = CRM.validate || { }); } + $.widget('civicrm.crmSnippet', { + options: { + url: document.location.href, + block: true, + crmForm: null + }, + _create: function() { + this.refresh(); + this.element.addClass('crm-ajax-container'); + if (!this.element.is('.crm-container *')) { + this.element.addClass('crm-container'); + } + }, + _onFailure: function(data) { + this.options.block && this.element.unblock(); + this.element.trigger('crmAjaxFail', data); + CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); + }, + _formatUrl: function(url) { + // Add snippet argument to url + if (url.search(/[&?]snippet=/) < 0) { + url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; + } + return url; + }, + refresh: function() { + var that = this; + var url = this._formatUrl(this.options.url); + this.options.block && this.element.block(); + $.getJSON(url, function(data) { + if (typeof(data) != 'object' || typeof(data.content) != 'string') { + that._onFailure(data); + return; + } + data.url = url; + that.element.html(data.content).trigger('crmLoad', data); + // This will also trigger crmFormLoad for forms + that.options.crmForm && that.element.trigger('crmFormLoad', data); + }).fail(function() { + that._onFailure(); + }); + } + }); + + var dialogCount = 0; + CRM.loadPage = function(url, options) { + var settings = { + target: '#crm-ajax-dialog-' + (dialogCount++), + dialog: { + modal: true, + width: '65%', + height: parseInt($(window).height() * .75), + close: function() { + $(this).dialog('destroy'); + $(this).remove(); + } + } + }; + options && $.extend(true, settings, options); + settings.url = url; + // Create new dialog + if (settings.dialog !== false && settings.target[0] == '#') { + $('
' + ts('Loading') + '...
').dialog(settings.dialog); + } + if (settings.dialog && !settings.dialog.title) { + $(settings.target).on('crmLoad', function(event, data) { + data.title && $(this).dialog('option', 'title', data.title); + }); + } + // Automatically open form links as new popups + settings.formLinks && $(settings.target).on('click', settings.formLinks, function() { + CRM.loadForm(this.href, { + dialog: { + width: '60%', + height: parseInt($(window).height() * .8) + } + }).on('crmFormSuccess', function() { + $(settings.target).crmSnippet('refresh'); + }); + return false; + }); + return $(settings.target).crmSnippet(settings); + }; + + CRM.loadForm = function(url, options) { + var settings = { + crmForm: { + ajaxForm: {}, + block: true, + autoClose: true, + validate: true, + refreshAction: ['next_new', 'submit_savenext'], + cancelButton: '.cancel.form-submit', + openInline: 'a.button', + onCancel: function(event) {}, + onError: function(data) { + var $el = $(this); + $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data); + if (typeof(data.errors) == 'object') { + $.each(data.errors, function(formElement, msg) { + $('[name="'+formElement+'"]', $el).crmError(msg); + }); + } + } + } + }; + // Move options that belong to crmForm. Others will be passed through to crmSnippet + options && $.each(options, function(key, value) { + if (typeof(settings.crmForm[key]) !== 'undefined') { + settings.crmForm[key] = value; + } + else { + settings[key] = value; + } + }); + settings.type = 'Form'; + + var widget = CRM.loadPage(url, settings); + + widget.on('crmFormLoad', function(event, data) { + var $el = $(this); + var settings = $el.data('crmSnippet').options.crmForm; + var dialog = $el.data('dialog'); + settings.cancelButton && $(settings.cancelButton, this).click(function(event) { + var returnVal = settings.onCancel.call($el, event); + if (returnVal !== false) { + $el.trigger('crmFormCancel', event); + dialog && settings.autoClose && $el.dialog('close'); + } + return returnVal === false; + }); + settings.openInline && $(settings.openInline, this).click(function(event) { + $el.crmSnippet('option', 'url', this.href).crmSnippet('refresh'); + return false; + }); + if (settings.validate) { + $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); + } + $("form", this).ajaxForm($.extend({ + url: data.url, + dataType: 'json', + success: function(response) { + if (response.status == 'success') { + settings.block && $el.unblock(); + $el.trigger('crmFormSuccess', response); + // Reset form for e.g. "save and new" + if (settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0) { + $el.crmSnippet('option', 'url', response.userContext).crmSnippet('refresh'); + } + else if (dialog && settings.autoClose) { + $el.dialog('close'); + } + } + else { + settings.onError.call($el, response); + } + }, + beforeSubmit: function(submission) { + settings.block && $el.block(); + $el.trigger('crmFormSubmit', submission); + } + }, settings.ajaxForm)); + }); + return widget; + }; + + // Preprocess all cj ajax calls to display messages + $(document).ajaxSuccess(function(event, xhr, settings) { + try { + if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { + var response = $.parseJSON(xhr.responseText); + if (typeof(response.crmMessages) == 'object') { + $.each(response.crmMessages, function(n, msg) { + CRM.alert(msg.text, msg.title, msg.type, msg.options); + }) + } + } + } + // Suppress errors + catch (e) {} + }); + $(function () { + // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. + $('#crm-container').trigger('crmLoad'); + if ($('#crm-notification-container').length) { // Initialize notifications $('#crm-notification-container').notify(); @@ -889,8 +1069,9 @@ CRM.validate = CRM.validate || { /** * Clientside currency formatting * @param value - * @param format + * @param format - currency representation of the number 1234.56 * @return string + * @see CRM_Core_Resources::addCoreResources */ var currencyTemplate; CRM.formatMoney = function(value, format) { @@ -909,7 +1090,6 @@ CRM.validate = CRM.validate || { sign = (value < 0) ? '-' : ''; //extracting the absolute value of the integer part of the number and converting to string i = parseInt(value = Math.abs(value).toFixed(2)) + ''; - j = ((j = i.length) > 3) ? j % 3 : 0; result = sign + (j ? i.substr(0, j) + separator : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + separator) + (2 ? decimal + Math.abs(value - i).toFixed(2).slice(2) : ''); return format.replace(/1.*234.*56/, result);