ginebra2/chrome/bedrockchrome/networkstatus.snippet/networkstatus.js
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
child 12 afcd8e6d025b
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*!
     1 /*!
     2   \file networkstatus.js This module contains the NetworkStatus class.
     2   \file networkstatus.js This module contains the NetworkStatusDialog class which
       
     3   displays network/http errors.
     3 */
     4 */
     4 
     5 
     5 function NetworkStatus()
     6 function NetworkStatusDialog() {
     6 {   
     7     var onChromeComplete = function()
     7 	   // attach internal funcs
     8     {
     8     this.setup = setupPage;
     9         // Get external mouse events.
       
    10         snippets.NetworkStatusChromeId.externalMouseEvent.connect(this.handleExternalMouseEvent.bind(this));
     9 
    11 
    10     // do setup
    12         // Watch for page load errors.
    11     this.setup();
    13         window.pageController.pageLoadFailed.connect(
       
    14           function() {
       
    15              if (!window.pageController.loadCanceled ) {
       
    16                 if (window.pageController.errorUrlMatches) {
       
    17                    update();
       
    18                    showNetworkStatus();
       
    19                 }
       
    20              }
       
    21           }
       
    22         );        
       
    23     }
       
    24     
       
    25     //! Add text to the given DOM element, truncate it at the given width (in pixels).
       
    26     //! Returns the remainder of the string.
       
    27     function truncateTextToWidth(element, text, width)
       
    28     {
       
    29        element.innerHTML = '<span style="white-space:nowrap;">' + text + '</span>';
       
    30        if(element.offsetWidth > width)
       
    31        {
       
    32           var i = 1;
       
    33           element.innerHTML = '';
       
    34           while(element.offsetWidth < (width) && i < text.length)
       
    35           {
       
    36              element.innerHTML = text.substr(0,i);
       
    37              i++;
       
    38           }
       
    39           return text.substr(i-1);
       
    40        }
       
    41        return "";
       
    42     } 
       
    43     
       
    44     //! Update text elements with error info.
       
    45     var update = function()
       
    46     {
       
    47         // Set URL.  Two lines of it are displayed, the first wraps to the second and the second
       
    48         // is truncated with an ellipsis appended by CSS.
       
    49         var parentWidth = document.getElementById("networkStatusTextUrlParent").offsetWidth;
       
    50         var restOfUrl = truncateTextToWidth(document.getElementById("networkStatusTextUrl1Id"), '"' + pageController.networkErrorUrl + '"', parentWidth);
       
    51         document.getElementById("networkStatusTextUrl2Id").innerHTML = restOfUrl;
    12 
    52 
    13     function onChromeComplete() 
    53         // Set error message.
       
    54         if(pageController.networkErrorMsg != undefined && pageController.networkErrorMsg != "") {
       
    55             document.getElementById("networkStatusTextMsgId").innerHTML = pageController.networkErrorMsg;
       
    56         }
       
    57         else {
       
    58             document.getElementById("networkStatusTextMsgId").innerHTML = window.localeDelegate.translateText("txt_browser_error_generic_error_msg");
       
    59         }
       
    60     }
       
    61     
       
    62     //! Handles external mouse events - dismisses status dialog.
       
    63     /*!
       
    64       \param type the type of event
       
    65       \param name the name of event
       
    66       \param description event description
       
    67     */
       
    68     this.handleExternalMouseEvent = function(type, name, description)
    14     {
    69     {
    15         window.pageController.pageLoadFinished.connect(
    70         if (name == "QGraphicsSceneMousePressEvent") {
    16         function(ok) {
    71             this.hideNetworkStatus();
    17         	processLoadStatus(ok); 
    72         }
    18         	var networkTimeoutId; 
    73     }
    19 	        if (!ok && !window.pageController.loadCanceled ) {         	
    74     
    20         		showNetworkStatus();   
    75     //! Show the network status dialog and shaded overlay.
    21         		networkTimeoutId = setTimeout ( 'hideNetworkStatus()', 2000 ); 
    76     var showNetworkStatus = function(){
    22  	        }  	        
    77 
    23         }                
    78         var snippet = snippets.NetworkStatusChromeId;
    24         );
    79         snippet.updateOwnerArea();
    25     } //End of onChromeComplete handler
    80         snippet.show(true);
    26            
    81         snippet.zValue = 1;
    27    window.chrome.chromeComplete.connect(onChromeComplete);    
    82         snippet.repaint();
       
    83     
       
    84         //networkTimeoutId = setTimeout(hideNetworkStatus, 2000);
       
    85     }
       
    86 
       
    87     //! Hide the network status dialog and shaded overlay.
       
    88     this.hideNetworkStatus = function(){
       
    89         snippets.NetworkStatusChromeId.hide();
       
    90     }
       
    91 
       
    92     //! Create the DOM elements for the dialog.
       
    93     this.setupPage = function(){    
       
    94         var html =
       
    95             '<div class="networkStatusBox">' +
       
    96                 '<ul>' +
       
    97                     '<li>' +
       
    98                         '<img src="networkstatus.snippet/icons/icon_dialog_error.png"/>&nbsp;&nbsp;' +
       
    99                         '<span class="networkStatusText">' +
       
   100                             window.localeDelegate.translateText("txt_browser_error_page_load_failed") +
       
   101                         '</span>' +
       
   102                     '</li>' +
       
   103                     '<li id="networkStatusTextUrlParent">' +
       
   104                         '<span class="networkStatusText" id="networkStatusTextUrl1Id"></span><br/>' +
       
   105                         '<div class="networkStatusText2" id="networkStatusTextUrl2Id"></div>' +
       
   106                     '</li>' +
       
   107                     '<li>' +
       
   108                         '<span class="networkStatusText" id="networkStatusTextMsgId"/>' +
       
   109                     '</li>' +
       
   110                     '<li>' +
       
   111                         '<center><img id="networkStatus_okId" class="networkStatusOkButton"/></center>' +
       
   112                     '</li>' +
       
   113                 '</ul>' +
       
   114             '</div>';
       
   115         document.write(html);
       
   116         new SimpleButton("networkStatus_okId",
       
   117                          "networkstatus.snippet/icons/button_dialog_ok_wait.png",
       
   118                          "networkstatus.snippet/icons/button_dialog_ok_press.png",
       
   119                          "",
       
   120                          this.onOkPressed.bind(this));
       
   121     }
       
   122 
       
   123     this.onOkPressed = function() {
       
   124         this.hideNetworkStatus();
       
   125     }
       
   126 
       
   127     this.setupPage();
       
   128 
       
   129     chrome.chromeComplete.connect(onChromeComplete.bind(this));
    28 }
   130 }
    29 
       
    30 // "Private" methods
       
    31 function processLoadStatus(ok) {
       
    32 	  
       
    33     var htmlText = "Page load ok"; 
       
    34     if (!ok) 
       
    35         htmlText = window.localeDelegate.translateText("page_load_failed"); 
       
    36         var ele = document.getElementById("NetworkStatusTextId");
       
    37 	if (ele) {
       
    38             ele.innerHTML = htmlText; 
       
    39 	}  
       
    40 	else {
       
    41 	  	document.write(htmlText); 
       
    42 	}
       
    43 	  
       
    44 }
       
    45 
       
    46 // "Private" methods
       
    47 function setupPage() {
       
    48 //	  var image = 'networkstatus.snippet/icons/network_status_icon.png';
       
    49 	    
       
    50     var html =
       
    51         '<div class="networkStatusBox">' +
       
    52               '<table><tr>' +
       
    53 //                '<td class="networkStatusIcon">' +
       
    54 //                    '<img src="' + image + '" alt="">' +
       
    55 //                '</td>' +
       
    56                 '<td class="networkStatusText" id="NetworkStatusTextId">' +
       
    57                     'Replace with localized message text' +
       
    58                 '</td>' +
       
    59             '</tr></table>' +
       
    60         '</div>';
       
    61   document.write(html);
       
    62 }
       
    63 
       
    64 // "Private" method
       
    65 function showNetworkStatus() {
       
    66     window.snippets.NetworkStatusChromeId.setPosition(10,80); 
       
    67  	  window.snippets.NetworkStatusChromeId.show(true);
       
    68  	  window.snippets.NetworkStatusChromeId.repaint();
       
    69 } 	            
       
    70 
       
    71 // "private" method 
       
    72 function hideNetworkStatus() {
       
    73     window.snippets.NetworkStatusChromeId.setPosition(10,80); 
       
    74  	  window.snippets.NetworkStatusChromeId.hide();
       
    75  	  window.snippets.NetworkStatusChromeId.repaint(); 	        	
       
    76 }