This PR adds date filters to the admin's leave report.
Before
After
Note: please ignore the date picker's border. See the comments for more information.
Technical details
The way default values for the custom filters are generated changed since for the leave report they depend on the current period provided by the API. The new sequence is as follows:
When loading the data for the report, also load the default values for the custom filter, if a resolveCustomFilterDefaultValues function is provided in the report configuration. This function must return a promise which resolves to an object containing the default values for each one of the inputs.
// from the leave report configuration:
'resolveCustomFilterDefaultValues': function () {
var today = moment().format(this.DEFAULT_DATE_FORMAT);
return CRM.api3('AbsencePeriod', 'get', {
'sequential': 1,
'start_date': { '<=': today },
'end_date': { '>=': today },
'options': { 'limit': 1 }
})
.then(function (periods) {
var period = _.first(periods.values);
if (!period) {
return;
}
// *from* and *to* are the name of the form inputs for the custom filters
// in the leave report:
return {
from: period.start_date,
to: period.end_date
};
});
},
As explained before, this resolveCustomFilterDefaultValues is executed when loading the report data:
// From the pivot table js file:
PivotTable.prototype.initPivotDataLoading = function () {
// ...
$.when(
CRM.api3(apiCalls),
this.resolveCustomFilterDefaultValues()
)
.then(function (results) {
// ...
});
};
PivotTable.prototype.resolveCustomFilterDefaultValues = function () {
var dateInputsDefaultValues = this.getDefaultValuesForDateInputs();
return $.when()
.then(function () {
return this.config.resolveCustomFilterDefaultValues
? this.config.resolveCustomFilterDefaultValues()
: {};
}.bind(this))
.then(function (defaultValues) {
// it also passes the date input default values so it's not necessary to define a
// resolver just for date inputs of which their default is today:
this.customFilterDefaultValues = _.extend({}, dateInputsDefaultValues, defaultValues);
}.bind(this));
};
PivotTable.prototype.getDefaultValuesForDateInputs = function () {
var defaultValues = {};
var today = moment().format(this.DEFAULT_DATE_FORMAT);
this.customFilterForm.find('.crm-ui-datepicker').each(function () {
var inputName = $(this).attr('name');
defaultValues[inputName] = today;
});
return defaultValues;
};
* After loading the pivot table data and the custom field default values, and during the post render sequence, the default values are assigned to the custom field inputs:
```js
PivotTable.prototype.initCustomFilterDefaultValues = function () {
this.customFilterForm.find('input, select, textarea')
.each(function (index, element) {
var input = $(element);
var inputName = input.attr('name');
var inputDefaultValue = this.customFilterDefaultValues[inputName];
if (!inputDefaultValue) {
return;
}
input.on('change', function () {
var isValueEmpty = _.isEmpty(input.val());
if (isValueEmpty) {
input.val(inputDefaultValue).change();
}
});
input.change();
}.bind(this));
};
If the input changes and the value is empty, it will use the default value instead.
Finally, for the actual filtering, the customFilter function was defined as follows:
'customFilter': function (record) {
var dates = {
start: moment(this.customFilterValues.from),
end: moment(this.customFilterValues.to)
};
var request = {
start: moment(record['Absence Start Date']),
end: moment(record['Absence End Date'])
};
return request.start.isSameOrAfter(dates.start) && request.end.isSameOrBefore(dates.end);
}
Overview
This PR adds date filters to the admin's leave report.
Before
After
Technical details
The way default values for the custom filters are generated changed since for the leave report they depend on the current period provided by the API. The new sequence is as follows:
When loading the data for the report, also load the default values for the custom filter, if a
resolveCustomFilterDefaultValues
function is provided in the report configuration. This function must return a promise which resolves to an object containing the default values for each one of the inputs.resolveCustomFilterDefaultValues
is executed when loading the report data:PivotTable.prototype.resolveCustomFilterDefaultValues = function () { var dateInputsDefaultValues = this.getDefaultValuesForDateInputs();
return $.when() .then(function () { return this.config.resolveCustomFilterDefaultValues ? this.config.resolveCustomFilterDefaultValues() : {}; }.bind(this)) .then(function (defaultValues) { // it also passes the date input default values so it's not necessary to define a // resolver just for date inputs of which their default is today: this.customFilterDefaultValues = _.extend({}, dateInputsDefaultValues, defaultValues); }.bind(this)); };
PivotTable.prototype.getDefaultValuesForDateInputs = function () { var defaultValues = {}; var today = moment().format(this.DEFAULT_DATE_FORMAT);
this.customFilterForm.find('.crm-ui-datepicker').each(function () { var inputName = $(this).attr('name');
});
return defaultValues; };
If the input changes and the value is empty, it will use the default value instead.
Finally, for the actual filtering, the
customFilter
function was defined as follows:Comments