| 1 | (function($, _, undefined) { |
| 2 | "use strict"; |
| 3 | /* jshint validthis: true */ |
| 4 | var |
| 5 | entity, |
| 6 | action, |
| 7 | actions = {values: ['get']}, |
| 8 | fields = [], |
| 9 | getFieldData = {}, |
| 10 | options = {}, |
| 11 | params = {}, |
| 12 | smartyStub, |
| 13 | entityDoc, |
| 14 | fieldTpl = _.template($('#api-param-tpl').html()), |
| 15 | optionsTpl = _.template($('#api-options-tpl').html()), |
| 16 | returnTpl = _.template($('#api-return-tpl').html()), |
| 17 | chainTpl = _.template($('#api-chain-tpl').html()), |
| 18 | docCodeTpl = _.template($('#doc-code-tpl').html()), |
| 19 | |
| 20 | // These types of entityRef don't require any input to open |
| 21 | OPEN_IMMEDIATELY = ['RelationshipType', 'Event', 'Group', 'Tag'], |
| 22 | |
| 23 | // Actions that don't support fancy operators |
| 24 | NO_OPERATORS = ['create', 'update', 'delete', 'setvalue', 'getoptions', 'getactions', 'getfields'], |
| 25 | |
| 26 | // Operators with special properties |
| 27 | BOOL = ['IS NULL', 'IS NOT NULL'], |
| 28 | TEXT = ['LIKE', 'NOT LIKE'], |
| 29 | MULTI = ['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']; |
| 30 | |
| 31 | /** |
| 32 | * Call prettyPrint function and perform additional formatting |
| 33 | * @param ele |
| 34 | */ |
| 35 | function prettyPrint(ele) { |
| 36 | if (typeof window.prettyPrint === 'function') { |
| 37 | $(ele).removeClass('prettyprinted').addClass('prettyprint'); |
| 38 | |
| 39 | window.prettyPrint(); |
| 40 | |
| 41 | // Highlight errors in api result |
| 42 | $('span:contains("error_code"),span:contains("error_message")', '#api-result') |
| 43 | .siblings('span.str').css('color', '#B00'); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Add a "fields" row |
| 49 | * @param name |
| 50 | */ |
| 51 | function addField(name) { |
| 52 | $('#api-params').append($(fieldTpl({name: name || '', noOps: _.includes(NO_OPERATORS, action)}))); |
| 53 | var $row = $('tr:last-child', '#api-params'); |
| 54 | $('input.api-param-name', $row).crmSelect2({ |
| 55 | data: fields.concat({id: '-', text: ts('Other') + '...', description: ts('Choose a field not in this list')}), |
| 56 | formatResult: function(field) { |
| 57 | return field.text + '<div class="api-field-desc">' + field.description + '</div>'; |
| 58 | } |
| 59 | }).change(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add a new "options" row |
| 64 | */ |
| 65 | function addOptionField() { |
| 66 | if ($('.api-options-row', '#api-params').length) { |
| 67 | $('.api-options-row:last', '#api-params').after($(optionsTpl({}))); |
| 68 | } else { |
| 69 | $('#api-params').append($(optionsTpl({}))); |
| 70 | } |
| 71 | var $row = $('.api-options-row:last', '#api-params'); |
| 72 | $('.api-option-name', $row).crmSelect2({data: [ |
| 73 | {id: 'limit', text: 'limit'}, |
| 74 | {id: 'offset', text: 'offset'}, |
| 75 | {id: 'sort', text: 'sort'}, |
| 76 | {id: 'metadata', text: 'metadata'}, |
| 77 | {id: '-', text: ts('Other') + '...'} |
| 78 | ]}); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add an "api chain" row |
| 83 | */ |
| 84 | function addChainField() { |
| 85 | $('#api-params').append($(chainTpl({}))); |
| 86 | var $row = $('tr:last-child', '#api-params'); |
| 87 | $('.api-chain-entity', $row).crmSelect2({ |
| 88 | formatSelection: function(item) { |
| 89 | return '<span class="icon ui-icon-link"></span> API ' + |
| 90 | ($(item.element).hasClass('strikethrough') ? '<span class="strikethrough">' + item.text + '</span>' : item.text); |
| 91 | }, |
| 92 | placeholder: '<span class="icon ui-icon-link"></span> ' + ts('Entity'), |
| 93 | escapeMarkup: function(m) {return m;} |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Fetch available actions for selected chained entity |
| 99 | */ |
| 100 | function getChainedAction() { |
| 101 | var |
| 102 | $selector = $(this), |
| 103 | entity = $selector.val(), |
| 104 | $row = $selector.closest('tr'); |
| 105 | if (entity) { |
| 106 | $selector.prop('disabled', true); |
| 107 | CRM.api3(entity, 'getactions') |
| 108 | .done(function(actions) { |
| 109 | $selector.prop('disabled', false); |
| 110 | CRM.utils.setOptions($('.api-chain-action', $row), _.transform(actions.values, function(ret, item) {ret.push({value: item, key: item});})); |
| 111 | }); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Fetch fields for entity+action |
| 117 | */ |
| 118 | function getFields(changedElement) { |
| 119 | var required = []; |
| 120 | fields = []; |
| 121 | options = getFieldData = {}; |
| 122 | // Special case for getfields |
| 123 | if (action === 'getfields') { |
| 124 | fields.push({ |
| 125 | id: 'api_action', |
| 126 | text: 'Action' |
| 127 | }); |
| 128 | options.api_action = _.reduce(actions.values, function(ret, item) { |
| 129 | ret[item] = item; |
| 130 | return ret; |
| 131 | }, {}); |
| 132 | showFields(['api_action']); |
| 133 | return; |
| 134 | } |
| 135 | CRM.api3(entity, 'getFields', {'api_action': action, options: {get_options: 'all'}}).done(function(data) { |
| 136 | getFieldData = data.values; |
| 137 | _.each(data.values, function(field) { |
| 138 | if (field.name) { |
| 139 | fields.push({ |
| 140 | id: field.name, |
| 141 | text: field.title || field.name, |
| 142 | multi: !!field['api.multiple'], |
| 143 | description: field.description || '', |
| 144 | required: !(!field['api.required'] || field['api.required'] === '0') |
| 145 | }); |
| 146 | if (field['api.required'] && field['api.required'] !== '0') { |
| 147 | required.push(field.name); |
| 148 | } |
| 149 | if (field.options) { |
| 150 | options[field.name] = field.options; |
| 151 | } |
| 152 | } |
| 153 | }); |
| 154 | if ($(changedElement).is('#api-entity') && data.deprecated) { |
| 155 | CRM.alert(data.deprecated, entity + ' Deprecated'); |
| 156 | } |
| 157 | showFields(required); |
| 158 | if (action === 'get' || action === 'getsingle' || action === 'getstat') { |
| 159 | showReturn(action === 'getstat' ? ts('Group by') : ts('Fields to return')); |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * For "get" actions show the "return" options |
| 166 | */ |
| 167 | function showReturn(title) { |
| 168 | $('#api-params').prepend($(returnTpl({title: title}))); |
| 169 | $('#api-return-value').crmSelect2({data: fields, multiple: true}); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Fetch actions for entity |
| 174 | */ |
| 175 | function getActions() { |
| 176 | if (entity) { |
| 177 | $('#api-action').addClass('loading'); |
| 178 | CRM.api3(entity, 'getactions').done(function(data) { |
| 179 | actions = data; |
| 180 | populateActions(); |
| 181 | }); |
| 182 | } else { |
| 183 | actions = {values: ['get']}; |
| 184 | populateActions(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Test whether an action is deprecated |
| 190 | * @param action |
| 191 | * @returns {boolean} |
| 192 | */ |
| 193 | function isActionDeprecated(action) { |
| 194 | return !!(typeof actions.deprecated === 'object' && actions.deprecated[action]); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Render action text depending on deprecation status |
| 199 | * @param option |
| 200 | * @returns {string} |
| 201 | */ |
| 202 | function renderAction(option) { |
| 203 | return isActionDeprecated(option.id) ? '<span class="strikethrough">' + option.text + '</span>' : option.text; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Called after getActions to populate action list |
| 208 | */ |
| 209 | function populateActions() { |
| 210 | var val = $('#api-action').val(); |
| 211 | $('#api-action').removeClass('loading').select2({ |
| 212 | data: _.transform(actions.values, function(ret, item) {ret.push({text: item, id: item});}), |
| 213 | formatSelection: renderAction, |
| 214 | formatResult: renderAction |
| 215 | }); |
| 216 | // If previously selected action is not available, set it to 'get' if possible |
| 217 | if (!_.includes(actions.values, val)) { |
| 218 | $('#api-action').select2('val', !_.includes(actions.values, 'get') ? actions.values[0] : 'get', true); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Check for and display action-specific deprecation notices |
| 224 | * @param action |
| 225 | */ |
| 226 | function onChangeAction(action) { |
| 227 | if (isActionDeprecated(action)) { |
| 228 | CRM.alert(actions.deprecated[action], action + ' deprecated'); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Called after getfields to show buttons and required fields |
| 234 | * @param required |
| 235 | */ |
| 236 | function showFields(required) { |
| 237 | $('#api-params').empty(); |
| 238 | $('#api-param-buttons').show(); |
| 239 | if (required.length) { |
| 240 | _.each(required, addField); |
| 241 | } else { |
| 242 | addField(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | function isYesNo(fieldName) { |
| 247 | return getFieldData[fieldName] && getFieldData[fieldName].type === 16; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Should we render a select or textfield? |
| 252 | * |
| 253 | * @param fieldName |
| 254 | * @param operator |
| 255 | * @returns boolean |
| 256 | */ |
| 257 | function isSelect(fieldName, operator) { |
| 258 | return (isYesNo(fieldName) || options[fieldName] || (getFieldData[fieldName] && getFieldData[fieldName].FKApiName)) && !_.includes(TEXT, operator); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Should we render a select as single or multi? |
| 263 | * |
| 264 | * @param fieldName |
| 265 | * @param operator |
| 266 | * @returns boolean |
| 267 | */ |
| 268 | function isMultiSelect(fieldName, operator) { |
| 269 | if (isYesNo(fieldName)) { |
| 270 | return false; |
| 271 | } |
| 272 | if (_.includes(MULTI, operator)) { |
| 273 | return true; |
| 274 | } |
| 275 | // The = operator is ambiguous but all others can be safely assumed to be single |
| 276 | if (operator !== '=') { |
| 277 | return false; |
| 278 | } |
| 279 | return true; |
| 280 | /* |
| 281 | * Attempt to resolve the ambiguity of the = operator using metadata |
| 282 | * commented out because there is not enough metadata in the api at this time |
| 283 | * to accurately figure it out. |
| 284 | */ |
| 285 | // var field = fieldName && _.find(fields, 'id', fieldName); |
| 286 | // return field && field.multi; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Render value input as a textfield, option list, entityRef, or hidden, |
| 291 | * Depending on selected param name and operator |
| 292 | */ |
| 293 | function renderValueField() { |
| 294 | var $row = $(this).closest('tr'), |
| 295 | name = $('input.api-param-name', $row).val(), |
| 296 | operator = $('.api-param-op', $row).val(), |
| 297 | $valField = $('input.api-param-value', $row), |
| 298 | multiSelect = isMultiSelect(name, operator), |
| 299 | currentVal = $valField.val(); |
| 300 | // Boolean fields only have 1 possible value |
| 301 | if (_.includes(BOOL, operator)) { |
| 302 | if ($valField.data('select2')) { |
| 303 | $valField.select2('destroy'); |
| 304 | } |
| 305 | $valField.css('visibility', 'hidden').val('1'); |
| 306 | return; |
| 307 | } |
| 308 | $valField.css('visibility', ''); |
| 309 | // Option list or entityRef input |
| 310 | if (isSelect(name, operator)) { |
| 311 | // Reset value before switching to a select from something else |
| 312 | if ($(this).is('.api-param-name') || !$valField.data('select2')) { |
| 313 | $valField.val(''); |
| 314 | } |
| 315 | // When switching from multi-select to single select |
| 316 | else if (!multiSelect && _.includes(currentVal, ',')) { |
| 317 | $valField.val(currentVal.split(',')[0]); |
| 318 | } |
| 319 | // Yes-No options |
| 320 | if (isYesNo(name)) { |
| 321 | $valField.select2({ |
| 322 | data: [{id: 1, text: ts('Yes')}, {id: 0, text: ts('No')}] |
| 323 | }); |
| 324 | } |
| 325 | // Select options |
| 326 | else if (options[name]) { |
| 327 | $valField.select2({ |
| 328 | multiple: multiSelect, |
| 329 | data: _.map(options[name], function (value, key) { |
| 330 | return {id: key, text: value}; |
| 331 | }) |
| 332 | }); |
| 333 | } |
| 334 | // EntityRef |
| 335 | else { |
| 336 | $valField.crmEntityRef({ |
| 337 | entity: getFieldData[name].FKApiName, |
| 338 | select: { |
| 339 | multiple: multiSelect, |
| 340 | minimumInputLength: _.includes(OPEN_IMMEDIATELY, getFieldData[name].FKApiName) ? 0 : 1 |
| 341 | } |
| 342 | }); |
| 343 | } |
| 344 | return; |
| 345 | } |
| 346 | // Plain text input |
| 347 | if ($valField.data('select2')) { |
| 348 | $valField.select2('destroy'); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Attempt to parse a string into a value of the intended type |
| 354 | * @param val string |
| 355 | * @param makeArray bool |
| 356 | */ |
| 357 | function evaluate(val, makeArray) { |
| 358 | try { |
| 359 | if (!val.length) { |
| 360 | return makeArray ? [] : ''; |
| 361 | } |
| 362 | var first = val.charAt(0), |
| 363 | last = val.slice(-1); |
| 364 | // Simple types |
| 365 | if (val === 'true' || val === 'false' || val === 'null') { |
| 366 | /* jshint evil: true */ |
| 367 | return eval(val); |
| 368 | } |
| 369 | // Quoted strings |
| 370 | if ((first === '"' || first === "'") && last === first) { |
| 371 | return val.slice(1, -1); |
| 372 | } |
| 373 | // Parse json - use eval rather than $.parseJSON because it's less strict about formatting |
| 374 | if ((first === '[' && last === ']') || (first === '{' && last === '}')) { |
| 375 | return eval('(' + val + ')'); |
| 376 | } |
| 377 | // Transform csv to array |
| 378 | if (makeArray) { |
| 379 | var result = []; |
| 380 | $.each(val.split(','), function(k, v) { |
| 381 | result.push(evaluate($.trim(v)) || v); |
| 382 | }); |
| 383 | return result; |
| 384 | } |
| 385 | // Integers - skip any multidigit number that starts with 0 to avoid oddities (it will be treated as a string below) |
| 386 | if (!isNaN(val) && val.search(/[^\d]/) < 0 && (val.length === 1 || first !== '0')) { |
| 387 | return parseInt(val, 10); |
| 388 | } |
| 389 | // Ok ok it's really a string |
| 390 | return val; |
| 391 | } catch(e) { |
| 392 | // If eval crashed return undefined |
| 393 | return undefined; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Format value to look like php code |
| 399 | * @param val |
| 400 | */ |
| 401 | function phpFormat(val) { |
| 402 | var ret = ''; |
| 403 | if ($.isPlainObject(val)) { |
| 404 | $.each(val, function(k, v) { |
| 405 | ret += (ret ? ', ' : '') + "'" + k + "' => " + phpFormat(v); |
| 406 | }); |
| 407 | return 'array(' + ret + ')'; |
| 408 | } |
| 409 | if ($.isArray(val)) { |
| 410 | $.each(val, function(k, v) { |
| 411 | ret += (ret ? ', ' : '') + phpFormat(v); |
| 412 | }); |
| 413 | return 'array(' + ret + ')'; |
| 414 | } |
| 415 | return JSON.stringify(val).replace(/\$/g, '\\$'); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Smarty doesn't support array literals so we provide a stub |
| 420 | * @param js string |
| 421 | * @param key string |
| 422 | */ |
| 423 | function smartyFormat(js, key) { |
| 424 | if (_.includes(js, '[') || _.includes(js, '{')) { |
| 425 | smartyStub = true; |
| 426 | return '$' + key.replace(/[. -]/g, '_'); |
| 427 | } |
| 428 | return js; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Create the params array from user input |
| 433 | * @param e |
| 434 | */ |
| 435 | function buildParams(e) { |
| 436 | params = {}; |
| 437 | $('.api-param-checkbox:checked').each(function() { |
| 438 | params[this.name] = 1; |
| 439 | }); |
| 440 | $('input.api-param-value, input.api-option-value').each(function() { |
| 441 | var $row = $(this).closest('tr'), |
| 442 | input = $(this).val(), |
| 443 | op = $('select.api-param-op', $row).val() || '=', |
| 444 | name = $('input.api-param-name', $row).val(), |
| 445 | // Workaround for ambiguity of the = operator |
| 446 | makeArray = (op === '=' && isSelect(name, op)) ? _.includes(input, ',') : op !== '=' && isMultiSelect(name, op), |
| 447 | val = evaluate(input, makeArray); |
| 448 | |
| 449 | // Ignore blank values for the return field |
| 450 | if ($(this).is('#api-return-value') && !val) { |
| 451 | return; |
| 452 | } |
| 453 | // Special syntax for api chaining |
| 454 | if (!name && $('select.api-chain-entity', $row).val()) { |
| 455 | name = 'api.' + $('select.api-chain-entity', $row).val() + '.' + $('select.api-chain-action', $row).val(); |
| 456 | } |
| 457 | // Special handling for options |
| 458 | if ($(this).is('.api-option-value')) { |
| 459 | op = $('input.api-option-name', $row).val(); |
| 460 | if (op) { |
| 461 | name = 'options'; |
| 462 | } |
| 463 | } |
| 464 | if (name && val !== undefined) { |
| 465 | params[name] = op === '=' ? val : (params[name] || {}); |
| 466 | if (op !== '=') { |
| 467 | params[name][op] = val; |
| 468 | } |
| 469 | if ($(this).hasClass('crm-error')) { |
| 470 | clearError(this); |
| 471 | } |
| 472 | } |
| 473 | else if (name && (!e || e.type !== 'keyup')) { |
| 474 | setError(this); |
| 475 | } |
| 476 | }); |
| 477 | if (entity && action) { |
| 478 | formatQuery(); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Display error message on incorrectly-formatted params |
| 484 | * @param el |
| 485 | */ |
| 486 | function setError(el) { |
| 487 | if (!$(el).hasClass('crm-error')) { |
| 488 | var msg = ts('Syntax error: input should be valid JSON or a quoted string.'); |
| 489 | $(el) |
| 490 | .addClass('crm-error') |
| 491 | .css('width', '82%') |
| 492 | .attr('title', msg) |
| 493 | .before('<div class="icon red-icon ui-icon-alert" title="'+msg+'"/>') |
| 494 | .tooltip(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Remove error message |
| 500 | * @param el |
| 501 | */ |
| 502 | function clearError(el) { |
| 503 | $(el) |
| 504 | .removeClass('crm-error') |
| 505 | .attr('title', '') |
| 506 | .css('width', '85%') |
| 507 | .tooltip('destroy') |
| 508 | .siblings('.ui-icon-alert').remove(); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Render the api request in various formats |
| 513 | */ |
| 514 | function formatQuery() { |
| 515 | var i = 0, q = { |
| 516 | smarty: "{crmAPI var='result' entity='" + entity + "' action='" + action + "'", |
| 517 | php: "$result = civicrm_api3('" + entity + "', '" + action + "'", |
| 518 | json: "CRM.api3('" + entity + "', '" + action + "'", |
| 519 | drush: "drush cvapi " + entity + '.' + action + ' ', |
| 520 | wpcli: "wp cv api " + entity + '.' + action + ' ', |
| 521 | rest: CRM.config.resourceBase + "extern/rest.php?entity=" + entity + "&action=" + action + "&json=" + JSON.stringify(params) + "&api_key=yourkey&key=sitekey" |
| 522 | }; |
| 523 | smartyStub = false; |
| 524 | $.each(params, function(key, value) { |
| 525 | var js = JSON.stringify(value); |
| 526 | if (!(i++)) { |
| 527 | q.php += ", array(\n"; |
| 528 | q.json += ", {\n"; |
| 529 | } else { |
| 530 | q.json += ",\n"; |
| 531 | } |
| 532 | q.php += " '" + key + "' => " + phpFormat(value) + ",\n"; |
| 533 | q.json += " \"" + key + '": ' + js; |
| 534 | q.smarty += ' ' + key + '=' + smartyFormat(js, key); |
| 535 | // FIXME: This is not totally correct cli syntax |
| 536 | q.drush += key + '=' + js + ' '; |
| 537 | q.wpcli += key + '=' + js + ' '; |
| 538 | }); |
| 539 | if (i) { |
| 540 | q.php += ")"; |
| 541 | q.json += "\n}"; |
| 542 | } |
| 543 | q.php += ");"; |
| 544 | q.json += ").done(function(result) {\n // do something\n});"; |
| 545 | q.smarty += "}\n{foreach from=$result.values item=" + entity.toLowerCase() + "}\n {$" + entity.toLowerCase() + ".some_field}\n{/foreach}"; |
| 546 | if (!_.includes(action, 'get')) { |
| 547 | q.smarty = '{* Smarty API only works with get actions *}'; |
| 548 | } else if (smartyStub) { |
| 549 | q.smarty = "{* Smarty does not have a syntax for array literals; assign complex variables from php *}\n" + q.smarty; |
| 550 | } |
| 551 | $.each(q, function(type, val) { |
| 552 | $('#api-' + type).text(val); |
| 553 | }); |
| 554 | prettyPrint('#api-generated pre'); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Submit button handler |
| 559 | * @param e |
| 560 | */ |
| 561 | function submit(e) { |
| 562 | e.preventDefault(); |
| 563 | if (!entity || !action) { |
| 564 | alert(ts('Select an entity.')); |
| 565 | return; |
| 566 | } |
| 567 | if (!_.includes(action, 'get') && action != 'check') { |
| 568 | var msg = action === 'delete' ? ts('This will delete data from CiviCRM. Are you sure?') : ts('This will write to the database. Continue?'); |
| 569 | CRM.confirm({title: ts('Confirm %1', {1: action}), message: msg}).on('crmConfirm:yes', execute); |
| 570 | } else { |
| 571 | execute(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Execute api call and display the results |
| 577 | * Note: We have to manually execute the ajax in order to add the secret extra "prettyprint" param |
| 578 | */ |
| 579 | function execute() { |
| 580 | $('#api-result').html('<div class="crm-loading-element"></div>'); |
| 581 | $.ajax({ |
| 582 | url: CRM.url('civicrm/ajax/rest'), |
| 583 | data: { |
| 584 | entity: entity, |
| 585 | action: action, |
| 586 | prettyprint: 1, |
| 587 | json: JSON.stringify(params) |
| 588 | }, |
| 589 | type: _.includes(action, 'get') ? 'GET' : 'POST', |
| 590 | dataType: 'text' |
| 591 | }).done(function(text) { |
| 592 | $('#api-result').text(text); |
| 593 | prettyPrint('#api-result'); |
| 594 | }); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Fetch list of example files for a given entity |
| 599 | */ |
| 600 | function getExamples() { |
| 601 | CRM.utils.setOptions($('#example-action').prop('disabled', true).addClass('loading'), []); |
| 602 | $.getJSON(CRM.url('civicrm/ajax/apiexample', {entity: $(this).val()})) |
| 603 | .done(function(result) { |
| 604 | CRM.utils.setOptions($('#example-action').prop('disabled', false).removeClass('loading'), result); |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Fetch and display an example file |
| 610 | */ |
| 611 | function getExample() { |
| 612 | var |
| 613 | entity = $('#example-entity').val(), |
| 614 | action = $('#example-action').val(); |
| 615 | if (entity && action) { |
| 616 | $('#example-result').block(); |
| 617 | $.get(CRM.url('civicrm/ajax/apiexample', {file: entity + '/' + action})) |
| 618 | .done(function(result) { |
| 619 | $('#example-result').unblock().text(result); |
| 620 | prettyPrint('#example-result'); |
| 621 | }); |
| 622 | } else { |
| 623 | $('#example-result').text($('#example-result').attr('placeholder')); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Fetch entity docs & actions |
| 629 | */ |
| 630 | function getDocEntity() { |
| 631 | CRM.utils.setOptions($('#doc-action').prop('disabled', true).addClass('loading'), []); |
| 632 | $.getJSON(CRM.url('civicrm/ajax/apidoc', {entity: $(this).val()})) |
| 633 | .done(function(result) { |
| 634 | entityDoc = result.doc; |
| 635 | CRM.utils.setOptions($('#doc-action').prop('disabled', false).removeClass('loading'), result.actions); |
| 636 | $('#doc-result').html(result.doc); |
| 637 | prettyPrint('#doc-result pre'); |
| 638 | }); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Fetch entity+action docs & code |
| 643 | */ |
| 644 | function getDocAction() { |
| 645 | var |
| 646 | entity = $('#doc-entity').val(), |
| 647 | action = $('#doc-action').val(); |
| 648 | if (entity && action) { |
| 649 | $('#doc-result').block(); |
| 650 | $.get(CRM.url('civicrm/ajax/apidoc', {entity: entity, action: action})) |
| 651 | .done(function(result) { |
| 652 | $('#doc-result').unblock().html(result.doc); |
| 653 | if (result.code) { |
| 654 | $('#doc-result').append(docCodeTpl(result)); |
| 655 | } |
| 656 | prettyPrint('#doc-result pre'); |
| 657 | }); |
| 658 | } else { |
| 659 | $('#doc-result').html(entityDoc); |
| 660 | prettyPrint('#doc-result pre'); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | $(document).ready(function() { |
| 665 | // Set up tabs - bind active tab to document hash because... it's cool? |
| 666 | document.location.hash = document.location.hash || 'explorer'; |
| 667 | $('#mainTabContainer') |
| 668 | .tabs({ |
| 669 | active: $(document.location.hash + '-tab').index() - 1 |
| 670 | }) |
| 671 | .on('tabsactivate', function(e, ui) { |
| 672 | if (ui.newPanel) { |
| 673 | document.location.hash = ui.newPanel.attr('id').replace('-tab', ''); |
| 674 | } |
| 675 | }); |
| 676 | $(window).on('hashchange', function() { |
| 677 | $('#mainTabContainer').tabs('option', 'active', $(document.location.hash + '-tab').index() - 1); |
| 678 | }); |
| 679 | |
| 680 | // Initialize widgets |
| 681 | $('#api-entity, #example-entity, #doc-entity').crmSelect2({ |
| 682 | // Add strikethough class to selection to indicate deprecated apis |
| 683 | formatSelection: function(option) { |
| 684 | return $(option.element).hasClass('strikethrough') ? '<span class="strikethrough">' + option.text + '</span>' : option.text; |
| 685 | } |
| 686 | }); |
| 687 | $('form#api-explorer') |
| 688 | .on('change', '#api-entity, #api-action', function() { |
| 689 | entity = $('#api-entity').val(); |
| 690 | action = $('#api-action').val(); |
| 691 | if ($(this).is('#api-entity')) { |
| 692 | getActions(); |
| 693 | } else { |
| 694 | onChangeAction(action); |
| 695 | } |
| 696 | if (entity && action) { |
| 697 | $('#api-params').html('<tr><td colspan="4" class="crm-loading-element"></td></tr>'); |
| 698 | $('#api-params-table thead').show(); |
| 699 | getFields(this); |
| 700 | buildParams(); |
| 701 | } else { |
| 702 | $('#api-params, #api-generated pre').empty(); |
| 703 | $('#api-param-buttons, #api-params-table thead').hide(); |
| 704 | } |
| 705 | }) |
| 706 | .on('change keyup', 'input.api-input, #api-params select', buildParams) |
| 707 | .on('submit', submit); |
| 708 | $('#api-params') |
| 709 | .on('change', 'input.api-param-name, select.api-param-op', renderValueField) |
| 710 | .on('change', 'input.api-param-name, .api-option-name', function() { |
| 711 | if ($(this).val() === '-' && $(this).data('select2')) { |
| 712 | $(this).select2('destroy'); |
| 713 | $(this).val('').focus(); |
| 714 | } |
| 715 | }) |
| 716 | .on('click', '.api-param-remove', function(e) { |
| 717 | e.preventDefault(); |
| 718 | $(this).closest('tr').remove(); |
| 719 | buildParams(); |
| 720 | }) |
| 721 | .on('change', 'select.api-chain-entity', getChainedAction); |
| 722 | $('#example-entity').on('change', getExamples); |
| 723 | $('#example-action').on('change', getExample); |
| 724 | $('#doc-entity').on('change', getDocEntity); |
| 725 | $('#doc-action').on('change', getDocAction); |
| 726 | $('#api-params-add').on('click', function(e) { |
| 727 | e.preventDefault(); |
| 728 | addField(); |
| 729 | }); |
| 730 | $('#api-option-add').on('click', function(e) { |
| 731 | e.preventDefault(); |
| 732 | addOptionField(); |
| 733 | }); |
| 734 | $('#api-chain-add').on('click', function(e) { |
| 735 | e.preventDefault(); |
| 736 | addChainField(); |
| 737 | }); |
| 738 | $('#api-entity').change(); |
| 739 | }); |
| 740 | }(CRM.$, CRM._)); |