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
== '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');
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() {
160 var oldName
= 'CiviCRM';
161 var nameTitle
= $('#crm-remove-title');
162 if (nameTitle
.length
> 0) {
163 oldName
= nameTitle
.text();
164 nameTitle
.parent('h1').remove();
167 $('h1').each(function() {
168 if ($(this).text() == oldName
) {
173 function refreshTitle() {
174 var contactName
= $('.crm-summary-display_name').text();
175 contactName
= $.trim(contactName
);
176 var title
= $('title').html().replace(oldName
, contactName
);
177 document
.title
= title
;
178 oldName
= contactName
;
180 $('#contactname-block').load(refreshTitle
);
184 $('.crm-inline-edit-container')
185 .addClass('crm-edit-ready')
186 // Allow links inside edit blocks to be clicked without triggering edit
187 .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
) {
188 if (event
.which
== 1) {
189 event
.stopPropagation();
193 // Respond to a click (not drag, not right-click) of crm-inline-edit blocks
194 .on('mousedown', '.crm-inline-edit:not(.form)', function(button
) {
195 if (button
.which
== 1) {
197 setTimeout(function() {clicking
= null;}, 500);
200 .on('mouseup', '.crm-inline-edit:not(.form)', function(button
) {
201 if (clicking
=== this && button
.which
== 1) {
202 crmFormInline($(this));
205 // Inline edit form cancel button
206 .on('click', '.crm-inline-edit :submit[name$=cancel]', function() {
207 var container
= $(this).closest('.crm-inline-edit.form');
208 $('.inline-edit-hidden-content', container
).nextAll().remove();
209 $('.inline-edit-hidden-content > *:first-child', container
).unwrap();
210 container
.removeClass('form');
211 $('.crm-inline-edit-container').addClass('crm-edit-ready');
212 $('a.ui-notify-close', '#crm-notification-container').click();
215 // Switch tabs when clicking tag link
216 .on('click', '#tagLink a', function() {
217 $('#tab_tag a').click();
220 // make sure only one is_primary radio is checked
221 .on('change', '[class$=is_primary] input', function() {
222 if ($(this).is(':checked')) {
223 $('[class$=is_primary] input', $(this).closest('form')).not(this).prop('checked', false);
226 // make sure only one builk_mail radio is checked
227 .on('change', '.crm-email-bulkmail input', function(){
228 if ($(this).is(':checked')) {
229 $('.crm-email-bulkmail input').not(this).prop('checked', false);
232 // handle delete link within blocks
233 .on('click', '.crm-delete-inline', function() {
234 var row
= $(this).closest('tr');
235 var form
= $(this).closest('form');
236 row
.addClass('hiddenElement');
237 $('input', row
).val('');
238 //if the primary is checked for deleted block
239 //unset and set first as primary
240 if ($('[class$=is_primary] input:checked', row
).length
> 0) {
241 $('[class$=is_primary] input', row
).prop('checked', false);
242 $('[class$=is_primary] input:first', form
).prop('checked', true );
244 $('.add-more-inline', form
).show();
247 .on('click', '.crm-inline-edit.address .delete-button', function() {
248 var $block
= $(this).closest('.crm-inline-edit.address');
249 CRM
.confirm(function() {
250 CRM
.api('address', 'delete', {id
: $block
.data('edit-params').aid
}, {success
:
252 CRM
.alert('', ts('Address Deleted'), 'success');
253 $('.crm-inline-edit-container').addClass('crm-edit-ready');
259 message
: ts('Are you sure you want to delete this address?')
264 // add more and set focus to new row
265 .on('click', '.add-more-inline', function() {
266 var form
= $(this).closest('form');
267 var row
= $('tr[class="hiddenElement"]:first', form
);
268 row
.removeClass('hiddenElement');
269 $('input:focus', form
).blur();
270 $('input:first', row
).focus();
271 if ($('tr[class="hiddenElement"]').length
< 1) {
275 // Trigger cancel button on esc keypress
276 $(document
).keydown(function(key
) {
277 if (key
.which
== 27) {
278 $('.crm-inline-edit.form :submit[name$=cancel]').click();
281 // Switch tabs when clicking log link
282 $('#crm-container').on('click', '#crm-record-log a.crm-log-view', function() {
283 $('#tab_log a').click();