Merge pull request #2061 from civicrm/4.3
[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 == 'save' || 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 $("#tab_log a em").html(response.changeLog.count);
94 }
95 $("#crm-record-log").replaceWith(response.changeLog.markup);
96 if ($('#Change_Log div').length) {
97 $('#Change_Log').load($("#tab_log a").attr('href'));
98 }
99 }
100 else {
101 // Handle formRule error
102 $('form', o).ajaxForm('destroy');
103 $('.crm-container-snippet', o).replaceWith(response.content);
104 $('form', o).validate(CRM.validate.params);
105 $('form', o).ajaxForm(ajaxFormParams);
106 o.trigger('crmFormError', [response]).trigger('crmFormLoad');
107 }
108 };
109
110 /**
111 * Configure optimistic locking mechanism for inplace editing
112 *
113 * options.ignoreLabel: string, text for a button
114 * options.reloadLabel: string, text for a button
115 */
116 $.fn.crmFormContactLock = function(options) {
117 var form = this;
118 // AFTER ERROR: Render any "Ignore" and "Restart" buttons
119 return this.on('crmFormError', function(event, obj, status) {
120 var o = $(event.target);
121 var data = o.data('edit-params');
122 var errorTag = o.find('.update_oplock_ts');
123 if (errorTag.length > 0) {
124 $('<span>')
125 .addClass('crm-lock-button')
126 .appendTo(errorTag);
127
128 var buttonContainer = o.find('.crm-lock-button');
129 $('<button>')
130 .addClass('crm-button')
131 .text(options.saveAnywayLabel)
132 .click(function() {
133 $(form).find('input[name=oplock_ts]').val(errorTag.attr('data:update_oplock_ts'));
134 errorTag.parent().hide();
135 $(this).closest('form').find('.form-submit.default').first().click();
136 return false;
137 })
138 .appendTo(buttonContainer)
139 ;
140 $('<button>')
141 .addClass('crm-button')
142 .text(options.reloadLabel)
143 .click(function() {
144 window.location.reload();
145 return false;
146 })
147 .appendTo(buttonContainer)
148 ;
149 }
150 });
151 };
152
153 function errorHandler(response) {
154 CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
155 removeCiviOverlay($('.crm-inline-edit.form form'));
156 }
157
158 $('document').ready(function() {
159 // don't perform inline edit during print mode
160 if (CRM.summaryPrint.mode) {
161 $('div').removeClass('crm-inline-edit');
162 $('.crm-inline-block-content > div.crm-edit-help').remove();
163 $('div.crm-inline-block-content').removeAttr('title');
164 }
165 // Set page title
166 var oldName = 'CiviCRM';
167 var nameTitle = $('#crm-remove-title');
168 if (nameTitle.length > 0) {
169 oldName = nameTitle.text();
170 nameTitle.parent('h1').remove();
171 }
172 else {
173 $('h1').each(function() {
174 if ($(this).text() == oldName) {
175 $(this).remove();
176 }
177 });
178 }
179 function refreshTitle() {
180 var contactName = $('.crm-summary-display_name').text();
181 contactName = $.trim(contactName);
182 var title = $('title').html().replace(oldName, contactName);
183 document.title = title;
184 oldName = contactName;
185 }
186 $('#contactname-block').load(refreshTitle);
187 refreshTitle();
188
189 var clicking;
190 $('.crm-inline-edit-container')
191 .addClass('crm-edit-ready')
192 // Allow links inside edit blocks to be clicked without triggering edit
193 .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) {
194 if (event.which == 1) {
195 event.stopPropagation();
196 return false;
197 }
198 })
199 // Respond to a click (not drag, not right-click) of crm-inline-edit blocks
200 .on('mousedown', '.crm-inline-edit:not(.form)', function(button) {
201 if (button.which == 1) {
202 clicking = this;
203 setTimeout(function() {clicking = null;}, 500);
204 }
205 })
206 .on('mouseup', '.crm-inline-edit:not(.form)', function(button) {
207 if (clicking === this && button.which == 1) {
208 crmFormInline($(this));
209 }
210 })
211 // Inline edit form cancel button
212 .on('click', '.crm-inline-edit :submit[name$=cancel]', function() {
213 var container = $(this).closest('.crm-inline-edit.form');
214 $('.inline-edit-hidden-content', container).nextAll().remove();
215 $('.inline-edit-hidden-content > *:first-child', container).unwrap();
216 container.removeClass('form');
217 $('.crm-inline-edit-container').addClass('crm-edit-ready');
218 $('a.ui-notify-close', '#crm-notification-container').click();
219 return false;
220 })
221 // Switch tabs when clicking tag link
222 .on('click', '#tagLink a', function() {
223 $('#tab_tag a').click();
224 return false;
225 })
226 // make sure only one is_primary radio is checked
227 .on('change', '[class$=is_primary] input', function() {
228 if ($(this).is(':checked')) {
229 $('[class$=is_primary] input', $(this).closest('form')).not(this).prop('checked', false);
230 }
231 })
232 // make sure only one builk_mail radio is checked
233 .on('change', '.crm-email-bulkmail input', function(){
234 if ($(this).is(':checked')) {
235 $('.crm-email-bulkmail input').not(this).prop('checked', false);
236 }
237 })
238 // handle delete link within blocks
239 .on('click', '.crm-delete-inline', function() {
240 var row = $(this).closest('tr');
241 var form = $(this).closest('form');
242 row.addClass('hiddenElement');
243 $('input', row).val('');
244 //if the primary is checked for deleted block
245 //unset and set first as primary
246 if ($('[class$=is_primary] input:checked', row).length > 0) {
247 $('[class$=is_primary] input', row).prop('checked', false);
248 $('[class$=is_primary] input:first', form).prop('checked', true );
249 }
250 $('.add-more-inline', form).show();
251 })
252 // Delete an address
253 .on('click', '.crm-inline-edit.address .delete-button', function() {
254 var $block = $(this).closest('.crm-inline-edit.address');
255 CRM.confirm(function() {
256 CRM.api('address', 'delete', {id: $block.data('edit-params').aid}, {success:
257 function(data) {
258 CRM.alert('', ts('Address Deleted'), 'success');
259 $('.crm-inline-edit-container').addClass('crm-edit-ready');
260 $block.remove();
261 }
262 });
263 },
264 {
265 message: ts('Are you sure you want to delete this address?')
266 }
267 );
268 return false;
269 })
270 // add more and set focus to new row
271 .on('click', '.add-more-inline', function() {
272 var form = $(this).closest('form');
273 var row = $('tr[class="hiddenElement"]:first', form);
274 row.removeClass('hiddenElement');
275 $('input:focus', form).blur();
276 $('input:first', row).focus();
277 if ($('tr[class="hiddenElement"]').length < 1) {
278 $(this).hide();
279 }
280 });
281 // Trigger cancel button on esc keypress
282 $(document).keydown(function(key) {
283 if (key.which == 27) {
284 $('.crm-inline-edit.form :submit[name$=cancel]').click();
285 }
286 });
287 // Switch tabs when clicking log link
288 $('#crm-container').on('click', '#crm-record-log a.crm-log-view', function() {
289 $('#tab_log a').click();
290 return false;
291 });
292 });
293 })(cj);