params = {},
smartyStub,
fieldTpl = _.template($('#api-param-tpl').html()),
+ optionsTpl = _.template($('#api-options-tpl').html()),
+ returnTpl = _.template($('#api-return-tpl').html()),
chainTpl = _.template($('#api-chain-tpl').html());
/**
}
}
+ /**
+ * Add a "fields" row
+ * @param name
+ */
function addField(name) {
$('#api-params').append($(fieldTpl({name: name || ''})));
var $row = $('tr:last-child', '#api-params');
- $('.api-param-name', $row).crmSelect2({data: fields}).change();
+ $('.api-param-name', $row).crmSelect2({
+ data: fields.concat({id: '-', text: ts('Other') + '...'})
+ }).change();
+ }
+
+ /**
+ * Add a new "options" row
+ * @param name
+ */
+ function addOptionField(name) {
+ if ($('.api-options-row', '#api-params').length) {
+ $('.api-options-row:last', '#api-params').after($(optionsTpl({})));
+ } else {
+ $('#api-params').append($(optionsTpl({})));
+ }
+ var $row = $('.api-options-row:last', '#api-params');
+ $('.api-option-name', $row).crmSelect2({data: [
+ {id: 'limit', text: 'limit'},
+ {id: 'offset', text: 'offset'},
+ {id: 'sort', text: 'sort'},
+ {id: 'metadata', text: 'metadata'},
+ {id: '-', text: ts('Other') + '...'}
+ ]});
}
+ /**
+ * Add an "api chain" row
+ */
function addChainField() {
$('#api-params').append($(chainTpl({})));
var $row = $('tr:last-child', '#api-params');
placeholder: '<span class="icon ui-icon-link"></span> ' + ts('Entity'),
escapeMarkup: function(m) {return m}
});
-
}
+ /**
+ * Fetch fields for entity+action
+ */
function getFields() {
var required = [];
fields = [];
}
});
showFields(required);
+ if (action === 'get' || action === 'getsingle') {
+ showReturn();
+ }
});
}
+ /**
+ * For "get" actions show the "return" options
+ */
+ function showReturn() {
+ $('#api-params').prepend($(returnTpl({})));
+ console.log($(returnTpl({})));
+ $('#api-return-value').crmSelect2({data: fields, multiple: true});
+ }
+
+ /**
+ * Fetch actions for entity
+ */
function getActions() {
if (entity) {
CRM.api3(entity, 'getactions').done(function(data) {
}
}
+ /**
+ * Called after getActions to populate action list
+ * @param el
+ */
function populateActions(el) {
$('#api-action').select2({
data: _.transform(actions, function(ret, item) {ret.push({text: item, id: item})})
});
}
+ /**
+ * Called after getfields to show buttons and required fields
+ * @param required
+ */
function showFields(required) {
- fields.push({
- id: '-',
- text: ts('Other') + '...'
- });
$('#api-params').empty();
$('#api-param-buttons').show();
if (required.length) {
}
}
+ /**
+ * Add/remove option list for selected field's pseudoconstant
+ */
function toggleOptions() {
var name = $(this).val(),
$valField = $(this).closest('tr').find('.api-param-value');
else if ($valField.data('select2')) {
$valField.select2('destroy');
}
- if (name === '-') {
- $(this).select2('destroy');
- $(this).val('').focus();
- }
}
/**
var ret = '';
if ($.isPlainObject(val)) {
$.each(val, function(k, v) {
- ret += (ret ? ',' : '') + "'" + k + "' => " + phpFormat(v);
+ ret += (ret ? ', ' : '') + "'" + k + "' => " + phpFormat(v);
});
return 'array(' + ret + ')';
}
return js;
}
+ /**
+ * Create the params array from user input
+ * @param e
+ */
function buildParams(e) {
params = {};
$('.api-param-checkbox:checked').each(function() {
params[this.name] = 1;
});
- $('input.api-param-value').each(function() {
+ $('input.api-param-value, input.api-option-value').each(function() {
var $row = $(this).closest('tr'),
val = evaluate($(this).val(), $(this).is('.select2-offscreen')),
name = $('input.api-param-name', $row).val(),
op = $('select.api-param-op', $row).val() || '=';
+
+ // Ignore blank values for the return field
+ if ($(this).is('#api-return-value') && !val) {
+ return;
+ }
// Special syntax for api chaining
if (!name && $('select.api-chain-entity', $row).val()) {
name = 'api.' + $('select.api-chain-entity', $row).val() + '.' + $('select.api-chain-action', $row).val();
}
+ // Special handling for options
+ if ($(this).is('.api-option-value')) {
+ op = $('input.api-option-name', $row).val();
+ if (op) {
+ name = 'options';
+ }
+ }
if (name && val !== undefined) {
- params[name] = op === '=' ? val : {};
+ params[name] = op === '=' ? val : (params[name] || {});
if (op !== '=') {
params[name][op] = val;
}
.on('submit', submit);
$('#api-params')
.on('change', '.api-param-name', toggleOptions)
+ .on('change', '.api-param-name, .api-option-name', function() {
+ if ($(this).val() === '-') {
+ $(this).select2('destroy');
+ $(this).val('').focus();
+ }
+ })
.on('click', '.api-param-remove', function(e) {
e.preventDefault();
$(this).closest('tr').remove();
e.preventDefault();
addField();
});
+ $('#api-option-add').on('click', function(e) {
+ e.preventDefault();
+ addOptionField();
+ });
$('#api-chain-add').on('click', function(e) {
e.preventDefault();
addChainField();