toolkit/components/printing/content/printPreviewProgress.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/printing/content/printPreviewProgress.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// dialog is just an array we'll use to store various properties from the dialog document...
    1.11 +var dialog;
    1.12 +
    1.13 +// the printProgress is a nsIPrintProgress object
    1.14 +var printProgress = null; 
    1.15 +
    1.16 +// random global variables...
    1.17 +var targetFile;
    1.18 +
    1.19 +var docTitle = "";
    1.20 +var docURL   = "";
    1.21 +var progressParams = null;
    1.22 +
    1.23 +function ellipseString(aStr, doFront)
    1.24 +{
    1.25 +  if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "..."))
    1.26 +    return aStr;
    1.27 +
    1.28 +  var fixedLen = 64;
    1.29 +  if (aStr.length <= fixedLen)
    1.30 +    return aStr;
    1.31 +
    1.32 +  if (doFront)
    1.33 +    return "..." + aStr.substr(aStr.length-fixedLen, fixedLen);
    1.34 +  
    1.35 +  return aStr.substr(0, fixedLen) + "...";
    1.36 +}
    1.37 +
    1.38 +// all progress notifications are done through the nsIWebProgressListener implementation...
    1.39 +var progressListener = {
    1.40 +
    1.41 +  onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus)
    1.42 +  {
    1.43 +    if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
    1.44 +      window.close();
    1.45 +  },
    1.46 +  
    1.47 +  onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
    1.48 +  {
    1.49 +    if (!progressParams)
    1.50 +      return;
    1.51 +    var docTitleStr = ellipseString(progressParams.docTitle, false);
    1.52 +    if (docTitleStr != docTitle) {
    1.53 +      docTitle = docTitleStr;
    1.54 +      dialog.title.value = docTitle;
    1.55 +    }
    1.56 +    var docURLStr = ellipseString(progressParams.docURL, true);
    1.57 +    if (docURLStr != docURL && dialog.title != null) {
    1.58 +      docURL = docURLStr;
    1.59 +      if (docTitle == "")
    1.60 +        dialog.title.value = docURLStr;
    1.61 +    }
    1.62 +  },
    1.63 +
    1.64 +  onLocationChange: function (aWebProgress, aRequest, aLocation, aFlags) {},
    1.65 +  onSecurityChange: function (aWebProgress, aRequest, state) {},
    1.66 +
    1.67 +  onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage)
    1.68 +  {
    1.69 +    if (aMessage)
    1.70 +      dialog.title.setAttribute("value", aMessage);
    1.71 +  },
    1.72 +
    1.73 +  QueryInterface: function (iid)
    1.74 +  {
    1.75 +    if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
    1.76 +      return this;   
    1.77 +    throw Components.results.NS_NOINTERFACE;
    1.78 +  }
    1.79 +}
    1.80 +
    1.81 +function onLoad() {
    1.82 +  // Set global variables.
    1.83 +  printProgress = window.arguments[0];
    1.84 +  if (window.arguments[1]) {
    1.85 +    progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
    1.86 +    if (progressParams) {
    1.87 +      docTitle = ellipseString(progressParams.docTitle, false);
    1.88 +      docURL   = ellipseString(progressParams.docURL, true);
    1.89 +    }
    1.90 +  }
    1.91 +
    1.92 +  if (!printProgress) {
    1.93 +    dump( "Invalid argument to printPreviewProgress.xul\n" );
    1.94 +    window.close()
    1.95 +    return;
    1.96 +  }
    1.97 +
    1.98 +  dialog         = new Object;
    1.99 +  dialog.strings = new Array;
   1.100 +  dialog.title   = document.getElementById("dialog.title");
   1.101 +  dialog.titleLabel = document.getElementById("dialog.titleLabel");
   1.102 +
   1.103 +  dialog.title.value = docTitle;
   1.104 +
   1.105 +  // set our web progress listener on the helper app launcher
   1.106 +  printProgress.registerListener(progressListener);
   1.107 +  moveToAlertPosition();
   1.108 +
   1.109 +  //We need to delay the set title else dom will overwrite it
   1.110 +  window.setTimeout(doneIniting, 100);
   1.111 +}
   1.112 +
   1.113 +function onUnload() 
   1.114 +{
   1.115 +  if (!printProgress)
   1.116 +    return;
   1.117 +  try {
   1.118 +    printProgress.unregisterListener(progressListener);
   1.119 +    printProgress = null;
   1.120 +  }
   1.121 +  catch(e) {}
   1.122 +}
   1.123 +
   1.124 +function getString (stringId) {
   1.125 +  // Check if we've fetched this string already.
   1.126 +  if (!(stringId in dialog.strings)) {
   1.127 +    // Try to get it.
   1.128 +    var elem = document.getElementById( "dialog.strings."+stringId);
   1.129 +    try {
   1.130 +      if (elem && elem.childNodes && elem.childNodes[0] &&
   1.131 +          elem.childNodes[0].nodeValue)
   1.132 +        dialog.strings[stringId] = elem.childNodes[0].nodeValue;
   1.133 +      // If unable to fetch string, use an empty string.
   1.134 +      else
   1.135 +        dialog.strings[stringId] = "";
   1.136 +    } catch (e) { dialog.strings[stringId] = ""; }
   1.137 +  }
   1.138 +  return dialog.strings[stringId];
   1.139 +}
   1.140 +
   1.141 +// If the user presses cancel, tell the app launcher and close the dialog...
   1.142 +function onCancel () 
   1.143 +{
   1.144 +  // Cancel app launcher.
   1.145 +  try {
   1.146 +    printProgress.processCanceledByUser = true;
   1.147 +  }
   1.148 +  catch(e) {return true;}
   1.149 +    
   1.150 +  // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
   1.151 +  return false;
   1.152 +}
   1.153 +
   1.154 +function doneIniting() 
   1.155 +{
   1.156 +  // called by function timeout in onLoad
   1.157 +  printProgress.doneIniting();
   1.158 +}

mercurial