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.auto_login = params.auto_login;
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();
}
if (this.type == "custom-os-password") {
var ts = this.auto_login ? { checked: true } : {};
return
;
}
return this.content;
}
function getColor() {
if (this.type == "input-password" || this.type == "custom-os-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.indexOf("nocancel") < 0;
var hasOk = this.type != "connecting" && this.type != "success" && 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);
if (this.close) this.close();
}
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 self = this;
var err = this.callback(values, function(a=1, b='') { self.show_progress(a, b); });
if (!err) {
if (this.close) this.close();
return;
}
if (err && err.trim()) this.show_progress(false, err);
} else {
this.close();
}
}
event keydown (evt) {
if (!evt.shortcutKey) {
if (isEnterKey(evt)) {
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();
}
}