Maintained cached flatnicklist, maintain shownicklist cached value for

template
This commit is contained in:
Tor Hveem 2013-10-27 10:48:20 +01:00
parent a415a3ec3c
commit e8e44d85ce
3 changed files with 58 additions and 13 deletions

View file

@ -225,8 +225,8 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel
</li>
</ul>
</div>
<div id="bufferlines" class="monospace" ng-class="{'withnicklist': nonicklist==false}">
<div id="nicklist" ng-hide="nonicklist" class="vertical-line-left">
<div id="bufferlines" class="monospace" ng-class="{'withnicklist': showNicklist}">
<div id="nicklist" ng-show="showNicklist" class="vertical-line-left">
<ul class="nicklistgroup list-unstyled" ng-repeat="group in activeBuffer().nicklist">
<li class="" ng-repeat="nick in group.nicks|orderBy:'nick.name'">
<a ng-click="nickAction(nick)"><span ng-class="nick.prefixClasses">{{nick.prefix}}</span><span ng-class="nick.nameClasses">{{nick.name}}</span></a>
@ -268,7 +268,7 @@ $ openssl req -nodes -newkey rsa:2048 -keyout relay.pem -x509 -days 365 -out rel
<div class="push"></div>
</div>
<div id="footer" ng-show="connected">
<div class="navbar navbar-inverse navbar-fixed-bottom" ng-class="{'withnicklist': nonicklist==false}">
<div class="navbar navbar-inverse navbar-fixed-bottom" ng-class="{'withnicklist': showNicklist}">
<form class="form form-horizontal" ng-submit="sendMessage()">
<div class="input-group">
<input id="sendMessage" type="text" class="form-control monospace" autocomplete="off" ng-model="command" autofocus>

View file

@ -19,6 +19,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
var notify = 3 // Default 3 == message
var lines = []
var nicklist = {}
var flatnicklist = []
var active = false
var notification = 0
var unread = 0
@ -44,6 +45,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
*/
var addNick = function(group, nick) {
nicklist[group].nicks.push(nick);
flatnicklist = getFlatNicklist();
}
/*
* Deletes a nick from nicklist
@ -51,6 +53,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
var delNick = function(group, nick) {
var group = nicklist[group];
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) {
@ -71,8 +74,29 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
break;
}
}
flatnicklist = getFlatNicklist();
}
/*
* Maintain a cached version of a flat sorted nicklist
*
*/
var getFlatNicklist = function() {
var newlist = [];
_.each(nicklist, function(nickGroup) {
_.each(nickGroup.nicks, function(nickObj) {
newlist.push(nickObj.name);
});
});
newlist.sort(function(a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
});
return newlist;
}
var flatNicklist = function() {
return flatnicklist;
}
return {
id: pointer,
@ -90,7 +114,8 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
nicklist: nicklist,
addNick: addNick,
delNick: delNick,
updateNick: updateNick
updateNick: updateNick,
flatNicklist: flatNicklist
}
}

View file

@ -128,7 +128,7 @@ weechat.factory('handlers', ['$rootScope', 'models', 'plugins', function($rootSc
buffer.nicklist[group] = g;
}else{
var nick = new models.Nick(n);
buffer.nicklist[group].nicks.push(nick);
buffer.addNick(group, nick);
}
});
}
@ -416,6 +416,9 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
// Clear search term on buffer change
$scope.search = '';
// Check if we should show nicklist or not
$scope.showNicklist = $scope.updateShowNicklist();
});
$rootScope.$on('notificationChanged', function() {
var notifications = _.reduce(models.model.buffers, function(memo, num) { return (memo||0) + num.notification;});
@ -558,6 +561,30 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
return true;
};
// Watch model and update show setting when it changes
$scope.$watch('nonicklist', function() {
$scope.showNicklist = $scope.updateShowNicklist();
});
$scope.showNicklist = false;
// Utility function that template can use to check if nicklist should
// 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
if(ab.flatNicklist().length === 0) {
return false;
}
return true;
}
$rootScope.switchToActivityBuffer = function() {
// Find next buffer with activity and switch to it
for(i in $scope.buffers) {
@ -583,18 +610,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
var caretPos = inputNode.selectionStart;
// create flat array of nicks
// TODO: compute this elsewhere since it doesn't change often
var activeBuffer = models.getActiveBuffer();
var flatNickList = [];
_.each(activeBuffer.nicklist, function(nickGroup) {
_.each(nickGroup.nicks, function(nickObj) {
flatNickList.push(nickObj.name);
});
});
// complete nick
var nickComp = IrcUtils.completeNick(inputText, caretPos,
$rootScope.iterCandidate, flatNickList, ':');
$rootScope.iterCandidate, activeBuffer.flatNicklist(), ':');
// remember iteration candidate
$rootScope.iterCandidate = nickComp.iterCandidate;