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

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-09-11 14:02:32 -07:00
define(['dialogHelper', 'layoutManager', 'scrollHelper', 'globalize', 'dom', 'require', 'material-icons', 'emby-button', 'paper-icon-button-light', 'emby-input', 'formDialogStyle'], function (dialogHelper, layoutManager, scrollHelper, globalize, dom, require) {
2016-10-17 22:06:48 -07:00
'use strict';
2016-05-06 10:49:58 -07:00
2016-09-07 23:15:44 -07:00
function setInputProperties(dlg, options) {
var txtInput = dlg.querySelector('#txtInput');
2016-09-12 11:09:40 -07:00
if (txtInput.label) {
txtInput.label(options.label || '');
} else {
txtInput.setAttribute('label', options.label || '');
}
2016-09-07 23:15:44 -07:00
txtInput.value = options.value || '';
2016-05-06 10:49:58 -07:00
}
2016-02-04 13:51:13 -07:00
2016-09-08 13:32:30 -07:00
function showDialog(options, template) {
2016-02-04 13:51:13 -07:00
var dialogOptions = {
2016-09-07 23:15:44 -07:00
removeOnClose: true,
scrollY: false
2016-02-04 13:51:13 -07:00
};
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
}
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog(dialogOptions);
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
dlg.classList.add('formDialog');
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
dlg.innerHTML = globalize.translateHtml(template, 'sharedcomponents');
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
if (layoutManager.tv) {
scrollHelper.centerFocus.on(dlg.querySelector('.formDialogContent'), false);
2016-09-08 13:32:30 -07:00
} else {
dlg.querySelector('.dialogContentInner').classList.add('dialogContentInner-mini');
2016-09-22 23:57:24 -07:00
dlg.classList.add('dialog-fullscreen-lowres');
2016-02-04 13:51:13 -07:00
}
2016-09-07 23:15:44 -07:00
dlg.querySelector('.btnCancel').addEventListener('click', function (e) {
dialogHelper.close(dlg);
});
2016-09-08 23:59:23 -07:00
dlg.querySelector('.formDialogHeaderTitle').innerHTML = options.title || '';
2016-02-15 07:41:07 -07:00
if (options.description) {
2016-09-07 23:15:44 -07:00
dlg.querySelector('.fieldDescription').innerHTML = options.description;
2016-02-04 20:44:29 -07:00
} else {
2016-09-07 23:15:44 -07:00
dlg.querySelector('.fieldDescription').classList.add('hide');
2016-02-04 20:44:29 -07:00
}
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
setInputProperties(dlg, options);
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
var submitValue;
2016-02-21 13:39:14 -07:00
dlg.querySelector('form').addEventListener('submit', function (e) {
2016-02-04 13:51:13 -07:00
2016-09-07 23:15:44 -07:00
submitValue = dlg.querySelector('#txtInput').value;
2016-02-21 13:39:14 -07:00
e.preventDefault();
2016-04-03 10:36:47 -07:00
e.stopPropagation();
// Important, don't close the dialog until after the form has completed submitting, or it will cause an error in Chrome
setTimeout(function () {
dialogHelper.close(dlg);
}, 300);
2016-02-21 13:39:14 -07:00
return false;
});
2016-10-04 21:26:45 -07:00
dlg.querySelector('.submitText').innerHTML = options.confirmText || globalize.translate('sharedcomponents#ButtonOk');
2016-09-22 23:57:24 -07:00
2016-09-11 14:02:32 -07:00
dlg.style.minWidth = (Math.min(400, dom.getWindowSize().innerWidth - 50)) + 'px';
2016-03-23 12:03:17 -07:00
return dialogHelper.open(dlg).then(function () {
2016-09-08 13:32:30 -07:00
if (layoutManager.tv) {
scrollHelper.centerFocus.off(dlg.querySelector('.formDialogContent'), false);
}
2016-02-04 13:51:13 -07:00
var value = submitValue;
2016-04-03 10:36:47 -07:00
2016-02-04 13:51:13 -07:00
if (value) {
2016-02-22 11:25:45 -07:00
return value;
2016-02-04 13:51:13 -07:00
} else {
2016-02-22 11:25:45 -07:00
return Promise.reject();
2016-02-04 13:51:13 -07:00
}
});
2016-09-07 23:15:44 -07:00
}
return function (options) {
return new Promise(function (resolve, reject) {
require(['text!./prompt.template.html'], function (template) {
if (typeof options === 'string') {
options = {
title: '',
text: options
};
}
2016-09-08 13:32:30 -07:00
showDialog(options, template).then(resolve, reject);
2016-09-07 23:15:44 -07:00
});
});
2016-02-04 13:51:13 -07:00
};
});