CRM-12872 - Search js - move search-only js out of Common.js
[civicrm-core.git] / js / crm.searchForm.js
1 // http://civicrm.org/licensing
2 // Controls search form action links and refreshes
3 cj(function($) {
4 $('#crm-main-content-wrapper')
5 // Widgetize the content area
6 .crmSnippet()
7 // Open action links in a popup
8 .off('click.crmLivePage')
9 .on('click.crmLivePage', 'a.button, a.action-item', function() {
10 var url = $(this).attr('href');
11 // only follow real links not javascript buttons
12 if (url === '#' || $(this).attr('onclick') || $(this).hasClass('no-popup')) {
13 return;
14 }
15 CRM.loadForm(url, {
16 openInline: 'a:not("[href=#], .no-popup")'
17 }).on('crmFormSuccess', function(e, data) {
18 // Refresh page when form completes
19 $('#crm-main-content-wrapper').crmSnippet('refresh');
20 });
21 return false;
22 });
23 });
24
25 /**
26 *
27 * Function for checking ALL or unchecking ALL check boxes in a resultset page.
28 *
29 * @access public
30 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
31 * belonging to the resultset's checkbox collection
32 * @param object - checkbox
33 * Sample usage: onClick="javascript:changeCheckboxValues('chk_', cj(this) );"
34 *
35 * @return
36 */
37 function toggleCheckboxVals(fldPrefix, object) {
38 var val = (object.id == 'toggleSelect' && cj(object).is(':checked'));
39 cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').prop('checked', val);
40 // change the class of selected rows
41 on_load_init_checkboxes(object.form.name);
42 }
43
44 function countSelectedCheckboxes(fldPrefix, form) {
45 fieldCount = 0;
46 for (i = 0; i < form.elements.length; i++) {
47 fpLen = fldPrefix.length;
48 if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0, fpLen) == fldPrefix && form.elements[i].checked == true) {
49 fieldCount++;
50 }
51 }
52 return fieldCount;
53 }
54
55 /**
56 * This function changes the style for a checkbox block when it is selected.
57 *
58 * @access public
59 * @param chkName - it is name of the checkbox
60 * @return null
61 */
62 function checkSelectedBox(chkName) {
63 var checkElement = cj('#' + chkName);
64 if (checkElement.prop('checked')) {
65 cj('input[value=ts_sel]:radio').prop('checked', true);
66 checkElement.parents('tr').addClass('crm-row-selected');
67 }
68 else {
69 checkElement.parents('tr').removeClass('crm-row-selected');
70 }
71 }
72
73 /**
74 * This function is to show the row with selected checkbox in different color
75 * @param form - name of form that checkboxes are part of
76 *
77 * @access public
78 * @return null
79 */
80 function on_load_init_checkboxes(form) {
81 var formName = form;
82 var fldPrefix = 'mark_x';
83 for (i = 0; i < document.forms[formName].elements.length; i++) {
84 fpLen = fldPrefix.length;
85 if (document.forms[formName].elements[i].type == 'checkbox' && document.forms[formName].elements[i].name.slice(0, fpLen) == fldPrefix) {
86 checkSelectedBox(document.forms[formName].elements[i].name, formName);
87 }
88 }
89 }
90
91 /**
92 * This function is used to check if any action is selected and also to check if any contacts are checked.
93 *
94 * @access public
95 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
96 * belonging to the resultset's checkbox collection
97 * @param form - name of form that checkboxes are part of
98 * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );"
99 *
100 */
101 function checkPerformAction(fldPrefix, form, taskButton, selection) {
102 var cnt;
103 var gotTask = 0;
104
105 // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task
106 if (taskButton == 1) {
107 gotTask = 1;
108 }
109 else {
110 if (document.forms[form].task.selectedIndex) {
111 //force user to select all search contacts, CRM-3711
112 if (document.forms[form].task.value == 13 || document.forms[form].task.value == 14) {
113 var toggleSelect = document.getElementsByName('toggleSelect');
114 if (toggleSelect[0].checked || document.forms[form].radio_ts[0].checked) {
115 return true;
116 }
117 else {
118 alert("Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
119 return false;
120 }
121 }
122 gotTask = 1;
123 }
124 }
125
126 if (gotTask == 1) {
127 // If user wants to perform action on ALL records and we have a task, return (no need to check further)
128 if (document.forms[form].radio_ts[0].checked) {
129 return true;
130 }
131
132 cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]);
133 if (!cnt) {
134 alert("Please select one or more contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
135 return false;
136 }
137 }
138 else {
139 alert("Please select an action from the drop-down menu.");
140 return false;
141 }
142 }
143
144 /**
145 * Function to enable task action select
146 */
147 function toggleTaskAction(status) {
148 var radio_ts = document.getElementsByName('radio_ts');
149 if (!radio_ts[1]) {
150 radio_ts[0].checked = true;
151 }
152 if (radio_ts[0].checked || radio_ts[1].checked) {
153 status = true;
154 }
155
156 var formElements = ['task', 'Go', 'Print'];
157 for (var i = 0; i < formElements.length; i++) {
158 var element = document.getElementById(formElements[i]);
159 if (element) {
160 if (status) {
161 element.disabled = false;
162 }
163 else {
164 element.disabled = true;
165 }
166 }
167 }
168 }