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') { | |
f54254d8 TO |
15 | tplURL = path; |
16 | return path; | |
53f2643c | 17 | } |
0cc040cf | 18 | if (!tplURL) { |
bba9b4f0 | 19 | CRM.console('error', 'Error: CRM.url called before initialization'); |
53f2643c | 20 | } |
8d83b77c | 21 | if (!mode) { |
f0c9c7dd | 22 | mode = CRM.config && CRM.config.isFrontend ? 'front' : 'back'; |
8d83b77c | 23 | } |
f0c9c7dd | 24 | query = query || ''; |
29bc93ee | 25 | var frag = path.split('?'); |
f0c9c7dd | 26 | var url = tplURL[mode].replace("*path*", frag[0]); |
53f2643c | 27 | |
f0c9c7dd CW |
28 | if (!query) { |
29 | url = url.replace(/[?&]\*query\*/, ''); | |
53f2643c CW |
30 | } |
31 | else { | |
f0c9c7dd | 32 | url = url.replace("*query*", typeof query === 'string' ? query : $.param(query)); |
53f2643c CW |
33 | } |
34 | if (frag[1]) { | |
f0c9c7dd | 35 | url += (url.indexOf('?') < 0 ? '?' : '&') + frag[1]; |
53f2643c CW |
36 | } |
37 | return url; | |
38 | }; | |
39 | ||
bba9b4f0 | 40 | // @deprecated |
53f2643c CW |
41 | $.extend ({'crmURL': |
42 | function (p, params) { | |
bba9b4f0 | 43 | CRM.console('warn', 'Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.'); |
53f2643c CW |
44 | return CRM.url(p, params); |
45 | } | |
46 | }); | |
47 | ||
48 | $.fn.crmURL = function () { | |
49 | return this.each(function() { | |
50 | if (this.href) { | |
51 | this.href = CRM.url(this.href); | |
52 | } | |
53 | }); | |
54 | }; | |
55 | ||
56 | /** | |
57 | * AJAX api | |
58 | */ | |
59 | CRM.api3 = function(entity, action, params, status) { | |
60 | if (typeof(entity) === 'string') { | |
61 | params = { | |
62 | entity: entity, | |
63 | action: action.toLowerCase(), | |
64 | json: JSON.stringify(params || {}) | |
65 | }; | |
66 | } else { | |
67 | params = { | |
68 | entity: 'api3', | |
69 | action: 'call', | |
70 | json: JSON.stringify(entity) | |
e4f4dc22 CW |
71 | }; |
72 | status = action; | |
53f2643c CW |
73 | } |
74 | var ajax = $.ajax({ | |
75 | url: CRM.url('civicrm/ajax/rest'), | |
76 | dataType: 'json', | |
77 | data: params, | |
78 | type: params.action.indexOf('get') < 0 ? 'POST' : 'GET' | |
79 | }); | |
80 | if (status) { | |
81 | // Default status messages | |
82 | if (status === true) { | |
83 | status = {success: params.action === 'delete' ? ts('Removed') : ts('Saved')}; | |
84 | if (params.action.indexOf('get') === 0) { | |
85 | status.start = ts('Loading...'); | |
86 | status.success = null; | |
87 | } | |
88 | } | |
89 | var messages = status === true ? {} : status; | |
90 | CRM.status(status, ajax); | |
91 | } | |
92 | return ajax; | |
93 | }; | |
94 | ||
95 | /** | |
96 | * @deprecated | |
97 | * AJAX api | |
98 | */ | |
99 | CRM.api = function(entity, action, params, options) { | |
100 | // Default settings | |
101 | var settings = { | |
102 | context: null, | |
103 | success: function(result, settings) { | |
104 | return true; | |
105 | }, | |
106 | error: function(result, settings) { | |
107 | $().crmError(result.error_message, ts('Error')); | |
108 | return false; | |
109 | }, | |
110 | callBack: function(result, settings) { | |
111 | if (result.is_error == 1) { | |
112 | return settings.error.call(this, result, settings); | |
113 | } | |
114 | return settings.success.call(this, result, settings); | |
115 | }, | |
116 | ajaxURL: 'civicrm/ajax/rest' | |
117 | }; | |
118 | action = action.toLowerCase(); | |
119 | // Default success handler | |
120 | switch (action) { | |
121 | case "update": | |
122 | case "create": | |
123 | case "setvalue": | |
124 | case "replace": | |
125 | settings.success = function() { | |
126 | CRM.status(ts('Saved')); | |
127 | return true; | |
128 | }; | |
129 | break; | |
130 | case "delete": | |
131 | settings.success = function() { | |
132 | CRM.status(ts('Removed')); | |
133 | return true; | |
134 | }; | |
135 | } | |
136 | params = { | |
137 | entity: entity, | |
138 | action: action, | |
139 | json: JSON.stringify(params) | |
140 | }; | |
141 | // Pass copy of settings into closure to preserve its value during multiple requests | |
142 | (function(stg) { | |
143 | $.ajax({ | |
144 | url: stg.ajaxURL.indexOf('http') === 0 ? stg.ajaxURL : CRM.url(stg.ajaxURL), | |
145 | dataType: 'json', | |
146 | data: params, | |
147 | type: action.indexOf('get') < 0 ? 'POST' : 'GET', | |
148 | success: function(result) { | |
149 | stg.callBack.call(stg.context, result, stg); | |
150 | } | |
151 | }); | |
152 | })($.extend({}, settings, options)); | |
153 | }; | |
154 | ||
155 | /** | |
156 | * Backwards compatible with jQuery fn | |
157 | * @deprecated | |
158 | */ | |
159 | $.fn.crmAPI = function(entity, action, params, options) { | |
bba9b4f0 | 160 | CRM.console('warn', 'Calling crmAPI from jQuery is deprecated. Please use CRM.api3() instead.'); |
53f2643c CW |
161 | return CRM.api.call(this, entity, action, params, options); |
162 | }; | |
163 | ||
164 | $.widget('civi.crmSnippet', { | |
165 | options: { | |
166 | url: null, | |
167 | block: true, | |
168 | crmForm: null | |
169 | }, | |
170 | _originalContent: null, | |
171 | _originalUrl: null, | |
172 | isOriginalUrl: function() { | |
173 | var | |
174 | args = {}, | |
175 | same = true, | |
176 | newUrl = this._formatUrl(this.options.url), | |
177 | oldUrl = this._formatUrl(this._originalUrl); | |
178 | // Compare path | |
179 | if (newUrl.split('?')[0] !== oldUrl.split('?')[0]) { | |
180 | return false; | |
181 | } | |
182 | // Compare arguments | |
183 | $.each(newUrl.split('?')[1].split('&'), function(k, v) { | |
184 | var arg = v.split('='); | |
185 | args[arg[0]] = arg[1]; | |
186 | }); | |
187 | $.each(oldUrl.split('?')[1].split('&'), function(k, v) { | |
188 | var arg = v.split('='); | |
189 | if (args[arg[0]] !== undefined && arg[1] !== args[arg[0]]) { | |
190 | same = false; | |
191 | } | |
192 | }); | |
193 | return same; | |
194 | }, | |
195 | resetUrl: function() { | |
196 | this.options.url = this._originalUrl; | |
197 | }, | |
198 | _create: function() { | |
199 | this.element.addClass('crm-ajax-container'); | |
200 | if (!this.element.is('.crm-container *')) { | |
201 | this.element.addClass('crm-container'); | |
202 | } | |
203 | this._handleOrderLinks(); | |
204 | // Set default if not supplied | |
205 | this.options.url = this.options.url || document.location.href; | |
206 | this._originalUrl = this.options.url; | |
207 | }, | |
3e897159 CW |
208 | _onFailure: function(data, status) { |
209 | var msg, title = ts('Network Error'); | |
f54254d8 | 210 | if (this.options.block) this.element.unblock(); |
53f2643c | 211 | this.element.trigger('crmAjaxFail', data); |
3e897159 CW |
212 | switch (status) { |
213 | case 'Forbidden': | |
214 | title = ts('Access Denied'); | |
215 | msg = ts('Ensure you are still logged in and have permission to access this feature.'); | |
216 | break; | |
217 | default: | |
218 | msg = ts('Unable to reach the server. Please refresh this page in your browser and try again.'); | |
219 | } | |
220 | CRM.alert(msg, title, 'error'); | |
53f2643c | 221 | }, |
61cab70a CW |
222 | _onError: function(data) { |
223 | this.element.attr('data-unsaved-changes', 'false').trigger('crmAjaxError', data); | |
f7aaf23c | 224 | if (this.options.crmForm && this.options.crmForm.autoClose && this.element.data('uiDialog')) { |
61cab70a CW |
225 | this.element.dialog('close'); |
226 | } | |
227 | }, | |
53f2643c CW |
228 | _formatUrl: function(url) { |
229 | // Strip hash | |
230 | url = url.split('#')[0]; | |
231 | // Add snippet argument to url | |
232 | if (url.search(/[&?]snippet=/) < 0) { | |
233 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json'; | |
234 | } else { | |
235 | url = url.replace(/snippet=[^&]*/, 'snippet=json'); | |
236 | } | |
237 | return url; | |
238 | }, | |
239 | // Hack to deal with civicrm legacy sort functionality | |
240 | _handleOrderLinks: function() { | |
241 | var that = this; | |
242 | $('a.crm-weight-arrow', that.element).click(function(e) { | |
f54254d8 | 243 | if (that.options.block) that.element.block(); |
53f2643c CW |
244 | $.getJSON(that._formatUrl(this.href)).done(function() { |
245 | that.refresh(); | |
246 | }); | |
247 | e.stopImmediatePropagation(); | |
248 | return false; | |
249 | }); | |
250 | }, | |
251 | refresh: function() { | |
252 | var that = this; | |
253 | var url = this._formatUrl(this.options.url); | |
f54254d8 | 254 | if (this.options.crmForm) $('form', this.element).ajaxFormUnbind(); |
f54254d8 | 255 | if (this.options.block) this.element.block(); |
53f2643c | 256 | $.getJSON(url, function(data) { |
f54254d8 | 257 | if (that.options.block) that.element.unblock(); |
61cab70a | 258 | if (!$.isPlainObject(data)) { |
53f2643c CW |
259 | that._onFailure(data); |
260 | return; | |
261 | } | |
61cab70a CW |
262 | if (data.status === 'error') { |
263 | that._onError(data); | |
264 | return; | |
265 | } | |
53f2643c | 266 | data.url = url; |
deb6ebbb | 267 | that.element.trigger('crmUnload').trigger('crmBeforeLoad', data); |
66663184 CW |
268 | that._beforeRemovingContent(); |
269 | that.element.html(data.content); | |
53f2643c CW |
270 | that._handleOrderLinks(); |
271 | that.element.trigger('crmLoad', data); | |
f54254d8 | 272 | if (that.options.crmForm) that.element.trigger('crmFormLoad', data); |
3e897159 CW |
273 | }).fail(function(data, msg, status) { |
274 | that._onFailure(data, status); | |
53f2643c CW |
275 | }); |
276 | }, | |
66663184 CW |
277 | // Perform any cleanup needed before removing/replacing content |
278 | _beforeRemovingContent: function() { | |
279 | var that = this; | |
8d6a3830 CW |
280 | // Save original content to be restored if widget is destroyed |
281 | if (this._originalContent === null) { | |
282 | $('.blockUI', this.element).remove(); | |
283 | this._originalContent = this.element.contents().detach(); | |
284 | } | |
66663184 CW |
285 | if (window.tinyMCE && tinyMCE.editors) { |
286 | $.each(tinyMCE.editors, function(k) { | |
287 | if ($.contains(that.element[0], this.getElement())) { | |
288 | this.remove(); | |
289 | } | |
290 | }); | |
291 | } | |
f54254d8 | 292 | if (this.options.crmForm) $('form', this.element).ajaxFormUnbind(); |
66663184 | 293 | }, |
53f2643c | 294 | _destroy: function() { |
af8c0ebe | 295 | this.element.removeClass('crm-ajax-container').trigger('crmUnload'); |
66663184 | 296 | this._beforeRemovingContent(); |
53f2643c CW |
297 | if (this._originalContent !== null) { |
298 | this.element.empty().append(this._originalContent); | |
299 | } | |
300 | } | |
301 | }); | |
302 | ||
98465f9e CW |
303 | var dialogCount = 0, |
304 | exclude = '[href^=#], [href^=javascript], [onclick], .no-popup, .cancel'; | |
305 | ||
53f2643c CW |
306 | CRM.loadPage = function(url, options) { |
307 | var settings = { | |
308 | target: '#crm-ajax-dialog-' + (dialogCount++), | |
b1fc510d | 309 | dialog: (options && options.target) ? false : {} |
53f2643c | 310 | }; |
f54254d8 | 311 | if (options) $.extend(true, settings, options); |
53f2643c CW |
312 | settings.url = url; |
313 | // Create new dialog | |
314 | if (settings.dialog) { | |
b1fc510d | 315 | settings.dialog = CRM.utils.adjustDialogDefaults(settings.dialog); |
202559b3 CW |
316 | $('<div id="' + settings.target.substring(1) + '"></div>').dialog(settings.dialog); |
317 | } | |
318 | if ($(settings.target).data('uiDialog')) { | |
73d2716e CW |
319 | $(settings.target) |
320 | .on('dialogclose', function() { | |
202559b3 | 321 | if (settings.dialog && $(this).attr('data-unsaved-changes') !== 'true') { |
73d2716e CW |
322 | $(this).crmSnippet('destroy').dialog('destroy').remove(); |
323 | } | |
324 | }) | |
325 | .on('crmLoad', function(e, data) { | |
326 | // Set title | |
327 | if (e.target === $(settings.target)[0] && data && !settings.dialog.title && data.title) { | |
328 | $(this).dialog('option', 'title', data.title); | |
329 | } | |
73d2716e | 330 | }); |
53f2643c CW |
331 | } |
332 | $(settings.target).crmSnippet(settings).crmSnippet('refresh'); | |
333 | return $(settings.target); | |
334 | }; | |
335 | CRM.loadForm = function(url, options) { | |
55cb6db1 | 336 | var formErrors = [], settings = { |
53f2643c CW |
337 | crmForm: { |
338 | ajaxForm: {}, | |
339 | autoClose: true, | |
340 | validate: true, | |
02c82dda | 341 | refreshAction: ['next_new', 'submit_savenext', 'upload_new'], |
98465f9e | 342 | cancelButton: '.cancel', |
5fffbbe4 | 343 | openInline: 'a.open-inline, a.button, a.action-item, a.open-inline-noreturn', |
1c1c1ae2 | 344 | onCancel: function(event) {} |
53f2643c CW |
345 | } |
346 | }; | |
347 | // Move options that belong to crmForm. Others will be passed through to crmSnippet | |
f54254d8 | 348 | if (options) $.each(options, function(key, value) { |
53f2643c CW |
349 | if (typeof(settings.crmForm[key]) !== 'undefined') { |
350 | settings.crmForm[key] = value; | |
351 | } | |
352 | else { | |
353 | settings[key] = value; | |
354 | } | |
355 | }); | |
356 | ||
357 | var widget = CRM.loadPage(url, settings).off('.crmForm'); | |
358 | ||
f582fc8f | 359 | // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out |
88e9380e | 360 | function cancelAction() { |
e9a3e054 | 361 | var dirty = CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', widget)); |
c4e00dbb | 362 | widget.attr('data-unsaved-changes', dirty ? 'true' : 'false'); |
7c2110fd CW |
363 | if (dirty) { |
364 | var id = widget.attr('id') + '-unsaved-alert', | |
e9a3e054 | 365 | title = widget.dialog('option', 'title'), |
15d60951 | 366 | 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 | 367 | $('#' + id).button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}}).click(function(e) { |
15d60951 | 368 | widget.attr('data-unsaved-changes', 'false').dialog('open'); |
8537b332 | 369 | e.preventDefault(); |
7c2110fd | 370 | }); |
88e9380e | 371 | } |
88e9380e | 372 | } |
c4e00dbb | 373 | |
f54254d8 | 374 | if (widget.data('uiDialog')) widget.on('dialogbeforeclose', function(e) { |
c4e00dbb CW |
375 | // CRM-14353 - Warn unsaved changes if user clicks close button or presses "esc" |
376 | if (e.originalEvent) { | |
377 | cancelAction(); | |
378 | } | |
379 | }); | |
88e9380e | 380 | |
53f2643c | 381 | widget.on('crmFormLoad.crmForm', function(event, data) { |
298f69da CW |
382 | var $el = $(this).attr('data-unsaved-changes', 'false'), |
383 | settings = $el.crmSnippet('option', 'crmForm'); | |
f54254d8 | 384 | if (settings.cancelButton) $(settings.cancelButton, this).click(function(e) { |
7c2110fd CW |
385 | e.preventDefault(); |
386 | var returnVal = settings.onCancel.call($el, e); | |
53f2643c | 387 | if (returnVal !== false) { |
7c2110fd | 388 | $el.trigger('crmFormCancel', e); |
53f2643c | 389 | if ($el.data('uiDialog') && settings.autoClose) { |
7c2110fd | 390 | cancelAction(); |
c4e00dbb | 391 | $el.dialog('close'); |
53f2643c CW |
392 | } |
393 | else if (!settings.autoClose) { | |
394 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
395 | } | |
396 | } | |
53f2643c CW |
397 | }); |
398 | if (settings.validate) { | |
3d527838 | 399 | $("form", this).crmValidate(); |
53f2643c | 400 | } |
cae80d9f | 401 | $("form:not('[data-no-ajax-submit=true]')", this).ajaxForm($.extend({ |
53f2643c CW |
402 | url: data.url.replace(/reset=1[&]?/, ''), |
403 | dataType: 'json', | |
404 | success: function(response) { | |
1c1c1ae2 | 405 | if (response.content === undefined) { |
53f2643c CW |
406 | $el.trigger('crmFormSuccess', response); |
407 | // Reset form for e.g. "save and new" | |
0a94ab7d | 408 | if (response.userContext && (response.status === 'redirect' || (settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0))) { |
b305fb88 CW |
409 | // Force reset of original url |
410 | $el.data('civiCrmSnippet')._originalUrl = response.userContext; | |
411 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
53f2643c | 412 | } |
9317128c CW |
413 | // Close if we are on the original url or the action was "delete" (in which case returning to view may be inappropriate) |
414 | else if ($el.data('uiDialog') && (settings.autoClose || response.action === 8)) { | |
53f2643c CW |
415 | $el.dialog('close'); |
416 | } | |
417 | else if (settings.autoClose === false) { | |
418 | $el.crmSnippet('resetUrl').crmSnippet('refresh'); | |
419 | } | |
420 | } | |
421 | else { | |
f54254d8 | 422 | if ($el.crmSnippet('option', 'block')) $el.unblock(); |
53f2643c | 423 | response.url = data.url; |
1c1c1ae2 CW |
424 | $el.html(response.content).trigger('crmLoad', response).trigger('crmFormLoad', response); |
425 | if (response.status === 'form_error') { | |
55cb6db1 | 426 | formErrors = []; |
1c1c1ae2 CW |
427 | $el.trigger('crmFormError', response); |
428 | $.each(response.errors || [], function(formElement, msg) { | |
55cb6db1 | 429 | formErrors.push($('[name="'+formElement+'"]', $el).crmError(msg)); |
1c1c1ae2 CW |
430 | }); |
431 | } | |
53f2643c CW |
432 | } |
433 | }, | |
53f2643c | 434 | beforeSubmit: function(submission) { |
55cb6db1 | 435 | $.each(formErrors, function() { |
f54254d8 | 436 | if (this && this.close) this.close(); |
55cb6db1 | 437 | }); |
f54254d8 | 438 | if ($el.crmSnippet('option', 'block')) $el.block(); |
53f2643c CW |
439 | $el.trigger('crmFormSubmit', submission); |
440 | } | |
441 | }, settings.ajaxForm)); | |
442 | if (settings.openInline) { | |
443 | settings.autoClose = $el.crmSnippet('isOriginalUrl'); | |
98465f9e | 444 | $(settings.openInline, this).not(exclude + ', .crm-popup').click(function(event) { |
5fffbbe4 CW |
445 | if ($(this).hasClass('open-inline-noreturn')) { |
446 | // Force reset of original url | |
447 | $el.data('civiCrmSnippet')._originalUrl = $(this).attr('href'); | |
448 | } | |
53f2643c CW |
449 | $el.crmSnippet('option', 'url', $(this).attr('href')).crmSnippet('refresh'); |
450 | return false; | |
451 | }); | |
452 | } | |
298f69da CW |
453 | // Show form buttons as part of the dialog |
454 | if ($el.data('uiDialog')) { | |
d34e9db0 CW |
455 | var buttonContainers = '.crm-submit-buttons, .action-link', |
456 | buttons = [], | |
457 | added = []; | |
458 | $(buttonContainers, $el).find('input.crm-form-submit, a.button').each(function() { | |
298f69da CW |
459 | var $el = $(this), |
460 | label = $el.is('input') ? $el.attr('value') : $el.text(), | |
461 | identifier = $el.attr('name') || $el.attr('href'); | |
462 | if (!identifier || identifier === '#' || $.inArray(identifier, added) < 0) { | |
d34e9db0 | 463 | var $icon = $el.find('.icon'), |
abbecaf2 | 464 | button = {'data-identifier': identifier, text: label, click: function() { |
19ec1d6e | 465 | $el[0].click(); |
d34e9db0 CW |
466 | }}; |
467 | if ($icon.length) { | |
468 | button.icons = {primary: $icon.attr('class')}; | |
298f69da | 469 | } else { |
deae896d | 470 | var action = $el.attr('crm-icon') || ($el.hasClass('cancel') ? 'close' : 'check'); |
d34e9db0 | 471 | button.icons = {primary: 'ui-icon-' + action}; |
298f69da CW |
472 | } |
473 | buttons.push(button); | |
474 | added.push(identifier); | |
475 | } | |
d34e9db0 | 476 | // display:none causes the form to not submit when pressing "enter" |
e22a8e7a | 477 | $el.parents(buttonContainers).css({height: 0, padding: 0, margin: 0, overflow: 'hidden'}).find('.crm-button-icon').hide(); |
298f69da CW |
478 | }); |
479 | $el.dialog('option', 'buttons', buttons); | |
480 | } | |
022d2163 | 481 | // Allow a button to prevent ajax submit |
1e29624c CW |
482 | $('input[data-no-ajax-submit=true]').click(function() { |
483 | $(this).closest('form').ajaxFormUnbind(); | |
484 | }); | |
53f2643c | 485 | // For convenience, focus the first field |
b18392cc | 486 | $('input[type=text], textarea, select', this).filter(':visible').first().not('.dateplugin').focus(); |
53f2643c CW |
487 | }); |
488 | return widget; | |
489 | }; | |
490 | /** | |
022d2163 | 491 | * Handler for jQuery click event e.g. $('a').click(CRM.popup); |
53f2643c | 492 | */ |
35df910c | 493 | CRM.popup = function(e) { |
53f2643c CW |
494 | var $el = $(this).first(), |
495 | url = $el.attr('href'), | |
496 | popup = $el.data('popup-type') === 'page' ? CRM.loadPage : CRM.loadForm, | |
497 | settings = $el.data('popup-settings') || {}, | |
7fe745d1 | 498 | formSuccess = false; |
53f2643c | 499 | settings.dialog = settings.dialog || {}; |
35df910c | 500 | if (e.isDefaultPrevented() || !CRM.config.ajaxPopupsEnabled || !url || $el.is(exclude)) { |
53f2643c CW |
501 | return; |
502 | } | |
a1c7d42f CW |
503 | // Sized based on css class |
504 | if ($el.hasClass('small-popup')) { | |
53f2643c CW |
505 | settings.dialog.width = 400; |
506 | settings.dialog.height = 300; | |
507 | } | |
508 | else if ($el.hasClass('medium-popup')) { | |
509 | settings.dialog.width = settings.dialog.height = '50%'; | |
510 | } | |
53f2643c CW |
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() { | |
f54254d8 | 538 | if ($(this).dataTable().fnSettings().sAjaxSource) $(this).unblock().dataTable().fnDraw(); |
554b1768 CW |
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(); | |
e5e90ec9 | 557 | }) |
f91b1c0c CW |
558 | // Ensure wysiwyg content is updated prior to ajax submit |
559 | .on('form-pre-serialize', function(e) { | |
560 | $('.crm-wysiwyg-enabled', e.target).each(function() { | |
561 | CRM.wysiwyg.updateElement(this); | |
562 | }); | |
563 | }) | |
e5e90ec9 | 564 | // Auto-resize dialogs when loading content |
b1fc510d | 565 | .on('crmLoad dialogopen', 'div.ui-dialog.ui-resizable.crm-container', function(e) { |
e5e90ec9 CW |
566 | var |
567 | $wrapper = $(this), | |
568 | $dialog = $wrapper.children('.ui-dialog-content'); | |
569 | // small delay to allow contents to render | |
570 | window.setTimeout(function() { | |
571 | var currentHeight = $wrapper.outerHeight(), | |
572 | padding = currentHeight - $dialog.height(), | |
573 | newHeight = $dialog.prop('scrollHeight') + padding, | |
574 | menuHeight = $('#civicrm-menu').outerHeight(), | |
575 | maxHeight = $(window).height() - menuHeight; | |
576 | newHeight = newHeight > maxHeight ? maxHeight : newHeight; | |
577 | if (newHeight > (currentHeight + 15)) { | |
578 | $dialog.dialog('option', { | |
579 | position: {my: 'center', at: 'center center+' + (menuHeight / 2), of: window}, | |
580 | height: newHeight | |
581 | }); | |
582 | } | |
583 | }, 500); | |
15d60951 | 584 | }); |
a1c7d42f CW |
585 | }); |
586 | ||
53f2643c | 587 | }(jQuery, CRM)); |