From fee3e3c38ae99e740e5015091a0d1d2cf2cba825 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 9 Jan 2016 19:02:58 -0500 Subject: [PATCH] Api explorer optimizations --- api/v3/Generic/Getlist.php | 2 +- templates/CRM/Admin/Page/APIExplorer.js | 83 ++++++++++++++----------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/api/v3/Generic/Getlist.php b/api/v3/Generic/Getlist.php index 683fb44c24..12aa202338 100644 --- a/api/v3/Generic/Getlist.php +++ b/api/v3/Generic/Getlist.php @@ -267,7 +267,7 @@ function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) { 'type' => CRM_Utils_Type::T_TEXT, ), 'label_field' => array( - 'title' => 'Search Field', + 'title' => 'Label Field', 'description' => "Field to display as title of results (usually automatic)", 'type' => CRM_Utils_Type::T_TEXT, ), diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 6425d19b34..d1d81e7327 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -7,6 +7,8 @@ actions = {values: ['get']}, fields = [], getFieldData = {}, + getFieldsCache = {}, + getActionsCache = {}, params = {}, smartyPhp, entityDoc, @@ -15,6 +17,7 @@ returnTpl = _.template($('#api-return-tpl').html()), chainTpl = _.template($('#api-chain-tpl').html()), docCodeTpl = _.template($('#doc-code-tpl').html()), + joinTpl = _.template($('#join-tpl').html()), // These types of entityRef don't require any input to open // FIXME: ought to be in getfields metadata @@ -122,9 +125,37 @@ } /** - * Fetch fields for entity+action + * Fetch metadata from the api and cache locally for performance + * Returns a deferred object which resolves to entity.getfields */ - function getFields(changedElement) { + function getMetadata(entity, action) { + var response = $.Deferred(); + if (getFieldsCache[entity+action]) { + response.resolve(getFieldsCache[entity+action]); + } else { + var apiCalls = { + getfields: [entity, 'getfields', { + api_action: action, + options: {get_options: 'all', get_options_context: 'match'} + }] + }; + if (!getActionsCache[entity]) { + apiCalls.getactions = [entity, 'getactions']; + } + CRM.api3(apiCalls) + .done(function(data) { + getFieldsCache[entity+action] = data.getfields; + getActionsCache[entity] = getActionsCache[entity] || data.getactions; + response.resolve(data.getfields); + }); + } + return response; + } + + /** + * Respond to changing the main entity+action + */ + function onChangeEntityOrAction(changedElement) { var required = []; fields = []; getFieldData = {}; @@ -143,7 +174,13 @@ showFields(['api_action']); return; } - CRM.api3(entity, 'getfields', {'api_action': action, options: {get_options: 'all', get_options_context: 'match'}}).done(function(data) { + getMetadata(entity, action).done(function(data) { + if ($(changedElement).is('#api-entity')) { + actions = getActionsCache[entity]; + populateActions(); + if (data.deprecated) CRM.alert(data.deprecated, entity + ' Deprecated'); + } + onChangeAction(action); _.each(data.values, function(field) { if (field.name) { getFieldData[field.name] = field; @@ -159,9 +196,6 @@ } } }); - if ($(changedElement).is('#api-entity') && data.deprecated) { - CRM.alert(data.deprecated, entity + ' Deprecated'); - } showFields(required); if (action === 'get' || action === 'getsingle' || action == 'getvalue' || action === 'getstat') { showReturn(); @@ -193,22 +227,6 @@ $('#api-return-value').crmSelect2(params); } - /** - * Fetch actions for entity - */ - function getActions() { - if (entity) { - $('#api-action').addClass('loading'); - CRM.api3(entity, 'getactions').done(function(data) { - actions = data; - populateActions(); - }); - } else { - actions = {values: ['get']}; - populateActions(); - } - } - /** * Test whether an action is deprecated * @param action @@ -756,20 +774,13 @@ entity = $('#api-entity').val(); action = $('#api-action').val(); if ($(this).is('#api-entity')) { - getActions(); - } else { - onChangeAction(action); - } - if (entity && action) { - $('#api-params').html(''); - $('#api-params-table thead').show(); - getFields(this); - buildParams(); - checkBookKeepingEntity(entity, action); - } else { - $('#api-params, #api-generated pre').empty(); - $('#api-param-buttons, #api-params-table thead').hide(); + $('#api-action').addClass('loading'); } + $('#api-params').html(''); + $('#api-params-table thead').show(); + onChangeEntityOrAction(this); + buildParams(); + checkBookKeepingEntity(entity, action); }) .on('change keyup', 'input.api-input, #api-params select', buildParams) .on('submit', submit); @@ -805,6 +816,6 @@ e.preventDefault(); addChainField(); }); - $('#api-entity').change(); + populateActions(); }); }(CRM.$, CRM._)); -- 2.25.1