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