code styling

This commit is contained in:
Rodolphe Breard 2014-11-06 18:52:14 +01:00
parent 00d22a5535
commit 7d9a97558c
12 changed files with 488 additions and 487 deletions

View file

@ -15,8 +15,8 @@
//
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('chromesoul.html', {
'width': 800,
'height': 500
});
chrome.app.window.create('chromesoul.html', {
'width': 800,
'height': 500
});
});

View file

@ -19,16 +19,16 @@
<div id="main-ctn">
<div id="config-pannel">
<input type="text" placeholder="Login" id="login" class="opt" value="" /><br>
<input type="password" placeholder="Password (SOCKS)" id="pwd_socks" class="opt" value="" /><br>
<input type="checkbox" id="enable_notif" class="opt" checked="checked" /><label for="enable_notif">Enable notifications</label><br>
<input type="checkbox" id="enable_msg" class="opt" checked="checked" /><label for="enable_msg">Enable messages</label><br>
<input type="number" placeholder="12" id="chat-size" class="opt" value="" /><label for="chat-size">Chat font-size</label><br>
<button id="save">Save</button> <span id="status"></span>
<input type="text" placeholder="Login" id="login" class="opt" value="" /><br>
<input type="password" placeholder="Password (SOCKS)" id="pwd_socks" class="opt" value="" /><br>
<input type="checkbox" id="enable_notif" class="opt" checked="checked" /><label for="enable_notif">Enable notifications</label><br>
<input type="checkbox" id="enable_msg" class="opt" checked="checked" /><label for="enable_msg">Enable messages</label><br>
<input type="number" placeholder="12" id="chat-size" class="opt" value="" /><label for="chat-size">Chat font-size</label><br>
<button id="save">Save</button> <span id="status"></span>
</div>
<div id="chat-pannel">
<ul id="tab-lst"></ul>
<div id="tab-body-wrapper"></div>
<ul id="tab-lst"></ul>
<div id="tab-body-wrapper"></div>
</div>
</div>

View file

@ -1,10 +1,10 @@
var chromesoul = (function() {
var chromesoul = {
"opts": new OptionsManager(),
"ui": new Nsui(),
"contacts": new ContactList(),
"client": new Client(),
"avatars": new AvatarManager()
"opts": new OptionsManager(),
"ui": new Nsui(),
"contacts": new ContactList(),
"client": new Client(),
"avatars": new AvatarManager()
};
return (window.chromesoul = window.$cs = chromesoul);

View file

@ -24,25 +24,25 @@ AvatarManager.prototype.getContactPic = function(name, callback) {
xhr.open("GET", "http://cdn.local.epitech.net/userprofil/profilview/" + name + ".jpg", true);
xhr.responseType = "blob";
xhr.onload = (function(elem) {
return function(e) {
if (e.target.status === 200) {
elem.avatars[name] = window.webkitURL.createObjectURL(this.response);
} else {
elem.avatars[name] = "img/default-avatar.jpg";
}
return function(e) {
if (e.target.status === 200) {
elem.avatars[name] = window.webkitURL.createObjectURL(this.response);
} else {
elem.avatars[name] = "img/default-avatar.jpg";
}
if (typeof callback !== "undefined") {
callback(elem.avatars[name]);
}
};
if (typeof callback !== "undefined") {
callback(elem.avatars[name]);
}
};
})(this);
xhr.send();
}
AvatarManager.prototype.get = function(name, callback) {
if (typeof this.avatars[name] === "undefined") {
this.getContactPic(name, callback);
this.getContactPic(name, callback);
} else {
callback(this.avatars[name]);
callback(this.avatars[name]);
}
};

View file

@ -26,41 +26,41 @@ var Client = function() {
Client.prototype.createSocket = function() {
if (this.socket === null) {
this.socket = new TxtSocket();
this.socket.onError = (function(elem) {
return function() {
elem.is_connected = false;
elem.connect();
};
})(this);
this.socket = new TxtSocket();
this.socket.onError = (function(elem) {
return function() {
elem.is_connected = false;
elem.connect();
};
})(this);
}
};
Client.prototype.connect = function() {
if (!this.is_connected) {
this.createSocket();
this.socket.connect(this.client.host, this.client.port, (function(elem) {
return function() {
elem.client.connect(elem, function() {
if (elem.is_connected) {
elem.daemonize();
elem.changeStatus();
elem.addContact();
} else {
elem.disconnect();
}
});
};
})(this));
this.createSocket();
this.socket.connect(this.client.host, this.client.port, (function(elem) {
return function() {
elem.client.connect(elem, function() {
if (elem.is_connected) {
elem.daemonize();
elem.changeStatus();
elem.addContact();
} else {
elem.disconnect();
}
});
};
})(this));
}
}
Client.prototype.disconnect = function() {
if (this.is_connected) {
this.is_connected = false;
this.client.disconnect(this);
this.socket.disconnect();
this.socket = null;
this.is_connected = false;
this.client.disconnect(this);
this.socket.disconnect();
this.socket = null;
}
this.changeStatus(this.client.status_disconnected);
};
@ -72,49 +72,49 @@ Client.prototype.reconnect = function() {
Client.prototype.daemonize = function() {
this.socket.read((function(elem) {
return function(data) {
elem.client.recv(elem, data);
elem.daemonize();
};
return function(data) {
elem.client.recv(elem, data);
elem.daemonize();
};
})(this));
};
Client.prototype.changeStatus = function(status) {
if (typeof status === 'undefined') {
status = this.client.default_status;
status = this.client.default_status;
}
if (status !== this.status) {
this.status = status;
this.updateStatus();
this.status = status;
this.updateStatus();
}
};
Client.prototype.updateStatus = function() {
if (this.status !== null) {
this.client.changeStatus(this);
this.client.changeStatus(this);
}
};
Client.prototype.addContact = function(name) {
if (typeof name !== 'undefined') {
this.waiting_contacts.push(name);
this.waiting_contacts.push(name);
}
if (this.is_connected) {
var tmp = this.waiting_contacts;
this.waiting_contacts = [];
this.client.addContact(this, tmp);
var tmp = this.waiting_contacts;
this.waiting_contacts = [];
this.client.addContact(this, tmp);
}
};
Client.prototype.rmContact = function(name) {
if (this.is_connected) {
this.client.rmContact(this, name);
this.client.rmContact(this, name);
}
};
Client.prototype.speak = function(to, msg) {
if (this.is_connected) {
this.client.speak(this, to, msg);
this.client.speak(this, to, msg);
}
};
@ -124,14 +124,14 @@ Client.prototype.init = function(client, ui) {
this.connect();
setInterval((function(elem) {
return function() {
elem.connect();
};
return function() {
elem.connect();
};
})(this), 10000);
setInterval((function(elem) {
return function() {
elem.updateStatus();
};
return function() {
elem.updateStatus();
};
})(this), 180000);
};

View file

@ -24,17 +24,17 @@ ContactList.prototype.insertContact = function(elem) {
var i, next = null;
for (i in this.contacts) {
if (this.contacts.hasOwnProperty(i)) {
if (elem.name < this.contacts[i].name && (next === null || this.contacts[i].name < next.name)) {
next = this.contacts[i];
}
}
if (this.contacts.hasOwnProperty(i)) {
if (elem.name < this.contacts[i].name && (next === null || this.contacts[i].name < next.name)) {
next = this.contacts[i];
}
}
}
if (next !== null) {
this.lst.insertBefore(elem.li, next.li);
this.lst.insertBefore(elem.li, next.li);
} else {
this.lst.appendChild(elem.li);
this.lst.appendChild(elem.li);
}
};
@ -42,83 +42,84 @@ ContactList.prototype.addContact = function(name) {
var infos = {}, li = null, login = null, close = null;
if (typeof this.contacts[name] === "undefined") {
infos = {
"name": name,
"li": null,
"avatar": null
};
infos = {
"name": name,
"li": null,
"avatar": null
};
li = document.createElement("li");
$cs.avatars.get(infos.name, function(url) {
li.style.backgroundImage = "url('" + url + "')";
});
img = document.createElement("img");
this.setImageStatus(img, "offline");
login = document.createElement("span");
close = document.createElement("span");
close.classList.add("remove");
login.innerHTML = name;
close.innerHTML = '<img src="img/delcontact.png" alt="x" title="delete contact">';
li.appendChild(img);
li.appendChild(login);
li.appendChild(close);
li.addEventListener("dblclick", function() {
var nel = $cs.ui.addNewTab(name);
li = document.createElement("li");
$cs.avatars.get(infos.name, function(url) {
li.style.backgroundImage = "url('" + url + "')";
});
img = document.createElement("img");
this.setImageStatus(img, "offline");
login = document.createElement("span");
close = document.createElement("span");
close.classList.add("remove");
login.innerHTML = name;
close.innerHTML = '<img src="img/delcontact.png" alt="x" title="delete contact">';
li.appendChild(img);
li.appendChild(login);
li.appendChild(close);
li.addEventListener("dblclick", function() {
var nel = $cs.ui.addNewTab(name);
$cs.ui.hideAllTabs();
$cs.ui.showContent("chat-pannel");
nel.show();
});
close.addEventListener("click", (function(elem) {
return function() {
elem.rmContact(name);
};
})(this));
$cs.ui.hideAllTabs();
$cs.ui.showContent("chat-pannel");
nel.show();
});
close.addEventListener("click", (function(elem) {
return function() {
elem.rmContact(name);
};
})(this));
infos.li = li;
infos.img = img;
this.contacts[name] = infos;
this.save();
this.insertContact(infos);
infos.li = li;
infos.img = img;
this.contacts[name] = infos;
this.save();
this.insertContact(infos);
}
$cs.client.addContact(name);
};
ContactList.prototype.rmContact = function(name) {
if (typeof this.contacts[name] !== "undefined") {
this.lst.removeChild(this.contacts[name].li);
delete this.contacts[name];
this.save();
this.lst.removeChild(this.contacts[name].li);
delete this.contacts[name];
this.save();
}
};
ContactList.prototype.setImageStatus = function(img, status) {
var status_list = {'default' : 'img/status/contact-connected.png',
var status_list = {
'default' : 'img/status/contact-connected.png',
'offline': 'img/status/contact-disconnected.png',
'disconnected': 'img/status/contact-disconnected.png',
'deconnecte': 'img/status/contact-disconnected.png',
'deco': 'img/status/contact-disconnected.png',
'hidden': 'img/status/contact-disconnected.png',
'cache': 'img/status/contact-disconnected.png',
'offline': 'img/status/contact-disconnected.png',
'disconnected': 'img/status/contact-disconnected.png',
'deconnecte': 'img/status/contact-disconnected.png',
'deco': 'img/status/contact-disconnected.png',
'hidden': 'img/status/contact-disconnected.png',
'cache': 'img/status/contact-disconnected.png',
'away': 'img/status/contact-away.png',
'inactif': 'img/status/contact-away.png',
'absent': 'img/status/contact-away.png',
'idle': 'img/status/contact-away.png',
'locked': 'img/status/contact-away.png',
'lock': 'img/status/contact-away.png',
'away': 'img/status/contact-away.png',
'inactif': 'img/status/contact-away.png',
'absent': 'img/status/contact-away.png',
'idle': 'img/status/contact-away.png',
'locked': 'img/status/contact-away.png',
'lock': 'img/status/contact-away.png',
'dnd': 'img/status/contact-dnd.png',
'occuped': 'img/status/contact-dnd.png',
'npd': 'img/status/contact-dnd.png',
'occupe': 'img/status/contact-dnd.png'
};
'dnd': 'img/status/contact-dnd.png',
'occuped': 'img/status/contact-dnd.png',
'npd': 'img/status/contact-dnd.png',
'occupe': 'img/status/contact-dnd.png'
};
if (status_list.hasOwnProperty(status)) {
img.src = status_list[status];
img.src = status_list[status];
} else {
img.src = status_list.default;
img.src = status_list.default;
}
img.alt = status;
img.title = status;
@ -126,8 +127,8 @@ ContactList.prototype.setImageStatus = function(img, status) {
ContactList.prototype.changeContactStatus = function(name, status) {
if (typeof this.contacts[name] !== "undefined") {
this.setImageStatus(this.contacts[name].img, status);
console.log(name + ' changed his status to ' + status);
this.setImageStatus(this.contacts[name].img, status);
console.log(name + ' changed his status to ' + status);
}
};
@ -135,9 +136,9 @@ ContactList.prototype.save = function() {
var i, data = {"contact_list": []};
for (i in this.contacts) {
if (this.contacts.hasOwnProperty(i)) {
data.contact_list.push(i);
}
if (this.contacts.hasOwnProperty(i)) {
data.contact_list.push(i);
}
}
data.contact_list.sort();
this.storage.set(data, function() {});
@ -146,13 +147,13 @@ ContactList.prototype.save = function() {
ContactList.prototype.restore = function() {
this.storage.get("contact_list", (function(elem) {
return function(items) {
elem.contacts = {};
elem.lst.innerHTML = "";
if (typeof items.contact_list !== "undefined") {
for (i = items.contact_list.length - 1; i >= 0; --i) {
elem.addContact(items.contact_list[i]);
}
}
elem.contacts = {};
elem.lst.innerHTML = "";
if (typeof items.contact_list !== "undefined") {
for (i = items.contact_list.length - 1; i >= 0; --i) {
elem.addContact(items.contact_list[i]);
}
}
};
})(this));
};
@ -163,9 +164,9 @@ ContactList.prototype.init = function() {
this.restore();
this.save();
add_btn.addEventListener("keyup", function(event) {
if (event.keyCode == 13 && this.value != "") {
$cs.contacts.addContact(this.value);
this.value = "";
}
if (event.keyCode == 13 && this.value != "") {
$cs.contacts.addContact(this.value);
this.value = "";
}
});
};

View file

@ -23,77 +23,77 @@ var NsClient = function() {
this.action = {};
this.action.ping = function(client, str) {
if (str.substr(0, 5) !== 'ping ') {
return false;
}
if (str.substr(0, 5) !== 'ping ') {
return false;
}
client.socket.write(str, function(inf) {});
return true;
client.socket.write(str, function(inf) {});
return true;
};
this.action.msg = (function(elem) {
return function(client, str) {
var mo = {}, mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/.exec(str);
return function(client, str) {
var mo = {}, mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/.exec(str);
if (mch === null) {
return false;
}
if (mch === null) {
return false;
}
mo.socket = mch[1];
mo.login = mch[2];
mo.host = mch[3];
mo.location = elem.msgDecode(mch[4]);
mo.group = mch[5];
mo.message = elem.msgDecode(mch[6]);
mo.socket = mch[1];
mo.login = mch[2];
mo.host = mch[3];
mo.location = elem.msgDecode(mch[4]);
mo.group = mch[5];
mo.message = elem.msgDecode(mch[6]);
$cs.ui.addContentToTab(mo.login, mo); // TODO: FIX ME!
$cs.ui.addContentToTab(mo.login, mo); // TODO: FIX ME!
return true;
};
return true;
};
})(this);
this.action.status = (function(elem) {
return function(client, str) {
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| state ([^ ]*):(\d+)/.exec(str);
return function(client, str) {
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| state ([^ ]*):(\d+)/.exec(str);
if (mch === null) {
return false;
}
if (mch === null) {
return false;
}
$cs.contacts.changeContactStatus(mch[2], elem.msgDecode(mch[6])); // TODO: FIX ME!
$cs.contacts.changeContactStatus(mch[2], elem.msgDecode(mch[6])); // TODO: FIX ME!
return true;
};
return true;
};
})(this);
this.action.login_out = function(client, str) {
var status = 'offline', mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| (login|logout)/.exec(str);
var status = 'offline', mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| (login|logout)/.exec(str);
if (mch === null) {
return false;
}
if (mch === null) {
return false;
}
if (mch[6] === 'login') {
status = 'actif';
}
if (mch[6] === 'login') {
status = 'actif';
}
$cs.contacts.changeContactStatus(mch[2], status); // TODO: FIX ME!
$cs.contacts.changeContactStatus(mch[2], status); // TODO: FIX ME!
return true;
return true;
};
this.action.who = (function(elem) {
return function(client, str) {
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| who (\d+) (.*?) (.*?) (\d+) (\d+) (\d+) (\d+) (.*?) (.*?) (.*?) (.*?):(.*) (.*)/.exec(str);
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| who (\d+) (.*?) (.*?) (\d+) (\d+) (\d+) (\d+) (.*?) (.*?) (.*?) (.*?):(.*) (.*)/.exec(str);
if (mch === null) {
return false;
}
if (mch === null) {
return false;
}
$cs.contacts.changeContactStatus(mch[7], elem.msgDecode(mch[16])); // TODO: FIX ME!
$cs.contacts.changeContactStatus(mch[7], elem.msgDecode(mch[16])); // TODO: FIX ME!
return true;
};
return true;
};
})(this);
};
@ -101,66 +101,66 @@ NsClient.prototype.connect = function(client, callback) {
var login, pwd_socks;
if (!client.is_connected) {
login = $cs.opts.get('login');
pwd_socks = $cs.opts.get('pwd_socks');
if (login !== null && pwd_socks !== null) {
client.socket.read(function(data) {
var auth = '';
login = $cs.opts.get('login');
pwd_socks = $cs.opts.get('pwd_socks');
if (login !== null && pwd_socks !== null) {
client.socket.read(function(data) {
var auth = '';
data = data.split(' ');
auth = 'ext_user_log ';
auth += login + ' ';
auth += hex_md5(data[2] + '-' + data[3] + '/' + data[4] + pwd_socks) + ' ';
auth += 'chromesoul chromesoul\n';
client.socket.write('auth_ag ext_user none none\n', function(inf) {
client.socket.read(function(data) {
client.socket.write(auth, function(inf) {
client.socket.read(function(data) {
if (data === 'rep 002 -- cmd end') {
client.is_connected = true;
console.info('connected to the netsoul server');
} else {
console.error('authentication failure');
}
callback();
});
});
});
});
});
} else {
callback();
}
data = data.split(' ');
auth = 'ext_user_log ';
auth += login + ' ';
auth += hex_md5(data[2] + '-' + data[3] + '/' + data[4] + pwd_socks) + ' ';
auth += 'chromesoul chromesoul\n';
client.socket.write('auth_ag ext_user none none\n', function(inf) {
client.socket.read(function(data) {
client.socket.write(auth, function(inf) {
client.socket.read(function(data) {
if (data === 'rep 002 -- cmd end') {
client.is_connected = true;
console.info('connected to the netsoul server');
} else {
console.error('authentication failure');
}
callback();
});
});
});
});
});
} else {
callback();
}
} else {
callback();
callback();
}
};
NsClient.prototype.disconnect = function(client) {
var msg = 'user_cmd msg_user exit\n';
client.socket.write(msg, function() {
client.socket.disconnect();
client.socket.disconnect();
});
};
NsClient.prototype.changeStatus = function(client) {
if (client.is_connected) {
status_msg = 'user_cmd state ';
status_msg += this.msgEncode(client.status) + ':';
status_msg += Math.round(new Date().getTime() / 1000) + '\n';
client.socket.write(status_msg, function(inf) {
$cs.ui.onUserStatusChange(client.status);
});
status_msg = 'user_cmd state ';
status_msg += this.msgEncode(client.status) + ':';
status_msg += Math.round(new Date().getTime() / 1000) + '\n';
client.socket.write(status_msg, function(inf) {
$cs.ui.onUserStatusChange(client.status);
});
} else {
$cs.ui.onUserStatusChange(client.status);
$cs.ui.onUserStatusChange(client.status);
}
};
NsClient.prototype.recv = function(client, data) {
for (var i = this.actions_enabled.length - 1; i >= 0; --i) {
if (this.action[this.actions_enabled[i]](client, data)) {
break;
}
if (this.action[this.actions_enabled[i]](client, data)) {
break;
}
}
};
@ -173,8 +173,8 @@ NsClient.prototype.addContact = function(client, lst) {
var msg = 'user_cmd watch_log_user {' + lst.join(',') + '}\n';
client.socket.write(msg, function(inf) {
var msg = 'user_cmd who {' + lst.join(',') + '}\n';
client.socket.write(msg, function(inf) {});
var msg = 'user_cmd who {' + lst.join(',') + '}\n';
client.socket.write(msg, function(inf) {});
});
};
@ -184,27 +184,27 @@ NsClient.prototype.rmContact = function(client, name) {
NsClient.prototype.replacePairs = function(str, pairs) {
for (var i in pairs) {
if (pairs.hasOwnProperty(i)) {
str = str.replace(pairs[i], i);
}
if (pairs.hasOwnProperty(i)) {
str = str.replace(pairs[i], i);
}
}
return str;
}
NsClient.prototype.msgDecode = function(msg) {
return this.replacePairs(unescape(msg), {
'@': /%40/g,
'*': /%2A/g,
'/': /%2F/g,
'+': /%2B/g
'@': /%40/g,
'*': /%2A/g,
'/': /%2F/g,
'+': /%2B/g
});
};
NsClient.prototype.msgEncode = function(msg) {
return this.replacePairs(escape(msg), {
'%40': /@/g,
'%2A': /\*/g,
'%2F': /\//g,
'%2B': /\+/g,
'%40': /@/g,
'%2A': /\*/g,
'%2F': /\//g,
'%2B': /\+/g,
});
};

View file

@ -21,8 +21,8 @@ var Nsui = function() {
Nsui.prototype.setReconnect = function(func) {
document.getElementById("user-status").addEventListener("click", function() {
func();
$cs.contacts.restore();
func();
$cs.contacts.restore();
}, false);
};
@ -30,13 +30,13 @@ Nsui.prototype.onUserStatusChange = function(new_status) {
var el = document.getElementById("user-status-img");
if (el !== null) {
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;
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;
}
};
@ -52,17 +52,17 @@ 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;
return tab.name !== element.name;
});
if (new_tab !== null) {
new_tab.show();
new_tab.show();
}
};
Nsui.prototype.hideAllTabs = function() {
for (i = this.tab_lst.length - 1; i >= 0; --i) {
this.tab_lst[i].hide();
this.tab_lst[i].hide();
}
};
@ -70,10 +70,10 @@ Nsui.prototype.getTabByName = function(name) {
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 ;
}
if (this.tab_lst[i].name === name) {
ret = this.tab_lst[i];
break ;
}
}
return ret;
@ -83,13 +83,13 @@ Nsui.prototype.getNextTab = function(current_name) {
var i, prev = null;
for (i = this.tab_lst.length - 1; i >= 0; --i) {
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];
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];
}
return prev;
@ -99,12 +99,12 @@ Nsui.prototype.addNewTab = function(tab_name) {
var tab = this.getTabByName(tab_name);
if (tab === null) {
tab = this.createTab(tab_name);
tab = this.createTab(tab_name);
}
if (this.tab_lst.length <= 1) {
tab.show();
tab.show();
} else if (!tab.isCurrent()) {
tab.setActive();
tab.setActive();
}
return tab;
@ -114,46 +114,46 @@ Nsui.prototype.sendNotification = function(image, title, message) {
var notif;
if (message.lenght > 32) {
message = message.substr(0, 32) + "…";
message = message.substr(0, 32) + "…";
}
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.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() {});
});
chrome.notifications.onClicked.addListener(function(id) {
window.focus();
chrome.notifications.clear(id, function() {});
});
} else {
notif = webkitNotifications.createNotification(
image,
title,
message
);
notif = webkitNotifications.createNotification(
image,
title,
message
);
notif.onclick = function() {
window.focus();
this.cancel();
};
notif.onclick = function() {
window.focus();
this.cancel();
};
notif.show();
setTimeout(function() {
notif.cancel();
}, 5000);
notif.show();
setTimeout(function() {
notif.cancel();
}, 5000);
}
}
@ -161,16 +161,16 @@ Nsui.prototype.addContentToTab = function(tab_name, content) {
var tab = null;
if ($cs.opts.get("enable_msg")) {
tab = this.addNewTab(tab_name);
tab.appendMessage(content);
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));
}
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));
}
}
};
@ -189,9 +189,9 @@ Nsui.prototype.formatMessage = function(msg) {
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">';
if (typeof msg.login !== "undefined" && msg.login !== null) {
fmt += '<span class="spk-oth">' + msg.login + ': </span>';
fmt += '<span class="spk-oth">' + msg.login + ': </span>';
} else {
fmt += '<span class="spk-me">' + document.getElementById('login').value + ': </span>';
fmt += '<span class="spk-me">' + document.getElementById('login').value + ': </span>';
}
fmt += this.sanitizeText(msg.message).replace(lnk_exp, '<a href="$1" target="_blank">$1</a>');
@ -205,59 +205,59 @@ Nsui.prototype.sanitizeText = function(str) {
};
Nsui.prototype.currentTabStyle = function() {
$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";
}
$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) {
var i, ctn_lst = ["config-pannel", "chat-pannel"];
if (part_id === "chat-pannel") {
this.currentTabStyle();
}
if (part_id === "chat-pannel") {
this.currentTabStyle();
}
for (i = ctn_lst.length - 1; i>= 0; --i) {
if (ctn_lst[i] === part_id) {
document.getElementById(ctn_lst[i]).style.display = "block";
} else {
document.getElementById(ctn_lst[i]).style.display = "none";
}
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") {
this.showContent(part_id);
this.showContent(part_id);
} else {
this.showContent(part_id_to);
this.showContent(part_id_to);
}
};
Nsui.prototype.init = function() {
window.onfocus = (function(elem) {
return function() {
elem.focus = true;
};
return function() {
elem.focus = true;
};
})(this);
window.onblur = (function(elem) {
return function() {
elem.focus = false;
};
return function() {
elem.focus = false;
};
})(this);
document.getElementById("settings-btn").addEventListener("click", (function(elem) {
return function() {
elem.switchContent("config-pannel", "chat-pannel");
};
return function() {
elem.switchContent("config-pannel", "chat-pannel");
};
})(this), false);
Tab.prototype.closeHandler = (function(elem) {
@ -268,7 +268,7 @@ Nsui.prototype.init = function() {
Tab.prototype.showHandler = (function(elem) {
return function() {
elem.hideAllTabs();
elem.hideAllTabs();
};
})(this);

View file

@ -25,36 +25,36 @@ var OptionsManager = function() {
OptionsManager.prototype.types = {
"get": {
"text": function(elem) {
return elem.value;
},
"password": function(elem) {
return elem.value;
},
"checkbox": function(elem) {
return elem.checked;
},
"number": function(elem) {
return elem.value;
}
"text": function(elem) {
return elem.value;
},
"password": function(elem) {
return elem.value;
},
"checkbox": function(elem) {
return elem.checked;
},
"number": function(elem) {
return elem.value;
}
},
"set": {
"text": function(elem, val) {
elem.value = val;
},
"password": function(elem, val) {
elem.value = val;
},
"checkbox": function(elem, val) {
if (!val) {
elem.removeAttribute('checked');
} else {
elem.setAttribute('checked');
}
},
"number": function(elem, val) {
elem.value = val;
}
"text": function(elem, val) {
elem.value = val;
},
"password": function(elem, val) {
elem.value = val;
},
"checkbox": function(elem, val) {
if (!val) {
elem.removeAttribute('checked');
} else {
elem.setAttribute('checked');
}
},
"number": function(elem, val) {
elem.value = val;
}
}
};
@ -62,7 +62,7 @@ OptionsManager.prototype.get = function(name) {
var ret = null;
if (typeof this.values[name] !== "undefined") {
ret = this.values[name];
ret = this.values[name];
}
return ret;
@ -76,47 +76,47 @@ OptionsManager.prototype.init = function() {
this.restore();
if (this.save !== null) {
this.save_btn.addEventListener("click", (function(elem) {
return function() {
elem.save();
};
})(this));
this.save_btn.addEventListener("click", (function(elem) {
return function() {
elem.save();
};
})(this));
}
};
OptionsManager.prototype.savePart = function(pass) {
var i = 0, data = {}, storage;
if (pass) {
storage = this.pass_storage;
if (pass) {
storage = this.pass_storage;
} else {
storage = this.storage;
storage = this.storage;
}
if (this.opts !== null) {
for (i = this.opts.length - 1; i >= 0; --i) {
if ((pass && (this.getElemType(this.opts[i]) === "password" || this.getElemType(this.opts[i]) === "text"))
|| (!pass && this.getElemType(this.opts[i]) !== "password" && this.getElemType(this.opts[i]) !== "text")) {
data[this.opts[i].id] = this.getElemValue(this.opts[i]);
this.values[this.opts[i].id] = data[this.opts[i].id];
}
}
storage.set(data, (function(elem) {
for (i = this.opts.length - 1; i >= 0; --i) {
if ((pass && (this.getElemType(this.opts[i]) === "password" || this.getElemType(this.opts[i]) === "text"))
|| (!pass && this.getElemType(this.opts[i]) !== "password" && this.getElemType(this.opts[i]) !== "text")) {
data[this.opts[i].id] = this.getElemValue(this.opts[i]);
this.values[this.opts[i].id] = data[this.opts[i].id];
}
}
storage.set(data, (function(elem) {
return function () {
if (elem !== null) {
if (elem !== null) {
elem.innerHTML = "Options saved.";
setTimeout(function() {
elem.innerHTML = "";
elem.innerHTML = "";
}, 3000);
}
}
};
})(this.status));
})(this.status));
}
};
OptionsManager.prototype.save = function() {
this.savePart(true);
this.savePart(false);
this.savePart(false);
};
OptionsManager.prototype.restore = function() {
@ -125,7 +125,7 @@ OptionsManager.prototype.restore = function() {
for (i in items) {
el = document.getElementById(i);
if (el !== null) {
elem.values[i] = items[i];
elem.values[i] = items[i];
elem.setElemValue(el, items[i]);
}
}
@ -144,7 +144,7 @@ OptionsManager.prototype.getElemValue = function(elem) {
var val = null, type = this.getElemType(elem);
if (typeof this.types.get[type] !== "undefined") {
val = this.types.get[type](elem);
val = this.types.get[type](elem);
}
return val;
@ -154,7 +154,7 @@ OptionsManager.prototype.setElemValue = function(elem, val) {
var type = elem.getAttribute('type');
if (typeof this.types.set[type] !== "undefined") {
this.types.set[type](elem, val);
this.types.set[type](elem, val);
}
};

74
lib/tab.nsui.js Executable file → Normal file
View file

@ -22,7 +22,7 @@ var Tab = function(name) {
this.buff_len = 1000;
this.history_index = 0;
this.history = [];
$cs.opts.restore();
$cs.opts.restore();
this.initListElement();
this.initBodyElement();
};
@ -69,7 +69,7 @@ Tab.prototype.initBodyElement = function() {
this.chat_log = document.createElement("p");
this.chat_log.classList.add("chat-log");
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
this.el_body = document.createElement("div");
this.el_body.classList.add("tab-body");
@ -81,35 +81,35 @@ Tab.prototype.initBodyElement = function() {
this.chat_input.classList.add("chat-input");
this.chat_input.setAttribute("type", "text");
this.chat_input.addEventListener("keyup", (function(elem) {
return function(event) {
var key_submit = 13, key_up = 38, key_down = 40;
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 (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 = "";
}
}
}
};
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 = "";
}
}
}
};
})(this), false);
chat_input_w.appendChild(this.chat_input);
@ -120,7 +120,7 @@ Tab.prototype.initBodyElement = function() {
Tab.prototype.close = function() {
if (typeof this.closeHandler !== "undefined") {
this.closeHandler();
this.closeHandler();
}
this.wr_body.removeChild(this.el_body);
this.wr_lst.removeChild(this.el_lst);
@ -132,16 +132,16 @@ Tab.prototype.hide = function() {
};
Tab.prototype.show = function() {
this.options.restore();
this.options.restore();
if (typeof this.showHandler !== "undefined") {
this.showHandler();
this.showHandler();
}
this.el_lst.classList.remove("tab-active");
this.el_lst.classList.add("tab-current");
this.el_body.style.display = "block";
$cs.opts.restore()
$cs.opts.restore()
this.chat_log.scrollTop = 42000;
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
this.chat_log.style.fontSize = $cs.opts.values["chat-size"] + "px";
this.chat_input.focus();
};
@ -155,10 +155,10 @@ Tab.prototype.setActive = function() {
Tab.prototype.flushText = function() {
while (this.chat_log.children.length > this.buff_len) {
this.chat_log.removeChild(this.chat_log.children[0]);
this.chat_log.removeChild(this.chat_log.children[0]);
}
while (this.history.length > this.buff_len) {
this.history.shift();
this.history.shift();
}
};
@ -167,7 +167,7 @@ Tab.prototype.appendMessage = function(msg) {
this.chat_log.innerHTML += '<div class="chat-message">' + $cs.ui.formatMessage(msg) + '</div>';
if (!(typeof msg.login !== "undefined" && msg.login !== null)) {
this.history.push(msg.message);
this.history.push(msg.message);
}
this.flushText();
this.chat_log.scrollTop = 42 * this.buff_len;

View file

@ -22,17 +22,17 @@ var TxtSocket = function() {
TxtSocket.prototype.connect = function(host, port, callback) {
chrome.socket.create('tcp', {}, (function(elem) {
return function(inf) {
elem.socket_id = inf.socketId;
chrome.socket.connect(elem.socket_id, host, port, callback);
};
return function(inf) {
elem.socket_id = inf.socketId;
chrome.socket.connect(elem.socket_id, host, port, callback);
};
})(this));
};
TxtSocket.prototype.disconnect = function() {
if (this.socket_id !== null) {
chrome.socket.disconnect(this.socket_id);
chrome.socket.destroy(this.socket_id);
chrome.socket.disconnect(this.socket_id);
chrome.socket.destroy(this.socket_id);
}
};
@ -40,32 +40,32 @@ TxtSocket.prototype.read = function(callback) {
var tmp = '', offset = this.buffer.indexOf("\n");
if (offset === -1) {
chrome.socket.read(this.socket_id, (function(elem) {
return function(rd_inf) {
if (rd_inf.resultCode > 0) {
elem.buffer += elem.ab2str(rd_inf.data);
elem.read(callback);
} else {
elem.throwError({code: rd_inf.resultCode});
}
};
})(this));
chrome.socket.read(this.socket_id, (function(elem) {
return function(rd_inf) {
if (rd_inf.resultCode > 0) {
elem.buffer += elem.ab2str(rd_inf.data);
elem.read(callback);
} else {
elem.throwError({code: rd_inf.resultCode});
}
};
})(this));
} else {
tmp = this.buffer.substr(0, offset);
this.buffer = this.buffer.substr(offset + 1);
callback(tmp);
tmp = this.buffer.substr(0, offset);
this.buffer = this.buffer.substr(offset + 1);
callback(tmp);
}
};
TxtSocket.prototype.write = function(str, callback) {
chrome.socket.write(this.socket_id, this.str2ab(str), (function(elem) {
return function(w_inf) {
if (w_inf.bytesWritten >= 0) {
callback(w_inf);
} else {
elem.throwError({code: w_inf.bytesWritten});
}
};
return function(w_inf) {
if (w_inf.bytesWritten >= 0) {
callback(w_inf);
} else {
elem.throwError({code: w_inf.bytesWritten});
}
};
})(this));
};
@ -83,7 +83,7 @@ TxtSocket.prototype.str2ab = function(str) {
var i = 0, buff = new ArrayBuffer(str.length), buff_v = new Uint8Array(buff);
for (i = str.length - 1; i >= 0; --i) {
buff_v[i] = str.charCodeAt(i);
buff_v[i] = str.charCodeAt(i);
}
return buff;

View file

@ -6,18 +6,18 @@
"offline_enabled": false,
"description": "Netsoul client for Google Chrome.",
"icons": {
"16": "icon_16.png",
"128": "icon_128.png"
"16": "icon_16.png",
"128": "icon_128.png"
},
"app": {
"background": {
"scripts": ["background.js"]
}
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
"http://cdn.local.epitech.net/userprofil/profilview/",
"storage",
"notifications",
{"socket": ["tcp-connect:ns-server.epita.fr:4242"]}
"http://cdn.local.epitech.net/userprofil/profilview/",
"storage",
"notifications",
{"socket": ["tcp-connect:ns-server.epita.fr:4242"]}
]
}