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/tab.nsui.js

173 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

2012-11-15 01:45:45 +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 Tab = function(name) {
this.name = this.filterName(name);
this.chat_input = null;
2012-11-21 21:13:18 +01:00
this.wr_lst = document.getElementById("tab-lst");
this.wr_body = document.getElementById("tab-body-wrapper");
this.buff_len = 1000;
this.history_index = 0;
this.history = [];
2012-11-15 01:45:45 +01:00
this.initListElement();
this.initBodyElement();
};
Tab.prototype.filterName = function(name) {
return name.replace(/[^a-z0-9\-_]/g, "").replace(/^[0-9]/g, "_");
2012-11-15 01:45:45 +01:00
};
Tab.prototype.initListElement = function() {
2013-05-26 18:58:13 +02:00
var inner = document.createElement("span"), close = document.createElement("img");
2012-11-15 01:45:45 +01:00
2013-05-26 18:58:13 +02:00
close.src = "img/delcontact.png";
close.alt = "x";
close.title = "Close tab";
close.classList.add("close-tab");
2012-11-15 01:45:45 +01:00
inner.innerHTML = this.name;
2012-11-21 21:13:18 +01:00
this.el_lst = document.createElement("li");
2013-05-26 18:58:13 +02:00
inner.addEventListener("click", (function(elem) {
2012-11-21 21:13:18 +01:00
return function() {
elem.show();
};
})(this), false);
2013-05-26 18:58:13 +02:00
close.addEventListener("click", (function(elem) {
return function() {
elem.close();
};
})(this), false);
2012-11-21 21:13:18 +01:00
this.el_lst.addEventListener("dblclick", (function(elem) {
return function() {
elem.close();
};
})(this), false);
2012-11-15 01:45:45 +01:00
this.el_lst.appendChild(inner);
2013-05-26 18:58:13 +02:00
this.el_lst.appendChild(close);
2012-11-15 01:45:45 +01:00
this.wr_lst.appendChild(this.el_lst);
};
Tab.prototype.initBodyElement = function() {
var chat_input_w = document.createElement("div");
2014-11-06 18:52:14 +01:00
this.chat_input = document.createElement("input");
2013-01-24 17:11:43 +01:00
this.chat_input.placeholder = "Some text here...";
2014-11-06 18:52:14 +01:00
2012-11-27 23:53:15 +01:00
this.chat_log = document.createElement("p");
2012-11-21 21:13:18 +01:00
this.chat_log.classList.add("chat-log");
2014-11-06 18:52:14 +01:00
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
2012-11-21 21:13:18 +01:00
this.el_body = document.createElement("div");
this.el_body.classList.add("tab-body");
2012-11-15 01:45:45 +01:00
this.el_body.id = this.name;
2012-11-21 21:13:18 +01:00
this.el_body.style.display = "none";
2014-11-06 18:52:14 +01:00
2012-11-21 21:13:18 +01:00
chat_input_w.classList.add("chat-input-wrapper");
2014-11-06 18:52:14 +01:00
this.chat_input.classList.add("chat-input");
this.chat_input.setAttribute("type", "text");
this.chat_input.addEventListener("keyup", (function(elem) {
2014-11-06 18:52:14 +01:00
return function(event) {
var key_submit = 13, key_up = 38, key_down = 40;
if (event.keyCode === key_submit && this.value != "") {
var msg = this.value;
this.value = "";
if (typeof $cs.client !== "undefined" && $cs.opts.get("enable_msg")) {
$cs.client.speak(elem.name, msg);
$cs.ui.addContentToTab(elem.name, {"message": msg});
} else {
console.error("chromesoul client not found");
}
} else if (event.keyCode === key_up) {
if (elem.history_index < elem.history.length) {
elem.history_index++;
this.value = elem.history[elem.history.length - elem.history_index];
}
} else if (event.keyCode === key_down) {
if (elem.history_index > 0) {
elem.history_index--;
if (elem.history_index > 0) {
this.value = elem.history[elem.history.length - elem.history_index];
} else {
this.value = "";
}
}
}
};
2012-11-26 15:31:15 +01:00
})(this), false);
2012-11-15 01:45:45 +01:00
chat_input_w.appendChild(this.chat_input);
2012-11-15 01:45:45 +01:00
this.el_body.appendChild(this.chat_log);
this.el_body.appendChild(chat_input_w);
this.wr_body.appendChild(this.el_body);
};
Tab.prototype.close = function() {
2012-11-21 21:13:18 +01:00
if (typeof this.closeHandler !== "undefined") {
2014-11-06 18:52:14 +01:00
this.closeHandler();
2012-11-15 01:45:45 +01:00
}
this.wr_body.removeChild(this.el_body);
this.wr_lst.removeChild(this.el_lst);
};
Tab.prototype.hide = function() {
2012-11-21 21:13:18 +01:00
this.el_lst.classList.remove("tab-current");
this.el_body.style.display = "none";
2012-11-15 01:45:45 +01:00
};
Tab.prototype.show = function() {
2012-11-21 21:13:18 +01:00
if (typeof this.showHandler !== "undefined") {
2014-11-06 18:52:14 +01:00
this.showHandler();
2012-11-15 01:45:45 +01:00
}
2012-11-21 21:13:18 +01:00
this.el_lst.classList.remove("tab-active");
this.el_lst.classList.add("tab-current");
this.el_body.style.display = "block";
2014-11-06 18:52:14 +01:00
$cs.opts.restore()
2012-11-15 01:45:45 +01:00
this.chat_log.scrollTop = 42000;
2014-11-06 18:52:14 +01:00
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
this.chat_input.focus();
2012-11-15 01:45:45 +01:00
};
2012-11-21 21:13:18 +01:00
Tab.prototype.isCurrent = function() {
return this.el_lst.classList.contains("tab-current");
};
2012-11-21 12:02:34 +01:00
Tab.prototype.setActive = function() {
2012-11-21 21:13:18 +01:00
this.el_lst.classList.add("tab-active");
2012-11-15 01:45:45 +01:00
}
2012-12-18 18:55:34 +01:00
Tab.prototype.flushText = function() {
2013-01-24 17:30:24 +01:00
while (this.chat_log.children.length > this.buff_len) {
2014-11-06 18:52:14 +01:00
this.chat_log.removeChild(this.chat_log.children[0]);
2012-12-18 18:55:34 +01:00
}
while (this.history.length > this.buff_len) {
2014-11-06 18:52:14 +01:00
this.history.shift();
}
2012-12-18 18:55:34 +01:00
};
Tab.prototype.appendMessage = function(msg) {
this.history_index = 0;
2013-01-24 17:11:43 +01:00
this.chat_log.innerHTML += '<div class="chat-message">' + $cs.ui.formatMessage(msg) + '</div>';
2014-11-06 18:52:14 +01:00
if (!(typeof msg.login !== "undefined" && msg.login !== null)) {
2014-11-06 18:52:14 +01:00
this.history.push(msg.message);
}
2012-12-18 18:55:34 +01:00
this.flushText();
this.chat_log.scrollTop = 42 * this.buff_len;
2012-11-15 01:45:45 +01:00
};