Changeset 12832


Ignore:
Timestamp:
Sep 3, 2011, 4:33:04 AM (12 years ago)
Author:
jordan
Message:

(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.

Location:
trunk/web/javascript
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/javascript/file-row.js

    r12791 r12832  
    77function FileRow(torrent, i)
    88{
    9         this.initialize(torrent, i);
    10 }
    11 
    12 FileRow.prototype =
    13 {
    14         initialize: function(torrent, i)
    15         {
    16                 this._torrent = torrent;
    17                 this._index = i;
    18                 this.createRow(torrent, i);
     9        var fields = {
     10                have: 0,
     11                index: 0,
     12                isDirty: false,
     13                isWanted: true,
     14                priority: 0,
     15                me: this,
     16                size: 0,
     17                torrent: null
    1918        },
    2019
    21         getTorrent: function()
    22         {
    23                 return this._torrent;
    24         },
    25         getIndex: function()
    26         {
    27                 return this._index;
     20        elements = {
     21                priority_control: null,
     22                progress: null,
     23                root: null
    2824        },
    2925
    30         readAttributes: function(file)
    31         {
    32                 if (file.index !== undefined && file.index !== this._index) {
    33                         this._index = file.index;
    34                         this._dirty = true;
     26        initialize = function(torrent, i) {
     27                fields.torrent = torrent;
     28                fields.index = i;
     29                createRow(torrent, i);
     30        },
     31
     32        readAttributes = function(file) {
     33                if (fields.have !== file.bytesCompleted) {
     34                        fields.have = file.bytesCompleted;
     35                        fields.isDirty = true;
    3536                }
    36                 if (file.bytesCompleted !== undefined && file.bytesCompleted !== this._done) {
    37                         this._done   = file.bytesCompleted;
    38                         this._dirty = true;
     37                if (fields.size !== file.length) {
     38                        fields.size = file.length;
     39                        fields.isDirty = true;
    3940                }
    40                 if (file.length !== undefined && file.length !== this._size) {
    41                         this._size   = file.length;
    42                         this._dirty = true;
     41                if (fields.priority !== file.priority) {
     42                        fields.priority = file.priority;
     43                        fields.isDirty = true;
    4344                }
    44                 if (file.priority !== undefined && file.priority !== this._prio) {
    45                         this._prio   = file.priority;
    46                         this._dirty = true;
    47                 }
    48                 if (file.wanted !== undefined && file.wanted !== this._wanted) {
    49                         this._wanted = file.wanted;
    50                         this._dirty = true;
     45                if (fields.isWanted !== file.wanted) {
     46                        fields.isWanted = file.wanted;
     47                        fields.isDirty = true;
    5148                }
    5249        },
    5350
    54         refreshWantedHTML: function()
     51        refreshWantedHTML = function()
    5552        {
    56                 var e = this.getElement(),
     53                var e = elements.root,
    5754                    c = [ e.classNameConst ];
    5855
    59                 if (!this._wanted) { c.push('skip'); }
    60                 if (this.isDone()) { c.push('complete'); }
     56                if (!fields.isWanted) { c.push('skip'); }
     57                if (isDone()) { c.push('complete'); }
    6158                e.className = c.join(' ');
    6259        },
    63         refreshPriorityHTML: function()
     60        refreshPriorityHTML = function()
    6461        {
    65                 var e = this._priority_control,
     62                var e = elements.priority_control,
    6663                    c = [ e.classNameConst ];
    6764
    68                 switch(this._prio) {
     65                switch(fields.priority) {
    6966                        case -1 : c.push('low'); break;
    7067                        case 1  : c.push('high'); break;
     
    7370                e.className = c.join(' ');
    7471        },
    75         refreshProgressHTML: function()
     72        refreshProgressHTML = function()
    7673        {
    77                 var pct = 100 * (this._size ? (this._done / this._size) : 1.0),
    78                     c = [ Transmission.fmt.size(this._done),
     74                var pct = 100 * (fields.size ? (fields.have / fields.size) : 1.0),
     75                    c = [ Transmission.fmt.size(fields.have),
    7976                          ' of ',
    80                           Transmission.fmt.size(this._size),
     77                          Transmission.fmt.size(fields.size),
    8178                          ' (',
    8279                          Transmission.fmt.percentString(pct),
    8380                          '%)' ].join('');
    84                 setInnerHTML(this._progress[0], c);
     81                setInnerHTML(elements.progress, c);
    8582        },
    86         refreshHTML: function() {
    87                 if (this._dirty) {
    88                         this._dirty = false;
    89                         this.refreshProgressHTML();
    90                         this.refreshWantedHTML();
    91                         this.refreshPriorityHTML();
     83        refreshHTML = function() {
     84                if (fields.isDirty) {
     85                        fields.isDirty = false;
     86                        refreshProgressHTML();
     87                        refreshWantedHTML();
     88                        refreshPriorityHTML();
    9289                }
    9390        },
    94         refresh: function()
    95         {
    96                 var i = this.getIndex(),
    97                     t = this.getTorrent();
    98                 this.readAttributes(t.getFile(i));
    99                 this.refreshHTML();
     91        refresh = function() {
     92                readAttributes(fields.torrent.getFile(fields.index));
     93                refreshHTML();
    10094        },
    10195
    102         isDone: function () {
    103                 return this._done >= this._size;
    104         },
    105         isEditable: function () {
    106                 return (this.getTorrent().getFileCount()>1) && !this.isDone();
     96        isDone = function () {
     97                return fields.have >= fields.size;
    10798        },
    10899
    109         createRow: function(torrent, i)
    110         {
    111                 var me = this,
    112                     file = torrent.getFile(i),
     100        createRow = function(torrent, i) {
     101                var file = torrent.getFile(i),
    113102                    name, root, wanted_div, pri_div, file_div, prog_div;
    114103
    115104                root = document.createElement('li');
    116                 root.id = 't' + this._torrent.getId() + 'f' + this._index;
     105                root.id = 't' + fields.torrent.getId() + 'f' + fields.index;
    117106                root.classNameConst = 'inspector_torrent_file_list_entry ' + ((i%2)?'odd':'even');
    118107                root.className = root.classNameConst;
     
    120109                wanted_div = document.createElement('div');
    121110                wanted_div.className = "file_wanted_control";
    122                 $(wanted_div).bind('click',function(){ me.fireWantedChanged(!me._wanted); });
     111                $(wanted_div).click(function(){ fireWantedChanged(!fields.isWanted); });
    123112
    124113                pri_div = document.createElement('div');
     
    133122                                e = e.offsetParent;
    134123                        }
     124                        // ugh.
    135125                        if (isMobileDevice) {
    136126                                if (x < 8) prio = -1;
     
    142132                                else prio = 1;
    143133                        }
    144                         me.firePriorityChanged(prio);
     134                        firePriorityChanged(prio);
    145135                });
    146136
     
    160150                root.appendChild(prog_div);
    161151
    162                 this._element = root;
    163                 this._priority_control = pri_div;
    164                 this._progress = $(prog_div);
     152                elements.root = root;
     153                elements.priority_control = pri_div;
     154                elements.progress = prog_div;
    165155
    166                 this.refresh();
     156                refresh();
    167157                return root;
    168158        },
    169159
    170         getElement: function()
    171         {
    172                 return this._element;
     160        fireWantedChanged = function(do_want) {
     161                $(fields.me).trigger('wantedToggled',[ fields.me, do_want ]);
    173162        },
     163        firePriorityChanged = function(priority) {
     164                $(fields.me).trigger('priorityToggled',[ fields.me, priority ]);
     165        };
    174166
    175         fireWantedChanged: function(do_want)
    176         {
    177                 $(this).trigger('wantedToggled',[ this, do_want ]);
    178         },
    179         firePriorityChanged: function(priority)
    180         {
    181                 $(this).trigger('priorityToggled',[ this, priority ]);
    182         }
     167        /***
     168        ****  PUBLIC
     169        ***/
     170
     171        this.getElement = function() {
     172                return elements.root;
     173        };
     174        this.getIndex = function() {
     175                return fields.index;
     176        };
     177        this.isEditable = function () {
     178                return (fields.torrent.getFileCount()>1) && !isDone();
     179        };
     180
     181        initialize(torrent, i);
    183182};
  • trunk/web/javascript/inspector.js

    r12829 r12832  
    202202    filesDeselectAllClicked = function() { filesAllClicked(false); },
    203203    filesAllClicked = function(s) {
    204         var t = data.file_torrent;
    205         if (t)
    206             toggleFilesWantedDisplay(t, s);
    207     },
    208 
    209     changeFileCommand = function(command, rows) {
    210         data.controller.changeFileCommand(command, rows);
    211     },
    212 
    213     toggleFilesWantedDisplay = function(torrent, wanted) {
    214         var i, row, rows=[];
     204        var i, row, rows=[], t=data.file_torrent;
     205        if (!t)
     206                return;
    215207        for (i=0; row=data.file_rows[i]; ++i)
    216             if (row.isEditable() && (torrent.getFile(i).wanted !== wanted))
     208            if (row.isEditable() && (t.getFile(i).wanted !== s))
    217209                rows.push(row);
    218210        if (rows.length > 0)
    219             changeFileCommand(wanted?'files-wanted':'files-unwanted', rows);
     211            changeFileCommand(rows, s?'files-wanted':'files-unwanted');
     212    },
     213
     214    changeFileCommand = function(rows, command) {
     215        var torrentId = data.file_torrent.getId();
     216        var rowIndices = $.map(rows.slice(0),function (row) {return row.getIndex();});
     217        data.controller.changeFileCommand(torrentId, rowIndices, command);
    220218    },
    221219
    222220    onFileWantedToggled = function(row, want) {
    223         changeFileCommand(want ? 'files-wanted' : 'files-unwanted',  [row]);
     221        changeFileCommand([row], want?'files-wanted':'files-unwanted');
    224222    },
    225223
     
    231229            default: command = 'priority-normal'; break;
    232230        }
    233         changeFileCommand(command, [ row ]);
     231        changeFileCommand([row], command);
    234232    },
    235233
  • trunk/web/javascript/remote.js

    r12828 r12832  
    147147        },
    148148
    149         changeFileCommand: function(command, rows) {
    150                 var remote = this;
    151                 var torrent_ids = [ rows[0].getTorrent().getId() ];
    152                 var files = [];
    153                 for (var i=0, row; row=rows[i]; ++i)
    154                         files.push(row.getIndex());
    155                 var o = {
    156                         method: 'torrent-set',
    157                         arguments: { ids: torrent_ids }
    158                 };
    159                 o.arguments[command] = files;
    160                 this.sendRequest(o, function() {
     149        changeFileCommand: function(torrentId, fileIndices, command) {
     150                var remote = this,
     151                    args = { ids: [torrentId] };
     152                args[command] = fileIndices;
     153                this.sendRequest({
     154                        arguments: args,
     155                        method: 'torrent-set'
     156                }, function() {
    161157                        remote._controller.refreshTorrents(torrent_ids);
    162158                });
  • trunk/web/javascript/transmission.js

    r12829 r12832  
    12631263                                         this.refreshTorrents, this);
    12641264        },
    1265         changeFileCommand: function(command, rows) {
    1266                 this.remote.changeFileCommand(command, rows);
     1265        changeFileCommand: function(torrentId, rowIndices, command) {
     1266                this.remote.changeFileCommand(torrentId, rowIndices, command);
    12671267        },
    12681268
Note: See TracChangeset for help on using the changeset viewer.