X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2Fcrm.ajax.js;h=276d02ddfd729d9ebaf9574c37d2baeba7fdd29c;hb=deb6ebbb7ecab1b5dbd94ce13e52da02c19bf689;hp=6fb3eba935a869d29c00f5ee9f512066d5375fc6;hpb=27f28244a9cd7e07b26a081ea6fdc899e45ecc92;p=civicrm-core.git diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 6fb3eba935..276d02ddfd 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -5,48 +5,41 @@ */ (function($, CRM, undefined) { /** - * @param string p - url - * @param string|object params + * @param string path + * @param string|object query * @param string mode - optionally specify "front" or "back" */ - var tplURL = '/civicrm/example?placeholder'; - var urlInitted = false; - CRM.url = function (p, params, mode) { - if (p == "init") { - tplURL = params; - urlInitted = true; - return; + var tplURL; + CRM.url = function (path, query, mode) { + if (typeof path === 'object') { + return tplURL = path; } - if (!urlInitted) { - console && console.log && console.log('Warning: CRM.url called before initialization'); + if (!tplURL) { + CRM.console('error', 'Error: CRM.url called before initialization'); } if (!mode) { - mode = CRM.config.isFrontend ? 'front' : 'back'; + mode = CRM.config && CRM.config.isFrontend ? 'front' : 'back'; } - params = params || ''; - var frag = p.split ('?'); - var url = tplURL.replace("civicrm/example", frag[0]); + query = query || ''; + var frag = path.split('?'); + var url = tplURL[mode].replace("*path*", frag[0]); - if (typeof(params) == 'string') { - url = url.replace("placeholder", params); + if (!query) { + url = url.replace(/[?&]\*query\*/, ''); } else { - url = url.replace("placeholder", $.param(params)); + url = url.replace("*query*", typeof query === 'string' ? query : $.param(query)); } if (frag[1]) { - url += (url.indexOf('?') === (url.length - 1) ? '' : '&') + frag[1]; - } - // remove trailing "?" - if (url.indexOf('?') === (url.length - 1)) { - url = url.slice(0, (url.length - 1)); + url += (url.indexOf('?') < 0 ? '?' : '&') + frag[1]; } return url; }; - // Backwards compatible with jQuery fn + // @deprecated $.extend ({'crmURL': function (p, params) { - console && console.log && console.log('Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.'); + CRM.console('warn', 'Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.'); return CRM.url(p, params); } }); @@ -163,7 +156,7 @@ * @deprecated */ $.fn.crmAPI = function(entity, action, params, options) { - console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.'); + CRM.console('warn', 'Calling crmAPI from jQuery is deprecated. Please use CRM.api3() instead.'); return CRM.api.call(this, entity, action, params, options); }; @@ -252,8 +245,9 @@ if (this._originalContent === null) { this._originalContent = this.element.contents().detach(); } - this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block(); + this.options.block && this.element.block(); $.getJSON(url, function(data) { + that.options.block && that.element.unblock(); if (!$.isPlainObject(data)) { that._onFailure(data); return; @@ -263,7 +257,9 @@ return; } data.url = url; - that.element.trigger('crmBeforeLoad', data).html(data.content); + that.element.trigger('crmUnload').trigger('crmBeforeLoad', data); + that._beforeRemovingContent(); + that.element.html(data.content); that._handleOrderLinks(); that.element.trigger('crmLoad', data); that.options.crmForm && that.element.trigger('crmFormLoad', data); @@ -271,9 +267,21 @@ that._onFailure(); }); }, - _destroy: function() { - this.element.removeClass('crm-ajax-container'); + // Perform any cleanup needed before removing/replacing content + _beforeRemovingContent: function() { + var that = this; + if (window.tinyMCE && tinyMCE.editors) { + $.each(tinyMCE.editors, function(k) { + if ($.contains(that.element[0], this.getElement())) { + this.remove(); + } + }); + } this.options.crmForm && $('form', this.element).ajaxFormUnbind(); + }, + _destroy: function() { + this.element.removeClass('crm-ajax-container').trigger('crmUnload'); + this._beforeRemovingContent(); if (this._originalContent !== null) { this.element.empty().append(this._originalContent); } @@ -303,6 +311,18 @@ if (typeof settings.dialog.height === 'string' && settings.dialog.height.indexOf('%') > 0) { settings.dialog.height = parseInt($(window).height() * (parseFloat(settings.dialog.height)/100), 10); } + // Increase percent width on small screens + if (typeof settings.dialog.width === 'string' && settings.dialog.width.indexOf('%') > 0) { + var screenWidth = $(window).width(), + percentage = parseInt(settings.dialog.width.replace('%', ''), 10), + gap = 100-percentage; + if (screenWidth < 701) { + settings.dialog.width = '100%'; + } + else if (screenWidth < 1400) { + settings.dialog.width = '' + parseInt(percentage+gap-((screenWidth - 700)/7*(gap)/100), 10) + '%'; + } + } $('
' + ts('Loading') + '...
').dialog(settings.dialog); $(settings.target) .on('dialogclose', function() { @@ -382,9 +402,8 @@ }); widget.on('crmFormLoad.crmForm', function(event, data) { - var $el = $(this) - .attr('data-unsaved-changes', 'false'); - var settings = $el.crmSnippet('option', 'crmForm'); + var $el = $(this).attr('data-unsaved-changes', 'false'), + settings = $el.crmSnippet('option', 'crmForm'); settings.cancelButton && $(settings.cancelButton, this).click(function(e) { e.preventDefault(); var returnVal = settings.onCancel.call($el, e); @@ -407,7 +426,6 @@ dataType: 'json', success: function(response) { if (response.content === undefined) { - $el.crmSnippet('option', 'block') && $el.unblock(); $el.trigger('crmFormSuccess', response); // Reset form for e.g. "save and new" if (response.userContext && (response.status === 'redirect' || (settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0))) { @@ -424,6 +442,7 @@ } } else { + $el.crmSnippet('option', 'block') && $el.unblock(); response.url = data.url; $el.html(response.content).trigger('crmLoad', response).trigger('crmFormLoad', response); if (response.status === 'form_error') { @@ -441,6 +460,11 @@ this.updateElement && this.updateElement(); }); } + if (window.tinyMCE && tinyMCE.editors) { + $.each(tinyMCE.editors, function() { + this.save(); + }); + } }, beforeSubmit: function(submission) { $.each(formErrors, function() { @@ -457,6 +481,34 @@ return false; }); } + // Show form buttons as part of the dialog + if ($el.data('uiDialog')) { + var buttonContainers = '.crm-submit-buttons, .action-link', + buttons = [], + added = []; + $(buttonContainers, $el).find('input.crm-form-submit, a.button').each(function() { + var $el = $(this), + label = $el.is('input') ? $el.attr('value') : $el.text(), + identifier = $el.attr('name') || $el.attr('href'); + if (!identifier || identifier === '#' || $.inArray(identifier, added) < 0) { + var $icon = $el.find('.icon'), + button = {'data-identifier': identifier, text: label, click: function() { + $el.click(); + }}; + if ($icon.length) { + button.icons = {primary: $icon.attr('class')}; + } else { + var action = $el.hasClass('cancel') ? 'close' : (identifier.substr(identifier.length-4) === '_new' ? 'plus' : 'check'); + button.icons = {primary: 'ui-icon-' + action}; + } + buttons.push(button); + added.push(identifier); + } + // display:none causes the form to not submit when pressing "enter" + $el.parents(buttonContainers).css({height: 0, padding: 0, margin: 0, overflow: 'hidden'}); + }); + $el.dialog('option', 'buttons', buttons); + } // Allow a button to prevent ajax submit $('input[data-no-ajax-submit=true]').click(function() { $(this).closest('form').ajaxFormUnbind(); @@ -487,9 +539,6 @@ else if ($el.hasClass('medium-popup')) { settings.dialog.width = settings.dialog.height = '50%'; } - else if ($el.hasClass('huge-popup')) { - settings.dialog.height = '90%'; - } var dialog = popup(url, settings); // Trigger events from the dialog on the original link element $el.trigger('crmPopupOpen', [dialog]);