CRM-13863 - Ajax improvements for relationship tab.
[civicrm-core.git] / templates / CRM / Contact / Page / View / Summary.js
CommitLineData
6a488035
TO
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
03a7ec8f 47 if (response.status == 'success' || response.status == 'cancel') {
6a488035
TO
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;
209742d1 55 var clone = o.closest('.crm-summary-block').clone();
6a488035 56 o.data('edit-params', data);
209742d1 57 $('form', clone).remove();
6a488035
TO
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');
209742d1 68 o.closest('.crm-summary-block').after(clone);
6a488035
TO
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();
60e37cae 75 CRM.alert('', ts('Address Deleted'), 'success');
6a488035
TO
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) {
4e8065a9 93 CRM.tabHeader.updateCount('#tab_log', response.changeLog.count);
6a488035
TO
94 }
95 $("#crm-record-log").replaceWith(response.changeLog.markup);
e4d290c4
CW
96 // Refresh tab contents - Advanced logging
97 if (CRM.reloadChangeLogTab) {
98 CRM.reloadChangeLogTab();
99 }
100 // Refresh tab contents - Simple logging
23223213 101 else if ($('#changeLog').closest('.ui-tabs-panel').data('civiCrmSnippet')) {
4b628e67 102 $('#changeLog').closest('.ui-tabs-panel').crmSnippet('destroy');
6a488035
TO
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() {
7d041da3
PJ
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 }
6a488035
TO
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);
db9b1503 187 document.title = $('title').html().replace(oldName, contactName);
6a488035
TO
188 oldName = contactName;
189 }
190 $('#contactname-block').load(refreshTitle);
191 refreshTitle();
192
193 var clicking;
194 $('.crm-inline-edit-container')
195 .addClass('crm-edit-ready')
196 // Allow links inside edit blocks to be clicked without triggering edit
197 .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) {
198 if (event.which == 1) {
199 event.stopPropagation();
200 return false;
201 }
202 })
203 // Respond to a click (not drag, not right-click) of crm-inline-edit blocks
204 .on('mousedown', '.crm-inline-edit:not(.form)', function(button) {
205 if (button.which == 1) {
206 clicking = this;
207 setTimeout(function() {clicking = null;}, 500);
208 }
209 })
210 .on('mouseup', '.crm-inline-edit:not(.form)', function(button) {
211 if (clicking === this && button.which == 1) {
212 crmFormInline($(this));
213 }
214 })
215 // Inline edit form cancel button
216 .on('click', '.crm-inline-edit :submit[name$=cancel]', function() {
217 var container = $(this).closest('.crm-inline-edit.form');
218 $('.inline-edit-hidden-content', container).nextAll().remove();
219 $('.inline-edit-hidden-content > *:first-child', container).unwrap();
220 container.removeClass('form');
221 $('.crm-inline-edit-container').addClass('crm-edit-ready');
222 $('a.ui-notify-close', '#crm-notification-container').click();
223 return false;
224 })
225 // Switch tabs when clicking tag link
226 .on('click', '#tagLink a', function() {
227 $('#tab_tag a').click();
228 return false;
229 })
230 // make sure only one is_primary radio is checked
231 .on('change', '[class$=is_primary] input', function() {
232 if ($(this).is(':checked')) {
233 $('[class$=is_primary] input', $(this).closest('form')).not(this).prop('checked', false);
234 }
235 })
236 // make sure only one builk_mail radio is checked
237 .on('change', '.crm-email-bulkmail input', function(){
238 if ($(this).is(':checked')) {
239 $('.crm-email-bulkmail input').not(this).prop('checked', false);
240 }
241 })
242 // handle delete link within blocks
243 .on('click', '.crm-delete-inline', function() {
244 var row = $(this).closest('tr');
245 var form = $(this).closest('form');
246 row.addClass('hiddenElement');
247 $('input', row).val('');
248 //if the primary is checked for deleted block
249 //unset and set first as primary
250 if ($('[class$=is_primary] input:checked', row).length > 0) {
251 $('[class$=is_primary] input', row).prop('checked', false);
252 $('[class$=is_primary] input:first', form).prop('checked', true );
253 }
254 $('.add-more-inline', form).show();
255 })
60e37cae
CW
256 // Delete an address
257 .on('click', '.crm-inline-edit.address .delete-button', function() {
258 var $block = $(this).closest('.crm-inline-edit.address');
259 CRM.confirm(function() {
260 CRM.api('address', 'delete', {id: $block.data('edit-params').aid}, {success:
261 function(data) {
0118cd79 262 CRM.alert('', ts('Address Deleted'), 'success');
60e37cae
CW
263 $('.crm-inline-edit-container').addClass('crm-edit-ready');
264 $block.remove();
265 }
266 });
267 },
268 {
269 message: ts('Are you sure you want to delete this address?')
270 }
271 );
272 return false;
273 })
6a488035
TO
274 // add more and set focus to new row
275 .on('click', '.add-more-inline', function() {
276 var form = $(this).closest('form');
277 var row = $('tr[class="hiddenElement"]:first', form);
278 row.removeClass('hiddenElement');
279 $('input:focus', form).blur();
280 $('input:first', row).focus();
281 if ($('tr[class="hiddenElement"]').length < 1) {
282 $(this).hide();
283 }
284 });
285 // Trigger cancel button on esc keypress
286 $(document).keydown(function(key) {
287 if (key.which == 27) {
288 $('.crm-inline-edit.form :submit[name$=cancel]').click();
289 }
290 });
db9b1503
CW
291 $('#crm-container')
292 // Switch tabs when clicking log link
293 .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 .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 });
23223213
CW
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 });
58b65bf6 319 $().crmAccordions();
6a488035
TO
320 });
321})(cj);