| 1 |
/*! |
| 2 |
* Dialogs Manager v4.9.4 |
| 3 |
* https://github.com/kobizz/dialogs-manager |
| 4 |
* |
| 5 |
* Copyright Kobi Zaltzberg |
| 6 |
* Released under the MIT license |
| 7 |
* https://github.com/kobizz/dialogs-manager/blob/master/LICENSE.txt |
| 8 |
*/ |
| 9 |
|
| 10 |
(function($, global) { |
| 11 |
'use strict'; |
| 12 |
|
| 13 |
/* |
| 14 |
* Dialog Manager |
| 15 |
*/ |
| 16 |
var DialogsManager = { |
| 17 |
widgetsTypes: {}, |
| 18 |
createWidgetType: function(typeName, properties, Parent) { |
| 19 |
|
| 20 |
if (!Parent) { |
| 21 |
Parent = this.Widget; |
| 22 |
} |
| 23 |
|
| 24 |
var WidgetType = function() { |
| 25 |
|
| 26 |
Parent.apply(this, arguments); |
| 27 |
}; |
| 28 |
|
| 29 |
var prototype = WidgetType.prototype = new Parent(typeName); |
| 30 |
|
| 31 |
prototype.types = prototype.types.concat([typeName]); |
| 32 |
|
| 33 |
$.extend(prototype, properties); |
| 34 |
|
| 35 |
prototype.constructor = WidgetType; |
| 36 |
|
| 37 |
WidgetType.extend = function(typeName, properties) { |
| 38 |
|
| 39 |
return DialogsManager.createWidgetType(typeName, properties, WidgetType); |
| 40 |
}; |
| 41 |
|
| 42 |
return WidgetType; |
| 43 |
}, |
| 44 |
addWidgetType: function(typeName, properties, Parent) { |
| 45 |
|
| 46 |
if (properties && properties.prototype instanceof this.Widget) { |
| 47 |
return this.widgetsTypes[typeName] = properties; |
| 48 |
} |
| 49 |
|
| 50 |
return this.widgetsTypes[typeName] = this.createWidgetType(typeName, properties, Parent); |
| 51 |
}, |
| 52 |
getWidgetType: function(widgetType) { |
| 53 |
|
| 54 |
return this.widgetsTypes[widgetType]; |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
/* |
| 59 |
* Dialog Manager instances constructor |
| 60 |
*/ |
| 61 |
DialogsManager.Instance = function() { |
| 62 |
|
| 63 |
var self = this, |
| 64 |
elements = {}, |
| 65 |
settings = {}; |
| 66 |
|
| 67 |
self.openDialogs = []; |
| 68 |
|
| 69 |
var initElements = function() { |
| 70 |
|
| 71 |
elements.body = $('body'); |
| 72 |
}; |
| 73 |
|
| 74 |
var initSettings = function(options) { |
| 75 |
|
| 76 |
var defaultSettings = { |
| 77 |
classPrefix: 'dialog', |
| 78 |
effects: { |
| 79 |
show: 'fadeIn', |
| 80 |
hide: 'fadeOut' |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
$.extend(settings, defaultSettings, options); |
| 85 |
}; |
| 86 |
|
| 87 |
this.createWidget = function(widgetType, properties) { |
| 88 |
|
| 89 |
var WidgetTypeConstructor = DialogsManager.getWidgetType(widgetType), |
| 90 |
widget = new WidgetTypeConstructor(widgetType); |
| 91 |
|
| 92 |
properties = properties || {}; |
| 93 |
|
| 94 |
widget.init(self, properties); |
| 95 |
|
| 96 |
return widget; |
| 97 |
}; |
| 98 |
|
| 99 |
this.getSettings = function(property) { |
| 100 |
|
| 101 |
if (property) { |
| 102 |
return settings[property]; |
| 103 |
} |
| 104 |
|
| 105 |
return Object.create(settings); |
| 106 |
}; |
| 107 |
|
| 108 |
this.maybeLoadAssets = async function () { |
| 109 |
const isFrontend = !! window.elementorFrontend?.utils?.assetsLoader; |
| 110 |
|
| 111 |
if ( ! isFrontend ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
try { |
| 116 |
await elementorFrontend.utils.assetsLoader.load( 'style', 'dialog' ); |
| 117 |
} catch ( error ) { |
| 118 |
console.error( 'Failed to load assets:', error ); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
this.init = function (settings) { |
| 123 |
|
| 124 |
this.maybeLoadAssets(); |
| 125 |
|
| 126 |
initSettings(settings); |
| 127 |
|
| 128 |
initElements(); |
| 129 |
|
| 130 |
return self; |
| 131 |
}; |
| 132 |
|
| 133 |
self.init(); |
| 134 |
}; |
| 135 |
|
| 136 |
/* |
| 137 |
* Widget types constructor |
| 138 |
*/ |
| 139 |
DialogsManager.Widget = function(widgetName) { |
| 140 |
|
| 141 |
var self = this, |
| 142 |
settings = {}, |
| 143 |
events = {}, |
| 144 |
elements = {}, |
| 145 |
hideTimeOut = 0, |
| 146 |
baseClosureMethods = ['refreshPosition']; |
| 147 |
|
| 148 |
var bindEvents = function() { |
| 149 |
|
| 150 |
var windows = [elements.window]; |
| 151 |
|
| 152 |
if (elements.iframe) { |
| 153 |
windows.push(jQuery(elements.iframe[0].contentWindow)); |
| 154 |
} |
| 155 |
|
| 156 |
windows.forEach(function(window) { |
| 157 |
if (settings.hide.onEscKeyPress) { |
| 158 |
window.on('keyup', onWindowKeyUp); |
| 159 |
} |
| 160 |
|
| 161 |
if (settings.hide.onOutsideClick) { |
| 162 |
window[0].addEventListener('click', hideOnOutsideClick, true); |
| 163 |
} |
| 164 |
|
| 165 |
if (settings.hide.onOutsideContextMenu) { |
| 166 |
window[0].addEventListener('contextmenu', hideOnOutsideClick, true); |
| 167 |
} |
| 168 |
|
| 169 |
if (settings.position.autoRefresh) { |
| 170 |
window.on('resize', self.refreshPosition); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
if (settings.hide.onClick || settings.hide.onBackgroundClick) { |
| 175 |
elements.widget.on('click', hideOnClick); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
var callEffect = function(intent, params) { |
| 180 |
|
| 181 |
var effect = settings.effects[intent], |
| 182 |
$widget = elements.widget; |
| 183 |
|
| 184 |
if ('function' === typeof effect) { |
| 185 |
effect.apply($widget, params); |
| 186 |
} else { |
| 187 |
|
| 188 |
if ($widget[effect]) { |
| 189 |
$widget[effect].apply($widget, params); |
| 190 |
} else { |
| 191 |
throw 'Reference Error: The effect ' + effect + ' not found'; |
| 192 |
} |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
var ensureClosureMethods = function() { |
| 197 |
|
| 198 |
var closureMethodsNames = baseClosureMethods.concat(self.getClosureMethods()); |
| 199 |
|
| 200 |
$.each(closureMethodsNames, function() { |
| 201 |
|
| 202 |
var methodName = this, |
| 203 |
oldMethod = self[methodName]; |
| 204 |
|
| 205 |
self[methodName] = function() { |
| 206 |
|
| 207 |
oldMethod.apply(self, arguments); |
| 208 |
}; |
| 209 |
}); |
| 210 |
}; |
| 211 |
|
| 212 |
var fixIframePosition = function(position) { |
| 213 |
if (! position.my) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
var horizontalOffsetRegex = /left|right/, |
| 218 |
extraOffsetRegex = /([+-]\d+)?$/, |
| 219 |
iframeOffset = elements.iframe.offset(), |
| 220 |
iframeWindow = elements.iframe[0].contentWindow, |
| 221 |
myParts = position.my.split(' '), |
| 222 |
fixedParts = []; |
| 223 |
|
| 224 |
if (myParts.length === 1) { |
| 225 |
if (horizontalOffsetRegex.test(myParts[0])) { |
| 226 |
myParts.push('center'); |
| 227 |
} else { |
| 228 |
myParts.unshift('center'); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
myParts.forEach(function(part, index) { |
| 233 |
var fixedPart = part.replace(extraOffsetRegex, function(partOffset) { |
| 234 |
partOffset = +partOffset || 0; |
| 235 |
|
| 236 |
if (! index) { |
| 237 |
partOffset += iframeOffset.left - iframeWindow.scrollX; |
| 238 |
} else { |
| 239 |
partOffset += iframeOffset.top - iframeWindow.scrollY; |
| 240 |
} |
| 241 |
|
| 242 |
if (partOffset >= 0) { |
| 243 |
partOffset = '+' + partOffset; |
| 244 |
} |
| 245 |
|
| 246 |
return partOffset; |
| 247 |
}); |
| 248 |
|
| 249 |
fixedParts.push(fixedPart); |
| 250 |
}); |
| 251 |
|
| 252 |
position.my = fixedParts.join(' '); |
| 253 |
}; |
| 254 |
|
| 255 |
var hideOnClick = function(event) { |
| 256 |
|
| 257 |
if (isContextMenuClickEvent(event)) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
if (settings.hide.onClick) { |
| 262 |
|
| 263 |
if ($(event.target).closest(settings.selectors.preventClose).length) { |
| 264 |
return; |
| 265 |
} |
| 266 |
} else if (event.target !== this) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
self.hide(); |
| 271 |
}; |
| 272 |
|
| 273 |
var isIgnoredTarget = function(event) { |
| 274 |
|
| 275 |
if (! settings.hide.ignore) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
return !! $(event.target).closest(settings.hide.ignore).length; |
| 280 |
}; |
| 281 |
|
| 282 |
var hideOnOutsideClick = function(event) { |
| 283 |
|
| 284 |
if (isContextMenuClickEvent(event) || $(event.target).closest(elements.widget).length || isIgnoredTarget(event)) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
self.hide(); |
| 289 |
}; |
| 290 |
|
| 291 |
var initElements = function() { |
| 292 |
|
| 293 |
self.addElement('widget'); |
| 294 |
|
| 295 |
self.addElement('header'); |
| 296 |
|
| 297 |
self.addElement('message'); |
| 298 |
|
| 299 |
self.addElement('window', window); |
| 300 |
|
| 301 |
self.addElement('body', document.body); |
| 302 |
|
| 303 |
self.addElement('container', settings.container); |
| 304 |
|
| 305 |
if (settings.iframe) { |
| 306 |
self.addElement('iframe', settings.iframe); |
| 307 |
} |
| 308 |
|
| 309 |
if (settings.closeButton) { |
| 310 |
if ( settings.closeButtonClass ) { |
| 311 |
// Backwards compatibility |
| 312 |
settings.closeButtonOptions.iconClass = settings.closeButtonClass; |
| 313 |
} |
| 314 |
|
| 315 |
const $button = $('<a>', settings.closeButtonOptions.attributes), |
| 316 |
$buttonIcon = $(settings.closeButtonOptions.iconElement).addClass(settings.closeButtonOptions.iconClass); |
| 317 |
|
| 318 |
$button.append($buttonIcon); |
| 319 |
|
| 320 |
self.addElement('closeButton', $button); |
| 321 |
} |
| 322 |
|
| 323 |
var id = self.getSettings('id'); |
| 324 |
|
| 325 |
if (id) { |
| 326 |
self.setID(id); |
| 327 |
} |
| 328 |
|
| 329 |
var classes = []; |
| 330 |
|
| 331 |
$.each(self.types, function() { |
| 332 |
classes.push(settings.classes.globalPrefix + '-type-' + this); |
| 333 |
}); |
| 334 |
|
| 335 |
classes.push(self.getSettings('className')); |
| 336 |
|
| 337 |
elements.widget |
| 338 |
.addClass(classes.join(' ')) |
| 339 |
.attr({ |
| 340 |
'aria-modal': true, |
| 341 |
'role': 'document', |
| 342 |
'tabindex': 0, |
| 343 |
}); |
| 344 |
}; |
| 345 |
|
| 346 |
var initSettings = function(parent, userSettings) { |
| 347 |
|
| 348 |
var parentSettings = $.extend(true, {}, parent.getSettings()); |
| 349 |
|
| 350 |
settings = { |
| 351 |
headerMessage: '', |
| 352 |
message: '', |
| 353 |
effects: parentSettings.effects, |
| 354 |
classes: { |
| 355 |
globalPrefix: parentSettings.classPrefix, |
| 356 |
prefix: parentSettings.classPrefix + '-' + widgetName, |
| 357 |
preventScroll: parentSettings.classPrefix + '-prevent-scroll', |
| 358 |
}, |
| 359 |
selectors: { |
| 360 |
preventClose: '.' + parentSettings.classPrefix + '-prevent-close', |
| 361 |
}, |
| 362 |
container: 'body', |
| 363 |
preventScroll: false, |
| 364 |
iframe: null, |
| 365 |
closeButton: false, |
| 366 |
closeButtonOptions: { |
| 367 |
iconClass: parentSettings.classPrefix + '-close-button-icon', |
| 368 |
attributes: { |
| 369 |
role: 'button', |
| 370 |
'tabindex': 0, |
| 371 |
'aria-label': 'Close', |
| 372 |
href: '#', |
| 373 |
}, |
| 374 |
iconElement: '<i>', |
| 375 |
}, |
| 376 |
position: { |
| 377 |
element: 'widget', |
| 378 |
my: 'center', |
| 379 |
at: 'center', |
| 380 |
enable: true, |
| 381 |
autoRefresh: false, |
| 382 |
}, |
| 383 |
hide: { |
| 384 |
auto: false, |
| 385 |
autoDelay: 5000, |
| 386 |
onClick: false, |
| 387 |
onOutsideClick: true, |
| 388 |
onOutsideContextMenu: false, |
| 389 |
onBackgroundClick: true, |
| 390 |
onEscKeyPress: true, |
| 391 |
ignore: '', |
| 392 |
}, |
| 393 |
}; |
| 394 |
|
| 395 |
$.extend(true, settings, self.getDefaultSettings(), userSettings); |
| 396 |
|
| 397 |
initSettingsEvents(); |
| 398 |
}; |
| 399 |
|
| 400 |
var initSettingsEvents = function() { |
| 401 |
|
| 402 |
$.each(settings, function(settingKey) { |
| 403 |
|
| 404 |
var eventName = settingKey.match(/^on([A-Z].*)/); |
| 405 |
|
| 406 |
if (!eventName) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
eventName = eventName[1].charAt(0).toLowerCase() + eventName[1].slice(1); |
| 411 |
|
| 412 |
self.on(eventName, this); |
| 413 |
}); |
| 414 |
}; |
| 415 |
|
| 416 |
var isContextMenuClickEvent = function(event) { |
| 417 |
// Firefox fires `click` event on every `contextmenu` event. |
| 418 |
return event.type === 'click' && event.button === 2; |
| 419 |
}; |
| 420 |
|
| 421 |
var normalizeClassName = function(name) { |
| 422 |
|
| 423 |
return name.replace(/([a-z])([A-Z])/g, function() { |
| 424 |
|
| 425 |
return arguments[1] + '-' + arguments[2].toLowerCase(); |
| 426 |
}); |
| 427 |
}; |
| 428 |
|
| 429 |
var onWindowKeyUp = function(event) { |
| 430 |
var ESC_KEY = 27, |
| 431 |
keyCode = event.which; |
| 432 |
|
| 433 |
if (ESC_KEY === keyCode) { |
| 434 |
self.hide(); |
| 435 |
} |
| 436 |
}; |
| 437 |
|
| 438 |
var unbindEvents = function() { |
| 439 |
|
| 440 |
var windows = [elements.window]; |
| 441 |
|
| 442 |
if (elements.iframe) { |
| 443 |
windows.push(jQuery(elements.iframe[0].contentWindow)); |
| 444 |
} |
| 445 |
|
| 446 |
windows.forEach(function(window) { |
| 447 |
if (settings.hide.onEscKeyPress) { |
| 448 |
window.off('keyup', onWindowKeyUp); |
| 449 |
} |
| 450 |
|
| 451 |
if (settings.hide.onOutsideClick) { |
| 452 |
window[0].removeEventListener('click', hideOnOutsideClick, true); |
| 453 |
} |
| 454 |
|
| 455 |
if (settings.hide.onOutsideContextMenu) { |
| 456 |
window[0].removeEventListener('contextmenu', hideOnOutsideClick, true); |
| 457 |
} |
| 458 |
|
| 459 |
if (settings.position.autoRefresh) { |
| 460 |
window.off('resize', self.refreshPosition); |
| 461 |
} |
| 462 |
}); |
| 463 |
|
| 464 |
if (settings.hide.onClick || settings.hide.onBackgroundClick) { |
| 465 |
elements.widget.off('click', hideOnClick); |
| 466 |
} |
| 467 |
}; |
| 468 |
|
| 469 |
this.addElement = function(name, element, classes) { |
| 470 |
|
| 471 |
var $newElement = elements[name] = $(element || '<div>'), |
| 472 |
normalizedName = normalizeClassName(name); |
| 473 |
|
| 474 |
classes = classes ? classes + ' ' : ''; |
| 475 |
|
| 476 |
classes += settings.classes.globalPrefix + '-' + normalizedName; |
| 477 |
|
| 478 |
classes += ' ' + settings.classes.prefix + '-' + normalizedName; |
| 479 |
|
| 480 |
$newElement.addClass(classes); |
| 481 |
|
| 482 |
return $newElement; |
| 483 |
}; |
| 484 |
|
| 485 |
this.destroy = function() { |
| 486 |
const widgetId = self.getElements('widget')?.attr('id'), |
| 487 |
index = self.parent.openDialogs.lastIndexOf(widgetId); |
| 488 |
|
| 489 |
if (index !== -1) { |
| 490 |
self.parent.openDialogs.splice(index, 1); |
| 491 |
} |
| 492 |
|
| 493 |
unbindEvents(); |
| 494 |
|
| 495 |
elements.widget.remove(); |
| 496 |
|
| 497 |
self.trigger('destroy'); |
| 498 |
|
| 499 |
return self; |
| 500 |
}; |
| 501 |
|
| 502 |
this.getElements = function(item) { |
| 503 |
|
| 504 |
return item ? elements[item] : elements; |
| 505 |
}; |
| 506 |
|
| 507 |
this.getSettings = function(setting) { |
| 508 |
|
| 509 |
var copy = Object.create(settings); |
| 510 |
|
| 511 |
if (setting) { |
| 512 |
return copy[setting]; |
| 513 |
} |
| 514 |
|
| 515 |
return copy; |
| 516 |
}; |
| 517 |
|
| 518 |
this.hide = function() { |
| 519 |
|
| 520 |
if (! self.isVisible()) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
const widgetId = self.getElements('widget')?.attr('id'), |
| 525 |
openDialogs = self.parent.openDialogs, |
| 526 |
topDialogId = openDialogs[openDialogs.length - 1]; |
| 527 |
|
| 528 |
if (topDialogId !== widgetId) { |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
openDialogs.pop(); |
| 533 |
|
| 534 |
clearTimeout(hideTimeOut); |
| 535 |
|
| 536 |
callEffect('hide', arguments); |
| 537 |
|
| 538 |
unbindEvents(); |
| 539 |
|
| 540 |
if (settings.preventScroll) { |
| 541 |
self.getElements('body').removeClass(settings.classes.preventScroll); |
| 542 |
} |
| 543 |
|
| 544 |
self.trigger('hide'); |
| 545 |
|
| 546 |
return self; |
| 547 |
}; |
| 548 |
|
| 549 |
this.init = function(parent, properties) { |
| 550 |
|
| 551 |
if (!(parent instanceof DialogsManager.Instance)) { |
| 552 |
throw 'The ' + self.widgetName + ' must to be initialized from an instance of DialogsManager.Instance'; |
| 553 |
} |
| 554 |
|
| 555 |
self.parent = parent; |
| 556 |
|
| 557 |
ensureClosureMethods(); |
| 558 |
|
| 559 |
self.trigger('init', properties); |
| 560 |
|
| 561 |
initSettings(parent, properties); |
| 562 |
|
| 563 |
initElements(); |
| 564 |
|
| 565 |
self.buildWidget(); |
| 566 |
|
| 567 |
self.attachEvents(); |
| 568 |
|
| 569 |
self.trigger('ready'); |
| 570 |
|
| 571 |
return self; |
| 572 |
}; |
| 573 |
|
| 574 |
this.isVisible = function() { |
| 575 |
|
| 576 |
return elements.widget.is(':visible'); |
| 577 |
}; |
| 578 |
|
| 579 |
this.on = function(eventName, callback) { |
| 580 |
|
| 581 |
if ('object' === typeof eventName) { |
| 582 |
$.each(eventName, function(singleEventName) { |
| 583 |
self.on(singleEventName, this); |
| 584 |
}); |
| 585 |
|
| 586 |
return self; |
| 587 |
} |
| 588 |
|
| 589 |
var eventNames = eventName.split(' '); |
| 590 |
|
| 591 |
eventNames.forEach(function(singleEventName) { |
| 592 |
if (!events[singleEventName]) { |
| 593 |
events[singleEventName] = []; |
| 594 |
} |
| 595 |
|
| 596 |
events[singleEventName].push(callback); |
| 597 |
}); |
| 598 |
|
| 599 |
return self; |
| 600 |
}; |
| 601 |
|
| 602 |
this.off = function(eventName, callback) { |
| 603 |
|
| 604 |
if (! events[ eventName ]) { |
| 605 |
return self; |
| 606 |
} |
| 607 |
|
| 608 |
if (! callback) { |
| 609 |
delete events[eventName]; |
| 610 |
|
| 611 |
return self; |
| 612 |
} |
| 613 |
|
| 614 |
var callbackIndex = events[eventName].indexOf(callback); |
| 615 |
|
| 616 |
if (-1 !== callbackIndex) { |
| 617 |
events[eventName].splice(callbackIndex, 1); |
| 618 |
} |
| 619 |
|
| 620 |
return self; |
| 621 |
}; |
| 622 |
|
| 623 |
this.refreshPosition = function() { |
| 624 |
|
| 625 |
if (! settings.position.enable) { |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
var position = $.extend({}, settings.position); |
| 630 |
|
| 631 |
if (elements[position.of]) { |
| 632 |
position.of = elements[position.of]; |
| 633 |
} |
| 634 |
|
| 635 |
if (! position.of) { |
| 636 |
position.of = window; |
| 637 |
} |
| 638 |
|
| 639 |
if (settings.iframe) { |
| 640 |
fixIframePosition(position); |
| 641 |
} |
| 642 |
|
| 643 |
elements[position.element].position(position); |
| 644 |
}; |
| 645 |
|
| 646 |
this.setID = function(id) { |
| 647 |
|
| 648 |
elements.widget.attr('id', id); |
| 649 |
|
| 650 |
return self; |
| 651 |
}; |
| 652 |
|
| 653 |
this.setHeaderMessage = function(message) { |
| 654 |
|
| 655 |
self.getElements('header').html(message); |
| 656 |
|
| 657 |
return self; |
| 658 |
}; |
| 659 |
|
| 660 |
this.setMessage = function(message) { |
| 661 |
|
| 662 |
elements.message.html(message); |
| 663 |
|
| 664 |
return self; |
| 665 |
}; |
| 666 |
|
| 667 |
this.setSettings = function(key, value) { |
| 668 |
|
| 669 |
if (jQuery.isPlainObject(value)) { |
| 670 |
$.extend(true, settings[key], value); |
| 671 |
} else { |
| 672 |
settings[key] = value; |
| 673 |
} |
| 674 |
|
| 675 |
return self; |
| 676 |
}; |
| 677 |
|
| 678 |
this.show = function() { |
| 679 |
|
| 680 |
clearTimeout(hideTimeOut); |
| 681 |
|
| 682 |
elements.widget.appendTo(elements.container).hide(); |
| 683 |
|
| 684 |
callEffect('show', arguments); |
| 685 |
|
| 686 |
self.refreshPosition(); |
| 687 |
|
| 688 |
if (settings.hide.auto) { |
| 689 |
hideTimeOut = setTimeout(self.hide, settings.hide.autoDelay); |
| 690 |
} |
| 691 |
|
| 692 |
bindEvents(); |
| 693 |
|
| 694 |
if (settings.preventScroll) { |
| 695 |
self.getElements('body').addClass(settings.classes.preventScroll); |
| 696 |
} |
| 697 |
|
| 698 |
self.trigger('show'); |
| 699 |
|
| 700 |
const widgetId = self.getElements('widget')?.attr('id'); |
| 701 |
|
| 702 |
self.parent.openDialogs.push(widgetId); |
| 703 |
|
| 704 |
return self; |
| 705 |
}; |
| 706 |
|
| 707 |
this.trigger = function(eventName, params) { |
| 708 |
|
| 709 |
var methodName = 'on' + eventName[0].toUpperCase() + eventName.slice(1); |
| 710 |
|
| 711 |
if (self[methodName]) { |
| 712 |
self[methodName](params); |
| 713 |
} |
| 714 |
|
| 715 |
var callbacks = events[eventName]; |
| 716 |
|
| 717 |
if (!callbacks) { |
| 718 |
return; |
| 719 |
} |
| 720 |
|
| 721 |
$.each(callbacks, function(index, callback) { |
| 722 |
|
| 723 |
callback.call(self, params); |
| 724 |
}); |
| 725 |
|
| 726 |
return self; |
| 727 |
}; |
| 728 |
}; |
| 729 |
|
| 730 |
DialogsManager.Widget.prototype.types = []; |
| 731 |
|
| 732 |
// Inheritable widget methods |
| 733 |
DialogsManager.Widget.prototype.buildWidget = function() { |
| 734 |
|
| 735 |
var elements = this.getElements(), |
| 736 |
settings = this.getSettings(); |
| 737 |
|
| 738 |
elements.widget.append(elements.header, elements.message); |
| 739 |
|
| 740 |
this.setHeaderMessage(settings.headerMessage); |
| 741 |
|
| 742 |
this.setMessage(settings.message); |
| 743 |
|
| 744 |
if (this.getSettings('closeButton')) { |
| 745 |
elements.widget.prepend(elements.closeButton); |
| 746 |
} |
| 747 |
}; |
| 748 |
|
| 749 |
DialogsManager.Widget.prototype.attachEvents = function() { |
| 750 |
|
| 751 |
var self = this; |
| 752 |
|
| 753 |
if (self.getSettings('closeButton')) { |
| 754 |
self.getElements('closeButton').on('click', function(event) { |
| 755 |
event.preventDefault(); |
| 756 |
self.hide(); |
| 757 |
}); |
| 758 |
} |
| 759 |
}; |
| 760 |
|
| 761 |
DialogsManager.Widget.prototype.getDefaultSettings = function() { |
| 762 |
|
| 763 |
return {}; |
| 764 |
}; |
| 765 |
|
| 766 |
DialogsManager.Widget.prototype.getClosureMethods = function() { |
| 767 |
|
| 768 |
return []; |
| 769 |
}; |
| 770 |
|
| 771 |
DialogsManager.Widget.prototype.onHide = function() { |
| 772 |
}; |
| 773 |
|
| 774 |
DialogsManager.Widget.prototype.onShow = function() { |
| 775 |
}; |
| 776 |
|
| 777 |
DialogsManager.Widget.prototype.onInit = function() { |
| 778 |
}; |
| 779 |
|
| 780 |
DialogsManager.Widget.prototype.onReady = function() { |
| 781 |
}; |
| 782 |
|
| 783 |
DialogsManager.widgetsTypes.simple = DialogsManager.Widget; |
| 784 |
|
| 785 |
DialogsManager.addWidgetType('buttons', { |
| 786 |
activeKeyUp: function(event) { |
| 787 |
|
| 788 |
var TAB_KEY = 9; |
| 789 |
|
| 790 |
if (event.which === TAB_KEY) { |
| 791 |
event.preventDefault(); |
| 792 |
} |
| 793 |
|
| 794 |
if (this.hotKeys[event.which]) { |
| 795 |
this.hotKeys[event.which](this); |
| 796 |
} |
| 797 |
}, |
| 798 |
activeKeyDown: function(event) { |
| 799 |
|
| 800 |
if (!this.focusedButton) { |
| 801 |
return; |
| 802 |
} |
| 803 |
|
| 804 |
var TAB_KEY = 9; |
| 805 |
|
| 806 |
if (event.which === TAB_KEY) { |
| 807 |
event.preventDefault(); |
| 808 |
|
| 809 |
var currentButtonIndex = this.focusedButton.index(), |
| 810 |
nextButtonIndex; |
| 811 |
|
| 812 |
if (event.shiftKey) { |
| 813 |
|
| 814 |
nextButtonIndex = currentButtonIndex - 1; |
| 815 |
|
| 816 |
if (nextButtonIndex < 0) { |
| 817 |
nextButtonIndex = this.buttons.length - 1; |
| 818 |
} |
| 819 |
} else { |
| 820 |
|
| 821 |
nextButtonIndex = currentButtonIndex + 1; |
| 822 |
|
| 823 |
if (nextButtonIndex >= this.buttons.length) { |
| 824 |
nextButtonIndex = 0; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
this.focusedButton = this.buttons[nextButtonIndex].trigger('focus'); |
| 829 |
} |
| 830 |
}, |
| 831 |
addButton: function(options) { |
| 832 |
|
| 833 |
var self = this, |
| 834 |
settings = self.getSettings(), |
| 835 |
buttonSettings = jQuery.extend(settings.button, options); |
| 836 |
|
| 837 |
var classes = options.classes ? options.classes + ' ' : ''; |
| 838 |
|
| 839 |
classes += settings.classes.globalPrefix + '-button'; |
| 840 |
|
| 841 |
var $button = self.addElement(options.name, $('<' + buttonSettings.tag + '>').html(options.text), classes); |
| 842 |
|
| 843 |
self.buttons.push($button); |
| 844 |
|
| 845 |
var buttonFn = function() { |
| 846 |
|
| 847 |
if (settings.hide.onButtonClick) { |
| 848 |
self.hide(); |
| 849 |
} |
| 850 |
|
| 851 |
if ('function' === typeof options.callback) { |
| 852 |
options.callback.call(this, self); |
| 853 |
} |
| 854 |
}; |
| 855 |
|
| 856 |
$button.on('click', buttonFn); |
| 857 |
|
| 858 |
if (options.hotKey) { |
| 859 |
this.hotKeys[options.hotKey] = buttonFn; |
| 860 |
} |
| 861 |
|
| 862 |
this.getElements('buttonsWrapper').append($button); |
| 863 |
|
| 864 |
if (options.focus) { |
| 865 |
this.focusedButton = $button; |
| 866 |
} |
| 867 |
|
| 868 |
return self; |
| 869 |
}, |
| 870 |
bindHotKeys: function() { |
| 871 |
|
| 872 |
this.getElements('window').on({ |
| 873 |
keyup: this.activeKeyUp, |
| 874 |
keydown: this.activeKeyDown |
| 875 |
}); |
| 876 |
}, |
| 877 |
buildWidget: function() { |
| 878 |
|
| 879 |
DialogsManager.Widget.prototype.buildWidget.apply(this, arguments); |
| 880 |
|
| 881 |
var $buttonsWrapper = this.addElement('buttonsWrapper'); |
| 882 |
|
| 883 |
this.getElements('widget').append($buttonsWrapper); |
| 884 |
}, |
| 885 |
getClosureMethods: function() { |
| 886 |
|
| 887 |
return [ |
| 888 |
'activeKeyUp', |
| 889 |
'activeKeyDown' |
| 890 |
]; |
| 891 |
}, |
| 892 |
getDefaultSettings: function() { |
| 893 |
|
| 894 |
return { |
| 895 |
hide: { |
| 896 |
onButtonClick: true |
| 897 |
}, |
| 898 |
button: { |
| 899 |
tag: 'button' |
| 900 |
} |
| 901 |
}; |
| 902 |
}, |
| 903 |
onHide: function() { |
| 904 |
|
| 905 |
this.unbindHotKeys(); |
| 906 |
}, |
| 907 |
onInit: function() { |
| 908 |
|
| 909 |
this.buttons = []; |
| 910 |
|
| 911 |
this.hotKeys = {}; |
| 912 |
|
| 913 |
this.focusedButton = null; |
| 914 |
}, |
| 915 |
onShow: function() { |
| 916 |
|
| 917 |
this.bindHotKeys(); |
| 918 |
|
| 919 |
if (!this.focusedButton) { |
| 920 |
this.focusedButton = this.buttons[0]; |
| 921 |
} |
| 922 |
|
| 923 |
if (this.focusedButton) { |
| 924 |
this.focusedButton.trigger('focus'); |
| 925 |
} |
| 926 |
}, |
| 927 |
unbindHotKeys: function() { |
| 928 |
|
| 929 |
this.getElements('window').off({ |
| 930 |
keyup: this.activeKeyUp, |
| 931 |
keydown: this.activeKeyDown |
| 932 |
}); |
| 933 |
} |
| 934 |
}); |
| 935 |
|
| 936 |
DialogsManager.addWidgetType('lightbox', DialogsManager.getWidgetType('buttons').extend('lightbox', { |
| 937 |
getDefaultSettings: function() { |
| 938 |
|
| 939 |
var settings = DialogsManager.getWidgetType('buttons').prototype.getDefaultSettings.apply(this, arguments); |
| 940 |
|
| 941 |
return $.extend(true, settings, { |
| 942 |
contentWidth: 'auto', |
| 943 |
contentHeight: 'auto', |
| 944 |
position: { |
| 945 |
element: 'widgetContent', |
| 946 |
of: 'widget', |
| 947 |
autoRefresh: true |
| 948 |
} |
| 949 |
}); |
| 950 |
}, |
| 951 |
buildWidget: function() { |
| 952 |
|
| 953 |
DialogsManager.getWidgetType('buttons').prototype.buildWidget.apply(this, arguments); |
| 954 |
|
| 955 |
var $widgetContent = this.addElement('widgetContent'), |
| 956 |
elements = this.getElements(); |
| 957 |
|
| 958 |
$widgetContent.append(elements.header, elements.message, elements.buttonsWrapper); |
| 959 |
|
| 960 |
elements.widget.html($widgetContent); |
| 961 |
|
| 962 |
if (elements.closeButton) { |
| 963 |
$widgetContent.prepend(elements.closeButton); |
| 964 |
} |
| 965 |
}, |
| 966 |
onReady: function() { |
| 967 |
|
| 968 |
var elements = this.getElements(), |
| 969 |
settings = this.getSettings(); |
| 970 |
|
| 971 |
if ('auto' !== settings.contentWidth) { |
| 972 |
elements.message.width(settings.contentWidth); |
| 973 |
} |
| 974 |
|
| 975 |
if ('auto' !== settings.contentHeight) { |
| 976 |
elements.message.height(settings.contentHeight); |
| 977 |
} |
| 978 |
} |
| 979 |
})); |
| 980 |
|
| 981 |
DialogsManager.addWidgetType('confirm', DialogsManager.getWidgetType('lightbox').extend('confirm', { |
| 982 |
onReady: function() { |
| 983 |
|
| 984 |
DialogsManager.getWidgetType('lightbox').prototype.onReady.apply(this, arguments); |
| 985 |
|
| 986 |
var strings = this.getSettings('strings'), |
| 987 |
isDefaultCancel = this.getSettings('defaultOption') === 'cancel'; |
| 988 |
|
| 989 |
this.addButton({ |
| 990 |
name: 'cancel', |
| 991 |
text: strings.cancel, |
| 992 |
callback: function(widget) { |
| 993 |
|
| 994 |
widget.trigger('cancel'); |
| 995 |
}, |
| 996 |
focus: isDefaultCancel |
| 997 |
}); |
| 998 |
|
| 999 |
this.addButton({ |
| 1000 |
name: 'ok', |
| 1001 |
text: strings.confirm, |
| 1002 |
callback: function(widget) { |
| 1003 |
|
| 1004 |
widget.trigger('confirm'); |
| 1005 |
}, |
| 1006 |
focus: !isDefaultCancel |
| 1007 |
}); |
| 1008 |
}, |
| 1009 |
getDefaultSettings: function() { |
| 1010 |
|
| 1011 |
var settings = DialogsManager.getWidgetType('lightbox').prototype.getDefaultSettings.apply(this, arguments); |
| 1012 |
|
| 1013 |
settings.strings = { |
| 1014 |
confirm: 'OK', |
| 1015 |
cancel: 'Cancel' |
| 1016 |
}; |
| 1017 |
|
| 1018 |
settings.defaultOption = 'cancel'; |
| 1019 |
|
| 1020 |
return settings; |
| 1021 |
} |
| 1022 |
})); |
| 1023 |
|
| 1024 |
DialogsManager.addWidgetType('alert', DialogsManager.getWidgetType('lightbox').extend('alert', { |
| 1025 |
onReady: function() { |
| 1026 |
|
| 1027 |
DialogsManager.getWidgetType('lightbox').prototype.onReady.apply(this, arguments); |
| 1028 |
|
| 1029 |
var strings = this.getSettings('strings'); |
| 1030 |
|
| 1031 |
this.addButton({ |
| 1032 |
name: 'ok', |
| 1033 |
text: strings.confirm, |
| 1034 |
callback: function(widget) { |
| 1035 |
|
| 1036 |
widget.trigger('confirm'); |
| 1037 |
} |
| 1038 |
}); |
| 1039 |
}, |
| 1040 |
getDefaultSettings: function() { |
| 1041 |
|
| 1042 |
var settings = DialogsManager.getWidgetType('lightbox').prototype.getDefaultSettings.apply(this, arguments); |
| 1043 |
|
| 1044 |
settings.strings = { |
| 1045 |
confirm: 'OK' |
| 1046 |
}; |
| 1047 |
|
| 1048 |
return settings; |
| 1049 |
} |
| 1050 |
})); |
| 1051 |
|
| 1052 |
// Exporting the DialogsManager variable to global |
| 1053 |
global.DialogsManager = DialogsManager; |
| 1054 |
})( |
| 1055 |
typeof jQuery !== 'undefined' ? jQuery : typeof require === 'function' && require('jquery'), |
| 1056 |
(typeof module !== 'undefined' && typeof module.exports !== 'undefined') ? module.exports : window |
| 1057 |
); |
| 1058 |
|