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