From 10d5a762ecbf80f144d596f4958abd7a12d15104 Mon Sep 17 00:00:00 2001 From: Kevin Glowacz Date: Sun, 7 Jun 2009 23:55:42 +0000 Subject: [PATCH] (trunk web) store torrents in a hash instead of an array --- web/javascript/torrent.js | 12 ------------ web/javascript/transmission.js | 24 ++++++++++++++---------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/web/javascript/torrent.js b/web/javascript/torrent.js index 3d84a9782..1f06cc69d 100644 --- a/web/javascript/torrent.js +++ b/web/javascript/torrent.js @@ -635,18 +635,6 @@ Torrent.indexOf = function( torrents, id ) } }; -/** - * @param torrents an array of torrents sorted by Id - * @param id the id to search for - * @return the torrent, or null - */ -Torrent.lookup = function( torrents, id ) -{ - var immutable_array = torrents.slice(); - var pos = Torrent.indexOf( immutable_array, id ); - return pos >= 0 ? immutable_array[pos] : null; -}; - function TorrentFile(file_data) { this.initialize(file_data); } diff --git a/web/javascript/transmission.js b/web/javascript/transmission.js index ea9a95d81..0762ee83f 100644 --- a/web/javascript/transmission.js +++ b/web/javascript/transmission.js @@ -25,7 +25,7 @@ Transmission.prototype = // Initialize the implementation fields this._current_search = ''; - this._torrents = [ ]; + this._torrents = { }; this._rows = [ ]; // Initialize the clutch preferences @@ -310,7 +310,10 @@ Transmission.prototype = getAllTorrents: function() { - return this._torrents; + var torrents = []; + for(var key in this._torrents) + torrents.push(this._torrents[key]); + return torrents; }, getVisibleTorrents: function() @@ -657,7 +660,7 @@ Transmission.prototype = var match = $(element).closest('.inspector_torrent_file_list_entry').attr('id').match(/^t(\d+)f(\d+)$/); var torrent_id = match[1]; var file_id = match[2]; - var torrent = Torrent.lookup( this._torrents, torrent_id ); + var torrent = this._torrents[torrent_id]; return torrent._file_view[file_id]; }, @@ -1151,7 +1154,7 @@ Transmission.prototype = var new_torrent_ids = []; var refresh_files_for = []; jQuery.each( active, function() { - var t = Torrent.lookup(tr._torrents, this.id); + var t = tr._torrents[this.id]; if (t){ t.refresh(this); if(t.isSelected()) @@ -1181,7 +1184,7 @@ Transmission.prototype = var tr = this; var listIsVisible = tr.fileListIsVisible( ); jQuery.each( torrents, function() { - var t = Torrent.lookup(tr._torrents, this.id); + var t = tr._torrents[this.id]; if (t) { t.refreshFileModel(this); if( listIsVisible && t.isSelected()) @@ -1200,8 +1203,10 @@ Transmission.prototype = var transferFragment = document.createDocumentFragment( ); var fileFragment = document.createDocumentFragment( ); - for( var i=0, row; row=new_torrents[i]; ++i ) - this._torrents.push( new Torrent( transferFragment, fileFragment, this, row ) ); + for( var i=0, row; row=new_torrents[i]; ++i ) { + var new_torrent = new Torrent( transferFragment, fileFragment, this, row ); + this._torrents[new_torrent.id()] = new_torrent; + } this._inspector_file_list.appendChild( fileFragment ); this._torrent_list.appendChild( transferFragment ); @@ -1215,7 +1220,7 @@ Transmission.prototype = var tr = this; var removedAny = false; $.each( torrent_ids, function(index, id){ - var torrent = Torrent.lookup(tr._torrents, id); + var torrent = tr._torrents[id]; if(torrent) { removedAny = true; @@ -1227,10 +1232,9 @@ Transmission.prototype = e.remove(); } - var pos = Torrent.indexOf( tr._torrents, torrent.id( ) ); torrent.hideFileList(); torrent.deleteFiles(); - tr._torrents.splice( pos, 1 ); + delete tr._torrents[torrent.id()]; } });