removeSelect(row);
}
else {
- buildSelect(row, field, op);
+ buildSelect(row, field, op, false);
}
if ((field in CRM.searchBuilder.fieldTypes) === true &&
* Add select list if appropriate for this operation
* @param row: jQuery object
* @param field: string
+ * @param skip_fetch: boolean
*/
- function buildSelect(row, field, op) {
+ function buildSelect(row, field, op, skip_fetch) {
var multiSelect = '';
// Operators that will get a single drop down list of choices.
var dropDownSingleOps = ['=', '!='];
.hide()
.after('<select class="crm-form-' + multiSelect.substr(0, 5) + 'select required" ' + multiSelect + '><option value="">' + ts('Loading') + '...</option></select>');
- fetchOptions(row, field);
+ // Avoid reloading state/county options IF already built, identified by skip_fetch
+ if (skip_fetch) {
+ buildOptions(row, field);
+ }
+ else {
+ fetchOptions(row, field);
+ }
}
/**
}
}
+ /**
+ * Load and build select options for state IF country is chosen OR county options if state is chosen
+ * @param mapper: string
+ * @param value: integer
+ * @param location_type: integer
+ */
+ function chainSelect(mapper, value, location_type) {
+ var apiParams = {
+ sequential: 1,
+ field: (mapper == 'country_id') ? 'state_province' : 'county',
+ };
+ apiParams[mapper] = value;
+ var fieldName = apiParams.field;
+ CRM.api3('address', 'getoptions', apiParams, {
+ success: function(result) {
+ CRM.searchBuilder.fieldOptions[fieldName] = result.count ? result.values : [];
+ $('select[id^=mapper][id$="_1"]').each(function() {
+ var row = $(this).closest('tr');
+ var op = $('select[id^=operator]', row).val();
+ if ($(this).val() === fieldName && location_type === $('select[id^=mapper][id$="_2"]', row).val()) {
+ buildSelect(row, fieldName, op, true);
+ }
+ });
+ }
+ });
+ }
+
// Initialize display: Hide empty blocks & fields
var newBlock = CRM.searchBuilder && CRM.searchBuilder.newBlock || 0;
function initialize() {
value = value.join(',');
}
$(this).siblings('input').val(value);
+ if (value !== '') {
+ var mapper = $('#' + $(this).siblings('input').attr('id').replace('value_', 'mapper_') + '_1').val();
+ var location_type = $('#' + $(this).siblings('input').attr('id').replace('value_', 'mapper_') + '_2').val();
+ if ($.inArray(mapper, ['state_province', 'country']) > -1) {
+ chainSelect(mapper + '_id', value, location_type);
+ }
+ }
})
.on('crmLoad', function() {
initialize();