1 // http://civicrm.org/licensing
2 (function($, _
, undefined) {
5 form
= 'form.crm-search-form',
6 active
= 'a.action-item:not(.crm-enable-disable), a.crm-popup';
8 function clearTaskMenu() {
9 $('select#task', form
).val('').select2('val', '').prop('disabled', true).select2('disable');
12 function enableTaskMenu() {
13 if (selected
|| $('[name=radio_ts][value=ts_all]', form
).is(':checked')) {
14 $('select#task', form
).prop('disabled', false).select2('enable');
18 function displayCount() {
19 $('label[for*=ts_sel] span', form
).text(selected
);
22 function countCheckboxes() {
23 return $('input.select-row:checked', form
).length
;
26 function clearSelections(e
) {
27 /* jshint validthis: true */
29 var $form
= $(this).closest('form');
30 $('input.select-row, input.select-rows', $form
).prop('checked', false).closest('tr').removeClass('crm-row-selected');
32 phoneHome(false, $(this));
41 return $(form
).hasClass('crm-ajax-selection-form');
44 // Use ajax to store selection server-side
45 function phoneHome(single
, $el
, event
) {
46 var url
= CRM
.url('civicrm/ajax/markSelection');
47 var params
= {qfKey
: 'civicrm search ' + $('input[name=qfKey]', form
).val()};
48 if (!$el
.is(':checked') || $el
.is('input[name=radio_ts][value=ts_all]')) {
49 params
.action
= 'unselect';
50 params
.state
= 'unchecked';
53 params
.name
= $el
.attr('id');
55 params
.variableType
= 'multiple';
58 event
.preventDefault();
59 $("input.select-row, input.select-rows", form
).prop('checked', false).closest('tr').removeClass('crm-row-selected');
62 else if ($el
.hasClass('select-rows')) {
63 params
.name
= $('input.select-row').map(function() {return $(this).attr('id');}).get().join('-');
66 $.post(url
, params
, function(data
) {
67 if (data
&& data
.getCount
!== undefined) {
68 selected
= data
.getCount
;
76 * Refresh the current page
79 // Clear cached search results using force=1 argument
80 var location
= $('#crm-main-content-wrapper').crmSnippet().crmSnippet('option', 'url');
81 if (!(location
.match(/[?&]force=1/))) {
82 location
+= '&force=1';
84 $('#crm-main-content-wrapper').crmSnippet({url
: location
}).crmSnippet('refresh');
88 * When initially loading and reloading (paging) the results
93 selected
= parseInt($('label[for*=ts_sel] span', form
).text(), 10);
95 selected
= countCheckboxes();
104 // Focus first search field
105 $('.crm-form-text:input:visible:first', 'form.crm-search-form').focus();
107 // Handle user interactions with search results
109 // When toggling between "all records" and "selected records only"
110 .on('change', '[name=radio_ts]', function() {
114 .on('click', 'input[name=radio_ts][value=ts_all]', clearSelections
)
115 // When making a selection
116 .on('click', 'input.select-row, input.select-rows, a.crm-selection-reset', function(event
) {
118 $form
= $el
.closest('form'),
119 single
= $el
.is('input.select-row');
121 $('input[name=radio_ts][value=ts_sel]', $form
).prop('checked', true);
124 selected
= countCheckboxes();
126 selected
= $el
.is(':checked') ? $('input.select-row', $form
).length
: 0;
131 phoneHome(single
, $el
, event
);
134 // When selecting a task
135 .on('change', 'select#task', function(e
) {
136 var $form
= $(this).closest('form'),
137 $go
= $('button.crm-search-go-button', $form
);
138 var $selectedOption
= $(this).find(':selected');
139 if (!$selectedOption
.val()) {
140 // do not blank refresh the empty option.
143 if ($selectedOption
.data('is_confirm')) {
144 var confirmed
= false;
145 var refresh_fields
= $selectedOption
.data('confirm_refresh_fields');
146 var $message
= '<tr>' + (($selectedOption
.data('confirm_message') !== undefined) ? $selectedOption
.data('confirm_message') : '') + '</tr>';
147 if (refresh_fields
=== undefined) {
150 $.each(refresh_fields
, function (refreshIndex
, refreshValue
) {
151 var $refresh_field
= $(refreshValue
.selector
);
152 var prependText
= (refreshValue
.prepend
!== undefined) ? refreshValue
.prepend
: '';
153 var existingInput
= $refresh_field
.find('input').val();
154 $message
= $message
+ '<tr>' + $refresh_field
.html().replace(existingInput
, prependText
+ existingInput
) + '</tr>';
158 title
: $selectedOption
.data('confirm_title') ? $selectedOption
.data('confirm_title') : ts('Confirm action'),
159 message
: '<table class="form-layout">' + $message
+ '</table>'
161 .on('crmConfirm:yes', function() {
163 $.each(refresh_fields
, function (refreshIndex
, refreshValue
) {
164 $('#' + refreshIndex
).val($('.crm-confirm #' + refreshIndex
).val());
168 .on('crmConfirm:no', function() {
169 $('#task').val('').change();
173 else if (!$(this).find(':selected').data('supports_modal')) {
175 $('#task').val('').select2('val', '');
177 // The following code can load the task in a popup, however not all tasks function correctly with this
178 // So it's a per-task opt-in mechanism.
180 var data
= $form
.serialize() + '&' + $go
.attr('name') + '=' + $go
.attr('value');
181 var url
= $form
.attr('action');
182 url
+= (url
.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
184 $.post(url
, data
, function(data
) {
185 CRM
.loadForm(data
.userContext
).on('crmFormSuccess', refresh
);
191 // Add a specialized version of livepage functionality
192 $('#crm-main-content-wrapper')
193 .on('crmLoad', function(e
) {
194 if ($(e
.target
).is(this)) {
198 // Open action links in a popup
200 .on('click.crmLivePage', active
, CRM
.popup
)
201 .on('crmPopupFormSuccess.crmLivePage', active
, refresh
);