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