This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
chromesoul/lib/nsui.js

161 lines
4.2 KiB
JavaScript
Raw Normal View History

2012-11-14 17:47:12 +01:00
//
// Copyright (c) 2012 Rodolphe Breard
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
var Nsui = function() {
2012-11-15 01:45:45 +01:00
this.tab_lst = [];
2012-11-14 17:47:12 +01:00
};
2012-11-15 01:45:45 +01:00
Nsui.prototype.setReconnect = function(func) {
2012-11-21 21:13:18 +01:00
document.getElementById("reconnect").addEventListener("click", func, false);
2012-11-14 17:47:12 +01:00
};
2012-11-21 12:02:34 +01:00
Nsui.prototype.createTab = function(name) {
var tab = new Tab(name);
tab.hide();
this.tab_lst.push(tab);
return tab;
};
Nsui.prototype.deleteTab = function(tab) {
var new_tab = this.getNextTab(tab.name);
this.tab_lst = this.tab_lst.filter(function(element, index, array) {
return tab.name !== element.name;
});
if (new_tab !== null) {
new_tab.show();
} else {
2012-11-21 21:13:18 +01:00
document.getElementById("configuration").style.display = "block";
2012-11-21 12:02:34 +01:00
}
};
Nsui.prototype.hideAllTabs = function() {
2012-11-21 21:13:18 +01:00
document.getElementById("configuration").style.display = "none";
2012-11-21 12:02:34 +01:00
for (i = this.tab_lst.length - 1; i >= 0; --i) {
this.tab_lst[i].hide();
}
};
Nsui.prototype.getTabByName = function(name) {
2012-11-15 01:45:45 +01:00
var i, ret = null;
for (i = this.tab_lst.length - 1; i >= 0; --i) {
if (this.tab_lst[i].name === name) {
ret = this.tab_lst[i];
break ;
2012-11-14 17:47:12 +01:00
}
}
2012-11-15 01:45:45 +01:00
return ret;
}
2012-11-21 12:02:34 +01:00
Nsui.prototype.getNextTab = function(current_name) {
var i, prev = null;
2012-11-14 17:47:12 +01:00
2012-11-21 12:02:34 +01:00
for (i = this.tab_lst.length - 1; i >= 0; --i) {
if (this.tab_lst[i].name === current_name) {
2012-11-21 21:13:18 +01:00
if (prev === null && typeof this.tab_lst[i - 1] !== "undefined") {
2012-11-21 12:02:34 +01:00
prev = this.tab_lst[i - 1];
}
break ;
}
prev = this.tab_lst[i];
}
return prev;
}
Nsui.prototype.addNewTab = function(tab_name) {
2012-11-21 12:02:34 +01:00
var tab = this.getTabByName(tab_name);
2012-11-21 21:13:18 +01:00
2012-11-21 12:02:34 +01:00
if (tab === null) {
tab = this.createTab(tab_name);
}
2012-11-21 21:13:18 +01:00
if (!tab.isCurrent()) {
tab.setActive();
}
return tab;
};
Nsui.prototype.addContentToTab = function(tab_name, content) {
var tab = this.addNewTab(tab_name);
2012-11-21 21:13:18 +01:00
tab.appendText(this.formatMessage(content));
};
2012-11-27 23:53:15 +01:00
Nsui.prototype.formatInteger = function(num, len) {
num = "" + num;
while (num.length < len) {
num = "0" + num;
}
return num;
};
2012-11-21 21:13:18 +01:00
Nsui.prototype.formatMessage = function(msg) {
2012-11-27 23:53:15 +01:00
var dt = new Date(), fmt = "";
fmt += "(" + this.formatInteger(dt.getHours(), 2) + ":" + this.formatInteger(dt.getMinutes(), 2) + ":" + this.formatInteger(dt.getSeconds(), 2) + ")";
2012-11-21 21:13:18 +01:00
if (typeof msg.login !== "undefined" && msg.login !== null) {
2012-11-21 21:13:18 +01:00
fmt += '<span class="spk-oth">' + msg.login + ': </span>';
} else {
fmt += '<span class="spk-me">' + document.getElementById('login').value + ': </span>';
}
msg.message = msg.message.replace("<", "&lt;");
msg.message = msg.message.replace(">", "&gt;");
fmt += msg.message;
fmt += '<br>';
return fmt;
2012-11-14 17:47:12 +01:00
};
Nsui.prototype.init = function() {
2012-11-21 21:13:18 +01:00
document.getElementById("tab-config").addEventListener("click", (function(elem) {
2012-11-15 01:45:45 +01:00
return function() {
2012-11-21 21:13:18 +01:00
for (var i = elem.tab_lst.length - 1; i >= 0; --i) {
2012-11-15 01:45:45 +01:00
elem.tab_lst[i].hide();
2012-11-21 21:13:18 +01:00
}
this.classList.add("tab-current");
document.getElementById("configuration").style.display = "block";
2012-11-15 01:45:45 +01:00
};
2012-11-21 21:13:18 +01:00
})(this), false);
Tab.prototype.closeHandler = (function(elem) {
return function() {
elem.deleteTab(this);
};
})(this);
Tab.prototype.showHandler = (function(elem) {
return function() {
2012-11-21 12:02:34 +01:00
elem.hideAllTabs();
2012-11-21 21:13:18 +01:00
};
})(this);
2012-11-15 01:45:45 +01:00
2012-11-26 15:31:15 +01:00
if (typeof $cs.client !== "undefined") {
NsClient.prototype.msgHandler = (function(elem) {
return function(msg) {
elem.addContentToTab(msg.login, msg);
};
})(this);
}
2012-11-14 17:47:12 +01:00
};