glowingbear-mainbox/js/models.js

210 lines
5.2 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', 'colors', function($rootScope, colors) {
2013-10-09 17:53:25 +02:00
/*
* Buffer class
*/
this.Buffer = function(message) {
// weechat properties
var fullName = message['full_name']
var shortName = message['short_name']
var title = message['title']
var number = message['number']
var pointer = message['pointers'][0]
var lines = []
var active = false
var notification = 0
var unread = 0
var lastSeen = -2
2013-10-09 17:53:25 +02:00
/*
* Adds a line to this buffer
*
* @param line the BufferLine object
* @return undefined
*/
var addLine = function(line) {
lines.push(line);
}
2013-10-09 17:53:25 +02:00
return {
id: pointer,
fullName: fullName,
shortName: shortName,
number: number,
title: title,
lines: lines,
2013-10-12 20:44:40 +02:00
addLine: addLine,
lastSeen: lastSeen,
unread: unread,
notification: notification,
2013-10-09 17:53:25 +02:00
}
}
/*
* BufferLine class
*/
this.BufferLine = function(message) {
/*
* Parse the text elements from the buffer line added
*
* @param message weechat message
*/
function parseLineAddedTextElements(message) {
2013-10-12 11:54:19 +02:00
var text = colors.parse(message);
2013-10-12 02:37:11 +02:00
text_elements =_.map(text, function(text_element) {
2013-10-09 17:53:25 +02:00
if (text_element && ('fg' in text_element)) {
text_element['fg'] = colors.prepareCss(text_element['fg']);
}
// TODO: parse background as well
return text_element;
});
return text_elements;
}
var buffer = message['buffer'];
var date = message['date'];
2013-10-12 02:37:11 +02:00
2013-10-12 11:54:19 +02:00
var prefix = parseLineAddedTextElements(message['prefix']);
2013-10-12 02:37:11 +02:00
2013-10-09 17:53:25 +02:00
var tags_array = message['tags_array'];
var displayed = message['displayed'];
var highlight = message['highlight'];
2013-10-12 11:54:19 +02:00
var content = parseLineAddedTextElements(message['message']);
2013-10-12 02:37:11 +02:00
2013-10-10 12:24:11 +02:00
var rtext = "";
2013-10-12 02:37:11 +02:00
if(content[0] != undefined) {
rtext = content[0]['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,
buffer: buffer,
tags: tags_array,
highlight: highlight,
displayed: displayed,
2013-10-10 12:24:11 +02:00
text: rtext,
2013-10-09 17:53:25 +02:00
}
}
var BufferList = []
activeBuffer = null;
unreads = 0;
notifications = 0;
2013-10-09 17:53:25 +02:00
this.model = { 'buffers': {} }
/*
* Adds a buffer to the list
*
* @param buffer buffer object
* @return undefined
*/
this.addBuffer = function(buffer) {
BufferList[buffer.id] = buffer;
if (BufferList.length == 1) {
activeBuffer = buffer.id;
}
this.model.buffers[buffer.id] = buffer;
}
this.getBufferByIndex = function(index) {
var i = 0;
for (var v in BufferList) {
if (index == ++i) {
return BufferList[v];
}
}
}
2013-10-09 17:53:25 +02:00
/*
* Returns the current active buffer
*
* @return active buffer object
*/
this.getActiveBuffer = function() {
return activeBuffer;
}
/*
* Sets the buffer specifiee by bufferId as active.
* Deactivates the previous current buffer.
*
* @param bufferId id of the new active buffer
* @return undefined
*/
this.setActiveBuffer = function(bufferId) {
2013-10-12 20:44:40 +02:00
var previousBuffer = this.getActiveBuffer();
2013-10-09 17:53:25 +02:00
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 = _.find(this.model['buffers'], function(buffer) {
if (buffer['id'] == bufferId) {
return buffer;
}
});
2013-10-09 17:53:25 +02:00
activeBuffer.active = true;
activeBuffer.unread = '';
activeBuffer.notification = '';
$rootScope.$emit('activeBufferChanged');
2013-10-09 17:53:25 +02:00
}
/*
* Returns the buffer list
*/
this.getBuffers = function() {
return BufferList;
}
/*
* Returns a specific buffer object
*
* @param bufferId id of the buffer
* @return the buffer object
*/
this.getBuffer = function(bufferId) {
return _.find(this.model['buffers'], function(buffer) {
if (buffer['id'] == bufferId) {
return buffer;
}
});
}
/*
* Closes a weechat buffer. Sets the first buffer
* as active.
*
* @param bufferId id of the buffer to close
* @return undefined
*/
this.closeBuffer = function(bufferId) {
delete(this.model['buffers'][bufferId.id]);
var firstBuffer = _.keys(this.model['buffers'])[0];
this.setActiveBuffer(firstBuffer);
}
}]);