Commit | Line | Data |
---|---|---|
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 | ||
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; | |
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) { | |
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 | // Set page title | |
160 | var oldName = 'CiviCRM'; | |
161 | var nameTitle = $('#crm-remove-title'); | |
162 | if (nameTitle.length > 0) { | |
163 | oldName = nameTitle.text(); | |
164 | nameTitle.parent('h1').remove(); | |
165 | } | |
166 | else { | |
167 | $('h1').each(function() { | |
168 | if ($(this).text() == oldName) { | |
169 | $(this).remove(); | |
170 | } | |
171 | }); | |
172 | } | |
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; | |
179 | } | |
180 | $('#contactname-block').load(refreshTitle); | |
181 | refreshTitle(); | |
182 | ||
183 | var clicking; | |
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(); | |
190 | return false; | |
191 | } | |
192 | }) | |
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) { | |
196 | clicking = this; | |
197 | setTimeout(function() {clicking = null;}, 500); | |
198 | } | |
199 | }) | |
200 | .on('mouseup', '.crm-inline-edit:not(.form)', function(button) { | |
201 | if (clicking === this && button.which == 1) { | |
202 | crmFormInline($(this)); | |
203 | } | |
204 | }) | |
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(); | |
213 | return false; | |
214 | }) | |
215 | // Switch tabs when clicking tag link | |
216 | .on('click', '#tagLink a', function() { | |
217 | $('#tab_tag a').click(); | |
218 | return false; | |
219 | }) | |
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); | |
224 | } | |
225 | }) | |
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); | |
230 | } | |
231 | }) | |
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 ); | |
243 | } | |
244 | $('.add-more-inline', form).show(); | |
245 | }) | |
60e37cae CW |
246 | // Delete an address |
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: | |
251 | function(data) { | |
252 | CRM.alert('', ts('Address Deleted.'), 'success'); | |
253 | $('.crm-inline-edit-container').addClass('crm-edit-ready'); | |
254 | $block.remove(); | |
255 | } | |
256 | }); | |
257 | }, | |
258 | { | |
259 | message: ts('Are you sure you want to delete this address?') | |
260 | } | |
261 | ); | |
262 | return false; | |
263 | }) | |
6a488035 TO |
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) { | |
272 | $(this).hide(); | |
273 | } | |
274 | }); | |
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(); | |
279 | } | |
280 | }); | |
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(); | |
284 | return false; | |
285 | }); | |
286 | }); | |
287 | })(cj); |