/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
define('ui', [], function () {
var Dialog = function (options) {
options = options || {};
this.className = 'dialog';
this.backdrop = 'static';
this.closeButton = true;
this.header = false;
this.body = '';
this.width = false;
this.height = false;
this.buttonList = [];
this.dropdownItemList = [];
this.removeOnClose = true;
this.draggable = false;
this.container = 'body'
this.onRemove = function () {};
this.options = options;
var params = [
'className',
'backdrop',
'keyboard',
'closeButton',
'header',
'body',
'width',
'height',
'fitHeight',
'buttons',
'buttonList',
'dropdownItemList',
'removeOnClose',
'draggable',
'container',
'onRemove'
];
params.forEach(function (param) {
if (param in options) {
this[param] = options[param];
}
}.bind(this));
if (this.buttons && this.buttons.length) {
this.buttonList = this.buttons;
}
this.id = 'dialog-' + Math.floor((Math.random() * 100000));
if (typeof this.backdrop === 'undefined') {
this.backdrop = 'static';
}
this.contents = '';
if (this.header) {
var headerClassName = '';
if (this.options.fixedHeaderHeight) {
headerClassName = ' fixed-height';
}
this.contents += '' +
((this.closeButton) ? '×' : '') +
'
' + this.header + '
' +
'';
}
var body = '
' + this.body + '
';
var footerHtml = this.getFooterHtml();
if (footerHtml !== '') {
footerHtml = '';
}
if (this.options.footerAtTheTop) {
this.contents += footerHtml + body;
} else {
this.contents += body + footerHtml;
}
this.contents = '
' + this.contents + '
'
$('').attr('id', this.id)
.attr('class', this.className + ' modal')
.attr('role', 'dialog')
.attr('tabindex', '-1')
.html(this.contents)
.appendTo($(this.container));
this.$el = $('#' + this.id);
this.el = this.$el.get(0);
this.$el.find('header a.close').on('click', function () {
//this.close();
}.bind(this));
this.initButtonEvents();
if (this.draggable) {
this.$el.find('header').css('cursor', 'pointer');
this.$el.draggable({
handle: 'header',
});
}
var modalContentEl = this.$el.find('.modal-content');
if (this.width) {
modalContentEl.css('width', this.width);
modalContentEl.css('margin-left', '-' + (parseInt(this.width.replace('px', '')) / 5) + 'px');
}
if (this.removeOnClose) {
this.$el.on('hidden.bs.modal', function (e) {
if (this.$el.get(0) == e.target) {
if (this.skipRemove) {
return;
}
this.remove();
}
}.bind(this));
}
$window = $(window);
this.$el.on('shown.bs.modal', function (e, r) {
$('.modal-backdrop').not('.stacked').addClass('stacked');
var headerHeight = this.$el.find('.modal-header').outerHeight();
var footerHeight = this.$el.find('.modal-footer').outerHeight();
var diffHeight = headerHeight + footerHeight;
if (!options.fullHeight) {
diffHeight = diffHeight + options.bodyDiffHeight;
}
var h = $window.height();
if (this.fitHeight || options.fullHeight) {
var processResize = function () {
var windowHeight = window.innerHeight;
var windowWidth = $window.width();
if (!options.fullHeight && windowHeight < 512) {
this.$el.find('div.modal-body').css({
maxHeight: 'none',
overflow: 'auto',
height: 'none'
});
return;
}
var cssParams = {
overflow: 'auto'
};
if (options.fullHeight) {
cssParams.height = (windowHeight - diffHeight) + 'px';
this.$el.css('paddingRight', 0);
} else {
if (windowWidth <= options.screenWidthXs) {
cssParams.maxHeight = 'none';
} else {
cssParams.maxHeight = (windowHeight - diffHeight) + 'px';
}
}
this.$el.find('div.modal-body').css(cssParams);
}.bind(this);
$window.off('resize.modal-height');
$window.on('resize.modal-height', processResize);
processResize();
}
}.bind(this));
var $body = $(document.body);
this.$el.on('hidden.bs.modal', function (e) {
if ($('.modal:visible').length > 0) {
$body.addClass('modal-open');
}
});
}
Dialog.prototype.initButtonEvents = function () {
this.buttonList.forEach(function (o) {
if (typeof o.onClick == 'function') {
$('#' + this.id + ' .modal-footer button[data-name="' + o.name + '"]').on('click', function () {
o.onClick(this);
}.bind(this));
}
}.bind(this));
this.dropdownItemList.forEach(function (o) {
if (typeof o.onClick == 'function') {
$('#' + this.id + ' .modal-footer a[data-name="' + o.name + '"]').on('click', function () {
o.onClick(this);
}.bind(this));
}
}.bind(this));
}
Dialog.prototype.getFooterHtml = function () {
var footer = '';
if (this.buttonList.length || this.dropdownItemList.length) {
var rightPart = '';
this.buttonList.forEach(function (o) {
if (o.pullLeft) return;
var className = '';
if (o.className) {
className = ' ' + o.className;
}
rightPart +=
' ';
}, this);
var leftPart = '';
this.buttonList.forEach(function (o) {
if (!o.pullLeft) return;
var className = '';
if (o.className) {
className = ' ' + o.className;
}
leftPart +=
' ';
}, this);
if (leftPart !== '') {
leftPart = '
'+leftPart+'
';
footer += leftPart;
}
if (this.dropdownItemList.length) {
var visibleCount = 0;
this.dropdownItemList.forEach(function (o) {
if (!o.hidden) {
visibleCount++;
}
});
rightPart += '