toolkit/components/printing/content/printPreviewProgress.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 // dialog is just an array we'll use to store various properties from the dialog document...
     8 var dialog;
    10 // the printProgress is a nsIPrintProgress object
    11 var printProgress = null; 
    13 // random global variables...
    14 var targetFile;
    16 var docTitle = "";
    17 var docURL   = "";
    18 var progressParams = null;
    20 function ellipseString(aStr, doFront)
    21 {
    22   if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "..."))
    23     return aStr;
    25   var fixedLen = 64;
    26   if (aStr.length <= fixedLen)
    27     return aStr;
    29   if (doFront)
    30     return "..." + aStr.substr(aStr.length-fixedLen, fixedLen);
    32   return aStr.substr(0, fixedLen) + "...";
    33 }
    35 // all progress notifications are done through the nsIWebProgressListener implementation...
    36 var progressListener = {
    38   onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus)
    39   {
    40     if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
    41       window.close();
    42   },
    44   onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
    45   {
    46     if (!progressParams)
    47       return;
    48     var docTitleStr = ellipseString(progressParams.docTitle, false);
    49     if (docTitleStr != docTitle) {
    50       docTitle = docTitleStr;
    51       dialog.title.value = docTitle;
    52     }
    53     var docURLStr = ellipseString(progressParams.docURL, true);
    54     if (docURLStr != docURL && dialog.title != null) {
    55       docURL = docURLStr;
    56       if (docTitle == "")
    57         dialog.title.value = docURLStr;
    58     }
    59   },
    61   onLocationChange: function (aWebProgress, aRequest, aLocation, aFlags) {},
    62   onSecurityChange: function (aWebProgress, aRequest, state) {},
    64   onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage)
    65   {
    66     if (aMessage)
    67       dialog.title.setAttribute("value", aMessage);
    68   },
    70   QueryInterface: function (iid)
    71   {
    72     if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
    73       return this;   
    74     throw Components.results.NS_NOINTERFACE;
    75   }
    76 }
    78 function onLoad() {
    79   // Set global variables.
    80   printProgress = window.arguments[0];
    81   if (window.arguments[1]) {
    82     progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
    83     if (progressParams) {
    84       docTitle = ellipseString(progressParams.docTitle, false);
    85       docURL   = ellipseString(progressParams.docURL, true);
    86     }
    87   }
    89   if (!printProgress) {
    90     dump( "Invalid argument to printPreviewProgress.xul\n" );
    91     window.close()
    92     return;
    93   }
    95   dialog         = new Object;
    96   dialog.strings = new Array;
    97   dialog.title   = document.getElementById("dialog.title");
    98   dialog.titleLabel = document.getElementById("dialog.titleLabel");
   100   dialog.title.value = docTitle;
   102   // set our web progress listener on the helper app launcher
   103   printProgress.registerListener(progressListener);
   104   moveToAlertPosition();
   106   //We need to delay the set title else dom will overwrite it
   107   window.setTimeout(doneIniting, 100);
   108 }
   110 function onUnload() 
   111 {
   112   if (!printProgress)
   113     return;
   114   try {
   115     printProgress.unregisterListener(progressListener);
   116     printProgress = null;
   117   }
   118   catch(e) {}
   119 }
   121 function getString (stringId) {
   122   // Check if we've fetched this string already.
   123   if (!(stringId in dialog.strings)) {
   124     // Try to get it.
   125     var elem = document.getElementById( "dialog.strings."+stringId);
   126     try {
   127       if (elem && elem.childNodes && elem.childNodes[0] &&
   128           elem.childNodes[0].nodeValue)
   129         dialog.strings[stringId] = elem.childNodes[0].nodeValue;
   130       // If unable to fetch string, use an empty string.
   131       else
   132         dialog.strings[stringId] = "";
   133     } catch (e) { dialog.strings[stringId] = ""; }
   134   }
   135   return dialog.strings[stringId];
   136 }
   138 // If the user presses cancel, tell the app launcher and close the dialog...
   139 function onCancel () 
   140 {
   141   // Cancel app launcher.
   142   try {
   143     printProgress.processCanceledByUser = true;
   144   }
   145   catch(e) {return true;}
   147   // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
   148   return false;
   149 }
   151 function doneIniting() 
   152 {
   153   // called by function timeout in onLoad
   154   printProgress.doneIniting();
   155 }

mercurial