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