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