CRM-13863 - Ajax-enable contact actions/activity list
[civicrm-core.git] / templates / CRM / Contact / Page / View / Summary.js
1 // http://civicrm.org/licensing
2 (function($) {
3
4 var ajaxFormParams = {
5 dataType:'json',
6 beforeSubmit: function(arr, $form, options) {
7 addCiviOverlay($form);
8 },
9 success: requestHandler,
10 error: errorHandler
11 };
12
13 function crmFormInline(o) {
14 var data = o.data('edit-params');
15 if (o.is('.crm-edit-ready .crm-inline-edit') && data) {
16 o.animate({height: '+=50px'}, 200);
17 data.snippet = 6;
18 data.reset = 1;
19 o.addClass('form');
20 $('.crm-edit-ready').removeClass('crm-edit-ready');
21 addCiviOverlay(o);
22 $.getJSON(CRM.url('civicrm/ajax/inline', data))
23 .fail(errorHandler)
24 .done(function(response) {
25 removeCiviOverlay(o);
26 o.css('overflow', 'hidden').wrapInner('<div class="inline-edit-hidden-content" style="display:none" />').append(response.content);
27 // Smooth resizing
28 var newHeight = $('.crm-container-snippet', o).height();
29 var diff = newHeight - parseInt(o.css('height'), 10);
30 if (diff < 0) {
31 diff = 0 - diff;
32 }
33 o.animate({height: '' + newHeight + 'px'}, diff * 2, function() {
34 o.removeAttr('style');
35 });
36 $('form', o).validate(CRM.validate.params);
37 ajaxFormParams.data = data;
38 $('form', o).ajaxForm(ajaxFormParams);
39 o.trigger('crmFormLoad');
40 });
41 }
42 };
43
44 function requestHandler(response) {
45 var o = $('div.crm-inline-edit.form');
46
47 if (response.status == 'success' || response.status == 'cancel') {
48 o.trigger('crmFormSuccess', [response]);
49 $('.crm-inline-edit-container').addClass('crm-edit-ready');
50 var data = o.data('edit-params');
51 var dependent = o.data('dependent-fields') || [];
52 // Clone the add-new link if replacing it, and queue the clone to be refreshed as a dependent block
53 if (o.hasClass('add-new') && response.addressId) {
54 data.aid = response.addressId;
55 var clone = o.closest('.crm-summary-block').clone();
56 o.data('edit-params', data);
57 $('form', clone).remove();
58 if (clone.hasClass('contactCardLeft')) {
59 clone.removeClass('contactCardLeft').addClass('contactCardRight');
60 }
61 else if (clone.hasClass('contactCardRight')) {
62 clone.removeClass('contactCardRight').addClass('contactCardLeft');
63 }
64 var cl = $('.crm-inline-edit', clone);
65 var clData = cl.data('edit-params');
66 var locNo = clData.locno++;
67 cl.attr('id', cl.attr('id').replace(locNo, clData.locno)).removeClass('form');
68 o.closest('.crm-summary-block').after(clone);
69 $.merge(dependent, $('.crm-inline-edit', clone));
70 }
71 $('a.ui-notify-close', '#crm-notification-container').click();
72 // Delete an address
73 if (o.hasClass('address') && !o.hasClass('add-new') && !response.addressId) {
74 o.parent().remove();
75 CRM.alert('', ts('Address Deleted'), 'success');
76 }
77 else {
78 // Reload this block plus all dependent blocks
79 var update = $.merge([o], dependent);
80 for (var i in update) {
81 $(update[i]).each(function() {
82 var data = $(this).data('edit-params');
83 data.snippet = data.reset = 1;
84 data.class_name = data.class_name.replace('Form', 'Page');
85 data.type = 'page';
86 $(this).closest('.crm-summary-block').load(CRM.url('civicrm/ajax/inline', data), function() {$(this).trigger('load');});
87 });
88 }
89 CRM.alert('', ts('Saved'), 'success');
90 }
91 // Update changelog tab and contact footer
92 if (response.changeLog.count) {
93 CRM.tabHeader.updateCount('#tab_log', response.changeLog.count);
94 }
95 $("#crm-record-log").replaceWith(response.changeLog.markup);
96 // Refresh tab contents - Advanced logging
97 if (CRM.reloadChangeLogTab) {
98 CRM.reloadChangeLogTab();
99 }
100 // Refresh tab contents - Simple logging
101 else if ($('#changeLog').closest('.ui-tabs-panel').data('civiCrmSnippet')) {
102 $('#changeLog').closest('.ui-tabs-panel').crmSnippet('destroy');
103 }
104 }
105 else {
106 // Handle formRule error
107 $('form', o).ajaxForm('destroy');
108 $('.crm-container-snippet', o).replaceWith(response.content);
109 $('form', o).validate(CRM.validate.params);
110 $('form', o).ajaxForm(ajaxFormParams);
111 o.trigger('crmFormError', [response]).trigger('crmFormLoad');
112 }
113 };
114
115 /**
116 * Configure optimistic locking mechanism for inplace editing
117 *
118 * options.ignoreLabel: string, text for a button
119 * options.reloadLabel: string, text for a button
120 */
121 $.fn.crmFormContactLock = function(options) {
122 var form = this;
123 // AFTER ERROR: Render any "Ignore" and "Restart" buttons
124 return this.on('crmFormError', function(event, obj, status) {
125 var o = $(event.target);
126 var data = o.data('edit-params');
127 var errorTag = o.find('.update_oplock_ts');
128 if (errorTag.length > 0) {
129 $('<span>')
130 .addClass('crm-lock-button')
131 .appendTo(errorTag);
132
133 var buttonContainer = o.find('.crm-lock-button');
134 $('<button>')
135 .addClass('crm-button')
136 .text(options.saveAnywayLabel)
137 .click(function() {
138 $(form).find('input[name=oplock_ts]').val(errorTag.attr('data:update_oplock_ts'));
139 errorTag.parent().hide();
140 $(this).closest('form').find('.form-submit.default').first().click();
141 return false;
142 })
143 .appendTo(buttonContainer)
144 ;
145 $('<button>')
146 .addClass('crm-button')
147 .text(options.reloadLabel)
148 .click(function() {
149 window.location.reload();
150 return false;
151 })
152 .appendTo(buttonContainer)
153 ;
154 }
155 });
156 };
157
158 function errorHandler(response) {
159 CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
160 removeCiviOverlay($('.crm-inline-edit.form form'));
161 }
162
163 $('document').ready(function() {
164 // don't perform inline edit during print mode
165 if (CRM.summaryPrint.mode) {
166 $('div').removeClass('crm-inline-edit');
167 $('.crm-inline-block-content > div.crm-edit-help').remove();
168 $('div.crm-inline-block-content').removeAttr('title');
169 }
170 // Set page title
171 var oldName = 'CiviCRM';
172 var nameTitle = $('#crm-remove-title');
173 if (nameTitle.length > 0) {
174 oldName = nameTitle.text();
175 nameTitle.parent('h1').remove();
176 }
177 else {
178 $('h1').each(function() {
179 if ($(this).text() == oldName) {
180 $(this).remove();
181 }
182 });
183 }
184 function refreshTitle() {
185 var contactName = $('.crm-summary-display_name').text();
186 contactName = $.trim(contactName);
187 var title = $('title').html().replace(oldName, contactName);
188 document.title = title;
189 oldName = contactName;
190 }
191 $('#contactname-block').load(refreshTitle);
192 refreshTitle();
193
194 var clicking;
195 $('.crm-inline-edit-container')
196 .addClass('crm-edit-ready')
197 // Allow links inside edit blocks to be clicked without triggering edit
198 .on('mousedown', '.crm-inline-edit:not(.form) a, .crm-inline-edit:not(.form) .crm-accordion-header, .crm-inline-edit:not(.form) .collapsible-title', function(event) {
199 if (event.which == 1) {
200 event.stopPropagation();
201 return false;
202 }
203 })
204 // Respond to a click (not drag, not right-click) of crm-inline-edit blocks
205 .on('mousedown', '.crm-inline-edit:not(.form)', function(button) {
206 if (button.which == 1) {
207 clicking = this;
208 setTimeout(function() {clicking = null;}, 500);
209 }
210 })
211 .on('mouseup', '.crm-inline-edit:not(.form)', function(button) {
212 if (clicking === this && button.which == 1) {
213 crmFormInline($(this));
214 }
215 })
216 // Inline edit form cancel button
217 .on('click', '.crm-inline-edit :submit[name$=cancel]', function() {
218 var container = $(this).closest('.crm-inline-edit.form');
219 $('.inline-edit-hidden-content', container).nextAll().remove();
220 $('.inline-edit-hidden-content > *:first-child', container).unwrap();
221 container.removeClass('form');
222 $('.crm-inline-edit-container').addClass('crm-edit-ready');
223 $('a.ui-notify-close', '#crm-notification-container').click();
224 return false;
225 })
226 // Switch tabs when clicking tag link
227 .on('click', '#tagLink a', function() {
228 $('#tab_tag a').click();
229 return false;
230 })
231 // make sure only one is_primary radio is checked
232 .on('change', '[class$=is_primary] input', function() {
233 if ($(this).is(':checked')) {
234 $('[class$=is_primary] input', $(this).closest('form')).not(this).prop('checked', false);
235 }
236 })
237 // make sure only one builk_mail radio is checked
238 .on('change', '.crm-email-bulkmail input', function(){
239 if ($(this).is(':checked')) {
240 $('.crm-email-bulkmail input').not(this).prop('checked', false);
241 }
242 })
243 // handle delete link within blocks
244 .on('click', '.crm-delete-inline', function() {
245 var row = $(this).closest('tr');
246 var form = $(this).closest('form');
247 row.addClass('hiddenElement');
248 $('input', row).val('');
249 //if the primary is checked for deleted block
250 //unset and set first as primary
251 if ($('[class$=is_primary] input:checked', row).length > 0) {
252 $('[class$=is_primary] input', row).prop('checked', false);
253 $('[class$=is_primary] input:first', form).prop('checked', true );
254 }
255 $('.add-more-inline', form).show();
256 })
257 // Delete an address
258 .on('click', '.crm-inline-edit.address .delete-button', function() {
259 var $block = $(this).closest('.crm-inline-edit.address');
260 CRM.confirm(function() {
261 CRM.api('address', 'delete', {id: $block.data('edit-params').aid}, {success:
262 function(data) {
263 CRM.alert('', ts('Address Deleted'), 'success');
264 $('.crm-inline-edit-container').addClass('crm-edit-ready');
265 $block.remove();
266 }
267 });
268 },
269 {
270 message: ts('Are you sure you want to delete this address?')
271 }
272 );
273 return false;
274 })
275 // add more and set focus to new row
276 .on('click', '.add-more-inline', function() {
277 var form = $(this).closest('form');
278 var row = $('tr[class="hiddenElement"]:first', form);
279 row.removeClass('hiddenElement');
280 $('input:focus', form).blur();
281 $('input:first', row).focus();
282 if ($('tr[class="hiddenElement"]').length < 1) {
283 $(this).hide();
284 }
285 });
286 // Trigger cancel button on esc keypress
287 $(document).keydown(function(key) {
288 if (key.which == 27) {
289 $('.crm-inline-edit.form :submit[name$=cancel]').click();
290 }
291 });
292 // Switch tabs when clicking log link
293 $('#crm-container').on('click', '#crm-record-log a.crm-log-view', function() {
294 $('#tab_log a').click();
295 return false;
296 });
297 // Handle action links in popup
298 $('#crm-container').on('click', '.crm-contact_actions-list a, .crm-contact_activities-list a', function() {
299 var tabName = $(this).data('tab') || 'activity';
300 var $tab = $('#tab_' + tabName);
301 var $panel = $('#' + $tab.attr('aria-controls'));
302 CRM.loadForm($(this).attr('href'))
303 .on('crmFormSuccess', function() {
304 if ($panel.data('civiCrmSnippet')) {
305 $panel.crmSnippet('refresh');
306 }
307 $('#mainTabContainer').tabs('option', 'active', $tab.prevAll().length);
308 });
309 return false;
310 });
311 // Actions menu
312 $(document).on('click', function(e) {
313 if ($(e.target).is('#crm-contact-actions-link, #crm-contact-actions-link *')) {
314 $('#crm-contact-actions-list').show();
315 return false;
316 }
317 $('#crm-contact-actions-list').hide();
318 });
319 $().crmAccordions();
320 });
321 })(cj);