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