From 6e29772f6b8f385a34433ebf735d56e735b2158b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 31 Jul 2015 16:41:57 -0400 Subject: [PATCH] API Explorer - Default contact-ref to select current user --- templates/CRM/Admin/Page/APIExplorer.js | 35 +++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index b2b0794bfc..b2aeb0877d 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -319,20 +319,23 @@ operator = $('.api-param-op', $row).val(), $valField = $('input.api-param-value', $row), multiSelect = isMultiSelect(name, operator), - currentVal = $valField.val(); + currentVal = $valField.val(), + wasSelect = $valField.data('select2'); + if (wasSelect) { + $valField.crmEntityRef('destroy'); + } + $valField.attr('placeholder', ts('Value')); // Boolean fields only have 1 possible value if (_.includes(BOOL, operator)) { - if ($valField.data('select2')) { - $valField.select2('destroy'); - } $valField.css('visibility', 'hidden').val('1'); return; } $valField.css('visibility', ''); // Option list or entityRef input if (isSelect(name, operator)) { + $valField.attr('placeholder', ts('- select -')); // Reset value before switching to a select from something else - if ($(this).is('.api-param-name') || !$valField.data('select2')) { + if ($(this).is('.api-param-name') || !wasSelect) { $valField.val(''); } // When switching from multi-select to single select @@ -356,19 +359,16 @@ } // EntityRef else { + var entity = getFieldData[name].FKApiName; + $valField.attr('placeholder', entity == 'Contact' ? '[' + ts('Auto-Select Current User') + ']' : ts('- select -')); $valField.crmEntityRef({ - entity: getFieldData[name].FKApiName, + entity: entity, select: { multiple: multiSelect, - minimumInputLength: _.includes(OPEN_IMMEDIATELY, getFieldData[name].FKApiName) ? 0 : 1 + minimumInputLength: _.includes(OPEN_IMMEDIATELY, entity) ? 0 : 1 } }); } - return; - } - // Plain text input - if ($valField.data('select2')) { - $valField.select2('destroy'); } } @@ -419,6 +419,7 @@ /** * Format value to look like php code + * TODO: Use short array syntax when we drop support for php 5.3 * @param val */ function phpFormat(val) { @@ -486,6 +487,10 @@ name = 'options'; } } + // Default for contact ref fields + if ($(this).is('.crm-contact-ref') && input === '') { + val = evaluate('user_contact_id', makeArray); + } if (name && val !== undefined) { params[name] = op === '=' ? val : (params[name] || {}); if (op !== '=') { @@ -737,8 +742,10 @@ .on('change', 'input.api-param-name, select.api-param-op', renderValueField) .on('change', 'input.api-param-name, .api-option-name', function() { if ($(this).val() === '-' && $(this).data('select2')) { - $(this).select2('destroy'); - $(this).val('').focus(); + $(this) + .crmSelect2('destroy') + .val('') + .focus(); } }) .on('click', '.api-param-remove', function(e) { -- 2.25.1