Changeset 8522


Ignore:
Timestamp:
May 25, 2009, 1:18:51 AM (14 years ago)
Author:
charles
Message:

(trunk web) various small changes (1) remove a dangling variable, (2) add some missing semicolons, (3) prefer === and !== over == and != where sensible, (4) avoid + for strings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/javascript/torrent.js

    r8521 r8522  
    232232                // 'Apple' button emulation on PC :
    233233                // Need settable meta-key and ctrl-key variables for mac emulation
    234                 var meta_key = event.metaKey
    235                 var ctrl_key = event.ctrlKey
     234                var meta_key = event.metaKey;
     235                var ctrl_key = event.ctrlKey;
    236236                if (event.ctrlKey && navigator.appVersion.toLowerCase().indexOf("mac") == -1) {
    237237                        meta_key = true;
     
    334334
    335335        refreshHTML: function() {
     336                var c;
     337                var e;
    336338                var progress_details;
    337339                var peer_details;
     
    346348                // Fix for situation
    347349                // when a verifying/downloading torrent gets state seeding
    348                 if( this._state == Torrent._StatusSeeding )
     350                if( this._state === Torrent._StatusSeeding )
    349351                        notDone = false ;
    350352               
     
    364366                        // Create the 'progress details' label
    365367                        // Eg: '101 MB of 631 MB (16.02%) - 2 hr remaining'
    366                         progress_details = Math.formatBytes( this._sizeWhenDone - this._leftUntilDone )
    367                                          + ' of '
    368                                          + Math.formatBytes( this._sizeWhenDone )
    369                                          + ' ('
    370                                          + this.getPercentDoneStr()
    371                                          + '%)'
    372                                          + eta;
     368                        c = Math.formatBytes( this._sizeWhenDone - this._leftUntilDone );
     369                        c += ' of ';
     370                        c += Math.formatBytes( this._sizeWhenDone );
     371                        c += ' (';
     372                        c += this.getPercentDoneStr();
     373                        c += '%)';
     374                        c += eta;
     375                        progress_details = c;
    373376               
    374377                        // Figure out the percent completed
     
    376379                       
    377380                        // Update the 'in progress' bar
    378                         var class_name = this.isActive() ? 'in_progress' : 'incomplete_stopped';
    379                         var e = root._progress_complete_container;
    380                         var str = 'torrent_progress_bar ' + class_name;
    381                         if(css_completed_width == 0) { str += ' empty'; }
    382                         e.className = str;
     381                        e = root._progress_complete_container;
     382                        c = 'torrent_progress_bar';
     383                        c += this.isActive() ? ' in_progress' : ' incomplete_stopped';
     384                        if(css_completed_width === 0) { c += ' empty'; }
     385                        e.className = c;
    383386                        e.style.width = css_completed_width + '%';
    384387                       
    385388                        // Update the 'incomplete' bar
    386389                        e = root._progress_incomplete_container;
    387                         if( e.className.indexOf( 'incomplete' ) == -1 )
     390                        if( e.className.indexOf( 'incomplete' ) === -1 )
    388391                                e.className = 'torrent_progress_bar in_progress';
    389392                        e.style.width =  (MaxBarWidth - css_completed_width) + '%';
     
    393396                        // Eg: 'Downloading from 36 of 40 peers - DL: 60.2 KB/s UL: 4.3 KB/s'
    394397                        if( !this.isDownloading( ) )
    395                                 peer_details = this.stateStr( );
     398                                c = this.stateStr( );
    396399                        else {
    397                                 peer_details = 'Downloading from '
    398                                              + this.peersSendingToUs()
    399                                              + ' of '
    400                                              + this._peers_connected
    401                                              + ' peers - DL: '
    402                                              + Math.formatBytes(this._download_speed)
    403                                              + '/s UL: '
    404                                              + Math.formatBytes(this._upload_speed)
    405                                              + '/s';
     400                                c = 'Downloading from ';
     401                                c += this.peersSendingToUs();
     402                                c += ' of ';
     403                                c += this._peers_connected;
     404                                c += ' peers - DL: ';
     405                                c += Math.formatBytes(this._download_speed);
     406                                c += '/s UL: ';
     407                                c += Math.formatBytes(this._upload_speed);
     408                                c += '/s';
    406409                        }
     410                        peer_details = c;
    407411                       
    408412                }
     
    410414                {
    411415                        // Update the 'in progress' bar
    412                         var class_name = (this.isActive()) ? 'complete' : 'complete_stopped';
    413                         var e = root._progress_complete_container;
    414                         e.className = 'torrent_progress_bar ' + class_name;
     416                        e = root._progress_complete_container;
     417                        c = 'torrent_progress_bar';
     418                        c += (this.isActive()) ? ' complete' : ' complete_stopped';
     419                        e.className = c;
    415420                       
    416421                        // Create the 'progress details' label
    417422                        // Eg: '698.05 MB, uploaded 8.59 GB (Ratio: 12.3)'
    418                         progress_details = Math.formatBytes( this._size )
    419                                          + ', uploaded '
    420                                          + Math.formatBytes( this._upload_total )
    421                                          + ' (Ratio '
    422                                          + Math.ratio( this._upload_total, this._download_total )
    423                                          + ')';
     423                        c = Math.formatBytes( this._size );
     424                        c += ', uploaded ';
     425                        c += Math.formatBytes( this._upload_total );
     426                        c += ' (Ratio ';
     427                        c += Math.ratio( this._upload_total, this._download_total );
     428                        c += ')';
     429                        progress_details = c;
    424430                       
    425431                        // Hide the 'incomplete' bar
     
    432438                        // Eg: 'Seeding to 13 of 22 peers - UL: 36.2 KB/s'
    433439                        if( !this.isSeeding( ) )
    434                                 peer_details = this.stateStr( );
    435                         else
    436                                 peer_details = 'Seeding to '
    437                                              + this.peersGettingFromUs()
    438                                              + ' of '
    439                                              + this._peers_connected
    440                                              + ' peers - UL: '
    441                                              + Math.formatBytes(this._upload_speed)
    442                                              + '/s';
     440                                c = this.stateStr( );
     441                        else {
     442                                c = 'Seeding to ';
     443                                c += this.peersGettingFromUs();
     444                                c += ' of ';
     445                                c += this._peers_connected;
     446                                c += ' peers - UL: ';
     447                                c += Math.formatBytes(this._upload_speed);
     448                                c += '/s';
     449                        }
     450                        peer_details = c;
    443451                }
    444452               
     
    448456                // Update the peer details and pause/resume button
    449457                e = root._pause_resume_button_image;
    450                 if ( this.state() == Torrent._StatusPaused ) {
     458                if ( this.state() === Torrent._StatusPaused ) {
    451459                        e.alt = 'Resume';
    452460                        e.className = "torrent_resume";
     
    457465               
    458466                if( this._error_message &&
    459                     this._error_message != '' &&
    460                     this._error_message != 'other' ) {
     467                    this._error_message !== '' &&
     468                    this._error_message !== 'other' ) {
    461469                        peer_details = this._error_message;
    462470                }
     
    475483        ensureFileListExists: function() {
    476484                if( this._file_view.length == 0 ) {
     485                        var v, e;
    477486                        for( var i=0; i<this._file_model.length; ++i ) {
    478                                 var v = new TorrentFile( this._file_model[i] );
     487                                v = new TorrentFile( this._file_model[i] );
    479488                                this._file_view[i] = v;
    480                                 var e = v.domElement( );
     489                                e = v.domElement( );
    481490                                e.className = (i % 2 ? 'even' : 'odd') + ' inspector_torrent_file_list_entry';
    482491                                this._fileList.appendChild( e );
     
    523532                        return pass;
    524533               
    525                 var pos = this._name_lc.indexOf( search.toLowerCase() );
    526                 pass = pos != -1;
    527                 return pass;
     534                return this._name_lc.indexOf( search.toLowerCase() ) !== -1;
    528535        }
    529536};
     
    566573        var b_ratio = Math.ratio( b._upload_total, b._download_total );
    567574        return a_ratio - b_ratio;
    568 }
     575};
    569576
    570577/**
     
    603610        }
    604611       
    605         if( sortDirection == Prefs._SortDescending )
     612        if( sortDirection === Prefs._SortDescending )
    606613                torrents.reverse( );
    607614       
     
    650657TorrentFile.prototype = {
    651658        initialize: function(file_data) {
    652                 //console.log( 'new TorrentFile ' + file_data.name );
    653659                this._dirty = true;
    654660                this._torrent = file_data.torrent;
    655661                this._index = file_data.index;
    656                 var pos = file_data.name.indexOf('/');
    657                 if (pos >= 0)
    658                         this.name = file_data.name.substring(pos + 1);
    659                 else
    660                         this.name = file_data.name;
     662                var name = file_data.name.substring (file_data.name.lastIndexOf('/')+1);
    661663                this.readAttributes(file_data);
    662664
     
    674676                var file_div = document.createElement('div');
    675677                file_div.className = "inspector_torrent_file_list_entry_name";
    676                 file_div.innerHTML = this.name.replace(/([\/_\.])/g, "$1&#8203;");
     678                file_div.innerHTML = name.replace(/([\/_\.])/g, "$1&#8203;");
    677679
    678680                var prog_div = document.createElement('div');
     
    696698                this.refreshHTML();
    697699        },
     700
     701        isDone: function () {
     702                return this._done >= this._size;
     703        },
     704
     705        isEditable: function () {
     706                return (this._torrent._file_model.length>1) && !this.isDone();
     707        },
    698708       
    699709        readAttributes: function(file_data) {
    700                 if( file_data.index != undefined && file_data.index != this._index ) {
     710                if( file_data.index !== undefined && file_data.index !== this._index ) {
    701711                        this._index = file_data.index;
    702712                        this._dirty = true;
    703713                }
    704                 if( file_data.bytesCompleted != undefined && file_data.bytesCompleted != this._done ) {
     714                if( file_data.bytesCompleted !== undefined && file_data.bytesCompleted !== this._done ) {
    705715                        this._done   = file_data.bytesCompleted;
    706716                        this._dirty = true;
    707717                }
    708                 if( file_data.length != undefined && file_data.length != this._size ) {
     718                if( file_data.length !== undefined && file_data.length !== this._size ) {
    709719                        this._size   = file_data.length;
    710720                        this._dirty = true;
    711721                }
    712                 if( file_data.priority != undefined && file_data.priority != this._prio ) {
     722                if( file_data.priority !== undefined && file_data.priority !== this._prio ) {
    713723                        this._prio   = file_data.priority;
    714724                        this._dirty = true;
    715725                }
    716                 if( file_data.wanted != undefined && file_data.wanted != this._wanted ) {
     726                if( file_data.wanted !== undefined && file_data.wanted !== this._wanted ) {
    717727                        this._wanted = file_data.wanted;
    718728                        this._dirty = true;
     
    728738        },
    729739       
    730         setPriority: function(priority) {
    731                 if(this.element().hasClass('complete') || this._torrent._file_model.length == 1)
    732                   return;
    733                 var priority_level = { high: 1, normal: 0, low: -1 }[priority];
    734                 if (this._prio == priority_level) { return; }
    735                 this._prio = priority_level;
    736                 this._torrent._controller.changeFileCommand("priority-" + priority, this._torrent, this);
    737                 this._dirty = true;
     740        setPriority: function(prio) {
     741                if (this.isEditable()) {
     742                        var cmd;
     743                        switch( prio ) {
     744                                case  1: cmd = 'priority-high';   break;
     745                                case -1: cmd = 'priority-low';    break;
     746                                default: cmd = 'priority-normal'; break;
     747                        }
     748                        this._prio = prio;
     749                        this._dirty = true;
     750                        this._torrent._controller.changeFileCommand( cmd, this._torrent, this );
     751                }
    738752        },
    739753       
     
    747761       
    748762        toggleWanted: function() {
    749                 if(this.element().hasClass('complete') || this._torrent._file_model.length == 1)
    750                   return;
    751                 this.setWanted(!this._wanted);
     763                if (this.isEditable())
     764                        this.setWanted( !this._wanted );
    752765        },
    753766       
     
    762775       
    763776        refreshProgressHTML: function() {
    764                 progress_details = Math.formatBytes(this._done) + ' of ' +
    765                                                         Math.formatBytes(this._size) + ' (' +
    766                                                         Math.ratio(100 * this._done, this._size) + '%)';
    767                 setInnerHTML(this._progress[0], progress_details);
     777                var c = Math.formatBytes(this._done);
     778                c += ' of ';
     779                c += Math.formatBytes(this._size);
     780                c += ' (';
     781                c += Math.ratio(100 * this._done, this._size);
     782                c += '%)';
     783                setInnerHTML(this._progress[0], c);
    768784        },
    769785       
    770786        refreshWantedHTML: function() {
    771                 var e = this.element()[0];
     787                var e = this.domElement();
    772788                var c = e.classNameConst;
    773                 if(!this._wanted) c += ' skip';
    774                 if(this._done>=this._size) c += ' complete';
     789                if(!this._wanted) { c += ' skip'; }
     790                if(this.isDone()) { c += ' complete'; }
    775791                e.className = c;
    776792        },
     
    794810                var x = event.pageX;
    795811                var target = this;
    796                 while (target != null) {
     812                while (target !== null) {
    797813                        x = x - target.offsetLeft;
    798814                        target = target.offsetParent;
    799815                }
    800                 var file = event.data.file;
    801                 if (x < 12) { file.setPriority('low'); }
    802                 else if (x < 23) { file.setPriority('normal'); }
    803                 else { file.setPriority('high'); }
     816                var prio;
     817                if( x < 12 ) prio = -1;
     818                else if( x < 23 ) prio = 0;
     819                else prio = 1;
     820                file.setPriority( prio );
    804821        }
    805822};
Note: See TracChangeset for help on using the changeset viewer.