CRM-15551 - crmEditable: add placeholder if empty after editing
[civicrm-core.git] / js / crm.searchForm.js
CommitLineData
c1b06d18 1// http://civicrm.org/licensing
6e8d1c11 2(function($, _, undefined) {
8d36b801 3 "use strict";
6e8d1c11 4 var selected = 0,
3da9b9f2 5 form = 'form.crm-search-form',
369f685e 6 active = 'a.button, a.action-item:not(.crm-enable-disable), a.crm-popup';
c1b06d18 7
6e8d1c11
CW
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');
c1b06d18
CW
15 }
16 }
8d36b801 17
6e8d1c11
CW
18 function displayCount() {
19 $('label[for*=ts_sel] span', form).text(selected);
20 }
21
14dbd611 22 function countCheckboxes() {
6e8d1c11
CW
23 return $('input.select-row:checked', form).length;
24 }
25
f715e511
CW
26 function clearSelections(e) {
27 if (selected) {
28 var $form = $(this).closest('form');
29 $('input.select-row, input.select-rows', $form).prop('checked', false).closest('tr').removeClass('crm-row-selected');
30 if (usesAjax()) {
31 phoneHome(false, $(this));
32 } else {
33 selected = 0;
34 displayCount();
35 }
36 }
37 }
38
6e8d1c11
CW
39 function usesAjax() {
40 return $(form).hasClass('crm-ajax-selection-form');
14dbd611
CW
41 }
42
1cb61235 43 // Use ajax to store selection server-side
6e8d1c11
CW
44 function phoneHome(single, $el, event) {
45 var url = CRM.url('civicrm/ajax/markSelection');
46 var params = {qfKey: 'civicrm search ' + $('input[name=qfKey]', form).val()};
f715e511 47 if (!$el.is(':checked') || $el.is('input[name=radio_ts][value=ts_all]')) {
6e8d1c11
CW
48 params.action = 'unselect';
49 params.state = 'unchecked';
50 }
51 if (single) {
52 params.name = $el.attr('id');
53 } else {
54 params.variableType = 'multiple';
1cb61235 55 // "Reset all" button
6e8d1c11
CW
56 if ($el.is('a')) {
57 event.preventDefault();
58 $("input.select-row, input.select-rows", form).prop('checked', false).closest('tr').removeClass('crm-row-selected');
1cb61235
CW
59 }
60 // Master checkbox
f715e511 61 else if ($el.hasClass('select-rows')) {
6e8d1c11
CW
62 params.name = $('input.select-row').map(function() {return $(this).attr('id')}).get().join('-');
63 }
64 }
65 $.getJSON(url, params, function(data) {
66 if (data && data.getCount !== undefined) {
67 selected = data.getCount;
68 displayCount();
69 enableTaskMenu();
70 }
71 });
72 }
73
3da9b9f2
CW
74 /**
75 * Refresh the current page
76 */
77 function refresh() {
78 // Clear cached search results using force=1 argument
79 var location = $('#crm-main-content-wrapper').crmSnippet().crmSnippet('option', 'url');
80 if (!(location.match(/[?&]force=1/))) {
81 location += '&force=1';
82 }
83 $('#crm-main-content-wrapper').crmSnippet({url: location}).crmSnippet('refresh');
84 }
85
6e8d1c11 86 // Handle user interactions with search results
8d36b801 87 $('#crm-container')
6e8d1c11
CW
88 // When initially loading and reloading (paging) the results
89 .on('crmLoad', function(e) {
90 if ($(e.target).is('#crm-container') || $(e.target).is('#crm-main-content-wrapper')) {
91 clearTaskMenu();
1cb61235
CW
92 if (usesAjax()) {
93 selected = parseInt($('label[for*=ts_sel] span', form).text(), 10);
94 } else {
95 selected = countCheckboxes();
96 displayCount();
97 }
6e8d1c11
CW
98 enableTaskMenu();
99 }
100 })
101 // When toggling between "all records" and "selected records only"
102 .on('change', '[name=radio_ts]', function() {
103 clearTaskMenu();
104 enableTaskMenu();
105 })
f715e511 106 .on('click', 'input[name=radio_ts][value=ts_all]', clearSelections)
6e8d1c11
CW
107 // When making a selection
108 .on('click', 'input.select-row, input.select-rows, a.crm-selection-reset', function(event) {
109 var $el = $(this),
110 $form = $el.closest('form'),
111 single = $el.is('input.select-row');
112 clearTaskMenu();
113 $('input[name=radio_ts][value=ts_sel]', $form).prop('checked', true);
114 if (!usesAjax()) {
115 if (single) {
116 selected = countCheckboxes();
117 } else {
118 selected = $el.is(':checked') ? $('input.select-row', $form).length : 0;
119 }
120 displayCount();
121 enableTaskMenu();
122 } else {
123 phoneHome(single, $el, event);
124 }
14dbd611 125 })
6e8d1c11 126 // When selecting a task
8d36b801 127 .on('change', 'select#task', function() {
7e9fdecf
CW
128 var $form = $(this).closest('form'),
129 $go = $('input.crm-search-go-button', $form);
130 if (1) {
131 $go.click();
132 }
133 // The following code can load the task in a popup, however not all tasks function correctly with this
134 // So it's disabled pending a per-task opt-in mechanism
135 else {
136 var data = $form.serialize() + '&' + $go.attr('name') + '=' + $go.attr('value');
137 var url = $form.attr('action');
138 url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
139 clearTaskMenu();
140 $.post(url, data, function(data) {
3da9b9f2 141 CRM.loadForm(data.userContext).on('crmFormSuccess', refresh);
7e9fdecf
CW
142 enableTaskMenu();
143 }, 'json');
144 }
8d36b801
CW
145 });
146
3da9b9f2
CW
147 // Add a specialized version of livepage functionality
148 $('#crm-main-content-wrapper')
149 // Open action links in a popup
150 .off('.crmLivePage')
151 .on('click.crmLivePage', active, CRM.popup)
152 .on('crmPopupFormSuccess.crmLivePage', active, refresh);
153
8d36b801 154})(CRM.$, CRM._);