X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2Fcrm.ajax.js;h=bfff407843dc2e02e4723c866e431ecc48d8854d;hb=25452108199c046e749701e072779c2492837315;hp=e1742da8fd9873c3bd318435de350b867686df80;hpb=0d5f2c6138df8949fac02b98d9487fd464a2ee3c;p=civicrm-core.git diff --git a/js/crm.ajax.js b/js/crm.ajax.js index e1742da8fd..bfff407843 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, undefined) { +(function($, CRM, _, undefined) { /** * @param string path * @param string|object query @@ -23,13 +23,13 @@ } query = query || ''; var frag = path.split('?'); - var url = tplURL[mode].replace("*path*", frag[0]); + var url = tplURL[mode].replace("civicrm-placeholder-url-path", frag[0]); if (!query) { - url = url.replace(/[?&]\*query\*/, ''); + url = url.replace(/[?&]civicrm-placeholder-url-query=1/, ''); } else { - url = url.replace("*query*", typeof query === 'string' ? query : $.param(query)); + url = url.replace("civicrm-placeholder-url-query=1", typeof query === 'string' ? query : $.param(query)); } if (frag[1]) { url += (url.indexOf('?') < 0 ? '?' : '&') + frag[1]; @@ -45,6 +45,47 @@ }); }; + // result is an array, but in js, an array is also an object + // Assign all the metadata properties to it, mirroring the results arrayObject in php + function arrayObject(data) { + var result = data.values || []; + if (_.isArray(result)) { + delete(data.values); + _.assign(result, data); + } + return result; + } + + CRM.api4 = function(entity, action, params, index) { + return new Promise(function(resolve, reject) { + if (typeof entity === 'string') { + $.post(CRM.url('civicrm/ajax/api4/' + entity + '/' + action), { + params: JSON.stringify(params), + index: index + }) + .done(function (data) { + resolve(arrayObject(data)); + }) + .fail(function (data) { + reject(data.responseJSON); + }); + } else { + $.post(CRM.url('civicrm/ajax/api4'), { + calls: JSON.stringify(entity) + }) + .done(function(data) { + _.each(data, function(item, key) { + data[key] = arrayObject(item); + }); + resolve(data); + }) + .fail(function (data) { + reject(data.responseJSON); + }); + } + }); + }; + /** * AJAX api * @link http://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface#AJAXInterface-CRM.api3 @@ -164,11 +205,11 @@ return false; } // Compare arguments - $.each(newUrl.split('?')[1].split('&'), function(k, v) { + $.each((newUrl.split('?')[1] || '').split('&'), function(k, v) { var arg = v.split('='); args[arg[0]] = arg[1]; }); - $.each(oldUrl.split('?')[1].split('&'), function(k, v) { + $.each((oldUrl.split('?')[1] || '').split('&'), function(k, v) { var arg = v.split('='); if (args[arg[0]] !== undefined && arg[1] !== args[arg[0]]) { same = false; @@ -456,7 +497,7 @@ var buttonContainers = '.crm-submit-buttons, .action-link', buttons = [], added = []; - $(buttonContainers, $el).find('input.crm-form-submit, a.button').each(function() { + $(buttonContainers, $el).find('input.crm-form-submit, a.button, button').each(function() { var $el = $(this), label = $el.is('input') ? $el.attr('value') : $el.text(), identifier = $el.attr('name') || $el.attr('href'); @@ -572,8 +613,11 @@ var currentHeight = $wrapper.outerHeight(), padding = currentHeight - $dialog.height(), newHeight = $dialog.prop('scrollHeight') + padding, - menuHeight = $('#civicrm-menu').outerHeight(), - maxHeight = $(window).height() - menuHeight; + menuHeight = $('#civicrm-menu').outerHeight(); + if ($('body').hasClass('crm-menubar-below-cms-menu')) { + menuHeight += $('#civicrm-menu').offset().top; + } + var maxHeight = $(window).height() - menuHeight; newHeight = newHeight > maxHeight ? maxHeight : newHeight; if (newHeight > (currentHeight + 15)) { $dialog.dialog('option', { @@ -585,4 +629,4 @@ }); }); -}(jQuery, CRM)); +}(jQuery, CRM, _));