glowingbear-mainbox/js/models.js

458 lines
13 KiB
JavaScript
Raw Normal View History

2013-10-09 17:53:25 +02:00
/*
* This file contains the weechat models and various
* helper methods to work with them.
*/
var models = angular.module('weechatModels', []);
models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) {
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;
var title = message.title;
var number = message.number;
var pointer = message.pointers[0];
2013-12-17 21:29:05 +01:00
var local_variables = message.local_vars;
2013-12-17 21:35:36 +01:00
var notify = 3; // Default 3 == message
var lines = [];
2014-02-10 19:43:52 +01:00
var requestedLines = 0;
2013-12-17 21:35:36 +01:00
var nicklist = {};
var flatnicklist = [];
var history = [];
var historyPos = 0;
2013-12-17 21:35:36 +01:00
var active = false;
var notification = 0;
var unread = 0;
var lastSeen = -1;
var serverSortKey = fullName.replace(/^irc.server.(\w+)/, "irc.$1");
var indent = (fullName.match(/^irc./) && !fullName.match(/^irc.server./));
2013-10-09 17:53:25 +02:00
// Buffer opened message does not include notify level
if (message.notify !== undefined) {
2013-12-17 21:29:05 +01:00
notify = message.notify;
}
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);
2013-12-17 21:35:36 +01:00
};
2013-10-26 22:19:13 +02:00
/*
* Adds a nick to nicklist
*/
var addNick = function(group, nick) {
nicklist[group].nicks.push(nick);
flatnicklist = getFlatNicklist();
2013-12-17 21:35:36 +01:00
};
/*
* Deletes a nick from nicklist
*/
var delNick = function(group, nick) {
2013-12-17 21:48:43 +01:00
group = nicklist[group];
2014-02-08 14:20:33 +01:00
group.nicks = _.filter(group.nicks, function(n) { return n.name !== nick.name;});
flatnicklist = getFlatNicklist();
/*
for (i in group.nicks) {
if (group.nicks[i].name == nick.name) {
delete group.nicks[i];
break;
}
}
*/
2013-12-17 21:35:36 +01:00
};
/*
* Updates a nick in nicklist
*/
var updateNick = function(group, nick) {
2013-12-17 21:48:43 +01:00
group = nicklist[group];
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) {
group.nicks[i] = nick;
break;
}
}
flatnicklist = getFlatNicklist();
2013-12-17 21:35:36 +01:00
};
/*
* Maintain a cached version of a flat sorted nicklist
2013-12-17 21:30:22 +01:00
*
*/
var getFlatNicklist = function() {
var newlist = [];
_.each(nicklist, function(nickGroup) {
_.each(nickGroup.nicks, function(nickObj) {
newlist.push(nickObj.name);
});
2013-12-17 21:30:22 +01:00
});
newlist.sort(function(a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
});
return newlist;
2013-12-17 21:35:36 +01:00
};
var flatNicklist = function() {
return flatnicklist;
2013-12-17 21:35:36 +01:00
};
2013-10-26 22:19:13 +02:00
var addToHistory = function(line) {
var result = "";
2014-02-08 14:20:33 +01:00
if (historyPos !== history.length) {
// 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
};
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
};
var getHistoryDown = function(currentLine) {
if (historyPos < 0 || historyPos >= history.length) {
// 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-10-09 17:53:25 +02:00
return {
id: pointer,
fullName: fullName,
shortName: shortName,
number: number,
title: title,
lines: lines,
2014-02-10 19:43:52 +01:00
requestedLines: requestedLines,
2013-10-12 20:44:40 +02:00
addLine: addLine,
lastSeen: lastSeen,
unread: unread,
notification: notification,
2013-10-16 00:32:56 +02:00
localvars: local_variables,
notify: notify,
nicklist: nicklist,
addNick: addNick,
delNick: delNick,
updateNick: updateNick,
flatNicklist: flatNicklist,
serverSortKey: serverSortKey,
indent: indent,
history: history,
addToHistory: addToHistory,
getHistoryUp: getHistoryUp,
getHistoryDown: getHistoryDown
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;
var shortTime = $filter('date')(date, 'HH:mm');
2013-10-09 17:53:25 +02:00
function addClasses(textElements) {
var typeToClassPrefixFg = {
'option': 'cof-',
'weechat': 'cwf-',
'ext': 'cef-'
};
var 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);
}
for (var attr in textEl.attrs.override) {
val = textEl.attrs.override[attr];
if (val) {
textEl.classes.push('a-' + attr);
} else {
textEl.classes.push('a-no-' + attr);
}
2013-10-09 17:53:25 +02:00
}
});
}
2013-12-17 21:29:05 +01:00
var prefix = weeChat.Protocol.rawText2Rich(message.prefix);
addClasses(prefix);
2013-10-12 02:37:11 +02:00
2013-12-17 21:29:05 +01:00
var tags_array = message.tags_array;
var displayed = message.displayed;
var highlight = message.highlight;
var content = weeChat.Protocol.rawText2Rich(message.message);
addClasses(content);
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 = "";
for (var i = 0; i < content.length; ++i) {
rtext += content[i].text;
2013-10-09 17:53:25 +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,
shortTime: shortTime,
2013-10-09 17:53:25 +02:00
buffer: buffer,
tags: tags_array,
highlight: highlight,
displayed: displayed,
text: rtext
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
2013-12-17 21:35:36 +01:00
var BufferList = [];
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) {
BufferList[buffer.id] = buffer;
2014-02-08 14:20:33 +01:00
if (BufferList.length === 1) {
2014-02-19 11:28:48 +01:00
activeBuffer = buffer;
2013-10-09 17:53:25 +02:00
}
this.model.buffers[buffer.id] = buffer;
2013-12-17 21:35:36 +01:00
};
2013-10-09 17:53:25 +02:00
this.getBufferByIndex = function(index) {
var i = 0;
for (var v in BufferList) {
2014-02-08 14:20:33 +01:00
if (index === ++i) {
return BufferList[v];
}
}
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
/*
* 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
* @return true on success, false if buffer was not found
2013-10-09 17:53:25 +02:00
*/
this.setActiveBuffer = function(bufferId, key) {
2014-02-08 14:20:33 +01:00
if (key === undefined) {
key = 'id';
}
2013-10-12 20:44:40 +02:00
previousBuffer = this.getActiveBuffer();
2013-12-17 21:30:22 +01:00
2013-12-17 21:29:05 +01:00
activeBuffer = _.find(this.model.buffers, function(buffer) {
2014-02-08 14:20:33 +01:00
if (buffer[key] === bufferId) {
return buffer;
}
});
2014-02-08 14:20:33 +01:00
if (activeBuffer === undefined) {
// 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
}
activeBuffer.active = true;
2013-10-15 15:21:13 +02:00
activeBuffer.unread = 0;
activeBuffer.notification = 0;
$rootScope.$emit('activeBufferChanged');
2013-10-15 15:21:13 +02:00
$rootScope.$emit('notificationChanged');
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() {
return BufferList;
2013-12-17 21:35:36 +01:00
};
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) {
2013-12-17 21:29:05 +01:00
return _.find(this.model.buffers, function(buffer) {
2014-02-08 14:20:33 +01:00
if (buffer.id === bufferId) {
2013-10-09 17:53:25 +02:00
return buffer;
}
});
2013-12-17 21:35:36 +01:00
};
2013-10-09 17:53:25 +02:00
/*
* Closes a weechat buffer. Sets the first buffer
* 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) {
2013-12-17 21:29:05 +01:00
var wasActive = this.model.buffers[bufferId.id].active;
if (wasActive) {
2013-12-17 21:29:05 +01:00
var firstBuffer = _.keys(this.model.buffers)[0];
this.setActiveBuffer(firstBuffer);
}
2013-12-17 21:29:05 +01:00
delete(this.model.buffers[bufferId.id]);
2013-12-17 21:35:36 +01:00
};
2013-10-09 17:53:25 +02:00
}]);