From bebc1c7bcec429e5845a93576643346d24582141 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Wed, 26 Feb 2014 19:39:48 +0100 Subject: [PATCH] Remove duplicate bufferlist storage. Only use object --- js/glowingbear.js | 8 ++++---- js/models.js | 40 ++++++++++++---------------------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 779d581..81ec6fd 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -1077,10 +1077,10 @@ weechat.directive('inputBar', function() { code = 58; } - var bufferNumber = code - 48; - var activeBuffer = models.getBufferByIndex(bufferNumber); - if (activeBuffer) { - models.setActiveBuffer(activeBuffer.id); + var bufferNumber = code - 48 - 1 ; + var activeBufferId = Object.keys(models.getBuffers())[bufferNumber]; + if (activeBufferId) { + models.setActiveBuffer(activeBufferId); $event.preventDefault(); } } diff --git a/js/models.js b/js/models.js index a222a52..427aa79 100644 --- a/js/models.js +++ b/js/models.js @@ -327,7 +327,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) }; - var BufferList = []; var activeBuffer = null; var previousBuffer = null; @@ -340,24 +339,9 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) * @return undefined */ this.addBuffer = function(buffer) { - BufferList[buffer.id] = buffer; - if (BufferList.length === 1) { - activeBuffer = buffer; - } this.model.buffers[buffer.id] = buffer; }; - this.getBufferByIndex = function(index) { - var i = 0; - - for (var v in BufferList) { - if (index === ++i) { - return BufferList[v]; - } - } - - }; - /* * Returns the current active buffer * @@ -390,11 +374,16 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) previousBuffer = this.getActiveBuffer(); - activeBuffer = _.find(this.model.buffers, function(buffer) { - if (buffer[key] === bufferId) { - return buffer; - } - }); + if (key === 'id') { + activeBuffer = this.model.buffers[bufferId]; + } + else { + activeBuffer = _.find(this.model.buffers, function(buffer) { + if (buffer[key] === bufferId) { + return buffer; + } + }); + } if (activeBuffer === undefined) { // Buffer not found, undo assignment @@ -422,14 +411,13 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) * Returns the buffer list */ this.getBuffers = function() { - return BufferList; + return this.model.buffers; }; /* * Reinitializes the model */ this.reinitialize = function() { - BufferList = []; this.model.buffers = {}; }; @@ -440,11 +428,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter) * @return the buffer object */ this.getBuffer = function(bufferId) { - return _.find(this.model.buffers, function(buffer) { - if (buffer.id === bufferId) { - return buffer; - } - }); + return this.model.buffers[bufferId]; }; /*