1 // http://civicrm.org/licensing
6 beforeSubmit: function(arr
, $form
, options
) {
9 success
: requestHandler
,
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);
20 $('.crm-edit-ready').removeClass('crm-edit-ready');
22 $.getJSON(CRM
.url('civicrm/ajax/inline', data
))
24 .done(function(response
) {
26 o
.css('overflow', 'hidden').wrapInner('<div class="inline-edit-hidden-content" style="display:none" />').append(response
.content
);
28 var newHeight
= $('.crm-container-snippet', o
).height();
29 var diff
= newHeight
- parseInt(o
.css('height'), 10);
33 o
.animate({height
: '' + newHeight
+ 'px'}, diff
* 2, function() {
34 o
.removeAttr('style');
36 $('form', o
).validate(CRM
.validate
.params
);
37 ajaxFormParams
.data
= data
;
38 $('form', o
).ajaxForm(ajaxFormParams
);
39 o
.trigger('crmFormLoad');
44 function requestHandler(response
) {
45 var o
= $('div.crm-inline-edit.form');
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');
61 else if (clone
.hasClass('contactCardRight')) {
62 clone
.removeClass('contactCardRight').addClass('contactCardLeft');
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
));
71 $('a.ui-notify-close', '#crm-notification-container').click();
73 if (o
.hasClass('address') && !o
.hasClass('add-new') && !response
.addressId
) {
75 CRM
.alert('', ts('Address Deleted'), 'success');
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');
86 $(this).closest('.crm-summary-block').load(CRM
.url('civicrm/ajax/inline', data
), function() {$(this).trigger('load');});
89 CRM
.alert('', ts('Saved'), 'success');
91 // Update changelog tab and contact footer
92 if (response
.changeLog
.count
) {
93 CRM
.updateTabCount('#tab_log', response
.changeLog
.count
);
95 $("#crm-record-log").replaceWith(response
.changeLog
.markup
);
96 // Refresh tab contents - Advanced logging
97 if (CRM
.reloadChangeLogTab
) {
98 CRM
.reloadChangeLogTab();
100 // Refresh tab contents - Simple logging
101 else if ($('#changeLog').closest('.ui-tabs-panel').data('civicrmCrmSnippet')) {
102 $('#changeLog').closest('.ui-tabs-panel').crmSnippet('destroy');
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');
116 * Configure optimistic locking mechanism for inplace editing
118 * options.ignoreLabel: string, text for a button
119 * options.reloadLabel: string, text for a button
121 $.fn
.crmFormContactLock = function(options
) {
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) {
130 .addClass('crm-lock-button')
133 var buttonContainer
= o
.find('.crm-lock-button');
135 .addClass('crm-button')
136 .text(options
.saveAnywayLabel
)
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();
143 .appendTo(buttonContainer
)
146 .addClass('crm-button')
147 .text(options
.reloadLabel
)
149 window
.location
.reload();
152 .appendTo(buttonContainer
)
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'));
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');
171 var oldName
= 'CiviCRM';
172 var nameTitle
= $('#crm-remove-title');
173 if (nameTitle
.length
> 0) {
174 oldName
= nameTitle
.text();
175 nameTitle
.parent('h1').remove();
178 $('h1').each(function() {
179 if ($(this).text() == oldName
) {
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
;
191 $('#contactname-block').load(refreshTitle
);
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();
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) {
208 setTimeout(function() {clicking
= null;}, 500);
211 .on('mouseup', '.crm-inline-edit:not(.form)', function(button
) {
212 if (clicking
=== this && button
.which
== 1) {
213 crmFormInline($(this));
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();
226 // Switch tabs when clicking tag link
227 .on('click', '#tagLink a', function() {
228 $('#tab_tag a').click();
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);
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);
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 );
255 $('.add-more-inline', form
).show();
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
:
263 CRM
.alert('', ts('Address Deleted'), 'success');
264 $('.crm-inline-edit-container').addClass('crm-edit-ready');
270 message
: ts('Are you sure you want to delete this address?')
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) {
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();
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();