From 347a720d392a4ede5445bb8197af089067d2e202 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 5 Nov 2014 10:49:05 -0500 Subject: [PATCH] CRM-15524 - Modify search builder js to work as header script --- templates/CRM/Contact/Form/Search/Builder.js | 126 ++++++++++--------- 1 file changed, 64 insertions(+), 62 deletions(-) diff --git a/templates/CRM/Contact/Form/Search/Builder.js b/templates/CRM/Contact/Form/Search/Builder.js index 5a572419b3..0e5c6dd6b8 100644 --- a/templates/CRM/Contact/Form/Search/Builder.js +++ b/templates/CRM/Contact/Form/Search/Builder.js @@ -196,71 +196,73 @@ }); } - $('#crm-main-content-wrapper') - // Reset and hide row - .on('click', '.crm-reset-builder-row', function() { - var row = $(this).closest('tr'); - $('input, select', row).val('').change(); - row.hide(); - // Hide entire block if this is the only visible row - if (row.siblings(':visible').length < 2) { - row.closest('.crm-search-block').hide(); - } - return false; - }) - // Add new field - if there's a hidden one, show it - // Otherwise allow form to submit and fetch more from the server - .on('click', 'input[name^=addMore]', function() { - var table = $(this).closest('table'); - if ($('tr:hidden', table).length) { - $('tr:hidden', table).first().show(); - return false; - } - }) - // Add new block - if there's a hidden one, show it - // Otherwise allow form to submit and fetch more from the server - .on('click', '#addBlock', function() { - if ($('.crm-search-block:hidden', '#Builder').length) { - var block = $('.crm-search-block:hidden', '#Builder').first(); - block.show(); - $('tr:first-child, tr.crm-search-builder-add-row', block).show(); + $(function($) { + $('#crm-main-content-wrapper') + // Reset and hide row + .on('click', '.crm-reset-builder-row', function() { + var row = $(this).closest('tr'); + $('input, select', row).val('').change(); + row.hide(); + // Hide entire block if this is the only visible row + if (row.siblings(':visible').length < 2) { + row.closest('.crm-search-block').hide(); + } return false; - } - }) - // Handle field and operator selection - .on('change', 'select[id^=mapper][id$="_1"], select[id^=operator]', handleUserInputField) - // Handle option selection - update hidden value field - .on('change', '.crm-search-value select', function() { - var value = $(this).val() || ''; - if ($(this).attr('multiple') == 'multiple' && value.length) { - value = '(' + value.join(',') + ')'; - } - $(this).siblings('input').val(value); - }) - .on('crmLoad', function() { - initialize(); - $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField); - }); + }) + // Add new field - if there's a hidden one, show it + // Otherwise allow form to submit and fetch more from the server + .on('click', 'input[name^=addMore]', function() { + var table = $(this).closest('table'); + if ($('tr:hidden', table).length) { + $('tr:hidden', table).first().show(); + return false; + } + }) + // Add new block - if there's a hidden one, show it + // Otherwise allow form to submit and fetch more from the server + .on('click', '#addBlock', function() { + if ($('.crm-search-block:hidden', '#Builder').length) { + var block = $('.crm-search-block:hidden', '#Builder').first(); + block.show(); + $('tr:first-child, tr.crm-search-builder-add-row', block).show(); + return false; + } + }) + // Handle field and operator selection + .on('change', 'select[id^=mapper][id$="_1"], select[id^=operator]', handleUserInputField) + // Handle option selection - update hidden value field + .on('change', '.crm-search-value select', function() { + var value = $(this).val() || ''; + if ($(this).attr('multiple') == 'multiple' && value.length) { + value = '(' + value.join(',') + ')'; + } + $(this).siblings('input').val(value); + }) + .on('crmLoad', function() { + initialize(); + $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField); + }); - initialize(); + initialize(); - // Fetch initial options during page refresh - it's more efficient to bundle them in a single ajax request - var initialFields = {}, fetchFields = false; - $('select[id^=mapper][id$="_1"] option:selected', '#Builder').each(function() { - var field = $(this).attr('value'); - if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') { - initialFields[field] = [CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}]; - fetchFields = true; - } - }); - if (fetchFields) { - CRM.api3(initialFields).done(function(data) { - $.each(data, function(field, result) { - CRM.searchBuilder.fieldOptions[field] = result.values; + // Fetch initial options during page refresh - it's more efficient to bundle them in a single ajax request + var initialFields = {}, fetchFields = false; + $('select[id^=mapper][id$="_1"] option:selected', '#Builder').each(function() { + var field = $(this).attr('value'); + if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') { + initialFields[field] = [CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}]; + fetchFields = true; + } + }); + if (fetchFields) { + CRM.api3(initialFields).done(function(data) { + $.each(data, function(field, result) { + CRM.searchBuilder.fieldOptions[field] = result.values; + }); + $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField); }); + } else { $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField); - }); - } else { - $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField); - } + } + }); })(cj, CRM); -- 2.25.1