Merge pull request #2407 from colemanw/ajaxSearch2
[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 function countSelectedCheckboxes(fldPrefix, form) {
26 fieldCount = 0;
27 for (i = 0; i < form.elements.length; i++) {
28 fpLen = fldPrefix.length;
29 if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0, fpLen) == fldPrefix && form.elements[i].checked == true) {
30 fieldCount++;
31 }
32 }
33 return fieldCount;
34 }
35
36 /**
37 * This function is used to check if any action is selected and also to check if any contacts are checked.
38 *
39 * @access public
40 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
41 * belonging to the resultset's checkbox collection
42 * @param form - name of form that checkboxes are part of
43 * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );"
44 *
45 */
46 function checkPerformAction(fldPrefix, form, taskButton, selection) {
47 var cnt;
48 var gotTask = 0;
49
50 // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task
51 if (taskButton == 1) {
52 gotTask = 1;
53 }
54 else {
55 if (document.forms[form].task.selectedIndex) {
56 //force user to select all search contacts, CRM-3711
57 if (document.forms[form].task.value == 13 || document.forms[form].task.value == 14) {
58 var toggleSelect = document.getElementsByName('toggleSelect');
59 if (toggleSelect[0].checked || document.forms[form].radio_ts[0].checked) {
60 return true;
61 }
62 else {
63 alert("Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
64 return false;
65 }
66 }
67 gotTask = 1;
68 }
69 }
70
71 if (gotTask == 1) {
72 // If user wants to perform action on ALL records and we have a task, return (no need to check further)
73 if (document.forms[form].radio_ts[0].checked) {
74 return true;
75 }
76
77 cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]);
78 if (!cnt) {
79 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.");
80 return false;
81 }
82 }
83 else {
84 alert("Please select an action from the drop-down menu.");
85 return false;
86 }
87 }
88
89 /**
90 * Function to enable task action select
91 */
92 function toggleTaskAction(status) {
93 var radio_ts = document.getElementsByName('radio_ts');
94 if (!radio_ts[1]) {
95 radio_ts[0].checked = true;
96 }
97 if (radio_ts[0].checked || radio_ts[1].checked) {
98 status = true;
99 }
100
101 var formElements = ['task', 'Go', 'Print'];
102 for (var i = 0; i < formElements.length; i++) {
103 var element = document.getElementById(formElements[i]);
104 if (element) {
105 if (status) {
106 element.disabled = false;
107 }
108 else {
109 element.disabled = true;
110 }
111 }
112 }
113 }