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

116 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-03-23 12:03:17 -07:00
define(['dialogHelper', 'layoutManager', 'dialogText', 'html!./icons.html', 'css!./style.css', 'paper-button', 'paper-input'], function (dialogHelper, layoutManager, dialogText) {
2016-02-04 13:51:13 -07:00
2016-02-22 11:25:45 -07:00
return function (options) {
if (typeof options === 'string') {
options = {
title: '',
text: options
};
}
2016-02-04 13:51:13 -07:00
var dialogOptions = {
removeOnClose: true
};
2016-02-04 20:44:29 -07:00
var backButton = false;
var raisedButtons = false;
2016-02-04 13:51:13 -07:00
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
2016-02-04 20:44:29 -07:00
backButton = true;
raisedButtons = true;
2016-02-04 21:56:19 -07:00
} else {
dialogOptions.modal = false;
dialogOptions.entryAnimationDuration = 160;
dialogOptions.exitAnimationDuration = 200;
2016-02-04 13:51:13 -07:00
}
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog(dialogOptions);
2016-02-04 13:51:13 -07:00
dlg.classList.add('promptDialog');
var html = '';
var submitValue = '';
2016-02-04 20:44:29 -07:00
html += '<div class="promptDialogContent">';
if (backButton) {
html += '<paper-icon-button tabindex="-1" icon="dialog:arrow-back" class="btnPromptExit"></paper-icon-button>';
2016-02-04 13:51:13 -07:00
}
2016-02-15 07:41:07 -07:00
if (options.title) {
html += '<h2>';
html += options.title;
html += '</h2>';
}
2016-02-21 13:39:14 -07:00
html += '<form>';
html += '<paper-input autoFocus class="txtPromptValue" value="' + (options.value || '') + '" label="' + (options.label || '') + '"></paper-input>';
2016-02-04 13:51:13 -07:00
2016-02-15 07:41:07 -07:00
if (options.description) {
html += '<div class="fieldDescription">';
html += options.description;
html += '</div>';
}
2016-02-04 13:51:13 -07:00
html += '<br/>';
2016-02-04 20:44:29 -07:00
if (raisedButtons) {
2016-02-22 11:25:45 -07:00
html += '<paper-button raised class="btnSubmit"><iron-icon icon="dialog:check"></iron-icon><span>' + dialogText.get('Ok') + '</span></paper-button>';
2016-02-04 20:44:29 -07:00
} else {
2016-04-03 10:36:47 -07:00
html += '<div class="buttons">';
2016-02-22 11:25:45 -07:00
html += '<paper-button class="btnSubmit">' + dialogText.get('Ok') + '</paper-button>';
html += '<paper-button class="btnPromptExit">' + dialogText.get('Cancel') + '</paper-button>';
2016-02-05 23:33:34 -07:00
html += '</div>';
2016-02-04 20:44:29 -07:00
}
2016-02-21 13:39:14 -07:00
html += '</form>';
2016-02-04 13:51:13 -07:00
html += '</div>';
dlg.innerHTML = html;
document.body.appendChild(dlg);
2016-02-21 13:39:14 -07:00
dlg.querySelector('form').addEventListener('submit', function (e) {
2016-02-04 13:51:13 -07:00
submitValue = dlg.querySelector('.txtPromptValue').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;
});
dlg.querySelector('.btnSubmit').addEventListener('click', function (e) {
// Do a fake form submit this the button isn't a real submit button
var fakeSubmit = document.createElement('input');
fakeSubmit.setAttribute('type', 'submit');
fakeSubmit.style.display = 'none';
var form = dlg.querySelector('form');
form.appendChild(fakeSubmit);
fakeSubmit.click();
form.removeChild(fakeSubmit);
2016-02-04 13:51:13 -07:00
});
dlg.querySelector('.btnPromptExit').addEventListener('click', function (e) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-04 13:51:13 -07:00
});
2016-03-23 12:03:17 -07:00
return dialogHelper.open(dlg).then(function () {
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
}
});
};
});