2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* This file contains the weechat models and various
|
|
|
|
* helper methods to work with them.
|
|
|
|
*/
|
2014-08-24 18:53:55 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
var models = angular.module('weechatModels', []);
|
|
|
|
|
2013-10-26 15:00:34 +02:00
|
|
|
models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) {
|
2015-03-21 14:08:09 +01:00
|
|
|
// WeeChat version
|
|
|
|
this.version = null;
|
|
|
|
|
2015-12-17 17:45:34 +01:00
|
|
|
// WeeChat configuration values
|
|
|
|
this.wconfig = {};
|
|
|
|
|
2015-04-22 15:31:12 +02:00
|
|
|
// Save outgoing queries
|
|
|
|
this.outgoingQueries = [];
|
|
|
|
|
2014-11-19 14:25:31 +01:00
|
|
|
var parseRichText = function(text) {
|
|
|
|
var textElements = weeChat.Protocol.rawText2Rich(text),
|
|
|
|
typeToClassPrefixFg = {
|
|
|
|
'option': 'cof-',
|
|
|
|
'weechat': 'cwf-',
|
|
|
|
'ext': 'cef-'
|
|
|
|
},
|
|
|
|
typeToClassPrefixBg = {
|
|
|
|
'option': 'cob-',
|
|
|
|
'weechat': 'cwb-',
|
|
|
|
'ext': 'ceb-'
|
|
|
|
};
|
|
|
|
|
|
|
|
textElements.forEach(function(textEl) {
|
|
|
|
textEl.classes = [];
|
|
|
|
|
|
|
|
// foreground color
|
|
|
|
var prefix = typeToClassPrefixFg[textEl.fgColor.type];
|
|
|
|
textEl.classes.push(prefix + textEl.fgColor.name);
|
|
|
|
|
|
|
|
// background color
|
|
|
|
prefix = typeToClassPrefixBg[textEl.bgColor.type];
|
|
|
|
textEl.classes.push(prefix + textEl.bgColor.name);
|
|
|
|
|
|
|
|
// attributes
|
|
|
|
if (textEl.attrs.name !== null) {
|
|
|
|
textEl.classes.push('coa-' + textEl.attrs.name);
|
|
|
|
}
|
|
|
|
var attr, val;
|
|
|
|
for (attr in textEl.attrs.override) {
|
|
|
|
val = textEl.attrs.override[attr];
|
|
|
|
if (val) {
|
|
|
|
textEl.classes.push('a-' + attr);
|
|
|
|
} else {
|
|
|
|
textEl.classes.push('a-no-' + attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return textElements;
|
|
|
|
};
|
|
|
|
this.parseRichText = parseRichText;
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* Buffer class
|
|
|
|
*/
|
|
|
|
this.Buffer = function(message) {
|
|
|
|
// weechat properties
|
2013-12-17 21:35:36 +01:00
|
|
|
var fullName = message.full_name;
|
|
|
|
var shortName = message.short_name;
|
2015-07-10 05:52:12 +02:00
|
|
|
var hidden = message.hidden;
|
2014-11-20 21:12:18 +01:00
|
|
|
// If it's a channel, trim away the prefix (#, &, or +). If that is empty and the buffer
|
|
|
|
// has a short name, use a space (because the prefix will be displayed separately, and we don't want
|
|
|
|
// prefix + fullname, which would happen otherwise). Else, use null so that full_name is used
|
|
|
|
var trimmedName = shortName.replace(/^[#&+]/, '') || (shortName ? ' ' : null);
|
2014-11-20 12:57:19 +01:00
|
|
|
// get channel identifier
|
|
|
|
var prefix = ['#', '&', '+'].indexOf(shortName.charAt(0)) >= 0 ? shortName.charAt(0) : '';
|
2014-11-19 14:25:31 +01:00
|
|
|
var title = parseRichText(message.title);
|
2013-12-17 21:35:36 +01:00
|
|
|
var number = message.number;
|
|
|
|
var pointer = message.pointers[0];
|
|
|
|
var notify = 3; // Default 3 == message
|
|
|
|
var lines = [];
|
2014-02-10 19:43:52 +01:00
|
|
|
var requestedLines = 0;
|
2014-05-06 18:38:20 +02:00
|
|
|
var allLinesFetched = false;
|
2013-12-17 21:35:36 +01:00
|
|
|
var nicklist = {};
|
|
|
|
var history = [];
|
2013-12-17 20:37:45 +01:00
|
|
|
var historyPos = 0;
|
2013-12-17 21:35:36 +01:00
|
|
|
var active = false;
|
|
|
|
var notification = 0;
|
|
|
|
var unread = 0;
|
|
|
|
var lastSeen = -1;
|
2015-11-28 20:55:24 +01:00
|
|
|
// There are two kinds of types: bufferType (free vs formatted) and
|
|
|
|
// the kind of type that distinguishes queries from channels etc
|
|
|
|
var bufferType = message.type;
|
2014-05-07 19:58:59 +02:00
|
|
|
var type = message.local_variables.type;
|
|
|
|
var indent = (['channel', 'private'].indexOf(type) >= 0);
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2015-12-05 13:58:02 +01:00
|
|
|
var plugin = message.local_variables.plugin;
|
|
|
|
var server = message.local_variables.server;
|
|
|
|
// Server buffers have this "irc.server.freenode" naming schema, which
|
|
|
|
// messes the sorting up. We need it to be "irc.freenode" instead.
|
|
|
|
var serverSortKey = plugin + "." + server +
|
|
|
|
(type === "server" ? "" : ("." + shortName));
|
|
|
|
// Lowercase it so alt+up/down traverses buffers in the same order
|
|
|
|
// angular's sortBy directive puts them in
|
|
|
|
serverSortKey = serverSortKey.toLowerCase();
|
|
|
|
|
2013-10-18 15:58:26 +02:00
|
|
|
// Buffer opened message does not include notify level
|
2014-02-08 13:49:21 +01:00
|
|
|
if (message.notify !== undefined) {
|
2013-12-17 21:29:05 +01:00
|
|
|
notify = message.notify;
|
2013-10-18 15:58:26 +02:00
|
|
|
}
|
|
|
|
|
2014-11-19 14:25:31 +01:00
|
|
|
var rtitle = "";
|
|
|
|
for (var i = 0; i < title.length; ++i) {
|
|
|
|
rtitle += title[i].text;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* Adds a line to this buffer
|
2013-12-17 21:30:22 +01:00
|
|
|
*
|
2013-10-09 17:53:25 +02:00
|
|
|
* @param line the BufferLine object
|
|
|
|
* @return undefined
|
|
|
|
*/
|
|
|
|
var addLine = function(line) {
|
|
|
|
lines.push(line);
|
2014-04-25 15:36:31 +02:00
|
|
|
updateNickSpeak(line);
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-15 14:59:06 +02:00
|
|
|
|
2013-10-26 22:19:13 +02:00
|
|
|
/*
|
|
|
|
* Adds a nick to nicklist
|
|
|
|
*/
|
2013-10-27 00:26:17 +02:00
|
|
|
var addNick = function(group, nick) {
|
2014-03-10 18:01:41 +01:00
|
|
|
if (nicklistRequested()) {
|
2014-04-25 15:36:31 +02:00
|
|
|
nick.spokeAt = Date.now();
|
2014-03-10 18:01:41 +01:00
|
|
|
nicklist[group].nicks.push(nick);
|
|
|
|
}
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-27 00:26:17 +02:00
|
|
|
/*
|
|
|
|
* Deletes a nick from nicklist
|
|
|
|
*/
|
|
|
|
var delNick = function(group, nick) {
|
2013-12-17 21:48:43 +01:00
|
|
|
group = nicklist[group];
|
2014-03-10 18:01:41 +01:00
|
|
|
if (group === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-08 14:20:33 +01:00
|
|
|
group.nicks = _.filter(group.nicks, function(n) { return n.name !== nick.name;});
|
2013-10-27 00:26:17 +02:00
|
|
|
/*
|
2014-02-08 13:49:21 +01:00
|
|
|
for (i in group.nicks) {
|
|
|
|
if (group.nicks[i].name == nick.name) {
|
2013-10-27 00:26:17 +02:00
|
|
|
delete group.nicks[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-27 00:26:17 +02:00
|
|
|
/*
|
|
|
|
* Updates a nick in nicklist
|
|
|
|
*/
|
|
|
|
var updateNick = function(group, nick) {
|
2013-12-17 21:48:43 +01:00
|
|
|
group = nicklist[group];
|
2015-03-07 13:00:05 +01:00
|
|
|
if (group === undefined) {
|
|
|
|
// We are getting nicklist events for a buffer where not yet
|
|
|
|
// have populated the nicklist, so there will be nothing to
|
|
|
|
// update. Just ignore the event.
|
|
|
|
return;
|
|
|
|
}
|
2013-12-17 21:55:41 +01:00
|
|
|
for(var i in group.nicks) {
|
2014-02-08 14:20:33 +01:00
|
|
|
if (group.nicks[i].name === nick.name) {
|
2013-10-27 00:26:17 +02:00
|
|
|
group.nicks[i] = nick;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-27 00:26:17 +02:00
|
|
|
|
2013-10-27 10:48:20 +01:00
|
|
|
/*
|
2014-04-25 15:36:31 +02:00
|
|
|
* Update a nick with a fresh timestamp so tab completion
|
|
|
|
* can use time to complete recent speakers
|
|
|
|
*/
|
|
|
|
var updateNickSpeak = function(line) {
|
|
|
|
// Try to find nick from prefix
|
|
|
|
var prefix = line.prefix;
|
2014-05-03 15:12:55 +02:00
|
|
|
if (prefix.length === 0) {
|
|
|
|
// some scripts produce lines without a prefix
|
|
|
|
return;
|
|
|
|
}
|
2014-04-25 15:36:31 +02:00
|
|
|
var nick = prefix[prefix.length - 1].text;
|
|
|
|
// Action / me, find the nick as the first word of the message
|
|
|
|
if (nick === " *") {
|
|
|
|
var match = line.text.match(/^(.+)\s/);
|
|
|
|
if (match) {
|
|
|
|
nick = match[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nick === "" || nick === "=!=") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.each(nicklist, function(nickGroup) {
|
|
|
|
_.each(nickGroup.nicks, function(nickObj) {
|
|
|
|
if (nickObj.name === nick) {
|
|
|
|
// Use the order the line arrive in for simplicity
|
|
|
|
// instead of using weechat's own timestamp
|
|
|
|
nickObj.spokeAt = Date.now();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a flat nicklist sorted by speaker time. This function is
|
|
|
|
* called for every tab key press by the user.
|
2013-12-17 21:30:22 +01:00
|
|
|
*
|
2013-10-27 10:48:20 +01:00
|
|
|
*/
|
2014-04-25 15:36:31 +02:00
|
|
|
var getNicklistByTime = function() {
|
2013-10-27 10:48:20 +01:00
|
|
|
var newlist = [];
|
|
|
|
_.each(nicklist, function(nickGroup) {
|
|
|
|
_.each(nickGroup.nicks, function(nickObj) {
|
2014-04-25 15:36:31 +02:00
|
|
|
newlist.push(nickObj);
|
2013-10-27 10:48:20 +01:00
|
|
|
});
|
2013-12-17 21:30:22 +01:00
|
|
|
});
|
2014-04-25 15:36:31 +02:00
|
|
|
|
2013-10-27 10:48:20 +01:00
|
|
|
newlist.sort(function(a, b) {
|
2014-04-25 15:36:31 +02:00
|
|
|
return a.spokeAt < b.spokeAt;
|
|
|
|
});
|
|
|
|
|
|
|
|
return newlist;
|
|
|
|
};
|
|
|
|
|
2013-12-17 20:37:45 +01:00
|
|
|
var addToHistory = function(line) {
|
|
|
|
var result = "";
|
2014-02-08 14:20:33 +01:00
|
|
|
if (historyPos !== history.length) {
|
2013-12-17 20:37:45 +01:00
|
|
|
// Pop cached line from history. Occurs if we submit something from history
|
|
|
|
result = history.pop();
|
|
|
|
}
|
|
|
|
history.push(line);
|
|
|
|
historyPos = history.length; // Go to end of history
|
|
|
|
return result;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-12-17 20:37:45 +01:00
|
|
|
|
|
|
|
var getHistoryUp = function(currentLine) {
|
|
|
|
if (historyPos >= history.length) {
|
|
|
|
// cache current line in history
|
|
|
|
history.push(currentLine);
|
|
|
|
}
|
|
|
|
if (historyPos <= 0 || historyPos >= history.length) {
|
|
|
|
// Can't go up from first message or from out-of-bounds index
|
|
|
|
return currentLine;
|
|
|
|
} else {
|
|
|
|
// Go up in history
|
|
|
|
historyPos--;
|
|
|
|
var line = history[historyPos];
|
|
|
|
return line;
|
|
|
|
}
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-12-17 20:37:45 +01:00
|
|
|
|
|
|
|
var getHistoryDown = function(currentLine) {
|
2014-07-21 16:35:53 +02:00
|
|
|
if (historyPos === history.length) {
|
|
|
|
// stash on history like weechat does
|
|
|
|
if (currentLine !== undefined && currentLine !== '') {
|
|
|
|
history.push(currentLine);
|
|
|
|
historyPos++;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
} else if (historyPos < 0 || historyPos > history.length) {
|
2013-12-17 20:37:45 +01:00
|
|
|
// Can't go down from out of bounds or last message
|
|
|
|
return currentLine;
|
|
|
|
} else {
|
|
|
|
historyPos++;
|
|
|
|
|
|
|
|
if (history.length > 0 && historyPos == (history.length-1)) {
|
|
|
|
// return cached line and remove from cache
|
|
|
|
return history.pop();
|
|
|
|
} else {
|
|
|
|
// Go down in history
|
|
|
|
return history[historyPos];
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-12-17 20:37:45 +01:00
|
|
|
|
2014-03-08 22:26:15 +01:00
|
|
|
|
|
|
|
// Check if the nicklist is empty, i.e., no nicks present
|
|
|
|
// This checks for the presence of people, not whether a
|
|
|
|
// request for the nicklist has been made
|
2014-03-07 18:52:32 +01:00
|
|
|
var isNicklistEmpty = function() {
|
|
|
|
for (var obj in nicklist) {
|
2014-03-08 22:26:15 +01:00
|
|
|
if (obj !== 'root') {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-07 18:52:32 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-03-08 22:26:15 +01:00
|
|
|
var nicklistRequested = function() {
|
|
|
|
// If the nicklist has been requested but is empty, it
|
|
|
|
// still has a 'root' property. Check for its existence.
|
|
|
|
return nicklist.hasOwnProperty('root');
|
|
|
|
};
|
|
|
|
|
2014-05-06 18:38:20 +02:00
|
|
|
/* Clear all our buffer lines */
|
|
|
|
var clear = function() {
|
|
|
|
while(lines.length > 0) {
|
|
|
|
lines.pop();
|
|
|
|
}
|
|
|
|
requestedLines = 0;
|
|
|
|
};
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
return {
|
|
|
|
id: pointer,
|
|
|
|
fullName: fullName,
|
|
|
|
shortName: shortName,
|
2015-07-10 05:52:12 +02:00
|
|
|
hidden: hidden,
|
2014-07-30 17:29:55 +02:00
|
|
|
trimmedName: trimmedName,
|
2014-11-20 12:57:19 +01:00
|
|
|
prefix: prefix,
|
2013-10-09 17:53:25 +02:00
|
|
|
number: number,
|
|
|
|
title: title,
|
2014-11-19 14:25:31 +01:00
|
|
|
rtitle: rtitle,
|
2013-10-09 17:53:25 +02:00
|
|
|
lines: lines,
|
2014-05-06 18:38:20 +02:00
|
|
|
clear: clear,
|
2014-02-10 19:43:52 +01:00
|
|
|
requestedLines: requestedLines,
|
2013-10-12 20:44:40 +02:00
|
|
|
addLine: addLine,
|
|
|
|
lastSeen: lastSeen,
|
2013-10-15 14:59:06 +02:00
|
|
|
unread: unread,
|
|
|
|
notification: notification,
|
2013-10-16 00:32:56 +02:00
|
|
|
notify: notify,
|
2013-10-27 00:26:17 +02:00
|
|
|
nicklist: nicklist,
|
|
|
|
addNick: addNick,
|
|
|
|
delNick: delNick,
|
2013-10-27 10:48:20 +01:00
|
|
|
updateNick: updateNick,
|
2014-04-25 15:36:31 +02:00
|
|
|
getNicklistByTime: getNicklistByTime,
|
2013-12-08 22:29:48 +01:00
|
|
|
serverSortKey: serverSortKey,
|
2014-02-02 12:26:43 +01:00
|
|
|
indent: indent,
|
2015-11-28 20:55:24 +01:00
|
|
|
bufferType: bufferType,
|
2014-07-30 17:29:55 +02:00
|
|
|
type: type,
|
2015-12-05 13:58:02 +01:00
|
|
|
plugin: plugin,
|
|
|
|
server: server,
|
2013-12-17 20:37:45 +01:00
|
|
|
history: history,
|
|
|
|
addToHistory: addToHistory,
|
|
|
|
getHistoryUp: getHistoryUp,
|
2014-03-07 18:52:32 +01:00
|
|
|
getHistoryDown: getHistoryDown,
|
2014-03-08 22:26:15 +01:00
|
|
|
isNicklistEmpty: isNicklistEmpty,
|
|
|
|
nicklistRequested: nicklistRequested
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* BufferLine class
|
|
|
|
*/
|
|
|
|
this.BufferLine = function(message) {
|
2013-12-17 21:29:05 +01:00
|
|
|
var buffer = message.buffer;
|
|
|
|
var date = message.date;
|
2013-10-26 15:00:34 +02:00
|
|
|
var shortTime = $filter('date')(date, 'HH:mm');
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2014-11-19 14:25:31 +01:00
|
|
|
var prefix = parseRichText(message.prefix);
|
2013-12-17 21:29:05 +01:00
|
|
|
var tags_array = message.tags_array;
|
|
|
|
var displayed = message.displayed;
|
|
|
|
var highlight = message.highlight;
|
2014-11-19 14:25:31 +01:00
|
|
|
var content = parseRichText(message.message);
|
2013-10-12 02:37:11 +02:00
|
|
|
|
2013-12-17 15:37:22 +01:00
|
|
|
if (highlight) {
|
|
|
|
prefix.forEach(function(textEl) {
|
|
|
|
textEl.classes.push('highlight');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-10 12:24:11 +02:00
|
|
|
var rtext = "";
|
2014-02-19 23:08:07 +01:00
|
|
|
for (var i = 0; i < content.length; ++i) {
|
|
|
|
rtext += content[i].text;
|
2013-10-09 17:53:25 +02:00
|
|
|
}
|
|
|
|
|
2013-10-12 22:07:46 +02:00
|
|
|
return {
|
2013-10-12 02:37:11 +02:00
|
|
|
prefix: prefix,
|
2013-10-09 17:53:25 +02:00
|
|
|
content: content,
|
|
|
|
date: date,
|
2013-10-26 15:00:34 +02:00
|
|
|
shortTime: shortTime,
|
2013-10-09 17:53:25 +02:00
|
|
|
buffer: buffer,
|
|
|
|
tags: tags_array,
|
|
|
|
highlight: highlight,
|
|
|
|
displayed: displayed,
|
2013-12-17 21:39:22 +01:00
|
|
|
text: rtext
|
2013-10-26 10:30:35 +02:00
|
|
|
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-27 09:49:59 +01:00
|
|
|
|
|
|
|
function nickGetColorClasses(nickMsg, propName) {
|
|
|
|
if (propName in nickMsg && nickMsg[propName] && nickMsg[propName].length > 0) {
|
|
|
|
var color = nickMsg[propName];
|
|
|
|
if (color.match(/^weechat/)) {
|
|
|
|
// color option
|
|
|
|
var colorName = color.match(/[a-zA-Z0-9_]+$/)[0];
|
|
|
|
return [
|
|
|
|
'cof-' + colorName,
|
|
|
|
'cob-' + colorName,
|
|
|
|
'coa-' + colorName
|
|
|
|
];
|
|
|
|
} else if (color.match(/^[a-zA-Z]+$/)) {
|
|
|
|
// WeeChat color name
|
|
|
|
return [
|
|
|
|
'cwf-' + color
|
|
|
|
];
|
2013-10-27 11:04:43 +01:00
|
|
|
} else if (color.match(/^[0-9]+$/)) {
|
|
|
|
// extended color
|
|
|
|
return [
|
|
|
|
'cef-' + color
|
|
|
|
];
|
2013-10-27 09:49:59 +01:00
|
|
|
}
|
2013-10-27 11:04:43 +01:00
|
|
|
|
2013-10-27 09:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'cwf-default'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function nickGetClasses(nickMsg) {
|
|
|
|
return {
|
|
|
|
'name': nickGetColorClasses(nickMsg, 'color'),
|
|
|
|
'prefix': nickGetColorClasses(nickMsg, 'prefix_color')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-10-26 22:19:13 +02:00
|
|
|
/*
|
|
|
|
* Nick class
|
|
|
|
*/
|
|
|
|
this.Nick = function(message) {
|
2013-12-17 21:29:05 +01:00
|
|
|
var prefix = message.prefix;
|
|
|
|
var visible = message.visible;
|
|
|
|
var name = message.name;
|
2013-10-27 09:49:59 +01:00
|
|
|
var colorClasses = nickGetClasses(message);
|
2013-10-26 22:19:13 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
prefix: prefix,
|
|
|
|
visible: visible,
|
|
|
|
name: name,
|
2013-10-27 09:49:59 +01:00
|
|
|
prefixClasses: colorClasses.prefix,
|
|
|
|
nameClasses: colorClasses.name
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
|
|
|
};
|
2013-10-26 22:19:13 +02:00
|
|
|
/*
|
|
|
|
* Nicklist Group class
|
|
|
|
*/
|
|
|
|
this.NickGroup = function(message) {
|
2013-12-17 21:29:05 +01:00
|
|
|
var name = message.name;
|
|
|
|
var visible = message.visible;
|
2013-10-26 22:19:13 +02:00
|
|
|
var nicks = [];
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2013-10-26 22:19:13 +02:00
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
visible: visible,
|
|
|
|
nicks: nicks
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
|
|
|
};
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2014-02-19 11:28:48 +01:00
|
|
|
var activeBuffer = null;
|
|
|
|
var previousBuffer = null;
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-12-17 21:35:36 +01:00
|
|
|
this.model = { 'buffers': {} };
|
2013-10-09 17:53:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a buffer to the list
|
|
|
|
*
|
|
|
|
* @param buffer buffer object
|
|
|
|
* @return undefined
|
|
|
|
*/
|
|
|
|
this.addBuffer = function(buffer) {
|
|
|
|
this.model.buffers[buffer.id] = buffer;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the current active buffer
|
|
|
|
*
|
|
|
|
* @return active buffer object
|
|
|
|
*/
|
|
|
|
this.getActiveBuffer = function() {
|
|
|
|
return activeBuffer;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2015-03-26 11:02:06 +01:00
|
|
|
/*
|
|
|
|
* Returns a reference to the currently active buffer that
|
|
|
|
* WeeChat understands without crashing, even if it's invalid
|
|
|
|
*
|
|
|
|
* @return active buffer pointer (WeeChat 1.0+) or fullname (older versions)
|
|
|
|
*/
|
|
|
|
this.getActiveBufferReference = function() {
|
2015-06-10 10:03:48 +02:00
|
|
|
if (this.version !== null && this.version[0] >= 1) {
|
2015-03-26 11:02:06 +01:00
|
|
|
// pointers are being validated, they're more reliable than
|
|
|
|
// fullName (e.g. if fullName contains spaces)
|
2015-03-26 13:45:53 +01:00
|
|
|
return "0x"+activeBuffer.id;
|
2015-03-26 11:02:06 +01:00
|
|
|
} else {
|
|
|
|
return activeBuffer.fullName;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-19 10:56:44 +01:00
|
|
|
/*
|
|
|
|
* Returns the previous current active buffer
|
|
|
|
*
|
|
|
|
* @return previous buffer object
|
|
|
|
*/
|
|
|
|
this.getPreviousBuffer = function() {
|
|
|
|
return previousBuffer;
|
|
|
|
};
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* Sets the buffer specifiee by bufferId as active.
|
|
|
|
* Deactivates the previous current buffer.
|
|
|
|
*
|
|
|
|
* @param bufferId id of the new active buffer
|
2013-12-14 15:40:11 +01:00
|
|
|
* @return true on success, false if buffer was not found
|
2013-10-09 17:53:25 +02:00
|
|
|
*/
|
2013-12-14 15:40:11 +01:00
|
|
|
this.setActiveBuffer = function(bufferId, key) {
|
2014-02-08 14:20:33 +01:00
|
|
|
if (key === undefined) {
|
2013-12-14 15:40:11 +01:00
|
|
|
key = 'id';
|
|
|
|
}
|
2013-10-12 20:44:40 +02:00
|
|
|
|
2014-02-19 10:56:44 +01:00
|
|
|
previousBuffer = this.getActiveBuffer();
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2014-02-26 19:39:48 +01:00
|
|
|
if (key === 'id') {
|
|
|
|
activeBuffer = this.model.buffers[bufferId];
|
|
|
|
}
|
2014-08-24 18:53:55 +02:00
|
|
|
else {
|
2014-02-26 19:39:48 +01:00
|
|
|
activeBuffer = _.find(this.model.buffers, function(buffer) {
|
|
|
|
if (buffer[key] === bufferId) {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-12-14 15:40:11 +01:00
|
|
|
|
2014-02-08 14:20:33 +01:00
|
|
|
if (activeBuffer === undefined) {
|
2013-12-14 15:40:11 +01:00
|
|
|
// Buffer not found, undo assignment
|
|
|
|
activeBuffer = previousBuffer;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-12 20:44:40 +02:00
|
|
|
if (previousBuffer) {
|
|
|
|
// turn off the active status for the previous buffer
|
|
|
|
previousBuffer.active = false;
|
|
|
|
// Save the last line we saw
|
2013-10-12 21:06:24 +02:00
|
|
|
previousBuffer.lastSeen = previousBuffer.lines.length-1;
|
2013-10-09 17:53:25 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 18:07:05 +01:00
|
|
|
var unreadSum = activeBuffer.unread + activeBuffer.notification;
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
activeBuffer.active = true;
|
2013-10-15 15:21:13 +02:00
|
|
|
activeBuffer.unread = 0;
|
|
|
|
activeBuffer.notification = 0;
|
2013-10-12 22:07:46 +02:00
|
|
|
|
2014-03-07 18:07:05 +01:00
|
|
|
$rootScope.$emit('activeBufferChanged', unreadSum);
|
2013-10-15 15:21:13 +02:00
|
|
|
$rootScope.$emit('notificationChanged');
|
2013-12-14 15:40:11 +01:00
|
|
|
return true;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the buffer list
|
|
|
|
*/
|
|
|
|
this.getBuffers = function() {
|
2014-02-26 19:39:48 +01:00
|
|
|
return this.model.buffers;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
2014-02-21 14:54:17 +01:00
|
|
|
/*
|
|
|
|
* Reinitializes the model
|
|
|
|
*/
|
|
|
|
this.reinitialize = function() {
|
|
|
|
this.model.buffers = {};
|
|
|
|
};
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
/*
|
|
|
|
* Returns a specific buffer object
|
|
|
|
*
|
|
|
|
* @param bufferId id of the buffer
|
|
|
|
* @return the buffer object
|
|
|
|
*/
|
|
|
|
this.getBuffer = function(bufferId) {
|
2014-02-26 19:39:48 +01:00
|
|
|
return this.model.buffers[bufferId];
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Closes a weechat buffer. Sets the first buffer
|
2013-10-29 12:34:16 +01:00
|
|
|
* as active, if the closing buffer was active before
|
2013-10-09 17:53:25 +02:00
|
|
|
*
|
|
|
|
* @param bufferId id of the buffer to close
|
|
|
|
* @return undefined
|
|
|
|
*/
|
|
|
|
this.closeBuffer = function(bufferId) {
|
2014-05-08 12:05:08 +02:00
|
|
|
var buffer = this.getBuffer(bufferId);
|
|
|
|
// Check if the buffer really exists, just in case
|
|
|
|
if (buffer === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (buffer.active) {
|
2013-12-17 21:29:05 +01:00
|
|
|
var firstBuffer = _.keys(this.model.buffers)[0];
|
2013-10-29 12:34:16 +01:00
|
|
|
this.setActiveBuffer(firstBuffer);
|
|
|
|
}
|
2014-05-08 12:05:08 +02:00
|
|
|
// Can't use `buffer` here, needs to be deleted from the list
|
|
|
|
delete(this.model.buffers[bufferId]);
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-09 17:53:25 +02:00
|
|
|
}]);
|
2014-08-24 18:53:55 +02:00
|
|
|
})();
|