}
});
- this.addArg = function(exprType) {
+ this.addArg = function(exprType, optional) {
var param = ctrl.getParam(ctrl.args.length),
val = '';
if (exprType === 'SqlNumber') {
// Number: default to 0
val = 0;
- } else if (exprType === 'SqlField') {
+ } else if (exprType === 'SqlField' && !optional) {
// Field: Default to first available field, making it easier to delete the value
val = ctrl.getFields().results[0].children[0].id;
}
ctrl.args.push({
type: ctrl.exprTypes[exprType].type,
flag_before: _.filter(_.keys(param.flag_before))[0],
+ flag_after: _.filter(_.keys(param.flag_after))[0],
name: param.name,
value: val
});
_.each(ctrl.fn.params, function(param, index) {
while (
(ctrl.args.length - index < param.min_expr) &&
- // TODO: Handle named params like "ORDER BY"
- !(param.name && param.optional) &&
+ // Exclude 'api_default' params (should not be changed by the user)
+ !param.api_default &&
(!param.optional || param.must_be.length === 1)
) {
- ctrl.addArg(param.must_be[0]);
+ ctrl.addArg(param.must_be[0], param.optional);
}
});
}
ctrl.args.splice(pos, 0, {
type: exprType ? ctrl.exprTypes[exprType].type : null,
flag_before: _.filter(_.keys(ctrl.fn.params[pos].flag_before))[0],
+ flag_after: _.filter(_.keys(ctrl.fn.params[pos].flag_after))[0],
name: ctrl.fn.params[pos].name,
value: exprType === 'SqlNumber' ? 0 : ''
});
// Update fieldArg
var fieldParam = ctrl.fn.params[pos];
ctrl.fieldArg.flag_before = _.keys(fieldParam.flag_before)[0];
+ ctrl.fieldArg.flag_after = _.keys(fieldParam.flag_after)[0];
ctrl.fieldArg.name = fieldParam.name;
initFunction();
}
};
this.changeArg = function(index) {
- var val = ctrl.args[index].value;
- // Delete empty value
- if (index && !val && val !== 0 && ctrl.args.length > ctrl.fn.params[0].min_expr) {
+ var val = ctrl.args[index].value,
+ param = ctrl.getParam(index);
+ // Delete empty value if allowed
+ if (index && !val && val !== 0 && !param.optional && ctrl.args.length > param.min_expr) {
ctrl.args.splice(index, 1);
}
ctrl.writeExpr();
var args = _.transform(ctrl.args, function(args, arg, index) {
if (arg.value || arg.value === 0 || arg.flag_before) {
var prefix = arg.flag_before || arg.name ? (index ? ' ' : '') + (arg.flag_before || arg.name) + (arg.value ? ' ' : '') : (index ? ', ' : '');
- args.push(prefix + (arg.type === 'string' ? JSON.stringify(arg.value) : arg.value));
+ var suffix = arg.flag_after ? ' ' + arg.flag_after : '';
+ args.push(prefix + (arg.type === 'string' ? JSON.stringify(arg.value) : arg.value) + suffix);
}
});
// Replace fake function "e"
<label ng-hide="$ctrl.mode !== 'select' && !$ctrl.fn">{{ $ctrl.fieldArg.field.label }}</label>
<div class="form-group" ng-repeat="arg in $ctrl.args">
- <crm-search-function-flag ng-if="$ctrl.fn" arg="arg" param="$ctrl.getParam($index)" write-expr="$ctrl.writeExpr()"></crm-search-function-flag>
+ <crm-search-function-flag ng-if="$ctrl.fn" flag="flag_before" arg="arg" param="$ctrl.getParam($index)" write-expr="$ctrl.writeExpr()"></crm-search-function-flag>
<span ng-switch="arg.type" ng-if="arg !== $ctrl.fieldArg">
<input ng-switch-when="number" class="form-control" type="number" ng-model="arg.value" placeholder="{{ $ctrl.getParam($index).label }}" ng-change="$ctrl.changeArg($index)" ng-model-options="{updateOn: 'blur'}">
<input ng-switch-when="string" class="form-control" ng-model="arg.value" placeholder="{{ $ctrl.getParam($index).label }}" ng-change="$ctrl.changeArg($index)" ng-trim="false" ng-model-options="{updateOn: 'blur'}">
<input ng-switch-when="field" class="form-control" ng-model="arg.value" crm-ui-select="{data: $ctrl.getFields, placeholder: $ctrl.getParam($index).label}" ng-change="$ctrl.changeArg($index)">
</span>
+ <crm-search-function-flag ng-if="$ctrl.fn && arg.value" flag="flag_after" arg="arg" param="$ctrl.getParam($index)" write-expr="$ctrl.writeExpr()"></crm-search-function-flag>
</div>
<div class="btn-group" ng-if="$ctrl.canAddArg()">
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span ng-switch="$ctrl.widget">
<span ng-switch-when="checkbox">
- <label ng-repeat="(val, label) in $ctrl.param.flag_before" ng-if="val">
- <input type="checkbox" ng-checked="$ctrl.arg.flag_before === val" ng-click="$ctrl.arg.flag_before = ($ctrl.arg.flag_before === val ? null : val); $ctrl.writeExpr();" >
+ <label ng-repeat="(val, label) in $ctrl.param[$ctrl.flag]" ng-if="val">
+ <input type="checkbox" ng-checked="$ctrl.arg[$ctrl.flag] === val" ng-click="$ctrl.arg[$ctrl.flag] = ($ctrl.arg[$ctrl.flag] === val ? null : val); $ctrl.writeExpr();" >
{{ label }}
</label>
</span>
- <select ng-switch-when="select" class="form-control" ng-model="$ctrl.arg.flag_before" ng-change="$ctrl.writeExpr();">
- <option ng-repeat="(val, label) in $ctrl.param.flag_before" value="{{ val }}">
+ <select ng-switch-when="select" class="form-control" ng-model="$ctrl.arg[$ctrl.flag]" ng-change="$ctrl.writeExpr();">
+ <option ng-repeat="(val, label) in $ctrl.param[$ctrl.flag]" value="{{ val }}">
{{ label }}
</option>
</select>