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

125 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-05-28 11:03:38 -07:00
define(['dialogHelper', 'layoutManager', 'globalize', 'html!./../icons/nav.html', 'css!./style.css', 'paper-button', 'paper-icon-button-light', 'emby-input'], function (dialogHelper, layoutManager, globalize) {
2016-05-06 10:49:58 -07:00
function getIcon(icon, cssClass, canFocus, autoFocus) {
var tabIndex = canFocus ? '' : ' tabindex="-1"';
autoFocus = autoFocus ? ' autofocus' : '';
return '<button is="paper-icon-button-light" class="' + cssClass + '"' + tabIndex + autoFocus + '><iron-icon icon="' + icon + '"></iron-icon></button>';
}
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) {
2016-05-06 10:49:58 -07:00
html += getIcon('dialog:arrow-back', 'btnPromptExit', false);
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>';
2016-05-28 11:03:38 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="text" autoFocus class="txtPromptValue" value="' + (options.value || '') + '" label="' + (options.label || '') + '"/>';
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-05-28 11:03:38 -07:00
html += '</div>';
2016-02-15 07:41:07 -07:00
2016-02-04 13:51:13 -07:00
html += '<br/>';
2016-02-04 20:44:29 -07:00
if (raisedButtons) {
2016-05-12 12:21:43 -07:00
html += '<paper-button raised class="btnSubmit"><iron-icon icon="nav:check"></iron-icon><span>' + globalize.translate('sharedcomponents#ButtonOk') + '</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-05-12 12:21:43 -07:00
html += '<paper-button class="btnSubmit">' + globalize.translate('sharedcomponents#ButtonOk') + '</paper-button>';
html += '<paper-button class="btnPromptExit">' + globalize.translate('sharedcomponents#ButtonCancel') + '</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
}
});
};
});