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