jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/dialog/dialog.js

110 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-09-09 19:21:00 -07:00
define(['dialogHelper', 'dom', 'layoutManager', 'scrollHelper', 'globalize', 'require', 'material-icons', 'emby-button', 'paper-icon-button-light', 'emby-input', 'formDialogStyle'], function (dialogHelper, dom, layoutManager, scrollHelper, globalize, require) {
2016-10-03 22:15:39 -07:00
'use strict';
2016-07-19 23:23:18 -07:00
2016-09-08 13:32:30 -07:00
function showDialog(options, template) {
2016-07-19 23:23:18 -07:00
var dialogOptions = {
2016-09-08 13:32:30 -07:00
removeOnClose: true,
scrollY: false
2016-07-19 23:23:18 -07:00
};
2016-09-17 22:52:10 -07:00
var enableTvLayout = layoutManager.tv;
if (enableTvLayout) {
2016-07-19 23:23:18 -07:00
dialogOptions.size = 'fullscreen';
}
var dlg = dialogHelper.createDialog(dialogOptions);
2016-08-06 23:15:03 -07:00
2016-09-08 13:32:30 -07:00
dlg.classList.add('formDialog');
2016-07-19 23:23:18 -07:00
2016-09-08 13:32:30 -07:00
dlg.innerHTML = globalize.translateHtml(template, 'sharedcomponents');
2016-08-06 23:15:03 -07:00
2016-09-22 23:57:24 -07:00
dlg.style['align-items'] = 'center';
dlg.style['justify-content'] = 'center';
var formDialogContent = dlg.querySelector('.formDialogContent');
formDialogContent.style['flex-grow'] = 'initial';
2016-09-17 22:52:10 -07:00
if (enableTvLayout) {
formDialogContent.style['max-width'] = '50%';
formDialogContent.style['max-height'] = '60%';
scrollHelper.centerFocus.on(formDialogContent, false);
2016-09-08 13:32:30 -07:00
} else {
2016-09-22 23:57:24 -07:00
formDialogContent.style.maxWidth = (Math.min((options.buttons.length * 150) + 200, dom.getWindowSize().innerWidth - 50)) + 'px';
dlg.classList.add('dialog-fullscreen-lowres');
2016-08-11 13:28:49 -07:00
}
2016-07-19 23:23:18 -07:00
2016-09-08 13:32:30 -07:00
//dlg.querySelector('.btnCancel').addEventListener('click', function (e) {
// dialogHelper.close(dlg);
//});
2016-07-19 23:23:18 -07:00
2016-09-18 09:08:32 -07:00
if (options.title) {
dlg.querySelector('.formDialogHeaderTitle').innerHTML = options.title || '';
} else {
dlg.querySelector('.formDialogHeaderTitle').classList.add('hide');
}
2016-07-19 23:23:18 -07:00
2016-09-08 13:32:30 -07:00
dlg.querySelector('.text').innerHTML = options.html || options.text || '';
2016-07-19 23:23:18 -07:00
var i, length;
2016-09-08 13:32:30 -07:00
var html = '';
2016-07-19 23:23:18 -07:00
for (i = 0, length = options.buttons.length; i < length; i++) {
var item = options.buttons[i];
2016-10-03 22:15:39 -07:00
var autoFocus = i === 0 ? ' autofocus' : '';
2016-09-08 23:59:23 -07:00
2016-09-17 22:52:10 -07:00
var buttonClass = 'btnOption raised formDialogFooterItem formDialogFooterItem-autosize';
2016-09-08 23:59:23 -07:00
if (item.type) {
buttonClass += ' button-' + item.type;
}
html += '<button is="emby-button" type="button" class="' + buttonClass + '" data-id="' + item.id + '"' + autoFocus + '>' + item.name + '</button>';
2016-07-19 23:23:18 -07:00
}
2016-09-08 13:32:30 -07:00
dlg.querySelector('.formDialogFooter').innerHTML = html;
2016-07-19 23:23:18 -07:00
var dialogResult;
function onButtonClick() {
dialogResult = this.getAttribute('data-id');
dialogHelper.close(dlg);
}
var buttons = dlg.querySelectorAll('.btnOption');
2016-09-08 13:32:30 -07:00
for (i = 0, length = buttons.length; i < length; i++) {
2016-07-19 23:23:18 -07:00
buttons[i].addEventListener('click', onButtonClick);
}
2016-09-08 13:32:30 -07:00
return dialogHelper.open(dlg).then(function () {
2016-09-17 22:52:10 -07:00
if (enableTvLayout) {
2016-09-08 13:32:30 -07:00
scrollHelper.centerFocus.off(dlg.querySelector('.formDialogContent'), false);
}
2016-07-19 23:23:18 -07:00
if (dialogResult) {
2016-09-08 13:32:30 -07:00
return dialogResult;
2016-07-19 23:23:18 -07:00
} else {
2016-09-08 13:32:30 -07:00
return Promise.reject();
2016-07-19 23:23:18 -07:00
}
});
}
return function (text, title) {
var options;
if (typeof text === 'string') {
options = {
title: title,
text: text
};
} else {
options = text;
}
2016-09-08 13:32:30 -07:00
return new Promise(function (resolve, reject) {
require(['text!./dialog.template.html'], function (template) {
showDialog(options, template).then(resolve, reject);
});
});
2016-07-19 23:23:18 -07:00
};
});