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