glowing-bear/js/models.js

166 lines
4.1 KiB
JavaScript
Raw Normal View History

/*
* This file contains the weechat models and various
* helper methods to work with them.
*/
2013-10-08 02:43:09 +02:00
var models = angular.module('weechatModels', []);
2013-10-08 02:42:19 +02:00
2013-10-08 15:55:07 +02:00
models.service('models', ['colors', function(colors) {
2013-10-08 16:25:20 +02:00
/*
* Buffer class
*/
this.Buffer = function(message) {
// weechat properties
var fullName = message['full_name']
var pointer = message['pointers'][0]
var lines = []
var active = false;
var notification = false;
/*
* Adds a line to this buffer
*
* @param line the BufferLine object
* @return undefined
*/
var addLine = function(line) {
lines.push(line);
}
return {
id: pointer,
fullName: fullName,
lines: lines,
addLine: addLine
}
}
/*
* BufferLine class
*/
this.BufferLine = function(weechatBufferLine) {
/*
* Parse the text elements from the buffer line added
*
* @param message weechat message
*/
function parseLineAddedTextElements(message) {
var prefix = colors.parse(message['objects'][0]['content'][0]['prefix']);
var buffer = message['objects'][0]['content'][0]['buffer'];
text_elements = _.union(prefix, text);
text_elements =_.map(text_elements, function(text_element) {
if ('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['objects'][0]['content'][0]['buffer'];
var date = message['objects'][0]['content'][0]['date'];
var text = colors.parse(message['objects'][0]['content'][0]['message']);
var content = parseLineAddedTextElements(message);
return {
content: content,
date: date,
buffer: buffer,
text: text[0]['text'],
}
}
2013-10-08 02:42:19 +02:00
2013-10-08 15:55:07 +02:00
var BufferList = []
activeBuffer = null;
this.model = { 'buffers': {} }
/*
* Adds a buffer to the list
*
* @param buffer buffer object
* @return undefined
*/
2013-10-08 15:55:07 +02:00
this.addBuffer = function(buffer) {
BufferList[buffer.id] = buffer;
if (BufferList.length == 1) {
activeBuffer = buffer.id;
}
this.model.buffers[buffer.id] = buffer;
}
/*
* Returns the current active buffer
*
* @return active buffer object
*/
2013-10-08 15:55:07 +02:00
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
*/
2013-10-08 15:55:07 +02:00
this.setActiveBuffer = function(bufferId) {
2013-10-08 16:03:44 +02:00
if (this.getActiveBuffer()) {
this.getActiveBuffer().active = false;
}
2013-10-08 15:55:07 +02:00
activeBuffer = _.find(this.model['buffers'], function(buffer) {
if (buffer['id'] == bufferId) {
return buffer;
}
});
2013-10-08 16:03:44 +02:00
activeBuffer.notification = false;
activeBuffer.active = true;
2013-10-08 15:55:07 +02:00
}
/*
* Returns the buffer list
*/
2013-10-08 15:55:07 +02:00
this.getBuffers = function() {
return BufferList;
}
/*
* Returns a specific buffer object
*
* @param bufferId id of the buffer
* @return the buffer object
*/
2013-10-08 15:55:07 +02:00
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
*/
2013-10-08 15:55:07 +02:00
this.closeBuffer = function(bufferId) {
2013-10-08 16:13:48 +02:00
delete(this.model['buffers'][bufferId.id]);
var firstBuffer = _.keys(this.model['buffers'])[0];
this.setActiveBuffer(firstBuffer);
2013-10-08 15:55:07 +02:00
}
2013-10-08 02:43:09 +02:00
}]);