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 $("#tab_log a em").html(response
.changeLog
.count
);
95 $("#crm-record-log").replaceWith(response
.changeLog
.markup
);
96 if ($('#Change_Log div').length
) {
97 $('#Change_Log').load($("#tab_log a").attr('href'));
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');
111 * Configure optimistic locking mechanism for inplace editing
113 * options.ignoreLabel: string, text for a button
114 * options.reloadLabel: string, text for a button
116 $.fn
.crmFormContactLock = function(options
) {
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) {
125 .addClass('crm-lock-button')
128 var buttonContainer
= o
.find('.crm-lock-button');
130 .addClass('crm-button')
131 .text(options
.saveAnywayLabel
)
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();
138 .appendTo(buttonContainer
)
141 .addClass('crm-button')
142 .text(options
.reloadLabel
)
144 window
.location
.reload();
147 .appendTo(buttonContainer
)
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'));
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');
166 var oldName
= 'CiviCRM';
167 var nameTitle
= $('#crm-remove-title');
168 if (nameTitle
.length
> 0) {
169 oldName
= nameTitle
.text();
170 nameTitle
.parent('h1').remove();
173 $('h1').each(function() {
174 if ($(this).text() == oldName
) {
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
;
186 $('#contactname-block').load(refreshTitle
);
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();
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) {
203 setTimeout(function() {clicking
= null;}, 500);
206 .on('mouseup', '.crm-inline-edit:not(.form)', function(button
) {
207 if (clicking
=== this && button
.which
== 1) {
208 crmFormInline($(this));
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();
221 // Switch tabs when clicking tag link
222 .on('click', '#tagLink a', function() {
223 $('#tab_tag a').click();
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);
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);
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 );
250 $('.add-more-inline', form
).show();
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
:
258 CRM
.alert('', ts('Address Deleted'), 'success');
259 $('.crm-inline-edit-container').addClass('crm-edit-ready');
265 message
: ts('Are you sure you want to delete this address?')
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) {
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();
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();