From f836308b42ffdf408ed1520cc77c61e5933dd713 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Sat, 3 Sep 2011 04:33:04 +0000 Subject: [PATCH] (trunk web) better encapsulation of FileRow -- make most of its fields and methods private. transmission.js and remote.js don't know about FileRow anymore. --- web/javascript/file-row.js | 171 ++++++++++++++++----------------- web/javascript/inspector.js | 28 +++--- web/javascript/remote.js | 20 ++-- web/javascript/transmission.js | 4 +- 4 files changed, 108 insertions(+), 115 deletions(-) diff --git a/web/javascript/file-row.js b/web/javascript/file-row.js index 94c6ce55b..057e64f27 100644 --- a/web/javascript/file-row.js +++ b/web/javascript/file-row.js @@ -6,120 +6,109 @@ function FileRow(torrent, i) { - this.initialize(torrent, i); -} - -FileRow.prototype = -{ - initialize: function(torrent, i) - { - this._torrent = torrent; - this._index = i; - this.createRow(torrent, i); + var fields = { + have: 0, + index: 0, + isDirty: false, + isWanted: true, + priority: 0, + me: this, + size: 0, + torrent: null }, - getTorrent: function() - { - return this._torrent; - }, - getIndex: function() - { - return this._index; + elements = { + priority_control: null, + progress: null, + root: null }, - readAttributes: function(file) - { - if (file.index !== undefined && file.index !== this._index) { - this._index = file.index; - this._dirty = true; + initialize = function(torrent, i) { + fields.torrent = torrent; + fields.index = i; + createRow(torrent, i); + }, + + readAttributes = function(file) { + if (fields.have !== file.bytesCompleted) { + fields.have = file.bytesCompleted; + fields.isDirty = true; } - if (file.bytesCompleted !== undefined && file.bytesCompleted !== this._done) { - this._done = file.bytesCompleted; - this._dirty = true; + if (fields.size !== file.length) { + fields.size = file.length; + fields.isDirty = true; } - if (file.length !== undefined && file.length !== this._size) { - this._size = file.length; - this._dirty = true; + if (fields.priority !== file.priority) { + fields.priority = file.priority; + fields.isDirty = true; } - if (file.priority !== undefined && file.priority !== this._prio) { - this._prio = file.priority; - this._dirty = true; - } - if (file.wanted !== undefined && file.wanted !== this._wanted) { - this._wanted = file.wanted; - this._dirty = true; + if (fields.isWanted !== file.wanted) { + fields.isWanted = file.wanted; + fields.isDirty = true; } }, - refreshWantedHTML: function() + refreshWantedHTML = function() { - var e = this.getElement(), + var e = elements.root, c = [ e.classNameConst ]; - if (!this._wanted) { c.push('skip'); } - if (this.isDone()) { c.push('complete'); } + if (!fields.isWanted) { c.push('skip'); } + if (isDone()) { c.push('complete'); } e.className = c.join(' '); }, - refreshPriorityHTML: function() + refreshPriorityHTML = function() { - var e = this._priority_control, + var e = elements.priority_control, c = [ e.classNameConst ]; - switch(this._prio) { + switch(fields.priority) { case -1 : c.push('low'); break; case 1 : c.push('high'); break; default : c.push('normal'); break; } e.className = c.join(' '); }, - refreshProgressHTML: function() + refreshProgressHTML = function() { - var pct = 100 * (this._size ? (this._done / this._size) : 1.0), - c = [ Transmission.fmt.size(this._done), + var pct = 100 * (fields.size ? (fields.have / fields.size) : 1.0), + c = [ Transmission.fmt.size(fields.have), ' of ', - Transmission.fmt.size(this._size), + Transmission.fmt.size(fields.size), ' (', Transmission.fmt.percentString(pct), '%)' ].join(''); - setInnerHTML(this._progress[0], c); + setInnerHTML(elements.progress, c); }, - refreshHTML: function() { - if (this._dirty) { - this._dirty = false; - this.refreshProgressHTML(); - this.refreshWantedHTML(); - this.refreshPriorityHTML(); + refreshHTML = function() { + if (fields.isDirty) { + fields.isDirty = false; + refreshProgressHTML(); + refreshWantedHTML(); + refreshPriorityHTML(); } }, - refresh: function() - { - var i = this.getIndex(), - t = this.getTorrent(); - this.readAttributes(t.getFile(i)); - this.refreshHTML(); + refresh = function() { + readAttributes(fields.torrent.getFile(fields.index)); + refreshHTML(); }, - isDone: function () { - return this._done >= this._size; - }, - isEditable: function () { - return (this.getTorrent().getFileCount()>1) && !this.isDone(); + isDone = function () { + return fields.have >= fields.size; }, - createRow: function(torrent, i) - { - var me = this, - file = torrent.getFile(i), + createRow = function(torrent, i) { + var file = torrent.getFile(i), name, root, wanted_div, pri_div, file_div, prog_div; root = document.createElement('li'); - root.id = 't' + this._torrent.getId() + 'f' + this._index; + root.id = 't' + fields.torrent.getId() + 'f' + fields.index; root.classNameConst = 'inspector_torrent_file_list_entry ' + ((i%2)?'odd':'even'); root.className = root.classNameConst; wanted_div = document.createElement('div'); wanted_div.className = "file_wanted_control"; - $(wanted_div).bind('click',function(){ me.fireWantedChanged(!me._wanted); }); + $(wanted_div).click(function(){ fireWantedChanged(!fields.isWanted); }); pri_div = document.createElement('div'); pri_div.classNameConst = "file_priority_control"; @@ -132,6 +121,7 @@ FileRow.prototype = x -= e.offsetLeft; e = e.offsetParent; } + // ugh. if (isMobileDevice) { if (x < 8) prio = -1; else if (x < 27) prio = 0; @@ -141,7 +131,7 @@ FileRow.prototype = else if (x < 23) prio = 0; else prio = 1; } - me.firePriorityChanged(prio); + firePriorityChanged(prio); }); name = file.name || 'Unknown'; @@ -159,25 +149,34 @@ FileRow.prototype = root.appendChild(file_div); root.appendChild(prog_div); - this._element = root; - this._priority_control = pri_div; - this._progress = $(prog_div); + elements.root = root; + elements.priority_control = pri_div; + elements.progress = prog_div; - this.refresh(); + refresh(); return root; }, - getElement: function() - { - return this._element; + fireWantedChanged = function(do_want) { + $(fields.me).trigger('wantedToggled',[ fields.me, do_want ]); }, + firePriorityChanged = function(priority) { + $(fields.me).trigger('priorityToggled',[ fields.me, priority ]); + }; - fireWantedChanged: function(do_want) - { - $(this).trigger('wantedToggled',[ this, do_want ]); - }, - firePriorityChanged: function(priority) - { - $(this).trigger('priorityToggled',[ this, priority ]); - } + /*** + **** PUBLIC + ***/ + + this.getElement = function() { + return elements.root; + }; + this.getIndex = function() { + return fields.index; + }; + this.isEditable = function () { + return (fields.torrent.getFileCount()>1) && !isDone(); + }; + + initialize(torrent, i); }; diff --git a/web/javascript/inspector.js b/web/javascript/inspector.js index 38b1a171b..e3148ebd5 100644 --- a/web/javascript/inspector.js +++ b/web/javascript/inspector.js @@ -201,26 +201,24 @@ function Inspector(controller) { filesSelectAllClicked = function() { filesAllClicked(true); }, filesDeselectAllClicked = function() { filesAllClicked(false); }, filesAllClicked = function(s) { - var t = data.file_torrent; - if (t) - toggleFilesWantedDisplay(t, s); - }, - - changeFileCommand = function(command, rows) { - data.controller.changeFileCommand(command, rows); - }, - - toggleFilesWantedDisplay = function(torrent, wanted) { - var i, row, rows=[]; + var i, row, rows=[], t=data.file_torrent; + if (!t) + return; for (i=0; row=data.file_rows[i]; ++i) - if (row.isEditable() && (torrent.getFile(i).wanted !== wanted)) + if (row.isEditable() && (t.getFile(i).wanted !== s)) rows.push(row); if (rows.length > 0) - changeFileCommand(wanted?'files-wanted':'files-unwanted', rows); + changeFileCommand(rows, s?'files-wanted':'files-unwanted'); + }, + + changeFileCommand = function(rows, command) { + var torrentId = data.file_torrent.getId(); + var rowIndices = $.map(rows.slice(0),function (row) {return row.getIndex();}); + data.controller.changeFileCommand(torrentId, rowIndices, command); }, onFileWantedToggled = function(row, want) { - changeFileCommand(want ? 'files-wanted' : 'files-unwanted', [row]); + changeFileCommand([row], want?'files-wanted':'files-unwanted'); }, onFilePriorityToggled = function(row, priority) { @@ -230,7 +228,7 @@ function Inspector(controller) { case 1: command = 'priority-high'; break; default: command = 'priority-normal'; break; } - changeFileCommand(command, [ row ]); + changeFileCommand([row], command); }, clearFileList = function() { diff --git a/web/javascript/remote.js b/web/javascript/remote.js index 179a8425c..e19880cce 100644 --- a/web/javascript/remote.js +++ b/web/javascript/remote.js @@ -146,18 +146,14 @@ TransmissionRemote.prototype = }); }, - changeFileCommand: function(command, rows) { - var remote = this; - var torrent_ids = [ rows[0].getTorrent().getId() ]; - var files = []; - for (var i=0, row; row=rows[i]; ++i) - files.push(row.getIndex()); - var o = { - method: 'torrent-set', - arguments: { ids: torrent_ids } - }; - o.arguments[command] = files; - this.sendRequest(o, function() { + changeFileCommand: function(torrentId, fileIndices, command) { + var remote = this, + args = { ids: [torrentId] }; + args[command] = fileIndices; + this.sendRequest({ + arguments: args, + method: 'torrent-set' + }, function() { remote._controller.refreshTorrents(torrent_ids); }); }, diff --git a/web/javascript/transmission.js b/web/javascript/transmission.js index 83f022a55..fc5ce966a 100644 --- a/web/javascript/transmission.js +++ b/web/javascript/transmission.js @@ -1262,8 +1262,8 @@ Transmission.prototype = this.remote.stopTorrents($.map(torrents.slice(0), function(t) {return t.getId();}), this.refreshTorrents, this); }, - changeFileCommand: function(command, rows) { - this.remote.changeFileCommand(command, rows); + changeFileCommand: function(torrentId, rowIndices, command) { + this.remote.changeFileCommand(torrentId, rowIndices, command); }, hideMobileAddressbar: function(delaySecs) {