function translate_text(text) {
if (text.indexOf('Failed') == 0 && text.indexOf(': ') > 0) {
var fds = text.split(': ');
for (var i = 0; i < fds.length; ++i) {
fds[i] = translate(fds[i]);
}
text = fds.join(': ');
}
return text;
}
var msgboxTimerFunc = function() {}
function closeMsgbox() {
self.timer(0, msgboxTimerFunc);
$(#msgbox).content();
}
class MsgboxComponent: Reactor.Component {
function this(params) {
this.width = params.width;
this.height = params.height;
this.type = params.type;
this.title = params.title;
this.content = params.content;
this.remember = params.remember;
this.callback = params.callback;
this.hasRetry = params.hasRetry;
this.contentStyle = params.contentStyle;
try { this.content = translate_text(this.content); } catch (e) {}
}
function getIcon(color) {
if (this.type == "input-password") {
return ;
}
if (this.type == "connecting") {
return ;
}
if (this.type == "success") {
return ;
}
if (this.type.indexOf("error") >= 0 || this.type == "re-input-password") {
return ;
}
return null;
}
function getInputPasswordContent() {
var ts = this.remember ? { checked: true } : {};
return
{translate('Please enter your password')}
;
}
function getContent() {
if (this.type == "input-password") {
return this.getInputPasswordContent();
}
return this.content;
}
function getColor() {
if (this.type == "input-password") {
return "#AD448E";
}
if (this.type == "success") {
return "#32bea6";
}
if (this.type.indexOf("error") >= 0 || this.type == "re-input-password") {
return "#e04f5f";
}
return "#2C8CFF";
}
function hasSkip() {
return this.type.indexOf("skip") >= 0;
}
function render() {
this.set_outline_focus();
var color = this.getColor();
var icon = this.getIcon(color);
var content = this.getContent();
var hasCancel = this.type.indexOf("error") < 0 && this.type != "success" && this.type.indexOf("nocancel") < 0;
var hasOk = this.type != "connecting" && this.type.indexOf("nook") < 0;
var hasClose = this.type.indexOf("hasclose") >= 0;
var show_progress = this.type == "connecting";
var me = this;
self.timer(0, msgboxTimerFunc);
msgboxTimerFunc = function() {
if (typeof content == "string")
me.$(#content).html = translate(content);
else
me.$(#content).content(content);
};
self.timer(3ms, msgboxTimerFunc);
return (
{translate(this.title)}
{hasCancel || this.hasRetry ?
: ""}
{this.hasSkip() ?
: ""}
{hasOk || this.hasRetry ?
: ""}
{hasClose ?
: ""}
);
}
event click $(.custom-event) (_, me) {
if (this.callback) this.callback(me);
}
function submit() {
if (this.$(button#submit)) {
this.$(button#submit).sendEvent("click");
}
}
function cancel() {
if (this.$(button#cancel)) {
this.$(button#cancel).sendEvent("click");
}
}
event click $(button#cancel) {
this.close();
if (this.callback) this.callback(null);
}
event click $(button#skip) {
var values = this.getValues();
values.skip = true;
if (this.callback) this.callback(values);
}
event click $(button#submit) {
if (this.type == "error") {
if (this.hasRetry) {
retryConnect(true);
return;
}
}
if (this.type == "re-input-password") {
this.type = "input-password";
this.update();
return;
}
var values = this.getValues();
if (this.callback) {
var err = (this.callback(values, this.show_progress) || '').trim();
if (!err) {
this.close();
return;
}
if (err) this.show_progress(false, err);
} else {
this.close();
}
}
event keydown (evt) {
if (!evt.shortcutKey) {
if (evt.keyCode == Event.VK_ENTER ||
(is_osx && evt.keyCode == 0x4C) ||
(is_linux && evt.keyCode == 65421)) {
this.submit();
}
if (evt.keyCode == Event.VK_ESCAPE) {
this.cancel();
}
}
}
function show_progress(show=1, err="") {
if (show == -1) {
this.close()
return;
}
this.$(#progress).style.set {
display: show ? "inline-block" : "none"
};
this.$(#error).text = err;
}
function getValues() {
var values = { type: this.type };
for (var el in this.$$(.form input)) {
values[el.attributes["name"]] = el.value;
}
for (var el in this.$$(.form textarea)) {
values[el.attributes["name"]] = el.value;
}
for (var el in this.$$(.form button)) {
values[el.attributes["name"]] = el.value;
}
if (this.type == "input-password") {
values.password = (values.password || "").trim();
if (!values.password) {
return;
}
}
return values;
}
function set_outline_focus() {
var me = this;
self.timer(30ms, function() {
var el = me.$(.outline-focus);
if (el) view.focus = el;
else {
el = me.$(#submit);
if (el) {
view.focus = el;
}
}
});
}
function close() {
closeMsgbox();
}
}