📄 nsprogressdialog.js
字号:
// Update the dialog to indicate specified percent complete. setPercent: function( percent ) { // Maximum percentage is 100. if ( percent > 100 ) { percent = 100; } // Test if percentage is changing. if ( this.percent != percent ) { this.mPercent = percent; // If dialog not opened yet, bail early. if ( !this.loaded ) { return this.mPercent; } if ( percent == -1 ) { // Progress meter needs to be in "undetermined" mode. this.mode = "undetermined"; // Update progress meter percentage text. this.setValue( "progressText", "" ); } else { // Progress meter needs to be in normal mode. this.mode = "normal"; // Set progress meter thermometer. this.setValue( "progress", percent ); // Update progress meter percentage text. this.setValue( "progressText", this.replaceInsert( this.getString( "percentMsg" ), 1, percent ) ); } // Update title. this.setTitle(); } return this.mPercent; }, // Update download rate and dialog display. // Note that we don't want the displayed value to quiver // between essentially identical values (e.g., 99.9Kb and // 100.0Kb) so we only update if we see a big change. setRate: function( rate ) { if ( rate ) { // rate is bytes/sec var change = Math.abs( this.rate - rate ); // Don't update too often! if ( change > this.rate / 10 ) { // Displayed rate changes. this.mRate = rate; } } return this.mRate; }, // Handle download completion. setCompleted: function() { // If dialog hasn't loaded yet, defer this. if ( !this.loaded ) { this.dialog.setTimeout( function(obj){obj.setCompleted()}, 100, this ); return false; } if ( !this.mCompleted ) { this.mCompleted = true; // If the "keep dialog open" box is checked, then update dialog. if ( this.dialog && this.dialogElement( "keep" ).checked ) { // Indicate completion in status area. var string = this.getString( "completeMsg" ); string = this.replaceInsert( string, 1, this.formatSeconds( this.elapsed/1000 ) ); string = this.replaceInsert( string, 2, this.targetFile.fileSize >> 10 ); this.setValue( "status", string); // Put progress meter at 100%. this.percent = 100; // Set time remaining to 00:00. this.setValue( "timeLeft", this.formatSeconds( 0 ) ); // Change Cancel button to Close, and give it focus. var cancelButton = this.dialogElement( "cancel" ); cancelButton.label = this.getString( "close" ); cancelButton.focus(); // Activate reveal/launch buttons if we enable them. var enableButtons = true; try { var prefs = Components.classes[ "@mozilla.org/preferences-service;1" ] .getService( Components.interfaces.nsIPrefBranch ); enableButtons = prefs.getBoolPref( "browser.download.progressDnldDialog.enable_launch_reveal_buttons" ); } catch ( e ) { } if ( enableButtons ) { this.enable( "reveal" ); try { if ( this.targetFile != null ) { this.enable( "launch" ); } } catch(e) { } } // Disable the Pause/Resume buttons. this.dialogElement( "pauseResume" ).disabled = true; // Fix up dialog layout (which gets messed up sometimes). this.dialog.sizeToContent(); // GetAttention to show the user that we're done this.dialog.getAttention(); } else if ( this.dialog ) { this.dialog.close(); } } return this.mCompleted; }, // Set progress meter to given mode ("normal" or "undetermined"). setMode: function( newMode ) { if ( this.mode != newMode ) { // Need to update progress meter. this.dialogElement( "progress" ).setAttribute( "mode", newMode ); } return this.mMode = newMode; }, // Set pause/resume state. setPaused: function( pausing ) { // If state changing, then update stuff. if ( this.paused != pausing ) { var string = pausing ? "resume" : "pause"; this.dialogElement( "pauseResume" ).label = this.getString(string); // If we have an observer, tell it to suspend/resume if ( this.observer ) { this.observer.observe( this, pausing ? "onpause" : "onresume" , "" ); } } return this.mPaused = pausing; }, // Convert raw rate (bytes/sec) to Kbytes/sec (to nearest tenth). rateToKRate: function( rate ) { return ( rate / 1024 ).toFixed(1); }, // Format number of seconds in hh:mm:ss form. formatSeconds: function( secs ) { // Round the number of seconds to remove fractions. secs = parseInt( secs + .5 ); var hours = parseInt( secs/3600 ); secs -= hours*3600; var mins = parseInt( secs/60 ); secs -= mins*60; var result; if ( hours ) result = this.getString( "longTimeFormat" ); else result = this.getString( "shortTimeFormat" ); if ( hours < 10 ) hours = "0" + hours; if ( mins < 10 ) mins = "0" + mins; if ( secs < 10 ) secs = "0" + secs; // Insert hours, minutes, and seconds into result string. result = this.replaceInsert( result, 1, hours ); result = this.replaceInsert( result, 2, mins ); result = this.replaceInsert( result, 3, secs ); return result; }, // Get dialog element using argument as id. dialogElement: function( id ) { // Check if we've already fetched it. if ( !( id in this.fields ) ) { // No, then get it from dialog. try { this.fields[ id ] = this.dialog.document.getElementById( id ); } catch(e) { this.fields[ id ] = { value: "", setAttribute: function(id,val) {}, removeAttribute: function(id) {} } } } return this.fields[ id ]; }, // Set dialog element value for given dialog element. setValue: function( id, val ) { this.dialogElement( id ).value = val; }, // Enable dialgo element. enable: function( field ) { this.dialogElement( field ).removeAttribute( "disabled" ); }, // Get localizable string from properties file. getProperty: function( propertyId, strings, len ) { if ( !this.mBundle ) { this.mBundle = Components.classes[ "@mozilla.org/intl/stringbundle;1" ] .getService( Components.interfaces.nsIStringBundleService ) .createBundle( "chrome://global/locale/nsProgressDialog.properties"); } return this.mBundle.formatStringFromName( propertyId, strings, len ); }, // Get localizable string (from dialog <data> elements). getString: function ( stringId ) { // Check if we've fetched this string already. if ( !( this.strings && stringId in this.strings ) ) { // Presume the string is empty if we can't get it. this.strings[ stringId ] = ""; // Try to get it. try { this.strings[ stringId ] = this.dialog.document.getElementById( "string."+stringId ).childNodes[0].nodeValue; } catch (e) {} } return this.strings[ stringId ]; }, // Replaces insert ("#n") with input text. replaceInsert: function( text, index, value ) { var result = text; var regExp = new RegExp( "#"+index ); result = result.replace( regExp, value ); return result; }, // Hide a given dialog field. hide: function( field ) { this.dialogElement( field ).hidden = true; // Also hide any related separator element... var sep = this.dialogElement( field+"Separator" ); if (sep) sep.hidden = true; }, // Return input in hex, prepended with "0x" and leading zeros (to 8 digits). hex: function( x ) { return "0x" + ("0000000" + Number(x).toString(16)).slice(-8); }, // Dump text (if debug is on). dump: function( text ) { if ( this.debug ) { dump( text ); } }}// This Component's module implementation. All the code below is used to get this// component registered and accessible via XPCOM.var module = { // registerSelf: Register this component. registerSelf: function (compMgr, fileSpec, location, type) { var compReg = compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar ); compReg.registerFactoryLocation( this.cid, "Mozilla Download Progress Dialog", this.contractId, fileSpec, location, type ); }, // getClassObject: Return this component's factory object. getClassObject: function (compMgr, cid, iid) { if (!cid.equals(this.cid)) throw Components.results.NS_ERROR_NO_INTERFACE; if (!iid.equals(Components.interfaces.nsIFactory)) throw Components.results.NS_ERROR_NOT_IMPLEMENTED; return this.factory; }, /* CID for this class */ cid: Components.ID("{F5D248FD-024C-4f30-B208-F3003B85BC92}"), /* Contract ID for this class */ contractId: "@mozilla.org/progressdialog;1", /* factory object */ factory: { // createInstance: Return a new nsProgressDialog object. createInstance: function (outer, iid) { if (outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION; return (new nsProgressDialog()).QueryInterface(iid); } }, // canUnload: n/a (returns true) canUnload: function(compMgr) { return true; }};// NSGetModule: Return the nsIModule object.function NSGetModule(compMgr, fileSpec) { return module;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -