Merge pull request #22507 from colemanw/searchImportCleanup
[civicrm-core.git] / js / crm.searchForm.js
1 // http://civicrm.org/licensing
2 (function($, _, undefined) {
3 "use strict";
4 var selected = 0,
5 form = 'form.crm-search-form',
6 active = 'a.action-item:not(.crm-enable-disable), a.crm-popup';
7
8 function clearTaskMenu() {
9 $('select#task', form).val('').select2('val', '').prop('disabled', true).select2('disable');
10 }
11
12 function enableTaskMenu() {
13 if (selected || $('[name=radio_ts][value=ts_all]', form).is(':checked')) {
14 $('select#task', form).prop('disabled', false).select2('enable');
15 }
16 }
17
18 function displayCount() {
19 $('label[for*=ts_sel] span', form).text(selected);
20 }
21
22 function countCheckboxes() {
23 return $('input.select-row:checked', form).length;
24 }
25
26 function clearSelections(e) {
27 /* jshint validthis: true */
28 if (selected) {
29 var $form = $(this).closest('form');
30 $('input.select-row, input.select-rows', $form).prop('checked', false).closest('tr').removeClass('crm-row-selected');
31 if (usesAjax()) {
32 phoneHome(false, $(this));
33 } else {
34 selected = 0;
35 displayCount();
36 }
37 }
38 }
39
40 function usesAjax() {
41 return $(form).hasClass('crm-ajax-selection-form');
42 }
43
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';
51 }
52 if (single) {
53 params.name = $el.attr('id');
54 } else {
55 params.variableType = 'multiple';
56 // "Reset all" button
57 if ($el.is('a')) {
58 event.preventDefault();
59 $("input.select-row, input.select-rows", form).prop('checked', false).closest('tr').removeClass('crm-row-selected');
60 }
61 // Master checkbox
62 else if ($el.hasClass('select-rows')) {
63 params.name = $('input.select-row').map(function() {return $(this).attr('id');}).get().join('-');
64 }
65 }
66 $.post(url, params, function(data) {
67 if (data && data.getCount !== undefined) {
68 selected = data.getCount;
69 displayCount();
70 enableTaskMenu();
71 }
72 }, 'json');
73 }
74
75 /**
76 * Refresh the current page
77 */
78 function refresh() {
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';
83 }
84 $('#crm-main-content-wrapper').crmSnippet({url: location}).crmSnippet('refresh');
85 }
86
87 /**
88 * When initially loading and reloading (paging) the results
89 */
90 function initForm() {
91 clearTaskMenu();
92 if (usesAjax()) {
93 selected = parseInt($('label[for*=ts_sel] span', form).text(), 10);
94 } else {
95 selected = countCheckboxes();
96 displayCount();
97 }
98 enableTaskMenu();
99 }
100
101 $(function() {
102 initForm();
103
104 // Focus first search field
105 $('.crm-form-text:input:visible:first', 'form.crm-search-form').focus();
106
107 // Handle user interactions with search results
108 $('#crm-container')
109 // When toggling between "all records" and "selected records only"
110 .on('change', '[name=radio_ts]', function() {
111 clearTaskMenu();
112 enableTaskMenu();
113 })
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) {
117 var $el = $(this),
118 $form = $el.closest('form'),
119 single = $el.is('input.select-row');
120 clearTaskMenu();
121 $('input[name=radio_ts][value=ts_sel]', $form).prop('checked', true);
122 if (!usesAjax()) {
123 if (single) {
124 selected = countCheckboxes();
125 } else {
126 selected = $el.is(':checked') ? $('input.select-row', $form).length : 0;
127 }
128 displayCount();
129 enableTaskMenu();
130 } else {
131 phoneHome(single, $el, event);
132 }
133 })
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.
141 return;
142 }
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) {
148 refresh_fields = {};
149 }
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>';
155 });
156
157 CRM.confirm({
158 title: $selectedOption.data('confirm_title') ? $selectedOption.data('confirm_title') : ts('Confirm action'),
159 message: '<table class="form-layout">' + $message + '</table>'
160 })
161 .on('crmConfirm:yes', function() {
162 confirmed = true;
163 $.each(refresh_fields, function (refreshIndex, refreshValue) {
164 $('#' + refreshIndex).val($('.crm-confirm #' + refreshIndex).val());
165 });
166 $go.click();
167 })
168 .on('crmConfirm:no', function() {
169 $('#task').val('').change();
170 return;
171 });
172 }
173 else if (!$(this).find(':selected').data('supports_modal')) {
174 $go.click();
175 $('#task').val('').select2('val', '');
176 }
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.
179 else {
180 var data = $form.serialize() + '&' + $go.attr('name') + '=' + $go.attr('value');
181 var url = $form.attr('action');
182 url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
183 clearTaskMenu();
184 $.post(url, data, function(data) {
185 CRM.loadForm(data.userContext).on('crmFormSuccess', refresh);
186 enableTaskMenu();
187 }, 'json');
188 }
189 });
190
191 // Add a specialized version of livepage functionality
192 $('#crm-main-content-wrapper')
193 .on('crmLoad', function(e) {
194 if ($(e.target).is(this)) {
195 initForm();
196 }
197 })
198 // Open action links in a popup
199 .off('.crmLivePage')
200 .on('click.crmLivePage', active, CRM.popup)
201 .on('crmPopupFormSuccess.crmLivePage', active, refresh);
202 });
203
204 })(CRM.$, CRM._);