1 // http://civicrm.org/licensing
2 (function($, _
, undefined) {
5 form
= 'form.crm-search-form';
7 function clearTaskMenu() {
8 $('select#task', form
).val('').select2('val', '').prop('disabled', true).select2('disable');
11 function enableTaskMenu() {
12 if (selected
|| $('[name=radio_ts][value=ts_all]', form
).is(':checked')) {
13 $('select#task', form
).prop('disabled', false).select2('enable');
17 function displayCount() {
18 $('label[for*=ts_sel] span', form
).text(selected
);
21 function countCheckboxes() {
22 return $('input.select-row:checked', form
).length
;
26 return $(form
).hasClass('crm-ajax-selection-form');
29 // Use ajax to store selection server-side
30 function phoneHome(single
, $el
, event
) {
31 var url
= CRM
.url('civicrm/ajax/markSelection');
32 var params
= {qfKey
: 'civicrm search ' + $('input[name=qfKey]', form
).val()};
33 if (!$el
.is(':checked')) {
34 params
.action
= 'unselect';
35 params
.state
= 'unchecked';
38 params
.name
= $el
.attr('id');
40 params
.variableType
= 'multiple';
43 event
.preventDefault();
44 $("input.select-row, input.select-rows", form
).prop('checked', false).closest('tr').removeClass('crm-row-selected');
48 params
.name
= $('input.select-row').map(function() {return $(this).attr('id')}).get().join('-');
51 $.getJSON(url
, params
, function(data
) {
52 if (data
&& data
.getCount
!== undefined) {
53 selected
= data
.getCount
;
60 // Handle user interactions with search results
62 // When initially loading and reloading (paging) the results
63 .on('crmLoad', function(e
) {
64 if ($(e
.target
).is('#crm-container') || $(e
.target
).is('#crm-main-content-wrapper')) {
67 selected
= parseInt($('label[for*=ts_sel] span', form
).text(), 10);
69 selected
= countCheckboxes();
75 // When toggling between "all records" and "selected records only"
76 .on('change', '[name=radio_ts]', function() {
80 // When making a selection
81 .on('click', 'input.select-row, input.select-rows, a.crm-selection-reset', function(event
) {
83 $form
= $el
.closest('form'),
84 single
= $el
.is('input.select-row');
86 $('input[name=radio_ts][value=ts_sel]', $form
).prop('checked', true);
89 selected
= countCheckboxes();
91 selected
= $el
.is(':checked') ? $('input.select-row', $form
).length
: 0;
96 phoneHome(single
, $el
, event
);
99 // When selecting a task
100 .on('change', 'select#task', function() {
101 var $form
= $(this).closest('form'),
102 $go
= $('input.crm-search-go-button', $form
);
106 // The following code can load the task in a popup, however not all tasks function correctly with this
107 // So it's disabled pending a per-task opt-in mechanism
109 var data
= $form
.serialize() + '&' + $go
.attr('name') + '=' + $go
.attr('value');
110 var url
= $form
.attr('action');
111 url
+= (url
.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
113 $.post(url
, data
, function(data
) {
114 CRM
.loadForm(data
.userContext
).on('crmFormSuccess', function() {
115 CRM
.refreshParent($form
);