2013-12-16 17:05:54 +01:00
|
|
|
var weechat = angular.module('weechat', ['ngRoute', 'localStorage', 'weechatModels', 'plugins', 'ngSanitize', 'pasvaz.bindonce']);
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-10-06 13:42:45 +02:00
|
|
|
weechat.filter('toArray', function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
return function (obj) {
|
|
|
|
if (!(obj instanceof Object)) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(obj).map(function (key) {
|
2013-12-17 14:40:49 +01:00
|
|
|
return Object.defineProperty(obj[key], '$key', { value: key });
|
2013-10-06 13:42:45 +02:00
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-06 13:42:45 +02:00
|
|
|
});
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-10-26 10:30:35 +02:00
|
|
|
weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootScope, models, plugins) {
|
2013-08-05 03:39:23 +02:00
|
|
|
|
2013-10-02 02:32:18 +02:00
|
|
|
var handleBufferClosing = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var bufferMessage = message.objects[0].content[0];
|
2013-10-08 03:15:25 +02:00
|
|
|
var buffer = new models.Buffer(bufferMessage);
|
2013-10-08 16:13:48 +02:00
|
|
|
models.closeBuffer(buffer);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-02 02:32:18 +02:00
|
|
|
|
2013-10-06 23:59:34 +02:00
|
|
|
var handleLine = function(line, initial) {
|
2013-10-09 17:53:25 +02:00
|
|
|
var message = new models.BufferLine(line);
|
2013-10-06 12:34:41 +02:00
|
|
|
// Only react to line if its displayed
|
2013-10-09 17:53:25 +02:00
|
|
|
if(message.displayed) {
|
|
|
|
var buffer = models.getBuffer(message.buffer);
|
2013-10-28 13:21:51 +01:00
|
|
|
message = plugins.PluginManager.contentForMessage(message, $rootScope.visible);
|
2013-10-09 17:53:25 +02:00
|
|
|
buffer.addLine(message);
|
2013-10-06 02:06:28 +02:00
|
|
|
|
2013-12-14 00:59:51 +01:00
|
|
|
if (initial) {
|
|
|
|
buffer.lastSeen++;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
if (buffer.active) {
|
2013-12-23 22:34:21 +01:00
|
|
|
$rootScope.scrollWithBuffer();
|
2013-10-06 12:34:41 +02:00
|
|
|
}
|
2013-10-08 15:55:07 +02:00
|
|
|
|
2013-10-23 15:29:28 +02:00
|
|
|
if (!initial && !buffer.active) {
|
|
|
|
if (buffer.notify>1 && _.contains(message.tags, 'notify_message') && !_.contains(message.tags, 'notify_none')) {
|
2013-10-15 14:59:06 +02:00
|
|
|
buffer.unread++;
|
2013-10-15 15:21:13 +02:00
|
|
|
$rootScope.$emit('notificationChanged');
|
2013-10-09 17:53:25 +02:00
|
|
|
}
|
2013-08-06 22:39:10 +02:00
|
|
|
|
2013-12-16 14:18:17 +01:00
|
|
|
if(buffer.notify !== 0 && message.highlight || _.contains(message.tags, 'notify_private') ) {
|
2013-10-15 14:59:06 +02:00
|
|
|
buffer.notification++;
|
2013-10-09 17:53:25 +02:00
|
|
|
$rootScope.createHighlight(buffer, message);
|
2013-10-15 15:21:13 +02:00
|
|
|
$rootScope.$emit('notificationChanged');
|
2013-10-09 17:53:25 +02:00
|
|
|
}
|
2013-10-06 12:34:41 +02:00
|
|
|
}
|
2013-10-08 16:03:44 +02:00
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-08-05 03:39:23 +02:00
|
|
|
|
2013-10-06 20:20:34 +02:00
|
|
|
var handleBufferLineAdded = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
message.objects[0].content.forEach(function(l) {
|
2013-10-19 20:11:01 +02:00
|
|
|
handleLine(l, false);
|
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-06 20:20:34 +02:00
|
|
|
|
2013-08-05 03:39:23 +02:00
|
|
|
var handleBufferOpened = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var bufferMessage = message.objects[0].content[0];
|
2013-10-08 03:15:25 +02:00
|
|
|
var buffer = new models.Buffer(bufferMessage);
|
2013-10-08 16:05:46 +02:00
|
|
|
models.addBuffer(buffer);
|
2013-12-14 18:53:35 +01:00
|
|
|
models.setActiveBuffer(buffer.id);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-06 02:06:28 +02:00
|
|
|
|
2013-10-07 00:24:18 +02:00
|
|
|
var handleBufferTitleChanged = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var obj = message.objects[0].content[0];
|
|
|
|
var buffer = obj.pointers[0];
|
2013-10-10 12:37:25 +02:00
|
|
|
var old = models.getBuffer(buffer);
|
2013-12-16 14:00:59 +01:00
|
|
|
old.fullName = obj.full_name;
|
|
|
|
old.title = obj.title;
|
|
|
|
old.number = obj.number;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-17 18:36:53 +02:00
|
|
|
|
2013-10-07 11:45:18 +02:00
|
|
|
var handleBufferRenamed = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var obj = message.objects[0].content[0];
|
|
|
|
var buffer = obj.pointers[0];
|
2013-10-10 12:37:25 +02:00
|
|
|
var old = models.getBuffer(buffer);
|
2013-12-16 14:00:59 +01:00
|
|
|
old.fullName = obj.full_name;
|
|
|
|
old.shortName = obj.short_name;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-08-05 03:39:23 +02:00
|
|
|
|
2013-10-06 20:20:34 +02:00
|
|
|
/*
|
|
|
|
* Handle answers to (lineinfo) messages
|
|
|
|
*
|
|
|
|
* (lineinfo) messages are specified by this client. It is request after bufinfo completes
|
|
|
|
*/
|
|
|
|
var handleLineInfo = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var lines = message.objects[0].content.reverse();
|
2013-10-19 20:11:01 +02:00
|
|
|
lines.forEach(function(l) {
|
|
|
|
handleLine(l, true);
|
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-06 02:06:28 +02:00
|
|
|
|
2013-10-16 01:09:31 +02:00
|
|
|
/*
|
|
|
|
* Handle answers to hotlist request
|
|
|
|
*/
|
|
|
|
var handleHotlistInfo = function(message) {
|
2013-12-16 14:18:17 +01:00
|
|
|
if (message.objects.length === 0) {
|
2013-10-19 20:11:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-12-16 14:00:59 +01:00
|
|
|
var hotlist = message.objects[0].content;
|
2013-10-19 20:11:01 +02:00
|
|
|
hotlist.forEach(function(l) {
|
|
|
|
var buffer = models.getBuffer(l.buffer);
|
|
|
|
// 1 is message
|
|
|
|
buffer.unread += l.count[1];
|
|
|
|
// 2 is ?
|
|
|
|
buffer.unread += l.count[2];
|
|
|
|
// 3 is highlight
|
|
|
|
buffer.notification += l.count[3];
|
|
|
|
/* Since there is unread messages, we can guess
|
|
|
|
* what the last read line is and update it accordingly
|
|
|
|
*/
|
|
|
|
var unreadSum = _.reduce(l.count, function(memo, num){ return memo + num; }, 0);
|
|
|
|
buffer.lastSeen = buffer.lines.length - 1 - unreadSum;
|
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-16 01:09:31 +02:00
|
|
|
|
2013-10-26 22:19:13 +02:00
|
|
|
/*
|
|
|
|
* Handle nicklist event
|
|
|
|
*/
|
|
|
|
var handleNicklist = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var nicklist = message.objects[0].content;
|
2013-10-26 22:19:13 +02:00
|
|
|
var group = 'root';
|
|
|
|
nicklist.forEach(function(n) {
|
|
|
|
var buffer = models.getBuffer(n.pointers[0]);
|
|
|
|
if(n.group == 1) {
|
|
|
|
var g = new models.NickGroup(n);
|
|
|
|
group = g.name;
|
|
|
|
buffer.nicklist[group] = g;
|
|
|
|
}else{
|
|
|
|
var nick = new models.Nick(n);
|
2013-10-27 10:48:20 +01:00
|
|
|
buffer.addNick(group, nick);
|
2013-10-26 22:19:13 +02:00
|
|
|
}
|
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-26 22:19:13 +02:00
|
|
|
/*
|
|
|
|
* Handle nicklist diff event
|
|
|
|
*/
|
|
|
|
var handleNicklistDiff = function(message) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var nicklist = message.objects[0].content;
|
2013-10-26 22:19:13 +02:00
|
|
|
var group;
|
|
|
|
nicklist.forEach(function(n) {
|
|
|
|
var buffer = models.getBuffer(n.pointers[0]);
|
2013-12-16 14:00:59 +01:00
|
|
|
var d = n._diff;
|
2013-10-26 22:19:13 +02:00
|
|
|
if(n.group == 1) {
|
2013-10-27 00:26:17 +02:00
|
|
|
group = n.name;
|
2013-12-16 14:18:17 +01:00
|
|
|
if(group === undefined) {
|
2013-10-26 23:40:00 +02:00
|
|
|
var g = new models.NickGroup(n);
|
|
|
|
buffer.nicklist[group] = g;
|
2013-10-27 00:26:17 +02:00
|
|
|
group = g.name;
|
2013-10-26 23:40:00 +02:00
|
|
|
}
|
2013-10-26 22:19:13 +02:00
|
|
|
}
|
2013-10-27 00:26:17 +02:00
|
|
|
else {
|
2013-10-26 22:19:13 +02:00
|
|
|
var nick = new models.Nick(n);
|
2013-10-27 00:26:17 +02:00
|
|
|
if(d == 43) { // +
|
|
|
|
buffer.addNick(group, nick);
|
|
|
|
}else if (d == 45) { // -
|
|
|
|
buffer.delNick(group, nick);
|
|
|
|
}else if (d == 42) { // *
|
|
|
|
buffer.updateNick(group, nick);
|
2013-10-26 23:40:00 +02:00
|
|
|
}
|
2013-10-26 22:19:13 +02:00
|
|
|
}
|
|
|
|
});
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-26 22:19:13 +02:00
|
|
|
|
2013-08-05 04:31:04 +02:00
|
|
|
var handleEvent = function(event) {
|
2013-10-12 18:13:32 +02:00
|
|
|
|
2013-12-16 14:00:59 +01:00
|
|
|
if (_.has(eventHandlers, event.id)) {
|
|
|
|
eventHandlers[event.id](event);
|
2013-08-05 04:31:04 +02:00
|
|
|
}
|
2013-08-05 03:59:29 +02:00
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-08-05 03:39:23 +02:00
|
|
|
|
2013-08-05 04:31:04 +02:00
|
|
|
var eventHandlers = {
|
2013-10-06 02:06:28 +02:00
|
|
|
_buffer_closing: handleBufferClosing,
|
2013-08-05 03:39:23 +02:00
|
|
|
_buffer_line_added: handleBufferLineAdded,
|
2013-10-07 00:24:18 +02:00
|
|
|
_buffer_opened: handleBufferOpened,
|
2013-10-07 11:45:18 +02:00
|
|
|
_buffer_title_changed: handleBufferTitleChanged,
|
2013-10-26 22:19:13 +02:00
|
|
|
_buffer_renamed: handleBufferRenamed,
|
|
|
|
_nicklist: handleNicklist,
|
|
|
|
_nicklist_diff: handleNicklistDiff
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-08-05 03:39:23 +02:00
|
|
|
|
|
|
|
return {
|
2013-10-12 18:29:10 +02:00
|
|
|
handleEvent: handleEvent,
|
2013-10-16 01:09:31 +02:00
|
|
|
handleLineInfo: handleLineInfo,
|
2013-10-26 22:19:13 +02:00
|
|
|
handleHotlistInfo: handleHotlistInfo,
|
|
|
|
handleNicklist: handleNicklist
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-08-05 03:39:23 +02:00
|
|
|
|
|
|
|
}]);
|
|
|
|
|
2013-10-26 10:30:35 +02:00
|
|
|
weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers', 'models', function($q, $rootScope, $log, storage, handlers, models) {
|
2013-10-16 23:10:49 +02:00
|
|
|
protocol = new weeChat.Protocol();
|
2013-08-02 03:54:12 +02:00
|
|
|
var websocket = null;
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
var callbacks = {};
|
2013-10-12 18:13:32 +02:00
|
|
|
var currentCallBackId = 0;
|
|
|
|
|
2013-10-27 20:41:45 +01:00
|
|
|
/*
|
|
|
|
* Returns the current callback id
|
|
|
|
*/
|
|
|
|
var getCurrentCallBackId = function() {
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-27 20:41:45 +01:00
|
|
|
currentCallBackId += 1;
|
|
|
|
|
|
|
|
if (currentCallBackId > 1000) {
|
|
|
|
currentCallBackId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentCallBackId;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 20:41:45 +01:00
|
|
|
|
2013-10-27 17:26:16 +01:00
|
|
|
/*
|
|
|
|
* Create a callback, adds it to the callback list
|
|
|
|
* and return it.
|
|
|
|
*/
|
|
|
|
var createCallback = function() {
|
2013-10-12 18:13:32 +02:00
|
|
|
var defer = $q.defer();
|
2013-10-27 20:41:45 +01:00
|
|
|
var cbId = getCurrentCallBackId();
|
|
|
|
|
|
|
|
callbacks[cbId] = {
|
2014-01-27 17:14:48 +01:00
|
|
|
time: new Date(),
|
2013-12-16 14:09:01 +01:00
|
|
|
cb: defer,
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 20:41:45 +01:00
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
defer.id = cbId;
|
2013-10-27 20:41:45 +01:00
|
|
|
|
2013-10-27 16:13:11 +01:00
|
|
|
return defer;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 16:13:11 +01:00
|
|
|
|
2013-10-27 17:13:10 +01:00
|
|
|
/*
|
|
|
|
* Fails every currently subscribed callback for the
|
|
|
|
* given reason
|
2013-10-27 17:26:16 +01:00
|
|
|
*
|
2013-10-27 17:13:10 +01:00
|
|
|
* @param reason reason for failure
|
|
|
|
*/
|
|
|
|
failCallbacks = function(reason) {
|
2013-12-16 14:20:31 +01:00
|
|
|
for(var i in callbacks) {
|
2013-10-27 17:13:10 +01:00
|
|
|
callbacks[i].cb.reject(reason);
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 17:13:10 +01:00
|
|
|
|
2013-10-27 17:26:16 +01:00
|
|
|
/* Send a message to the websocket and returns a promise.
|
|
|
|
* See: http://docs.angularjs.org/api/ng.$q
|
|
|
|
*
|
|
|
|
* @param message message to send
|
|
|
|
* @returns a promise
|
|
|
|
*/
|
2013-10-27 17:05:37 +01:00
|
|
|
var send = function(message) {
|
2013-10-27 17:03:45 +01:00
|
|
|
message.replace(/[\r\n]+$/g, "").split("\n");
|
2013-10-27 16:13:11 +01:00
|
|
|
var cb = createCallback(message);
|
2013-10-27 20:41:45 +01:00
|
|
|
websocket.send("(" + cb.id + ") " + message);
|
2013-10-27 16:13:11 +01:00
|
|
|
return cb.promise;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-07-27 16:43:41 +02:00
|
|
|
|
2013-10-27 16:28:34 +01:00
|
|
|
/*
|
|
|
|
* Send all messages to the websocket and returns a promise that is resolved
|
|
|
|
* when all message are resolved.
|
|
|
|
*
|
|
|
|
* @param messages list of messages
|
|
|
|
* @returns a promise
|
|
|
|
*/
|
2013-10-27 17:05:49 +01:00
|
|
|
var sendAll = function(messages) {
|
2013-10-27 16:28:34 +01:00
|
|
|
var promises = [];
|
2013-12-16 14:20:31 +01:00
|
|
|
for(var i in messages) {
|
2013-10-27 17:05:37 +01:00
|
|
|
var promise = send(messages[i]);
|
2013-10-27 17:03:45 +01:00
|
|
|
promises.push(promise);
|
2013-12-16 14:09:01 +01:00
|
|
|
}
|
2013-10-27 16:28:34 +01:00
|
|
|
return $q.all(promises);
|
|
|
|
};
|
|
|
|
|
2013-08-05 04:25:59 +02:00
|
|
|
// Takes care of the connection and websocket hooks
|
2013-12-20 17:10:58 +01:00
|
|
|
var connect = function (host, port, passwd, ssl, noCompression) {
|
2013-10-07 15:38:47 +02:00
|
|
|
var proto = ssl ? 'wss':'ws';
|
2013-10-11 23:15:08 +02:00
|
|
|
websocket = new WebSocket(proto+"://" + host + ':' + port + "/weechat");
|
2013-12-16 14:09:01 +01:00
|
|
|
websocket.binaryType = "arraybuffer";
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-12-17 14:40:49 +01:00
|
|
|
websocket.onopen = function () {
|
2013-10-17 18:36:53 +02:00
|
|
|
|
2013-10-15 17:20:35 +02:00
|
|
|
$log.info("Connected to relay");
|
2013-10-17 18:36:53 +02:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
// First command asks for the password and issues
|
|
|
|
// a version command. If it fails, it means the we
|
|
|
|
// did not provide the proper password.
|
|
|
|
sendAll([
|
|
|
|
weeChat.Protocol.formatInit({
|
2013-10-06 01:54:07 +02:00
|
|
|
password: passwd,
|
2013-12-20 17:10:58 +01:00
|
|
|
compression: noCompression ? 'off' : 'zlib'
|
2013-11-09 22:52:26 +01:00
|
|
|
}),
|
2013-10-17 18:36:53 +02:00
|
|
|
|
2013-10-27 16:47:25 +01:00
|
|
|
weeChat.Protocol.formatInfo({
|
2013-12-17 21:39:22 +01:00
|
|
|
name: 'version'
|
2013-11-09 22:52:26 +01:00
|
|
|
})
|
|
|
|
]).then(
|
|
|
|
null,
|
2013-12-17 14:40:49 +01:00
|
|
|
function() {
|
2013-11-09 22:52:26 +01:00
|
|
|
$rootScope.passwordError = true;
|
|
|
|
}
|
|
|
|
);
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
send(
|
2013-10-27 16:47:25 +01:00
|
|
|
weeChat.Protocol.formatHdata({
|
2013-10-17 18:36:53 +02:00
|
|
|
path: 'buffer:gui_buffers(*)',
|
2013-11-09 22:52:26 +01:00
|
|
|
keys: ['local_variables,notify,number,full_name,short_name,title']
|
|
|
|
})
|
|
|
|
).then(function(bufinfo) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var bufferInfos = bufinfo.objects[0].content;
|
2013-11-09 22:52:26 +01:00
|
|
|
// buffers objects
|
|
|
|
for (var i = 0; i < bufferInfos.length ; i++) {
|
|
|
|
var buffer = new models.Buffer(bufferInfos[i]);
|
|
|
|
models.addBuffer(buffer);
|
|
|
|
// Switch to first buffer on startup
|
2013-12-16 14:18:17 +01:00
|
|
|
if (i === 0) {
|
2013-11-09 22:52:26 +01:00
|
|
|
models.setActiveBuffer(buffer.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
|
|
|
|
// Send all the other commands required for initialization
|
|
|
|
send(
|
2013-10-27 16:47:25 +01:00
|
|
|
weeChat.Protocol.formatHdata({
|
|
|
|
path: "buffer:gui_buffers(*)/own_lines/last_line(-"+storage.get('lines')+")/data",
|
|
|
|
keys: []
|
2013-11-09 22:52:26 +01:00
|
|
|
})
|
|
|
|
).then(function(lineinfo) {
|
|
|
|
handlers.handleLineInfo(lineinfo);
|
|
|
|
});
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
send(
|
2013-10-27 16:47:25 +01:00
|
|
|
weeChat.Protocol.formatHdata({
|
|
|
|
path: "hotlist:gui_hotlist(*)",
|
|
|
|
keys: []
|
2013-11-09 22:52:26 +01:00
|
|
|
})
|
|
|
|
).then(function(hotlist) {
|
|
|
|
handlers.handleHotlistInfo(hotlist);
|
|
|
|
});
|
|
|
|
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
send(
|
2013-10-27 16:47:25 +01:00
|
|
|
weeChat.Protocol.formatNicklist({
|
|
|
|
})
|
2013-11-09 22:52:26 +01:00
|
|
|
).then(function(nicklist) {
|
2013-12-16 14:09:01 +01:00
|
|
|
handlers.handleNicklist(nicklist);
|
2013-11-09 22:52:26 +01:00
|
|
|
});
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
send(
|
|
|
|
weeChat.Protocol.formatSync({})
|
2013-12-16 14:09:01 +01:00
|
|
|
);
|
2013-10-17 18:36:53 +02:00
|
|
|
|
2013-11-09 22:52:26 +01:00
|
|
|
$rootScope.connected = true;
|
2013-10-27 16:47:25 +01:00
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 15:49:54 +01:00
|
|
|
|
2013-12-17 14:40:49 +01:00
|
|
|
websocket.onclose = function () {
|
2013-08-05 04:25:59 +02:00
|
|
|
$log.info("Disconnected from relay");
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.connected = false;
|
2013-10-27 15:49:54 +01:00
|
|
|
failCallbacks('disconnection');
|
2013-08-05 04:25:59 +02:00
|
|
|
$rootScope.$apply();
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onmessage = function (evt) {
|
2013-12-16 14:09:01 +01:00
|
|
|
message = protocol.parse(evt.data);
|
2013-12-16 14:00:59 +01:00
|
|
|
if (_.has(callbacks, message.id)) {
|
|
|
|
var promise = callbacks[message.id];
|
2013-10-12 18:29:10 +02:00
|
|
|
promise.cb.resolve(message);
|
2013-12-16 14:00:59 +01:00
|
|
|
delete(callbacks[message.id]);
|
2013-10-12 18:13:32 +02:00
|
|
|
} else {
|
|
|
|
handlers.handleEvent(message);
|
|
|
|
}
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.commands.push("RECV: " + evt.data + " TYPE:" + evt.type) ;
|
|
|
|
$rootScope.$apply();
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onerror = function (evt) {
|
2013-10-17 18:36:53 +02:00
|
|
|
// on error it means the connection problem
|
|
|
|
// come from the relay not from the password.
|
|
|
|
|
2013-10-07 15:38:47 +02:00
|
|
|
if (evt.type == "error" && websocket.readyState != 1) {
|
2013-10-27 15:49:54 +01:00
|
|
|
failCallbacks('error');
|
2013-10-03 01:55:30 +02:00
|
|
|
$rootScope.errorMessage = true;
|
|
|
|
}
|
2013-08-05 04:25:59 +02:00
|
|
|
$log.error("Relay error " + evt.data);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
this.websocket = websocket;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-07-31 14:40:43 +02:00
|
|
|
|
2013-10-16 14:25:07 +02:00
|
|
|
var disconnect = function() {
|
2013-10-22 16:09:22 +02:00
|
|
|
/* TODO: Send protocol disconnect */
|
2013-10-19 20:11:01 +02:00
|
|
|
this.websocket.close();
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-16 14:25:07 +02:00
|
|
|
|
2013-12-14 16:32:14 +01:00
|
|
|
/*
|
|
|
|
* Format and send a weechat message
|
|
|
|
*
|
|
|
|
* @returns the angular promise
|
|
|
|
*/
|
2013-08-02 03:54:12 +02:00
|
|
|
var sendMessage = function(message) {
|
2013-12-14 16:32:14 +01:00
|
|
|
return send(weeChat.Protocol.formatInput({
|
2013-12-16 14:00:59 +01:00
|
|
|
buffer: models.getActiveBuffer().fullName,
|
2013-10-06 01:54:07 +02:00
|
|
|
data: message
|
|
|
|
}));
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-10-19 12:14:32 +02:00
|
|
|
var sendCoreCommand = function(command) {
|
2013-10-27 17:07:01 +01:00
|
|
|
send(weeChat.Protocol.formatInput({
|
2013-10-19 12:14:32 +02:00
|
|
|
buffer: 'core.weechat',
|
|
|
|
data: command
|
|
|
|
}));
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-19 12:14:32 +02:00
|
|
|
|
2013-10-06 20:20:34 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
return {
|
2013-10-27 17:07:01 +01:00
|
|
|
send: send,
|
2013-08-02 03:54:12 +02:00
|
|
|
connect: connect,
|
2013-10-16 14:25:07 +02:00
|
|
|
disconnect: disconnect,
|
2013-10-19 12:14:32 +02:00
|
|
|
sendMessage: sendMessage,
|
|
|
|
sendCoreCommand: sendCoreCommand
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-02-18 00:49:42 +01:00
|
|
|
}]);
|
|
|
|
|
2013-12-17 14:40:49 +01:00
|
|
|
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', '$log', 'models', 'connection', function ($rootScope, $scope, $store, $timeout, $log, models, connection) {
|
2013-10-24 22:51:50 +02:00
|
|
|
if(window.Notification) {
|
2013-10-24 15:26:37 +02:00
|
|
|
// Request notification permission
|
|
|
|
Notification.requestPermission(function (status) {
|
|
|
|
$log.info('Notification permission status:',status);
|
|
|
|
if (Notification.permission !== status) {
|
|
|
|
Notification.permission = status;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2013-12-16 17:09:17 +01:00
|
|
|
|
2013-12-16 14:18:17 +01:00
|
|
|
|
2013-12-17 21:30:22 +01:00
|
|
|
$rootScope.countWatchers = function () {
|
2013-12-16 17:09:17 +01:00
|
|
|
var root = $(document.getElementsByTagName('body'));
|
|
|
|
var watchers = [];
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-12-16 17:09:17 +01:00
|
|
|
var f = function (element) {
|
|
|
|
if (element.data().hasOwnProperty('$scope')) {
|
|
|
|
angular.forEach(element.data().$scope.$$watchers, function (watcher) {
|
|
|
|
watchers.push(watcher);
|
|
|
|
});
|
|
|
|
}
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-12-16 17:09:17 +01:00
|
|
|
angular.forEach(element.children(), function (childElement) {
|
|
|
|
f($(childElement));
|
|
|
|
});
|
|
|
|
};
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-12-16 17:09:17 +01:00
|
|
|
f(root);
|
|
|
|
console.log(watchers.length);
|
|
|
|
};
|
|
|
|
|
2013-12-16 14:18:17 +01:00
|
|
|
if(window.webkitNotifications !== undefined) {
|
|
|
|
if (window.webkitNotifications.checkPermission() === 0) { // 0 is PERMISSION_ALLOWED
|
|
|
|
$log.info('Notification permission status:', window.webkitNotifications.checkPermission() === 0);
|
2013-10-06 12:34:41 +02:00
|
|
|
window.webkitNotifications.requestPermission();
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 15:51:05 +02:00
|
|
|
// Check for firefox & app installed
|
2013-12-16 14:18:17 +01:00
|
|
|
if(navigator.mozApps !== undefined) {
|
2013-10-21 15:51:05 +02:00
|
|
|
navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) {
|
|
|
|
var app = evt.target.result;
|
|
|
|
if(app) {
|
|
|
|
$scope.isinstalled = true;
|
|
|
|
}else {
|
|
|
|
$scope.isinstalled = false;
|
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-21 15:51:05 +02:00
|
|
|
}else {
|
|
|
|
$scope.isinstalled = false;
|
|
|
|
}
|
2013-10-06 12:34:41 +02:00
|
|
|
|
2013-10-12 22:09:02 +02:00
|
|
|
$rootScope.$on('activeBufferChanged', function() {
|
2013-12-28 16:50:31 +01:00
|
|
|
$rootScope.scrollWithBuffer(true);
|
2013-10-27 22:04:21 +01:00
|
|
|
|
2013-10-12 22:09:02 +02:00
|
|
|
var ab = models.getActiveBuffer();
|
|
|
|
$rootScope.pageTitle = ab.shortName + ' | ' + ab.title;
|
2013-10-19 12:14:32 +02:00
|
|
|
|
|
|
|
// If user wants to sync hotlist with weechat
|
|
|
|
// we will send a /buffer bufferName command every time
|
|
|
|
// the user switches a buffer. This will ensure that notifications
|
|
|
|
// are cleared in the buffer the user switches to
|
2013-12-17 21:30:22 +01:00
|
|
|
if($scope.hotlistsync && ab.fullName) {
|
2013-10-19 12:14:32 +02:00
|
|
|
connection.sendCoreCommand('/buffer ' + ab.fullName);
|
|
|
|
}
|
2013-10-22 14:24:09 +02:00
|
|
|
|
|
|
|
// Clear search term on buffer change
|
|
|
|
$scope.search = '';
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-27 10:48:20 +01:00
|
|
|
// Check if we should show nicklist or not
|
|
|
|
$scope.showNicklist = $scope.updateShowNicklist();
|
2013-10-12 22:09:02 +02:00
|
|
|
});
|
2013-10-15 15:21:13 +02:00
|
|
|
$rootScope.$on('notificationChanged', function() {
|
2014-01-22 11:32:08 +01:00
|
|
|
var notifications = _.reduce(models.model.buffers, function(memo, num) { return (parseInt(memo)||0) + num.notification;});
|
2013-10-15 15:21:13 +02:00
|
|
|
if (notifications > 0 ) {
|
|
|
|
$scope.favico = new Favico({
|
|
|
|
animation:'none'
|
|
|
|
});
|
|
|
|
$scope.favico.badge(notifications);
|
|
|
|
}else {
|
2014-01-22 11:32:08 +01:00
|
|
|
var unread = _.reduce(models.model.buffers, function(memo, num) { return (parseInt(memo)||0) + num.unread;});
|
2013-10-15 15:21:13 +02:00
|
|
|
$scope.favico = new Favico({
|
|
|
|
animation:'none',
|
|
|
|
bgColor : '#5CB85C',
|
2013-12-17 21:39:22 +01:00
|
|
|
textColor : '#ff0'
|
2013-10-15 15:21:13 +02:00
|
|
|
});
|
|
|
|
$scope.favico.badge(unread);
|
|
|
|
}
|
|
|
|
});
|
2013-10-12 22:09:02 +02:00
|
|
|
|
2013-10-08 15:55:07 +02:00
|
|
|
$scope.buffers = models.model.buffers;
|
2013-12-16 14:09:01 +01:00
|
|
|
$scope.activeBuffer = models.getActiveBuffer;
|
2013-10-08 15:55:07 +02:00
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
$rootScope.commands = [];
|
2013-02-16 19:18:14 +01:00
|
|
|
|
2013-10-08 15:55:07 +02:00
|
|
|
$rootScope.models = models;
|
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
$rootScope.buffer = [];
|
2013-10-08 15:55:07 +02:00
|
|
|
|
2013-10-27 07:48:30 +01:00
|
|
|
$rootScope.iterCandidate = null;
|
|
|
|
|
2013-10-11 23:15:08 +02:00
|
|
|
$store.bind($scope, "host", "localhost");
|
|
|
|
$store.bind($scope, "port", "9001");
|
2013-10-05 16:05:16 +02:00
|
|
|
$store.bind($scope, "proto", "weechat");
|
2013-10-09 17:53:25 +02:00
|
|
|
$store.bind($scope, "ssl", false);
|
2013-10-11 23:21:51 +02:00
|
|
|
$store.bind($scope, "lines", "40");
|
2013-10-25 13:43:03 +02:00
|
|
|
$store.bind($scope, "savepassword", false);
|
|
|
|
if($scope.savepassword) {
|
|
|
|
$store.bind($scope, "password", "");
|
|
|
|
}
|
2013-10-16 15:45:38 +02:00
|
|
|
|
2013-10-17 10:50:29 +02:00
|
|
|
// Save setting for displaying only buffers with unread messages
|
2013-10-16 15:45:38 +02:00
|
|
|
$store.bind($scope, "onlyUnread", false);
|
2013-10-17 10:50:29 +02:00
|
|
|
// Save setting for not showing timestamp
|
|
|
|
$store.bind($scope, "notimestamp", false);
|
2013-10-19 12:14:32 +02:00
|
|
|
// Save setting for syncing hotlist
|
|
|
|
$store.bind($scope, "hotlistsync", true);
|
2013-10-26 22:56:52 +02:00
|
|
|
// Save setting for displaying nicklist
|
2013-12-17 21:30:22 +01:00
|
|
|
$store.bind($scope, "nonicklist", false);
|
2013-10-28 13:21:51 +01:00
|
|
|
// Save setting for displaying embeds
|
2013-12-17 21:30:22 +01:00
|
|
|
$store.bind($scope, "noembed", false);
|
2013-12-08 22:29:48 +01:00
|
|
|
// Save setting for channel ordering
|
|
|
|
$store.bind($scope, "orderbyserver", false);
|
2013-10-28 13:21:51 +01:00
|
|
|
// Save setting for displaying embeds in rootscope so it can be used from service
|
2013-12-16 14:18:17 +01:00
|
|
|
$rootScope.visible = $scope.noembed === false;
|
2013-12-18 23:09:53 +01:00
|
|
|
|
|
|
|
// If we are on mobile chhange some defaults
|
|
|
|
// We use 968 px as the cutoff, which should match the value in glowingbear.css
|
|
|
|
if(document.body.clientWidth < 968) {
|
|
|
|
$scope.nonicklist = true;
|
|
|
|
$scope.noembed = true;
|
|
|
|
$scope.notimestamp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-28 13:21:51 +01:00
|
|
|
// Watch model and update show setting when it changes
|
|
|
|
$scope.$watch('noembed', function() {
|
2013-12-16 14:18:17 +01:00
|
|
|
$rootScope.visible = $scope.noembed === false;
|
2013-10-28 13:21:51 +01:00
|
|
|
});
|
2013-12-08 22:29:48 +01:00
|
|
|
// Watch model and update channel sorting when it changes
|
|
|
|
$scope.$watch('orderbyserver', function() {
|
|
|
|
$rootScope.predicate = $scope.orderbyserver ? 'serverSortKey' : 'number';
|
|
|
|
});
|
|
|
|
|
|
|
|
$rootScope.predicate = $scope.orderbyserver ? 'serverSortKey' : 'number';
|
2013-10-02 02:32:18 +02:00
|
|
|
|
2013-12-14 15:40:11 +01:00
|
|
|
$scope.setActiveBuffer = function(bufferId, key) {
|
2013-12-18 23:09:53 +01:00
|
|
|
// If we are on mobile we need to collapse the menu on sidebar clicks
|
|
|
|
// We use 968 px as the cutoff, which should match the value in glowingbear.css
|
|
|
|
if(document.body.clientWidth<968) {
|
|
|
|
$('#sidebar').collapse();
|
|
|
|
}
|
2013-12-14 15:40:11 +01:00
|
|
|
return models.setActiveBuffer(bufferId, key);
|
2013-07-30 15:22:37 +02:00
|
|
|
};
|
|
|
|
|
2013-12-14 15:40:11 +01:00
|
|
|
$scope.openQuery = function(nick) {
|
2013-12-16 14:00:59 +01:00
|
|
|
var buffName = models.getActiveBuffer().fullName;
|
2013-12-14 15:40:11 +01:00
|
|
|
buffName = buffName.substring(0, buffName.lastIndexOf('.')) + '.' + nick;
|
|
|
|
|
|
|
|
if (!$scope.setActiveBuffer(buffName, 'fullName')) {
|
|
|
|
connection.sendMessage('/query ' + nick);
|
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-12-14 15:40:11 +01:00
|
|
|
|
2013-12-28 16:50:31 +01:00
|
|
|
$rootScope.scrollWithBuffer = function(nonIncremental) {
|
|
|
|
// First, get scrolling status *before* modification
|
|
|
|
// This is required to determine where we were in the buffer pre-change
|
|
|
|
var bl = document.getElementById('bufferlines');
|
|
|
|
var sVal = bl.scrollHeight - bl.clientHeight;
|
|
|
|
|
2013-10-16 23:59:27 +02:00
|
|
|
var scroll = function() {
|
2013-12-09 11:10:11 +01:00
|
|
|
var sTop = bl.scrollTop;
|
2013-12-28 16:50:31 +01:00
|
|
|
// Determine if we want to scroll at all
|
|
|
|
if (nonIncremental && sTop < sVal || sTop == sVal) {
|
|
|
|
var readmarker = document.getElementById('readmarker');
|
|
|
|
if(nonIncremental && readmarker) {
|
|
|
|
// Switching channels, scroll to read marker
|
|
|
|
readmarker.scrollIntoView();
|
|
|
|
} else {
|
|
|
|
// New message, scroll with buffer (i.e. to bottom)
|
|
|
|
bl.scrollTop = bl.scrollHeight - bl.clientHeight;
|
2013-10-22 19:58:12 +02:00
|
|
|
}
|
2013-10-11 18:45:01 +02:00
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-22 19:58:12 +02:00
|
|
|
// Here be scrolling dragons
|
|
|
|
$timeout(scroll);
|
|
|
|
$timeout(scroll, 100);
|
2013-10-16 23:59:27 +02:00
|
|
|
$timeout(scroll, 300);
|
2013-10-22 19:58:12 +02:00
|
|
|
$timeout(scroll, 500);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-07 16:58:41 +02:00
|
|
|
|
2013-02-16 19:18:14 +01:00
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.connect = function() {
|
2013-12-20 17:10:58 +01:00
|
|
|
connection.connect($scope.host, $scope.port, $scope.password, $scope.ssl);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-16 14:44:44 +02:00
|
|
|
$scope.disconnect = function() {
|
2013-10-16 14:25:07 +02:00
|
|
|
connection.disconnect();
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-21 15:51:05 +02:00
|
|
|
$scope.install = function() {
|
2013-12-16 14:18:17 +01:00
|
|
|
if(navigator.mozApps !== undefined) {
|
2013-10-21 15:51:05 +02:00
|
|
|
var request = navigator.mozApps.install('http://torhve.github.io/glowing-bear/manifest.webapp');
|
|
|
|
request.onsuccess = function () {
|
|
|
|
$scope.isinstalled = true;
|
|
|
|
// Save the App object that is returned
|
|
|
|
var appRecord = this.result;
|
|
|
|
// Start the app.
|
|
|
|
appRecord.launch();
|
|
|
|
alert('Installation successful!');
|
|
|
|
};
|
|
|
|
request.onerror = function () {
|
|
|
|
// Display the error information from the DOMError object
|
|
|
|
alert('Install failed, error: ' + this.error.name);
|
|
|
|
};
|
|
|
|
}else{
|
|
|
|
alert('Sorry. Only supported in Firefox v26+');
|
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-21 15:51:05 +02:00
|
|
|
|
2013-10-06 12:34:41 +02:00
|
|
|
|
|
|
|
/* Function gets called from bufferLineAdded code if user should be notified */
|
2013-10-09 17:53:25 +02:00
|
|
|
$rootScope.createHighlight = function(buffer, message) {
|
2013-10-06 12:34:41 +02:00
|
|
|
var messages = "";
|
2013-10-09 17:53:25 +02:00
|
|
|
message.content.forEach(function(part) {
|
2013-12-16 14:18:17 +01:00
|
|
|
if (part.text !== undefined)
|
2013-10-09 17:53:25 +02:00
|
|
|
messages += part.text + " ";
|
|
|
|
});
|
2013-10-06 12:34:41 +02:00
|
|
|
|
2013-10-09 17:53:25 +02:00
|
|
|
var title = buffer.fullName;
|
|
|
|
var content = messages;
|
2013-10-06 12:34:41 +02:00
|
|
|
|
|
|
|
var timeout = 15*1000;
|
2013-10-16 01:09:31 +02:00
|
|
|
$log.info('Displaying notification:buffer:',buffer,',message:',message,',with timeout:',timeout);
|
2013-10-06 12:34:41 +02:00
|
|
|
var notification = new Notification(title, {body:content, icon:'img/favicon.png'});
|
|
|
|
// Cancel notification automatically
|
|
|
|
notification.onshow = function() {
|
2013-12-17 21:30:22 +01:00
|
|
|
setTimeout(function() {
|
2013-12-16 14:09:01 +01:00
|
|
|
notification.close();
|
|
|
|
}, timeout);
|
|
|
|
};
|
2013-10-06 12:34:41 +02:00
|
|
|
};
|
2013-10-11 14:44:05 +02:00
|
|
|
|
|
|
|
$scope.hasUnread = function(buffer) {
|
2013-12-17 21:30:22 +01:00
|
|
|
// if search is set, return every buffer
|
2013-12-16 14:18:17 +01:00
|
|
|
if($scope.search && $scope.search !== "") {
|
2013-10-11 23:47:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-10-19 20:11:01 +02:00
|
|
|
if($scope.onlyUnread) {
|
|
|
|
// Always show current buffer in list
|
|
|
|
if (models.getActiveBuffer() == buffer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return buffer.unread > 0 || buffer.notification > 0;
|
|
|
|
}
|
|
|
|
return true;
|
2013-10-11 14:44:05 +02:00
|
|
|
};
|
2013-10-11 15:59:55 +02:00
|
|
|
|
2013-10-27 10:48:20 +01:00
|
|
|
// Watch model and update show setting when it changes
|
|
|
|
$scope.$watch('nonicklist', function() {
|
|
|
|
$scope.showNicklist = $scope.updateShowNicklist();
|
|
|
|
});
|
|
|
|
$scope.showNicklist = false;
|
2013-12-17 21:30:22 +01:00
|
|
|
// Utility function that template can use to check if nicklist should
|
2013-10-27 10:48:20 +01:00
|
|
|
// be displayed for current buffer or not
|
|
|
|
// is called on buffer switch
|
|
|
|
$scope.updateShowNicklist = function() {
|
|
|
|
var ab = models.getActiveBuffer();
|
|
|
|
if(!ab) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if option no nicklist is set
|
|
|
|
if($scope.nonicklist) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Use flat nicklist to check if empty
|
2013-12-17 21:30:22 +01:00
|
|
|
if(ab.flatNicklist().length === 0) {
|
2013-10-27 10:48:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 10:48:20 +01:00
|
|
|
|
2013-10-11 15:59:55 +02:00
|
|
|
$rootScope.switchToActivityBuffer = function() {
|
|
|
|
// Find next buffer with activity and switch to it
|
2013-12-16 14:20:31 +01:00
|
|
|
for(var i in $scope.buffers) {
|
2013-10-11 22:43:01 +02:00
|
|
|
var buffer = $scope.buffers[i];
|
2013-10-15 14:59:06 +02:00
|
|
|
if(buffer.notification > 0) {
|
2013-10-11 15:59:55 +02:00
|
|
|
$scope.setActiveBuffer(buffer.id);
|
2013-10-11 22:43:01 +02:00
|
|
|
break;
|
2013-10-15 14:59:06 +02:00
|
|
|
}else if(buffer.unread > 0) {
|
2013-10-11 15:59:55 +02:00
|
|
|
$scope.setActiveBuffer(buffer.id);
|
2013-10-11 22:43:01 +02:00
|
|
|
break;
|
2013-10-11 15:59:55 +02:00
|
|
|
}
|
2013-10-11 22:43:01 +02:00
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-11 15:59:55 +02:00
|
|
|
|
2013-10-27 07:48:30 +01:00
|
|
|
|
2013-10-17 13:13:01 +02:00
|
|
|
$scope.handleSearchBoxKey = function($event) {
|
|
|
|
// Support different browser quirks
|
|
|
|
var code = $event.keyCode ? $event.keyCode : $event.charCode;
|
|
|
|
// Handle escape
|
|
|
|
if(code == 27) {
|
2013-10-19 20:11:01 +02:00
|
|
|
$event.preventDefault();
|
|
|
|
$scope.search = '';
|
2013-10-17 13:13:01 +02:00
|
|
|
} // Handle enter
|
|
|
|
else if (code == 13) {
|
2013-10-19 20:11:01 +02:00
|
|
|
$event.preventDefault();
|
|
|
|
// TODO Switch to first matching buffer and reset query
|
|
|
|
$scope.search = '';
|
2013-10-17 13:13:01 +02:00
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-15 15:21:13 +02:00
|
|
|
|
2013-10-28 13:55:46 +01:00
|
|
|
// Prevent user from accidentally leaving the page
|
|
|
|
window.onbeforeunload = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
};
|
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
}]
|
2013-10-26 10:30:35 +02:00
|
|
|
);
|
2013-10-27 20:49:51 +01:00
|
|
|
|
2013-12-17 21:30:22 +01:00
|
|
|
weechat.config(['$routeProvider',
|
2013-10-27 20:49:51 +01:00
|
|
|
|
|
|
|
function($routeProvider) {
|
|
|
|
$routeProvider.when('/', {
|
|
|
|
templateUrl: 'index.html',
|
2013-12-17 21:39:22 +01:00
|
|
|
controller: 'WeechatCtrl'
|
2013-10-27 20:49:51 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
]);
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
weechat.directive('inputBar', function() {
|
2013-10-27 22:04:21 +01:00
|
|
|
|
2013-10-27 21:41:27 +01:00
|
|
|
return {
|
|
|
|
|
|
|
|
templateUrl: 'directives/input.html',
|
|
|
|
controller: function($rootScope,
|
|
|
|
$scope,
|
|
|
|
connection,
|
|
|
|
models) {
|
|
|
|
|
|
|
|
// Focuses itself when active buffer is changed
|
2013-12-14 19:51:34 +01:00
|
|
|
/*
|
2013-10-27 21:41:27 +01:00
|
|
|
$rootScope.$on('activeBufferChanged', function() {
|
|
|
|
angular.element('#sendMessage').focus();
|
|
|
|
});
|
2013-12-14 19:51:34 +01:00
|
|
|
*/
|
2013-10-27 21:41:27 +01:00
|
|
|
|
2013-10-27 22:09:38 +01:00
|
|
|
$scope.completeNick = function() {
|
2013-10-27 21:41:27 +01:00
|
|
|
// input DOM node
|
|
|
|
var inputNode = document.getElementById('sendMessage');
|
|
|
|
|
|
|
|
// get current input
|
|
|
|
var inputText = inputNode.value;
|
|
|
|
|
|
|
|
// get current caret position
|
|
|
|
var caretPos = inputNode.selectionStart;
|
|
|
|
|
|
|
|
// create flat array of nicks
|
|
|
|
var activeBuffer = models.getActiveBuffer();
|
|
|
|
|
|
|
|
// complete nick
|
|
|
|
var nickComp = IrcUtils.completeNick(inputText, caretPos,
|
2013-10-27 22:09:38 +01:00
|
|
|
$scope.iterCandidate, activeBuffer.flatNicklist(), ':');
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
// remember iteration candidate
|
2013-10-27 22:09:38 +01:00
|
|
|
$scope.iterCandidate = nickComp.iterCandidate;
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
// update current input
|
|
|
|
$scope.command = nickComp.text;
|
|
|
|
|
|
|
|
// update current caret position
|
|
|
|
inputNode.focus();
|
|
|
|
inputNode.setSelectionRange(nickComp.caretPos, nickComp.caretPos);
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Send the message to the websocket
|
|
|
|
$scope.sendMessage = function() {
|
2013-11-09 18:40:27 +01:00
|
|
|
connection.sendMessage($scope.command);
|
2013-12-17 20:37:45 +01:00
|
|
|
$scope.command = models.getActiveBuffer().addToHistory($scope.command); // log to buffer history
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
// Handle key presses in the input bar
|
|
|
|
$scope.handleKeyPress = function($event) {
|
|
|
|
// don't do anything if not connected
|
|
|
|
if (!$rootScope.connected) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-27 21:41:27 +01:00
|
|
|
// Support different browser quirks
|
|
|
|
var code = $event.keyCode ? $event.keyCode : $event.charCode;
|
|
|
|
|
|
|
|
// any other key than Tab resets nick completion iteration
|
2013-10-27 22:09:38 +01:00
|
|
|
var tmpIterCandidate = $scope.iterCandidate;
|
|
|
|
$scope.iterCandidate = null;
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
// Left Alt+[0-9] -> jump to buffer
|
|
|
|
if ($event.altKey && !$event.ctrlKey && (code > 47 && code < 58)) {
|
|
|
|
if (code == 48) {
|
|
|
|
code = 58;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufferNumber = code - 48;
|
|
|
|
var activeBuffer = models.getBufferByIndex(bufferNumber);
|
|
|
|
if (activeBuffer) {
|
|
|
|
models.setActiveBuffer(activeBuffer.id);
|
|
|
|
$event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tab -> nick completion
|
|
|
|
if (code == 9 && !$event.altKey && !$event.ctrlKey) {
|
|
|
|
$event.preventDefault();
|
2013-10-27 22:09:38 +01:00
|
|
|
$scope.iterCandidate = tmpIterCandidate;
|
|
|
|
$scope.completeNick();
|
2013-10-27 21:41:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-29 12:50:02 +01:00
|
|
|
// Left Alt+n -> toggle nicklist
|
|
|
|
if ($event.altKey && !$event.ctrlKey && code == 78) {
|
|
|
|
$event.preventDefault();
|
|
|
|
$scope.nonicklist = !$scope.nonicklist;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-27 21:41:27 +01:00
|
|
|
// Alt+A -> switch to buffer with activity
|
|
|
|
if ($event.altKey && (code == 97 || code == 65)) {
|
|
|
|
$event.preventDefault();
|
2013-10-27 22:09:38 +01:00
|
|
|
$scope.switchToActivityBuffer();
|
2013-10-27 21:41:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alt+L -> focus on input bar
|
|
|
|
if ($event.altKey && (code == 76 || code == 108)) {
|
|
|
|
$event.preventDefault();
|
|
|
|
var inputNode = document.getElementById('sendMessage');
|
|
|
|
inputNode.focus();
|
|
|
|
inputNode.setSelectionRange(inputNode.value.length, inputNode.value.length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escape -> disconnect
|
|
|
|
if (code == 27) {
|
|
|
|
$event.preventDefault();
|
|
|
|
connection.disconnect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+G -> focus on buffer filter input
|
|
|
|
if ($event.ctrlKey && (code == 103 || code == 71)) {
|
|
|
|
$event.preventDefault();
|
|
|
|
document.getElementById('bufferFilter').focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-17 20:37:45 +01:00
|
|
|
// Arrow up -> go up in history
|
|
|
|
if (code == 38) {
|
|
|
|
$scope.command = models.getActiveBuffer().getHistoryUp($scope.command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrow down -> go down in history
|
|
|
|
if (code == 40) {
|
|
|
|
$scope.command = models.getActiveBuffer().getHistoryDown($scope.command);
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 21:41:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-16 14:09:01 +01:00
|
|
|
};
|
2013-10-27 21:41:27 +01:00
|
|
|
});
|