Skip to content

Commit

Permalink
Afform - Allow selecting search operator for filter fields
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Apr 15, 2023
1 parent f018f63 commit 8b5164c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
{{:: ts('Search by range') }}
</a>
</li>
<li ng-if="$ctrl.isSearch()">
<div href ng-click="$event.stopPropagation()" class="af-gui-field-select-in-dropdown">
<label>{{:: ts('Operator:') }}</label>
<select class="form-control" ng-model="getSet('search_operator')" ng-model-options="{getterSetter: true}" title="{{:: ts('Field type') }}">
<option ng-repeat="(name, label) in $ctrl.searchOperators" value="{{ name }}">{{ label }}</option>
</select>
</div>
</li>
<li role="separator" class="divider" ng-if="hasOptions()"></li>
<li ng-if="hasOptions()" ng-click="$event.stopPropagation()">
<a href ng-click="resetOptions()" title="{{:: ts('Reset the option list for this field') }}">
Expand Down
17 changes: 17 additions & 0 deletions ext/afform/admin/ang/afGuiEditor/elements/afGuiField.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@
$scope.editingOptions = val;
};

this.searchOperators = {
'': ts('Auto'),
'=': '=',
'!=': '≠',
'>': '>',
'<': '<',
'>=': '≥',
'<=': '≤',
'CONTAINS': ts('Contains'),
'IN': ts('Is One Of'),
'NOT IN': ts('Not One Of'),
'LIKE': ts('Is Like'),
'NOT LIKE': ts('Not Like'),
'REGEXP': ts('Matches Pattern'),
'NOT REGEXP': ts("Doesn't Match Pattern"),
};

// Returns a reference to a path n-levels deep within an object
function drillDown(parent, path) {
var container = parent;
Expand Down
29 changes: 29 additions & 0 deletions ext/afform/core/ang/af/afField.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,26 @@
};
};

// Getter/Setter function for most fields (except select & entityRef)
$scope.getSetValue = function(val) {
var currentVal = $scope.dataProvider.getFieldData()[ctrl.fieldName];
// Setter
if (arguments.length) {
if (ctrl.defn.search_operator) {
if (typeof currentVal !== 'object') {
$scope.dataProvider.getFieldData()[ctrl.fieldName] = {};
}
return ($scope.dataProvider.getFieldData()[ctrl.fieldName][ctrl.defn.search_operator] = val);
}
return ($scope.dataProvider.getFieldData()[ctrl.fieldName] = val);
}
// Getter
if (ctrl.defn.search_operator) {
return (currentVal || {})[ctrl.defn.search_operator];
}
return currentVal;
};

// Getter/Setter function for fields of type select or entityRef.
$scope.getSetSelect = function(val) {
var currentVal = $scope.dataProvider.getFieldData()[ctrl.fieldName];
Expand All @@ -241,6 +261,12 @@
else if (ctrl.defn.search_range) {
return ($scope.dataProvider.getFieldData()[ctrl.fieldName]['>='] = val);
}
else if (ctrl.defn.search_operator) {
if (typeof currentVal !== 'object') {
$scope.dataProvider.getFieldData()[ctrl.fieldName] = {};
}
return ($scope.dataProvider.getFieldData()[ctrl.fieldName][ctrl.defn.search_operator] = val);
}
return ($scope.dataProvider.getFieldData()[ctrl.fieldName] = val);
}
// Getter
Expand All @@ -251,6 +277,9 @@
else if (ctrl.defn.search_range) {
return currentVal['>='];
}
else if (ctrl.defn.search_operator) {
return (currentVal || {})[ctrl.defn.search_operator];
}
return currentVal;
};

Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/ChainSelect.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<input class="form-control" ng-required="$ctrl.defn.required" crm-ui-select="{data: select2Options, multiple: $ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<input class="form-control" ng-required="$ctrl.defn.required" crm-ui-select="{data: select2Options, multiple: $ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" />
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/CheckBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<label for="{{ fieldId + opt.id }}">{{:: opt.label }}</label>
</li>
</ul>
<input type="checkbox" ng-required="$ctrl.defn.required" ng-if="!$ctrl.defn.options" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<input type="checkbox" ng-required="$ctrl.defn.required" ng-if="!$ctrl.defn.options" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" />
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Date.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" crm-ui-datepicker=":: $ctrl.defn.input_attrs" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" crm-ui-datepicker=":: $ctrl.defn.input_attrs" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" />
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" ng-required="$ctrl.defn.required" crm-ui-datepicker=":: $ctrl.inputAttrs[1]" id="{{:: fieldId }}1" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" />
<span class="af-field-range-sep">-</span>
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Number.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" ng-required="$ctrl.defn.required" type="number" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" ng-required="$ctrl.defn.required" type="number" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" type="number" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<span class="af-field-range-sep">-</span>
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Radio.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<label ng-repeat="opt in getOptions() track by opt.id" >
<input class="crm-form-radio" type="radio" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" ng-value="opt.id" />
<input class="crm-form-radio" type="radio" ng-model="getSetValue" ng-model-options="{getterSetter: true}" ng-value="opt.id" />
{{:: opt.label }}
</label>
<a ng-if="!$ctrl.defn.required" class="crm-hover-button" title="{{:: ts('Clear') }}" ng-show="!!dataProvider.getFieldData()[$ctrl.fieldName] || dataProvider.getFieldData()[$ctrl.fieldName] === false || dataProvider.getFieldData()[$ctrl.fieldName] === 0" ng-click="dataProvider.getFieldData()[$ctrl.fieldName] = null">
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/RichTextEditor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<textarea crm-ui-richtext id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" ></textarea>
<textarea crm-ui-richtext id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" ></textarea>
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Text.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" type="text" ng-required="$ctrl.defn.required" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" type="text" ng-required="$ctrl.defn.required" id="{{:: fieldId }}" ng-model="getSetValue" ng-model-options="{getterSetter: true}" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" type="text" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<span class="af-field-range-sep">-</span>
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/TextArea.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<textarea class="crm-form-textarea" id="{{:: fieldId }}" ng-required="$ctrl.defn.required" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" ></textarea>
<textarea class="crm-form-textarea" id="{{:: fieldId }}" ng-required="$ctrl.defn.required" ng-model="getSetValue" ng-model-options="{getterSetter: true}" ></textarea>

0 comments on commit 8b5164c

Please sign in to comment.