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

277 lines
7.6 KiB
JavaScript
Raw Permalink 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 = [];
2013-01-13 22:21:44 +01:00
this.focus = false;
2012-11-14 17:47:12 +01:00
};
2012-11-15 01:45:45 +01:00
Nsui.prototype.setReconnect = function(func) {
2013-02-27 15:37:08 +01:00
document.getElementById("user-status").addEventListener("click", function() {
2014-11-06 18:52:14 +01:00
func();
$cs.contacts.restore();
2013-02-27 15:37:08 +01:00
}, false);
2013-01-21 17:20:22 +01:00
};
Nsui.prototype.onUserStatusChange = function(new_status) {
var el = document.getElementById("user-status-img");
if (el !== null) {
2014-11-06 18:52:14 +01:00
if (new_status !== "disconnected") {
el.src = 'img/status/me-connected.png';
} else {
el.src = 'img/status/me-disconnected.png';
}
el.alt = new_status;
el.title = new_status;
2013-01-21 17:20:22 +01:00
}
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) {
2014-11-06 18:52:14 +01:00
return tab.name !== element.name;
2012-11-21 12:02:34 +01:00
});
if (new_tab !== null) {
2014-11-06 18:52:14 +01:00
new_tab.show();
2012-11-21 12:02:34 +01:00
}
};
Nsui.prototype.hideAllTabs = function() {
for (i = this.tab_lst.length - 1; i >= 0; --i) {
2014-11-06 18:52:14 +01:00
this.tab_lst[i].hide();
2012-11-21 12:02:34 +01:00
}
};
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) {
2014-11-06 18:52:14 +01:00
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-15 01:45:45 +01:00
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) {
2014-11-06 18:52:14 +01:00
if (this.tab_lst[i].name === current_name) {
if (prev === null && typeof this.tab_lst[i - 1] !== "undefined") {
prev = this.tab_lst[i - 1];
}
break ;
}
prev = this.tab_lst[i];
2012-11-21 12:02:34 +01:00
}
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) {
2014-11-06 18:52:14 +01:00
tab = this.createTab(tab_name);
2012-11-21 12:02:34 +01:00
}
if (this.tab_lst.length <= 1) {
2014-11-06 18:52:14 +01:00
tab.show();
} else if (!tab.isCurrent()) {
2014-11-06 18:52:14 +01:00
tab.setActive();
2012-11-21 21:13:18 +01:00
}
return tab;
};
2013-01-26 21:21:29 +01:00
Nsui.prototype.sendNotification = function(image, title, message) {
var notif;
if (message.lenght > 32) {
2014-11-06 18:52:14 +01:00
message = message.substr(0, 32) + "…";
2013-01-26 21:21:29 +01:00
}
2014-11-06 18:52:14 +01:00
if (typeof chrome.notifications !== "undefined") {
chrome.notifications.create(
'',
{
type: 'basic',
iconUrl: image,
title: title,
message: message,
priority: 0
},
function(id) {
setTimeout(function() {
chrome.notifications.clear(id, function() {});
}, 5000);
}
);
chrome.notifications.onClicked.addListener(function(id) {
window.focus();
chrome.notifications.clear(id, function() {});
});
} else {
2014-11-06 18:52:14 +01:00
notif = webkitNotifications.createNotification(
image,
title,
message
);
notif.onclick = function() {
window.focus();
this.cancel();
};
notif.show();
setTimeout(function() {
notif.cancel();
}, 5000);
}
2013-01-26 21:21:29 +01:00
}
Nsui.prototype.addContentToTab = function(tab_name, content) {
2012-12-18 17:33:16 +01:00
var tab = null;
if ($cs.opts.get("enable_msg")) {
2014-11-06 18:52:14 +01:00
tab = this.addNewTab(tab_name);
tab.appendMessage(content);
if (!this.focus && typeof content.login !== "undefined" && content.login !== null && $cs.opts.get("enable_notif")) {
$cs.avatars.get(content.login, (function(elem) {
return function(url) {
elem.sendNotification(url, content.login, content.message);
};
})(this));
}
2012-12-18 17:33:16 +01:00
}
2012-11-21 21:13:18 +01:00
};
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) {
2013-01-28 13:25:17 +01:00
var dt = new Date(), fmt = "", lnk_exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
2012-11-27 23:53:15 +01:00
2013-01-24 17:11:43 +01:00
fmt += '<span class="chat-timestamp">' + this.formatInteger(dt.getHours(), 2) + ':' + this.formatInteger(dt.getMinutes(), 2) + ':' + this.formatInteger(dt.getSeconds(), 2) + '</span><div class="chat-message-body">';
2012-11-21 21:13:18 +01:00
if (typeof msg.login !== "undefined" && msg.login !== null) {
2014-11-06 18:52:14 +01:00
fmt += '<span class="spk-oth">' + msg.login + ': </span>';
2012-11-21 21:13:18 +01:00
} else {
2014-11-06 18:52:14 +01:00
fmt += '<span class="spk-me">' + document.getElementById('login').value + ': </span>';
2012-11-21 21:13:18 +01:00
}
2013-01-28 13:25:17 +01:00
fmt += this.sanitizeText(msg.message).replace(lnk_exp, '<a href="$1" target="_blank">$1</a>');
fmt += '</div>';
2012-11-21 21:13:18 +01:00
return fmt;
2012-11-14 17:47:12 +01:00
};
Nsui.prototype.sanitizeText = function(str) {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};
Nsui.prototype.currentTabStyle = function() {
2014-11-06 18:52:14 +01:00
$cs.opts.restore();
if (this.tab_lst.length > 1) {
for (var tab in this.tab_lst) {
if (tab.el_lst.classList.contains("tab-current")) {
tab.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
break;
}
}
}
else if (this.tab_lst.length > 0) {
this.tab_lst[0].chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
}
};
Nsui.prototype.showContent = function(part_id) {
2013-02-27 15:37:08 +01:00
var i, ctn_lst = ["config-pannel", "chat-pannel"];
2014-11-06 18:52:14 +01:00
if (part_id === "chat-pannel") {
this.currentTabStyle();
}
for (i = ctn_lst.length - 1; i>= 0; --i) {
2014-11-06 18:52:14 +01:00
if (ctn_lst[i] === part_id) {
document.getElementById(ctn_lst[i]).style.display = "block";
} else {
document.getElementById(ctn_lst[i]).style.display = "none";
}
}
};
Nsui.prototype.switchContent = function(part_id, part_id_to) {
if (document.getElementById(part_id).style.display !== "block") {
2014-11-06 18:52:14 +01:00
this.showContent(part_id);
} else {
2014-11-06 18:52:14 +01:00
this.showContent(part_id_to);
}
};
2012-11-14 17:47:12 +01:00
Nsui.prototype.init = function() {
2013-01-13 22:21:44 +01:00
window.onfocus = (function(elem) {
2014-11-06 18:52:14 +01:00
return function() {
elem.focus = true;
};
2013-01-13 22:21:44 +01:00
})(this);
window.onblur = (function(elem) {
2014-11-06 18:52:14 +01:00
return function() {
elem.focus = false;
};
2013-01-13 22:21:44 +01:00
})(this);
document.getElementById("settings-btn").addEventListener("click", (function(elem) {
2014-11-06 18:52:14 +01:00
return function() {
elem.switchContent("config-pannel", "chat-pannel");
};
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() {
2014-11-06 18:52:14 +01:00
elem.hideAllTabs();
2012-11-21 21:13:18 +01:00
};
})(this);
this.showContent("chat-pannel");
2012-11-14 17:47:12 +01:00
};