update livechan to be aware of convos
This commit is contained in:
parent
1c632666ab
commit
72947dbbcc
@ -431,8 +431,6 @@ function buildChat(chat, domElem, channel) {
|
|||||||
input.appendChild(submit);
|
input.appendChild(submit);
|
||||||
domElem.appendChild(output);
|
domElem.appendChild(output);
|
||||||
domElem.appendChild(input);
|
domElem.appendChild(input);
|
||||||
// inject convobar
|
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
convobar : convobar,
|
convobar : convobar,
|
||||||
@ -564,6 +562,8 @@ var messageRules = [
|
|||||||
out.className = 'livechan_internallink';
|
out.className = 'livechan_internallink';
|
||||||
out.addEventListener('click', function() {
|
out.addEventListener('click', function() {
|
||||||
var selected = document.getElementById('livechan_chat_'+m[1]);
|
var selected = document.getElementById('livechan_chat_'+m[1]);
|
||||||
|
console.log(selected.convo);
|
||||||
|
selected.select();
|
||||||
selected.scrollIntoView(true);
|
selected.scrollIntoView(true);
|
||||||
// TODO: highlight
|
// TODO: highlight
|
||||||
});
|
});
|
||||||
@ -634,7 +634,8 @@ function buildConvoBar(domElem) {
|
|||||||
|
|
||||||
var convo = document.createElement('input');
|
var convo = document.createElement('input');
|
||||||
convo.className = 'livechan_chat_input_convo';
|
convo.className = 'livechan_chat_input_convo';
|
||||||
convo.setAttribute("value", "General");
|
convo.setAttribute("value", "");
|
||||||
|
convo.contentEditable = false;
|
||||||
elem.appendChild(convo);
|
elem.appendChild(convo);
|
||||||
domElem.appendChild(elem);
|
domElem.appendChild(elem);
|
||||||
return {
|
return {
|
||||||
@ -654,42 +655,37 @@ function ConvoBar(chat, domElem) {
|
|||||||
this.elem = convo.input;
|
this.elem = convo.input;
|
||||||
this.widget = convo.widget;
|
this.widget = convo.widget;
|
||||||
this.active = null;
|
this.active = null;
|
||||||
this.convoPosts = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* @brief update the convo bar
|
/* @brief update the convo bar
|
||||||
* @param convoId the name of this covnorsattion
|
* @param convoId the name of this covnorsattion
|
||||||
*/
|
*/
|
||||||
ConvoBar.prototype.update = function(convo, chat) {
|
ConvoBar.prototype.update = function(msgid, data) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if ( self.holder[convo] === undefined ) {
|
if ( self.holder[msgid] === undefined ) {
|
||||||
// new convo
|
// new convo
|
||||||
// register convo
|
// register convo
|
||||||
self.registerConvo(convo);
|
self.registerConvo(msgid, data);
|
||||||
}
|
}
|
||||||
// bump existing convo
|
// bump existing convo
|
||||||
var convoId = self.holder[convo];
|
var convoId = self.holder[msgid].id;
|
||||||
var convoElem = document.getElementById("livechan_convobar_item_"+convoId);
|
var convoElem = document.getElementById("livechan_convobar_item_"+convoId);
|
||||||
var convoParent = convoElem.parentElement;
|
var convoParent = convoElem.parentElement;
|
||||||
if ( convoParent.children.length > 1 ) {
|
if ( convoParent.children.length > 1 ) {
|
||||||
convoParent.removeChild(convoElem);
|
convoParent.removeChild(convoElem);
|
||||||
convoParent.insertBefore(convoElem, convoParent.childNodes[0]);
|
convoParent.insertBefore(convoElem, convoParent.childNodes[0]);
|
||||||
}
|
}
|
||||||
// begin tracking a convo's posts if not already
|
|
||||||
if ( self.convoPosts[convo] === undefined ) {
|
|
||||||
self.convoPosts[convo] = [];
|
|
||||||
}
|
|
||||||
// add post to convo
|
// add post to convo
|
||||||
self.convoPosts[convo].push(chat);
|
self.holder[msgid].posts.push(data);
|
||||||
// do roll over
|
// do roll over
|
||||||
var scrollback = self.parent.options.scrollback || 30;
|
var scrollback = self.parent.options.scrollback || 30;
|
||||||
while(self.convoPosts[convo].length > scrollback) {
|
while(self.holder[msgid].posts.length > scrollback) {
|
||||||
// remove oldest from convo tracker
|
// remove oldest from convo tracker
|
||||||
var child_data = self.convoPosts[convo].shift();
|
var child_data = self.holder[msgid].posts.shift();
|
||||||
//var child = document.getElementById("livechan_chat_"+child_data.Count);
|
var child = document.getElementById("livechan_chat_"+child_data.ShortHash);
|
||||||
// remove element from main chat element
|
// remove element from main chat element
|
||||||
//self.parent.chatElems.output.removeChild(child.parentNode.parentElement);
|
self.parent.chatElems.output.removeChild(child.parentNode.parentElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -697,27 +693,37 @@ ConvoBar.prototype.update = function(convo, chat) {
|
|||||||
|
|
||||||
|
|
||||||
/** @brief register a new convorsation
|
/** @brief register a new convorsation
|
||||||
* @param convo the name of the convo
|
|
||||||
*/
|
*/
|
||||||
ConvoBar.prototype.registerConvo = function(convo) {
|
ConvoBar.prototype.registerConvo = function(msgid, data) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var max_id = 0;
|
var max_id = 0;
|
||||||
// get the highest convo id
|
// get the highest convo id
|
||||||
for ( c in self.holder ) {
|
for ( c in self.holder ) {
|
||||||
var id = self.holder[c];
|
var id = self.holder[c].id;
|
||||||
if (id > max_id ) {
|
if (id > max_id ) {
|
||||||
max_id = id
|
max_id = id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// put it in the holder
|
|
||||||
self.holder[convo] = max_id + 1;
|
self.holder[msgid] = {
|
||||||
|
subject: data.PostSubject,
|
||||||
|
msgid: data.Message_id,
|
||||||
|
id: max_id + 1,
|
||||||
|
posts: [],
|
||||||
|
select: function() {
|
||||||
|
console.log("selected convo "+msgid);
|
||||||
|
if ( self.active !== msgid ) {
|
||||||
|
self.show(msgid);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
// make a new entry in the convo bar
|
// make a new entry in the convo bar
|
||||||
var elem = document.createElement("div");
|
var elem = document.createElement("div");
|
||||||
elem.className = "livechan_convobar_item";
|
elem.className = "livechan_convobar_item";
|
||||||
elem.setAttribute("id", "livechan_convobar_item_"+ self.holder[convo]);
|
elem.setAttribute("id", "livechan_convobar_item_"+ self.holder[msgid].id);
|
||||||
var link = document.createElement("span");
|
var link = document.createElement("span");
|
||||||
elem.addEventListener("click", function() { self.show(convo); });
|
elem.addEventListener("click", function() { self.show(msgid); });
|
||||||
link.appendChild(document.createTextNode(convo));
|
link.appendChild(document.createTextNode(data.PostSubject));
|
||||||
elem.appendChild(link);
|
elem.appendChild(link);
|
||||||
// prepend the element
|
// prepend the element
|
||||||
if (self.widget.children.length > 0 ) {
|
if (self.widget.children.length > 0 ) {
|
||||||
@ -727,39 +733,9 @@ ConvoBar.prototype.registerConvo = function(convo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @brief load the converstation list from server
|
|
||||||
*/
|
|
||||||
ConvoBar.prototype.load = function() {
|
|
||||||
var self = this;
|
|
||||||
var prefix = self.parent.options.prefix || "/";
|
|
||||||
var ajax = new XMLHttpRequest();
|
|
||||||
// prepare ajax
|
|
||||||
ajax.onreadystatechange = function() {
|
|
||||||
if (ajax.status == 200 && ajax.readyState == XMLHttpRequest.DONE ) {
|
|
||||||
// clear state
|
|
||||||
self.holder = {};
|
|
||||||
// clear widget
|
|
||||||
while(self.widget.firstChild) {
|
|
||||||
self.widget.removeChild(self.widget.firstChild);
|
|
||||||
}
|
|
||||||
// register all convos
|
|
||||||
var convos = json.parse(ajax.responseText);
|
|
||||||
for ( var idx = 0; idx < convos.length ; idx ++ ) {
|
|
||||||
self.registerConvo(convos[idx]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// send ajax
|
|
||||||
ajax.open(prefix+"convos/"+self.parent.name);
|
|
||||||
ajax.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @brief Only Show chats from a convorsation
|
/* @brief Only Show chats from a convorsation
|
||||||
* @param convo the name of the convorsation or null for all
|
|
||||||
*/
|
*/
|
||||||
ConvoBar.prototype.show = function(convo) {
|
ConvoBar.prototype.show = function(msgid) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var sheet = null;
|
var sheet = null;
|
||||||
for(var idx = 0; idx < document.styleSheets.length; idx++ ) {
|
for(var idx = 0; idx < document.styleSheets.length; idx++ ) {
|
||||||
@ -780,7 +756,7 @@ ConvoBar.prototype.show = function(convo) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( convo === self.active) {
|
if (msgid === self.active) {
|
||||||
// this is resetting the view
|
// this is resetting the view
|
||||||
if (sheet.insertRule) { // firefox
|
if (sheet.insertRule) { // firefox
|
||||||
sheet.insertRule(".livechan_chat_output_chat { display: block; }", 0);
|
sheet.insertRule(".livechan_chat_output_chat { display: block; }", 0);
|
||||||
@ -788,19 +764,21 @@ ConvoBar.prototype.show = function(convo) {
|
|||||||
sheet.addRule(".livechan_chat_output_chat", "display: block");
|
sheet.addRule(".livechan_chat_output_chat", "display: block");
|
||||||
}
|
}
|
||||||
// unset active highlight
|
// unset active highlight
|
||||||
var convoId = self.holder[self.active];
|
var convoId = self.holder[self.active].id;
|
||||||
var itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
var itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
||||||
itemElem.style.background = null;
|
itemElem.style.background = null;
|
||||||
self.active = null;
|
self.active = null;
|
||||||
|
self.elem.value = "";
|
||||||
} else {
|
} else {
|
||||||
// unset active highlight if it's there
|
// unset active highlight if it's there
|
||||||
if (self.active) {
|
if (self.active) {
|
||||||
var convoId = self.holder[self.active];
|
var convoId = self.holder[self.active].id;
|
||||||
var itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
var itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
||||||
itemElem.style.background = null;
|
itemElem.style.background = null;
|
||||||
|
self.elem.value = "";
|
||||||
}
|
}
|
||||||
// set active highlight to new element
|
// set active highlight to new element
|
||||||
convoId = self.holder[convo];
|
convoId = self.holder[msgid].id;
|
||||||
itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
itemElem = document.getElementById("livechan_convobar_item_"+convoId);
|
||||||
itemElem.style.background = "red";
|
itemElem.style.background = "red";
|
||||||
var elemClass = ".livechan_chat_convo_" + convoId;
|
var elemClass = ".livechan_chat_convo_" + convoId;
|
||||||
@ -812,10 +790,15 @@ ConvoBar.prototype.show = function(convo) {
|
|||||||
sheet.addRule(elemClass, "display: block");
|
sheet.addRule(elemClass, "display: block");
|
||||||
}
|
}
|
||||||
// this convo is now active
|
// this convo is now active
|
||||||
self.active = convo;
|
self.active = msgid;
|
||||||
}
|
}
|
||||||
// set the convobar value
|
// set the convobar value
|
||||||
self.elem.value = self.active || "General";
|
var a = self.holder[self.active];
|
||||||
|
if(a)
|
||||||
|
self.elem.value = a.msgid;
|
||||||
|
else {
|
||||||
|
self.elem.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
// scroll view
|
// scroll view
|
||||||
self.parent.scroll();
|
self.parent.scroll();
|
||||||
@ -962,18 +945,21 @@ Chat.prototype.sendInput = function(event) {
|
|||||||
if (inputElem.submit.disabled == false) {
|
if (inputElem.submit.disabled == false) {
|
||||||
var message = inputElem.message.value;
|
var message = inputElem.message.value;
|
||||||
var name = inputElem.name.value;
|
var name = inputElem.name.value;
|
||||||
var convo = inputElem.convo.value;
|
var convo = self.chatElems.convobar.active;
|
||||||
self.readImage(inputElem.file, function(fdata, fname, ftype) {
|
self.readImage(inputElem.file, function(fdata, fname, ftype) {
|
||||||
if (fdata) {
|
if (fdata) {
|
||||||
connection.send({Type: "post", Post: {
|
connection.send({Type: "post", Post: {
|
||||||
message: message,
|
message: message,
|
||||||
name: name,
|
name: name,
|
||||||
|
reference: convo,
|
||||||
files: [{name: fname, data: fdata, type: ftype}],
|
files: [{name: fname, data: fdata, type: ftype}],
|
||||||
}});
|
}});
|
||||||
} else {
|
} else {
|
||||||
connection.send({Type: "post", Post: {
|
connection.send({Type: "post", Post: {
|
||||||
message: message,
|
message: message,
|
||||||
name: name, }});
|
reference: convo,
|
||||||
|
name: name,
|
||||||
|
}});
|
||||||
}
|
}
|
||||||
inputElem.file.value = "";
|
inputElem.file.value = "";
|
||||||
inputElem.message.value = '';
|
inputElem.message.value = '';
|
||||||
@ -1162,14 +1148,16 @@ Chat.prototype.generateChat = function(data) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var chat = document.createElement('div');
|
var chat = document.createElement('div');
|
||||||
conv = "General";
|
self.chatElems.convobar.update(data.Parent, data);
|
||||||
self.chatElems.convobar.update(conv, data);
|
var convo = self.chatElems.convobar.holder[data.Parent];
|
||||||
var convo = self.chatElems.convobar.holder[conv];
|
chat.select = function() {
|
||||||
chat.className = 'livechan_chat_output_chat livechan_chat_convo_' + convo;
|
console.log("selecting...");
|
||||||
chat.className = 'livechan_chat_output_chat';
|
convo.select();
|
||||||
|
}
|
||||||
|
chat.className = 'livechan_chat_output_chat livechan_chat_convo_' + convo.id;
|
||||||
var convoLabel = document.createElement('span');
|
var convoLabel = document.createElement('span');
|
||||||
convoLabel.className = 'livechan_convo_label';
|
convoLabel.className = 'livechan_convo_label';
|
||||||
convoLabel.appendChild(document.createTextNode(conv));
|
convoLabel.appendChild(document.createTextNode(convo.subject));
|
||||||
|
|
||||||
var header = document.createElement('div');
|
var header = document.createElement('div');
|
||||||
header.className = 'livechan_chat_output_header';
|
header.className = 'livechan_chat_output_header';
|
||||||
@ -1273,6 +1261,7 @@ Chat.prototype.generateChat = function(data) {
|
|||||||
count.addEventListener('click', function() {
|
count.addEventListener('click', function() {
|
||||||
self.chatElems.input.message.value += '>>'+h+'\n';
|
self.chatElems.input.message.value += '>>'+h+'\n';
|
||||||
self.chatElems.input.message.focus();
|
self.chatElems.input.message.focus();
|
||||||
|
chat.select();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user