Commit | Line | Data |
---|---|---|
53f2643c CW |
1 | // https://civicrm.org/licensing |
2 | /** | |
3 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface | |
4 | * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Ajax+Pages+and+Forms | |
5 | */ | |
1c1c1ae2 | 6 | (function($, CRM, undefined) { |
53f2643c | 7 | /** |
f0c9c7dd CW |
8 | * @param string path |
9 | * @param string|object query | |
8d83b77c | 10 | * @param string mode - optionally specify "front" or "back" |
53f2643c | 11 | */ |
0cc040cf | 12 | var tplURL; |
f0c9c7dd CW |
13 | CRM.url = function (path, query, mode) { |
14 | if (typeof path === 'object') { | |
15 | return tplURL = path; | |
53f2643c | 16 | } |
0cc040cf | 17 | if (!tplURL) { |
53f2643c CW |
18 | console && console.log && console.log('Warning: CRM.url called before initialization'); |
19 | } | |
8d83b77c | 20 | if (!mode) { |
f0c9c7dd | 21 | mode = CRM.config && CRM.config.isFrontend ? 'front' : 'back'; |
8d83b77c | 22 | } |
f0c9c7dd | 23 | query = query || ''; |
29bc93ee | 24 | var frag = path.split('?'); |
f0c9c7dd | 25 | var url = tplURL[mode].replace("*path*", frag[0]); |
53f2643c | 26 | |
f0c9c7dd CW |
27 | if (!query) { |
28 | url = url.replace(/[?&]\*query\*/, ''); | |
53f2643c CW |
29 | } |
30 | else { | |
f0c9c7dd | 31 | url = url.replace("*query*", typeof query === 'string' ? query : $.param(query)); |
53f2643c CW |
32 | } |
33 | if (frag[1]) { | |
f0c9c7dd | 34 | url += (url.indexOf('?') < 0 ? '?' : '&') + frag[1]; |
53f2643c CW |
35 | } |
36 | return url; | |
37 | }; | |
38 | ||
39 | // Backwards compatible with jQuery fn | |
40 | $.extend ({'crmURL': | |
41 | function (p, params) { | |
42 | console && console.log && console.log('Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.'); | |
43 | return CRM.url(p, params); | |
44 | } | |
45 | }); | |
46 | ||
47 | $.fn.crmURL = function () { | |
48 | return this.each(function() { | |
49 | if (this.href) { | |
50 | this.href = CRM.url(this.href); | |
51 | } | |
52 | }); | |
53 | }; | |
54 | ||
55 | /** | |
56 | * AJAX api | |
57 | */ | |
58 | CRM.api3 = function(entity, action, params, status) { | |
59 | if (typeof(entity) === 'string') { | |
60 | params = { | |
61 | entity: entity, | |
62 | action: action.toLowerCase(), | |
63 | json: JSON.stringify(params || {}) | |
64 | }; | |
65 | } else { | |
66 | params = { | |
67 | entity: 'api3', | |
68 | action: 'call', | |
69 | json: JSON.stringify(entity) | |
e4f4dc22 CW |
70 | }; |
71 | status = action; | |
53f2643c CW |
72 | } |
73 | var ajax = $.ajax({ | |
74 | url: CRM.url('civicrm/ajax/rest'), | |
75 | dataType: 'json', | |
76 | data: params, | |
77 | type: params.action.indexOf('get') < 0 ? 'POST' : 'GET' | |
78 | }); | |
79 | if (status) { | |
80 | // Default status messages | |
81 | if (status === true) { | |
82 | status = {success: params.action === 'delete' ? ts('Removed') : ts('Saved')}; | |
83 | if (params.action.indexOf('get') === 0) { | |
84 | status.start = ts('Loading...'); | |
85 | status.success = null; | |
86 | } | |
87 | } | |
88 | var messages = status === true ? {} : status; | |
89 | CRM.status(status, ajax); | |
90 | } | |
91 | return ajax; | |
92 | }; | |
93 | ||
94 | /** | |
95 | * @deprecated | |
96 | * AJAX api | |
97 | */ | |
98 | CRM.api = function(entity, action, params, options) { | |
99 | // Default settings | |
100 | var settings = { | |
101 | context: null, | |
102 | success: function(result, settings) { | |
103 | return true; | |
104 | }, | |
105 | error: function(result, settings) { | |
106 | $().crmError(result.error_message, ts('Error')); | |
107 | return false; | |
108 | }, | |
109 | callBack: function(result, settings) { | |
110 | if (result.is_error == 1) { | |
111 | return settings.error.call(this, result, settings); | |
112 | } | |
113 | return settings.success.call(this, result, settings); | |
114 | }, | |
115 | ajaxURL: 'civicrm/ajax/rest' | |
116 | }; | |
117 | action = action.toLowerCase(); | |
118 | // Default success handler | |
119 | switch (action) { | |
120 | case "update": | |
121 | case "create": | |
122 | case "setvalue": | |
123 | case "replace": | |
124 | settings.success = function() { | |
125 | CRM.status(ts('Saved')); | |
126 | return true; | |
127 | }; | |
128 | break; | |
129 | case "delete": | |
130 | settings.success = function() { | |
131 | CRM.status(ts('Removed')); | |
132 | return true; | |
133 | }; | |
134 | } | |
135 | params = { | |
136 | entity: entity, | |
137 | action: action, | |
138 | json: JSON.stringify(params) | |
139 | }; | |
140 | // Pass copy of settings into closure to preserve its value during multiple requests | |
141 | (function(stg) { | |
142 | $.ajax({ | |
143 | url: stg.ajaxURL.indexOf('http') === 0 ? stg.ajaxURL : CRM.url(stg.ajaxURL), | |
144 | dataType: 'json', | |
145 | data: params, | |
146 | type: action.indexOf('get') < 0 ? 'POST' : 'GET', | |
147 | success: function(result) { | |
148 | stg.callBack.call(stg.context, result, stg); | |
149 | } | |
150 | }); | |
151 | })($.extend({}, settings, options)); | |
152 | }; | |
153 | ||
154 | /** | |
155 | * Backwards compatible with jQuery fn | |
156 | * @deprecated | |
157 | */ | |
158 | $.fn.crmAPI = function(entity, action, params, options) { | |
159 | console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.'); | |
160 | return CRM.api.call(this, entity, action, params, options); | |
161 | }; | |
162 | ||
163 | $.widget('civi.crmSnippet', { | |
164 | options: { | |
165 | url: null, | |
166 | block: true, | |
167 | crmForm: null | |
168 | }, | |
169 | _originalContent: null, | |
170 | _originalUrl: null, | |
171 | isOriginalUrl: function() { | |
172 | var | |
173 | args = {}, | |
174 | same = true, | |
175 | newUrl = this._formatUrl(this.options.url), | |
176 | oldUrl = this._formatUrl(this._originalUrl); | |
177 | // Compare path | |
178 | if (newUrl.split('?')[0] !== oldUrl.split('?')[0]) { | |
179 | return false; | |
180 | } | |
181 | // Compare arguments | |
182 | $.each(newUrl.split('?')[1].split('&'), function(k, v) { | |
183 | var arg = v.split('='); | |
184 | args[arg[0]] = arg[1]; | |
185 | }); | |
186 | $.each(oldUrl.split('?')[1].split('&'), function(k, v) { | |
187 | var arg = v.split('='); | |
188 | if (args[arg[0]] !== undefined && arg[1] !== args[arg[0]]) { | |
189 | same = false; | |
190 | } | |
191 | }); | |
192 | return same; | |
193 | }, | |
194 | resetUrl: function() { | |
195 | this.options.url = this._originalUrl; | |
196 | }, | |
197 | _create: function() { | |
198 | this.element.addClass('crm-ajax-container'); | |
199 | if (!this.element.is('.crm-container *')) { | |
200 | this.element.addClass('crm-container'); | |
201 | } | |
202 | this._handleOrderLinks(); | |
203 | // Set default if not supplied | |
204 | this.options.url = this.options.url || document.location.href; | |
205 | this._originalUrl = this.options.url; | |
206 | }, | |
207 | _onFailure: function(data) { | |
208 | this.options.block && this.element.unblock(); | |
209 | this.element.trigger('crmAjaxFail', data); | |
210 | CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); | |
211 | }, | |
61cab70a CW |
212 | _onError: function(data) { |
213 | this.element.attr('data-unsaved-changes', 'false').trigger('crmAjaxError', data); | |
f7aaf23c | 214 | if (this.options.crmForm && this.options.crmForm.autoClose && this.element.data('uiDialog')) { |
61cab70a CW |
215 | this.element.dialog('close'); |
216 | } | |
217 | }, | |
53f2643c CW |
218 | _formatUrl: function(url) { |
219 | // Strip hash | |
220 | url = url.split('#')[0]; | |
221 | // Add snippet argument to url | |
222 | if (url.search(/[&?]snippet=/) < 0) { | |
223 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json'; | |
224 | } else { | |
225 | url = url.replace(/snippet=[^&]*/, 'snippet=json'); | |
226 | } | |
227 | return url; | |
228 | }, | |
229 | // Hack to deal with civicrm legacy sort functionality | |
230 | _handleOrderLinks: function() { | |
231 | var that = this; | |
232 | $('a.crm-weight-arrow', that.element).click(function(e) { | |
233 | that.options.block && that.element.block(); | |
234 | $.getJSON(that._formatUrl(this.href)).done(function() { | |
235 | that.refresh(); | |
236 | }); | |
237 | e.stopImmediatePropagation(); | |
238 | return false; | |
239 | }); | |
240 | }, | |
241 | refresh: function() { | |
242 | var that = this; | |
243 | var url = this._formatUrl(this.options.url); | |
244 | this.options.crmForm && $('form', this.element).ajaxFormUnbind(); | |
7e13d44e CW |
245 | if (this._originalContent === null) { |
246 | this._originalContent = this.element.contents().detach(); | |
247 | } | |
53f2643c CW |
248 | this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block(); |
249 | $.getJSON(url, function(data) { | |
61cab70a | 250 | if (!$.isPlainObject(data)) { |
53f2643c CW |
251 | that._onFailure(data); |
252 | return; | |
253 | } | |
61cab70a CW |
254 | if (data.status === 'error') { |
255 | that._onError(data); | |
256 | return; | |
257 | } | |
53f2643c | 258 | data.url = url; |
7e13d44e | 259 | that.element.trigger('crmBeforeLoad', data).html(data.content); |
53f2643c CW |
260 | that._handleOrderLinks(); |
261 | that.element.trigger('crmLoad', data); | |
262 | that.options.crmForm && that.element.trigger('crmFormLoad', data); | |
263 | }).fail(function() { | |
264 | that._onFailure(); | |
265 | }); | |
266 | }, | |
267 | _destroy: function() { | |
268 | this.element.removeClass('crm-ajax-container'); | |
269 | this.options.crmForm && $('form', this.element).ajaxFormUnbind(); | |
270 | if (this._originalContent !== null) { | |
271 | this.element.empty().append(this._originalContent); | |
272 | } | |
273 | } | |
274 | }); | |
275 | ||
98465f9e CW |
276 | var dialogCount = 0, |
277 | exclude = '[href^=#], [href^=javascript], [onclick], .no-popup, .cancel'; | |
278 | ||
53f2643c CW |
279 | CRM.loadPage = function(url, options) { |
280 | var settings = { | |
281 | target: '#crm-ajax-dialog-' + (dialogCount++), | |
282 | dialog: false | |
283 | }; | |
284 | if (!options || !options.target) { | |
285 | settings.dialog = { | |
286 | modal: true, | |
287 | width: '65%', | |
288 | height: '75%' | |
289 | }; | |
290 | } | |
291 | options && $.extend(true, settings, options); | |
292 | settings.url = url; | |
293 | // Create new dialog | |
294 | if (settings.dialog) { | |
295 | // HACK: jQuery UI doesn't support relative height | |
a1c7d42f | 296 | if (typeof settings.dialog.height === 'string' && settings.dialog.height.indexOf('%') > 0) { |
53f2643c CW |
297 | settings.dialog.height = parseInt($(window).height() * (parseFloat(settings.dialog.height)/100), 10); |
298 | } | |
299 | $('<div id="'+ settings.target.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog); | |
73d2716e CW |
300 | $(settings.target) |
301 | .on('dialogclose', function() { | |
302 | if ($(this).attr('data-unsaved-changes') !== 'true') { | |
303 | $(this).crmSnippet('destroy').dialog('destroy').remove(); | |
304 | } | |
305 | }) | |
306 | .on('crmLoad', function(e, data) { | |
307 | // Set title | |
308 | if (e.target === $(settings.target)[0] && data && !settings.dialog.title && data.title) { | |
309 | $(this).dialog('option', 'title', data.title); | |
310 | } | |
311 | // Adjust height to fit content (small delay to allow elements to render) | |
312 | window.setTimeout(function() { | |
28d510ab | 313 | var currentHeight = $(settings.target).parent().outerHeight(), |
73d2716e CW |
314 | padding = currentHeight - $(settings.target).height(), |
315 | newHeight = $(settings.target).prop('scrollHeight') + padding, | |
28d510ab | 316 | menuHeight = $('#civicrm-menu').outerHeight(), |
73d2716e CW |
317 | maxHeight = $(window).height() - menuHeight; |
318 | newHeight = newHeight > maxHeight ? maxHeight : newHeight; | |
319 | if (newHeight > (currentHeight + 15)) { | |
320 | $(settings.target).dialog('option', { | |
321 | position: {my: 'center', at: 'center center+' + (menuHeight / 2), of: window}, | |
322 | height: newHeight | |
323 | }); | |
324 | } | |
325 | }, 500); | |
326 | }); | |
53f2643c CW |
327 | } |
328 | $(settings.target).crmSnippet(settings).crmSnippet('refresh'); | |
329 | return $(settings.target); | |
330 | }; | |
331 | CRM.loadForm = function(url, options) { | |
55cb6db1 | 332 | var formErrors = [], settings = { |
53f2643c CW |
333 | crmForm: { |
334 | ajaxForm: {}, | |
335 | autoClose: true, | |
336 | validate: true, | |
02c82dda | 337 | refreshAction: ['next_new', 'submit_savenext', 'upload_new'], |
98465f9e CW |
338 | cancelButton: '.cancel', |
339 | openInline: 'a.open-inline, a.button, a.action-item', | |
1c1c1ae2 | 340 | onCancel: function(event) {} |
53f2643c CW |
341 | } |
342 | }; | |
343 | // Move options that belong to crmForm. Others will be passed through to crmSnippet | |
344 | options && $.each(options, function(key, value) { | |
345 | if (typeof(settings.crmForm[key]) !== 'undefined') { | |
346 | settings.crmForm[key] = value; | |
347 | } | |
348 | else { | |
349 | settings[key] = value; | |
350 | } | |
351 | }); | |
352 | ||
353 | var widget = CRM.loadPage(url, settings).off('.crmForm'); | |
354 | ||
f582fc8f | 355 | // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out |
88e9380e | 356 | function cancelAction() { |
e9a3e054 | 357 | var dirty = CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', widget)); |
c4e00dbb | 358 | widget.attr('data-unsaved-changes', dirty ? 'true' : 'false'); |
7c2110fd CW |
359 | if (dirty) { |
360 | var id = widget.attr('id') + '-unsaved-alert', | |
e9a3e054 | 361 | title = widget.dialog('option', 'title'), |
15d60951 | 362 | alert = CRM.alert('<p>' + ts('%1 has not been saved.', {1: title}) + '</p><p><a href="#" id="' + id + '">' + ts('Restore') + '</a></p>', ts('Unsaved Changes'), 'alert unsaved-dialog', {expires: 60000}); |
8537b332 | 363 | $('#' + id).button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}}).click(function(e) { |
15d60951 | 364 | widget.attr('data-unsaved-changes', 'false').dialog('open'); |
8537b332 | 365 | e.preventDefault(); |
7c2110fd | 366 | }); |
88e9380e | 367 | } |
88e9380e | 368 | } |
c4e00dbb CW |
369 | |
370 | widget.data('uiDialog') && widget.on('dialogbeforeclose', function(e) { | |
371 | // CRM-14353 - Warn unsaved changes if user clicks close button or presses "esc" | |
372 | if (e.originalEvent) { | |
373 | cancelAction(); | |
374 | } | |
375 | }); | |
88e9380e | 376 | |
53f2643c | 377 | widget.on('crmFormLoad.crmForm', function(event, data) { |
298f69da CW |
378 | var $el = $(this).attr('data-unsaved-changes', 'false'), |
379 | settings = $el.crmSnippet('option', 'crmForm'); | |
7c2110fd CW |
380 | settings.cancelButton && $(settings.cancelButton, this).click(function(e) { |
381 | e.preventDefault(); | |
382 | var returnVal = settings.onCancel.call($el, e); | |
53f2643c | 383 | if (returnVal !== false) { |
7c2110fd | 384 | $el.trigger('crmFormCancel', e); |
53f2643c | 385 | if ($el.data('uiDialog') && settings.autoClose) { |
7c2110fd | 386 | cancelAction(); |
c4e00dbb | 387 | $el.dialog('close'); |
53f2643c CW |
388 | } |
389 | else if (!settings.autoClose) { | |
390 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
391 | } | |
392 | } | |
53f2643c CW |
393 | }); |
394 | if (settings.validate) { | |
3d527838 | 395 | $("form", this).crmValidate(); |
53f2643c | 396 | } |
cae80d9f | 397 | $("form:not('[data-no-ajax-submit=true]')", this).ajaxForm($.extend({ |
53f2643c CW |
398 | url: data.url.replace(/reset=1[&]?/, ''), |
399 | dataType: 'json', | |
400 | success: function(response) { | |
1c1c1ae2 | 401 | if (response.content === undefined) { |
53f2643c CW |
402 | $el.crmSnippet('option', 'block') && $el.unblock(); |
403 | $el.trigger('crmFormSuccess', response); | |
404 | // Reset form for e.g. "save and new" | |
0a94ab7d | 405 | if (response.userContext && (response.status === 'redirect' || (settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0))) { |
b305fb88 CW |
406 | // Force reset of original url |
407 | $el.data('civiCrmSnippet')._originalUrl = response.userContext; | |
408 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
53f2643c | 409 | } |
9317128c CW |
410 | // Close if we are on the original url or the action was "delete" (in which case returning to view may be inappropriate) |
411 | else if ($el.data('uiDialog') && (settings.autoClose || response.action === 8)) { | |
53f2643c CW |
412 | $el.dialog('close'); |
413 | } | |
414 | else if (settings.autoClose === false) { | |
415 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
416 | } | |
417 | } | |
418 | else { | |
419 | response.url = data.url; | |
1c1c1ae2 CW |
420 | $el.html(response.content).trigger('crmLoad', response).trigger('crmFormLoad', response); |
421 | if (response.status === 'form_error') { | |
55cb6db1 | 422 | formErrors = []; |
1c1c1ae2 CW |
423 | $el.trigger('crmFormError', response); |
424 | $.each(response.errors || [], function(formElement, msg) { | |
55cb6db1 | 425 | formErrors.push($('[name="'+formElement+'"]', $el).crmError(msg)); |
1c1c1ae2 CW |
426 | }); |
427 | } | |
53f2643c CW |
428 | } |
429 | }, | |
430 | beforeSerialize: function(form, options) { | |
431 | if (window.CKEDITOR && window.CKEDITOR.instances) { | |
432 | $.each(CKEDITOR.instances, function() { | |
433 | this.updateElement && this.updateElement(); | |
434 | }); | |
435 | } | |
436 | }, | |
437 | beforeSubmit: function(submission) { | |
55cb6db1 CW |
438 | $.each(formErrors, function() { |
439 | this && this.close && this.close(); | |
440 | }); | |
53f2643c CW |
441 | $el.crmSnippet('option', 'block') && $el.block(); |
442 | $el.trigger('crmFormSubmit', submission); | |
443 | } | |
444 | }, settings.ajaxForm)); | |
445 | if (settings.openInline) { | |
446 | settings.autoClose = $el.crmSnippet('isOriginalUrl'); | |
98465f9e | 447 | $(settings.openInline, this).not(exclude + ', .crm-popup').click(function(event) { |
53f2643c CW |
448 | $el.crmSnippet('option', 'url', $(this).attr('href')).crmSnippet('refresh'); |
449 | return false; | |
450 | }); | |
451 | } | |
298f69da CW |
452 | // Show form buttons as part of the dialog |
453 | if ($el.data('uiDialog')) { | |
454 | var buttons = [], added = []; | |
455 | $('input.crm-form-submit, a.button', $el).each(function() { | |
456 | var $el = $(this), | |
457 | label = $el.is('input') ? $el.attr('value') : $el.text(), | |
458 | identifier = $el.attr('name') || $el.attr('href'); | |
459 | if (!identifier || identifier === '#' || $.inArray(identifier, added) < 0) { | |
460 | var button = {text: label, click: function () { | |
461 | $el.click(); | |
462 | }}; | |
463 | if ($el.find('.icon').length) { | |
464 | button.icons = {primary: $el.find('.icon').attr('class')}; | |
465 | } else { | |
466 | button.icons = {primary: $el.hasClass('cancel') ? 'ui-icon-close' : 'ui-icon-check'}; | |
467 | if (identifier.substr(identifier.length-4) === '_new') { | |
468 | button.icons.secondary = 'ui-icon-plus'; | |
469 | } | |
470 | } | |
471 | buttons.push(button); | |
472 | added.push(identifier); | |
473 | } | |
474 | $el.hide().parents('.crm-button, .crm-submit-buttons, .action-link').hide(); | |
475 | }); | |
476 | $el.dialog('option', 'buttons', buttons); | |
477 | } | |
022d2163 | 478 | // Allow a button to prevent ajax submit |
1e29624c CW |
479 | $('input[data-no-ajax-submit=true]').click(function() { |
480 | $(this).closest('form').ajaxFormUnbind(); | |
481 | }); | |
53f2643c | 482 | // For convenience, focus the first field |
b18392cc | 483 | $('input[type=text], textarea, select', this).filter(':visible').first().not('.dateplugin').focus(); |
53f2643c CW |
484 | }); |
485 | return widget; | |
486 | }; | |
487 | /** | |
022d2163 | 488 | * Handler for jQuery click event e.g. $('a').click(CRM.popup); |
53f2643c | 489 | */ |
35df910c | 490 | CRM.popup = function(e) { |
53f2643c CW |
491 | var $el = $(this).first(), |
492 | url = $el.attr('href'), | |
493 | popup = $el.data('popup-type') === 'page' ? CRM.loadPage : CRM.loadForm, | |
494 | settings = $el.data('popup-settings') || {}, | |
7fe745d1 | 495 | formSuccess = false; |
53f2643c | 496 | settings.dialog = settings.dialog || {}; |
35df910c | 497 | if (e.isDefaultPrevented() || !CRM.config.ajaxPopupsEnabled || !url || $el.is(exclude)) { |
53f2643c CW |
498 | return; |
499 | } | |
a1c7d42f CW |
500 | // Sized based on css class |
501 | if ($el.hasClass('small-popup')) { | |
53f2643c CW |
502 | settings.dialog.width = 400; |
503 | settings.dialog.height = 300; | |
504 | } | |
505 | else if ($el.hasClass('medium-popup')) { | |
506 | settings.dialog.width = settings.dialog.height = '50%'; | |
507 | } | |
508 | else if ($el.hasClass('huge-popup')) { | |
77042884 | 509 | settings.dialog.height = '90%'; |
53f2643c CW |
510 | } |
511 | var dialog = popup(url, settings); | |
512 | // Trigger events from the dialog on the original link element | |
513 | $el.trigger('crmPopupOpen', [dialog]); | |
4b472ff2 CW |
514 | // Listen for success events and buffer them so we only trigger once |
515 | dialog.on('crmFormSuccess.crmPopup crmPopupFormSuccess.crmPopup', function() { | |
7fe745d1 CW |
516 | formSuccess = true; |
517 | }); | |
518 | dialog.on('dialogclose.crmPopup', function(e, data) { | |
519 | if (formSuccess) { | |
520 | $el.trigger('crmPopupFormSuccess', [dialog, data]); | |
521 | } | |
522 | $el.trigger('crmPopupClose', [dialog, data]); | |
53f2643c | 523 | }); |
35df910c | 524 | e.preventDefault(); |
53f2643c | 525 | }; |
554b1768 | 526 | /** |
7e9fdecf | 527 | * An event callback for CRM.popup or a standalone function to refresh the content around a given element |
022d2163 | 528 | * @param e {event|selector} |
554b1768 CW |
529 | */ |
530 | CRM.refreshParent = function(e) { | |
531 | // Use e.target if input smells like an event, otherwise assume it's a jQuery selector | |
532 | var $el = (e.stopPropagation && e.target) ? $(e.target) : $(e), | |
533 | $table = $el.closest('.dataTable'); | |
534 | // Call native refresh method on ajax datatables | |
022d2163 | 535 | if ($table.length && $.fn.DataTable.fnIsDataTable($table[0]) && $table.dataTable().fnSettings().sAjaxSource) { |
554b1768 CW |
536 | // Refresh ALL datatables - needed for contact relationship tab |
537 | $.each($.fn.dataTable.fnTables(), function() { | |
538 | $(this).dataTable().fnSettings().sAjaxSource && $(this).unblock().dataTable().fnDraw(); | |
539 | }); | |
540 | } | |
541 | // Otherwise refresh the nearest crmSnippet | |
542 | else { | |
543 | $el.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh'); | |
544 | } | |
545 | }; | |
a1c7d42f CW |
546 | |
547 | $(function($) { | |
15d60951 CW |
548 | $('body') |
549 | .on('click', 'a.crm-popup', CRM.popup) | |
550 | // Close unsaved dialog messages | |
551 | .on('dialogopen', function(e) { | |
552 | $('.alert.unsaved-dialog .ui-notify-cross', '#crm-notification-container').click(); | |
553 | }) | |
554 | // Destroy old unsaved dialog | |
555 | .on('dialogcreate', function(e) { | |
556 | $('.ui-dialog-content.crm-ajax-container:hidden[data-unsaved-changes=true]').crmSnippet('destroy').dialog('destroy').remove(); | |
557 | }); | |
a1c7d42f CW |
558 | }); |
559 | ||
53f2643c | 560 | }(jQuery, CRM)); |