ginebra2/chrome/bedrockchrome/urlsearch.snippet/urlsearch.js
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*!
       
     2   \file urlsearch.js This module contains the UrlSearch class.
       
     3 */
       
     4 
       
     5 var enumObj = new Object();
       
     6 enumObj.state = {Loading:0, Editing:1, Reloadable:2}
       
     7 
       
     8 function UrlSearch(lastUrl)
       
     9 {   
       
    10     function onChromeComplete() 
       
    11     {
       
    12         // When restoring session we're already loading when chrome complete signal is sent
       
    13         // So update button and progress to reflect the loading state
       
    14         switch (window.pageController.loadState) {
       
    15         case enumObj.state.Loading:
       
    16             urlBarButton.setButton();
       
    17             urlBarProgressBar.setProgress("5%");
       
    18             break;
       
    19         default:
       
    20             urlBarProgressBar.setProgress("0%");
       
    21             break;
       
    22         }
       
    23         //window.snippets.FaviconSnippet.anchorTo("UrlSearchChromeId", 1,5);
       
    24         //window.snippets.FaviconSnippet.connectToWebpageController();
       
    25         window.pageController.pageUrlChanged.connect(
       
    26         function(url) {
       
    27             //window.chrome.alert(url);
       
    28             //url is coming from loadContoller. So need not set it back
       
    29             urlBarTextBox.setTextBoxValue(url);
       
    30         }
       
    31         );
       
    32 
       
    33         window.pageController.pageLoadStarted.connect(
       
    34         function() {
       
    35             //window.chrome.alert("Load start");
       
    36             //window.app.debug("Load start" + window.pageController.currentRequestedUrl);
       
    37             window.chrome.slideView(100);
       
    38             urlBarProgressBar.setProgress("5%");
       
    39             urlBarButton.setButton();
       
    40             urlBarTextBox.setTextBoxValue(window.pageController.currentRequestedUrl); 
       
    41         }
       
    42         );
       
    43 
       
    44         window.pageController.pageLoadProgress.connect(
       
    45         function(percent) {
       
    46             urlBarProgressBar.handlePageLoadProgress(percent);
       
    47         }
       
    48         );
       
    49 
       
    50         window.pageController.pageLoadFinished.connect(
       
    51         function(ok) {
       
    52 
       
    53         //window.chrome.alert("pageLoadFinished: ");
       
    54         // If the load was finished normally and not due to user stopping it,
       
    55         // simulate progress completion
       
    56         if(!window.pageController.loadCanceled) {
       
    57             urlBarProgressBar.setProgress("99%");
       
    58         }
       
    59         setTimeout("urlsearch.clearPBar()", 500);
       
    60         __blockDeselect = false;
       
    61         urlBarTextBox.setFocus(false);
       
    62         urlBarButton.setButton();
       
    63       
       
    64         if (window.views.current().type == "webView") {
       
    65             if (window.snippets.UrlSearchChromeId && window.pageController.contentsYPos > 0) {
       
    66                 window.chrome.slideView(-100);
       
    67             }
       
    68         }
       
    69         }
       
    70         );
       
    71   
       
    72 		    window.pageController.pageCreated.connect(
       
    73 		    function() {
       
    74 		    	  window.chrome.slideView(100);
       
    75 		    	}
       
    76 		    );
       
    77   
       
    78 
       
    79     window.pageController.pageChanged.connect(
       
    80     function() {
       
    81         //window.chrome.alert("Page  changed");
       
    82         // Set the values of the new page 
       
    83         setUrlSearchValues();
       
    84     }
       
    85     );
       
    86 
       
    87     // Connect View Manager current view changed signal to slot.
       
    88     window.views.currentViewChanged.connect(
       
    89         function() {
       
    90             if (window.views.current().type ==  "webView") {
       
    91                 /*window.chrome.alert("currentViewChanged  window.views.current().type =" + window.views.current().type + "Pos " + window.pageController.contentsYPos == 0  + "Loading still: " + pageController.isPageLoading);*/
       
    92                 // When we change back to content view, show the urlsearch bar if either we are
       
    93                 // at the top or we are still loading the page (in this case, we will hide the urlsearch bar
       
    94                 // if needed on getting loadFinished
       
    95                 if (!window.views.WebView.currentPageIsSuperPage() && 
       
    96                     (window.pageController.contentsYPos <= 0 || pageController.isPageLoading) ){
       
    97                     // show url bar with no animation
       
    98                     //showUrlBar();
       
    99                     window.chrome.slideView(100);
       
   100                 }
       
   101                 else {
       
   102                 	window.chrome.slideView(-100);
       
   103                 } 
       
   104                 //set the correct values based on the window 
       
   105                 //window.chrome.alert("currentViewChanged " + window.pageController.loadText);
       
   106                 setUrlSearchValues();
       
   107                  
       
   108                 // place focus in urlsearch bar when returning from adding a new window in windows view
       
   109                 if (!window.views.WebView.currentPageIsSuperPage() &&
       
   110                     (window.pageController.loadText == "")) {
       
   111                     __blockDeselect = false;
       
   112                     window.snippets.UrlSearchChromeId.grabFocus();
       
   113                     document.getElementById("urlBox").focus();
       
   114                     return;
       
   115                  }
       
   116              } else {
       
   117                  //Save url text box value
       
   118                  window.pageController.urlTextChanged(urlBarTextBox.getTextBoxValue());
       
   119              
       
   120                  // Remove progress bar and url text field value so that 
       
   121                  // incorrect values are not seen before we can update when we come back 
       
   122                  urlBarTextBox.setTextBoxValue("");
       
   123                  urlBarProgressBar.setProgress("0%");
       
   124                  //hideUrlBar();
       
   125                  window.chrome.slideView(-100);
       
   126              }
       
   127      
       
   128         }
       
   129     );
       
   130 
       
   131 
       
   132 window.ViewStack.currentViewChanged.connect(
       
   133         function() {
       
   134             if (window.views.current().type ==  "webView") {
       
   135                 /*window.chrome.alert("currentViewChanged  window.views.current().type =" + window.views.current().type + "Pos " + window.pageController.contentsYPos == 0  + "Loading still: " + pageController.isPageLoading);*/
       
   136                 // When we change back to content view, show the urlsearch bar if either we are
       
   137                 // at the top or we are still loading the page (in this case, we will hide the urlsearch bar
       
   138                 // if needed on getting loadFinished
       
   139                 if (!window.views.WebView.currentPageIsSuperPage() && 
       
   140                     (window.pageController.contentsYPos <= 0 || pageController.isPageLoading) ){
       
   141                     // show url bar with no animation
       
   142                     //showUrlBar();
       
   143                     window.chrome.slideView(100);                    
       
   144                 }
       
   145                 else {
       
   146                 	window.chrome.slideView(-100);                	
       
   147                 } 
       
   148                 //set the correct values based on the window 
       
   149                 //window.chrome.alert("currentViewChanged " + window.pageController.loadText);
       
   150                 setUrlSearchValues();
       
   151                  
       
   152                 // place focus in urlsearch bar when returning from adding a new window in windows view
       
   153                 if (window.pageController.loadText == "") {
       
   154                     __blockDeselect = false;
       
   155                     window.snippets.UrlSearchChromeId.grabFocus();
       
   156                     document.getElementById("urlBox").focus();
       
   157                     return;
       
   158                  }
       
   159              } else {
       
   160                  //Save url text box value
       
   161                  window.pageController.urlTextChanged(urlBarTextBox.getTextBoxValue());
       
   162              
       
   163                  // Remove progress bar and url text field value so that 
       
   164                  // incorrect values are not seen before we can update when we come back 
       
   165                  urlBarTextBox.setTextBoxValue("");
       
   166                  urlBarProgressBar.setProgress("0%");
       
   167                  //hideUrlBar();
       
   168                  window.chrome.slideView(-100);
       
   169              }
       
   170      
       
   171         }
       
   172     );
       
   173 
       
   174 
       
   175     // Connect snippets CR signal to slot.
       
   176     window.chrome.symbianCarriageReturn.connect(
       
   177         function() {
       
   178             if (urlBarTextBox.hasFocus()) {
       
   179                 // When we get load started, the button will be changed
       
   180                 loadToMainWindow(urlBarTextBox.getTextBoxValue());
       
   181             }
       
   182         }
       
   183     );
       
   184 
       
   185     } //End of onChromeComplete handler
       
   186 
       
   187 
       
   188     /*!
       
   189       Class to handle displaying and updating the URL text box.
       
   190     */
       
   191     function UrlTextBox()
       
   192     {
       
   193     }
       
   194     
       
   195     //! UrlTextBox Public Methods
       
   196     
       
   197     UrlTextBox.prototype.getTextBoxValue = function()
       
   198     {
       
   199         return document.gotoUrlForm.textBox.value;
       
   200     }
       
   201     
       
   202     UrlTextBox.prototype.setTextBoxValue = function(text)
       
   203     {
       
   204         //window.chrome.alert("setTextBoxValue" + text);
       
   205         document.gotoUrlForm.textBox.value = text;
       
   206     }
       
   207     
       
   208     // Unused Method
       
   209     UrlTextBox.prototype.textBoxFocus = function()
       
   210     {
       
   211         document.gotoUrlForm.textBox.focus();
       
   212     }
       
   213     
       
   214     UrlTextBox.prototype.hasFocus = function()
       
   215     {
       
   216         return document.getElementById("urlBox").hasFocus;
       
   217     }
       
   218     
       
   219     UrlTextBox.prototype.setFocus = function(hasFocus)
       
   220     {
       
   221         document.getElementById("urlBox").hasFocus = hasFocus;
       
   222     }
       
   223     
       
   224     /*!
       
   225       Class to handle displaying and updating the URL tri-state button.
       
   226     */
       
   227     function UrlTriStateButton()
       
   228     {
       
   229     }
       
   230     
       
   231     //! UrlTriStateButton Public Methods
       
   232     UrlTriStateButton.prototype.setButton = function()
       
   233     {
       
   234         //window.app.debug("setButton ");
       
   235         btnDom = document.getElementById("tristateBtn").button;
       
   236         switch (window.pageController.loadState) {
       
   237         case enumObj.state.Loading:
       
   238             btnDom.updateImages("urlsearch.snippet/icons/stop_btn.png",
       
   239                                     "urlsearch.snippet/icons/stop_btn_pressed.png",
       
   240                                     "urlsearch.snippet/icons/stop_btn.png");
       
   241             break;
       
   242         case enumObj.state.Editing:
       
   243             btnDom.updateImages("urlsearch.snippet/icons/go_btn.png",
       
   244                                 "urlsearch.snippet/icons/go_btn_pressed.png",
       
   245                                 "urlsearch.snippet/icons/go_btn.png");
       
   246             break;
       
   247          case enumObj.state.Reloadable:
       
   248             btnDom.updateImages("urlsearch.snippet/icons/refresh_btn.png",
       
   249                                     "urlsearch.snippet/icons/refresh_btn_pressed.png",
       
   250                                     "urlsearch.snippet/icons/refresh_btn.png");
       
   251             break;
       
   252          default:
       
   253             window.App.debug("Incorrect state");
       
   254             break;
       
   255         }
       
   256 
       
   257         document.getElementById("tristateBtn").button.updateButton();
       
   258 
       
   259         // Don't need repaint anymore?
       
   260         //if (window.snippets.UrlSearchChromeId) {
       
   261         //    window.snippets.UrlSearchChromeId.repaint();
       
   262         //}
       
   263     }
       
   264 
       
   265     // Unused Method
       
   266     UrlTriStateButton.prototype.changeTriState = function(a)
       
   267     {
       
   268        if (this.getValue() != a) {
       
   269             this.setValue(a);
       
   270             /* Shouln't have to do this check. This function should be 
       
   271              * called only after chrome has completed loading which 
       
   272              * means that all snippets have also been created.
       
   273              */
       
   274             if (window.snippets.UrlSearchChromeId) {
       
   275                 window.snippets.UrlSearchChromeId.repaint();
       
   276             } 
       
   277        }
       
   278     }
       
   279 
       
   280     /*!
       
   281       Class to handle displaying and updating the URL progress bar.
       
   282     */
       
   283     function UrlProgressBar()
       
   284     {
       
   285         this.oldpercent = 0;
       
   286     }
       
   287     
       
   288     //! UrlProgressBar Public Methods
       
   289     
       
   290     UrlProgressBar.prototype.setProgress = function(widthStr)
       
   291     {
       
   292         document.getElementById("PBar").style.width = widthStr;
       
   293     }
       
   294     
       
   295     UrlProgressBar.prototype.handlePageLoadProgress = function(percent)
       
   296     {
       
   297         this.updateProgress(percent);
       
   298     }
       
   299     
       
   300     UrlProgressBar.prototype.updateProgress = function(percent)
       
   301     {
       
   302         if (percent != 100) {
       
   303             //window.chrome.alert("Load prog "+percent);
       
   304             var parentWidth = document.getElementById("textEntryCell").offsetWidth;
       
   305             this.setProgress((parentWidth * percent)/100 + "px");
       
   306         } else {
       
   307             //clearProgressBar();
       
   308             this.setProgress("0%");
       
   309             oldpercent = 0;
       
   310         }
       
   311     }
       
   312     
       
   313     // UrlSearch Private Member Variables
       
   314     var animator;
       
   315     var animating = false;
       
   316     var defaultPos;
       
   317     var defaultHeight;
       
   318     var animateDuration = 500;
       
   319     var __blockDeselect = false;
       
   320     var urlBarTextBox = new UrlTextBox();
       
   321     var urlBarButton = new UrlTriStateButton();
       
   322     var urlBarProgressBar = new UrlProgressBar();
       
   323 
       
   324     // UrlSearch Private Methods
       
   325     function _urlsearch_write(lastUrl) {
       
   326         var html = '' +
       
   327             '<div id="urlsearchSnippet">' +
       
   328 
       
   329                     '<div class = "tableMain" id="mainLayoutTable">' +
       
   330                         '<div class = "tableRow" id="mainLayoutRow">' +
       
   331                                     '<div class = "tableCell mainLayoutCell" id = "inputboxl"></div>' +
       
   332                             '<div class = "mainLayoutCell" id = "inputboxc">' +
       
   333                                 '<form name = "gotoUrlForm">' +
       
   334             
       
   335                                 '<div class = "tableRow">' +
       
   336                                     '<div class = "tableCell" id = "textEntryCell">' +
       
   337                                     '<input type ="text" id = "urlBox" name = "textBox" value="' + lastUrl + '" '  +
       
   338                                     'maxlength="256" '+
       
   339                                     		'OnFocus="urlsearch.focusElement(this,event)" '+
       
   340                                     		'OnMouseUp="return urlsearch.mouseUpElement(this,event)" '+
       
   341                                     		'OnMouseDown="urlsearch.mouseDownElement(this,event)" '+
       
   342                                     		'OnBlur="urlsearch.mouseOutElement(this,event)" '+
       
   343                                     		'OnKeyUp="urlsearch.resetTriState();"/>' +
       
   344                                       	'<div id="PBar"></div>' +
       
   345                                       '</div>' +
       
   346                                     '<div class = "tableCell" id = "tristateBtnCell">' +
       
   347                                     '<img id="tristateBtn">' +
       
   348                                         '</div>' +
       
   349                                 '</div>' +
       
   350 
       
   351                                 '</form>' +
       
   352                             '</div>' +
       
   353                         '<div class = "tableCell" id = "inputboxr"></div>' +
       
   354                         '</div>' +
       
   355                     '</div>' +
       
   356                         '</div>';
       
   357 
       
   358         document.write(html);
       
   359     }
       
   360     
       
   361     function loadToMainWindow(gotourl){
       
   362         var gotourl = window.pageController.guessUrlFromString(gotourl);
       
   363         urlBarTextBox.setTextBoxValue(gotourl);
       
   364         window.pageController.currentLoad(gotourl);
       
   365         window.pageController.urlTextChanged(gotourl);
       
   366 
       
   367     }
       
   368     
       
   369     function setUrlSearchValues()
       
   370     {
       
   371           //window.app.debug("seturlsearchvalues")
       
   372         urlBarTextBox.setTextBoxValue(window.pageController.loadText);
       
   373         urlBarButton.setButton();
       
   374 
       
   375         //window.chrome.alert("setProgress " + "New: " +  window.pageController.loadProgressValue +  " Old Value: " + oldpercent );
       
   376         var percent = window.pageController.loadProgressValue;
       
   377         
       
   378         urlBarProgressBar.updateProgress(percent);
       
   379         if (percent == 100) {
       
   380             __blockDeselect = false;
       
   381             urlBarTextBox.setFocus(false);
       
   382         }
       
   383         //window.snippets.UrlSearchChromeId.repaint();
       
   384     }
       
   385 
       
   386     function chromeAlert(){
       
   387         window.chrome.alert("chrome Alert!");
       
   388     }
       
   389     
       
   390     // Public Methods
       
   391     this.focusElement = function(el, event)
       
   392     {
       
   393         el.select();
       
   394         __blockDeselect = true;
       
   395         document.getElementById("urlBox").scrollLeft = 1000;
       
   396         urlBarTextBox.setFocus(true);
       
   397     }
       
   398 		
       
   399 		this.mouseOutElement = function(el, event)
       
   400     {
       
   401         __blockDeselect = false;
       
   402         el.scrollRight = 0;
       
   403         urlBarTextBox.setFocus(false); 
       
   404     }
       
   405     
       
   406     this.mouseDownElement = function(el, event)
       
   407     {
       
   408         // Clear __justSelected in case the element gained focus through some non-mouse event
       
   409         // and still has focus.
       
   410          __blockDeselect = false;
       
   411     }
       
   412 
       
   413     this.mouseUpElement = function(el, event)
       
   414     {
       
   415         // Return false to prevent Qt from deselecting the text if we've just selected it
       
   416         // in focusElement(), return true otherwise.
       
   417 
       
   418         var result = !__blockDeselect;
       
   419         __blockDeselect = false;
       
   420         return result;
       
   421     }
       
   422 
       
   423     this.clearPBar = function()
       
   424     {
       
   425         urlBarProgressBar.setProgress("0%");
       
   426     }
       
   427 
       
   428     this.pushTriState = function()
       
   429     {
       
   430         //window.app.debug("pushTriState ");
       
   431         switch (window.pageController.loadState) {
       
   432         case enumObj.state.Loading:
       
   433             //window.chrome.alert("In loading state, stop loading");
       
   434             window.pageController.currentStop();
       
   435             setTimeout("urlsearch.clearPBar()", 50);
       
   436             break;
       
   437         case enumObj.state.Editing:
       
   438             //window.chrome.alert("In editing state, start loading");
       
   439             loadToMainWindow(urlBarTextBox.getTextBoxValue());
       
   440             break;
       
   441         case enumObj.state.Reloadable:
       
   442             //window.chrome.alert("In reloadable state, start reloading");
       
   443             window.pageController.currentReload();
       
   444             break;
       
   445         default:
       
   446             window.chrome.alert("Incorrect state");
       
   447             break;
       
   448         }
       
   449         urlBarButton.setButton();
       
   450     }
       
   451 
       
   452     this.resetTriState = function()
       
   453     {
       
   454         //window.app.debug("resetTriState");
       
   455         if(window.pageController.loadState == enumObj.state.Reloadable) {
       
   456             urlBarTextBox.setFocus(true);
       
   457             //window.chrome.alert("Change to editing");
       
   458             window.pageController.setLoadState(enumObj.state.Editing);
       
   459             urlBarButton.setButton();
       
   460         }
       
   461     }
       
   462 
       
   463     this.updateSuggests = function()
       
   464     {
       
   465        var text = urlBarTextBox.getTextBoxValue();
       
   466        if (text.length == 0) {  // hide the suggests if no text
       
   467            searchSuggests.hideSuggests();
       
   468            return;
       
   469        }
       
   470 
       
   471        if (text.length >= 8) { // only for demo
       
   472            searchSuggests.hideSuggests();
       
   473            return
       
   474        }
       
   475 
       
   476        searchSuggests.removeAllItems();
       
   477        searchSuggests.addItem(text);
       
   478        var i = 0;
       
   479        var n = 100;
       
   480        for (i = text.length; i < 8; i += 1, n += 100)
       
   481            searchSuggests.addItem(text + n);
       
   482        searchSuggests.showSuggests();
       
   483     }
       
   484 
       
   485     // UrlSearch Constructor
       
   486     window.chrome.chromeComplete.connect(onChromeComplete);    
       
   487     // do setup
       
   488     _urlsearch_write(lastUrl);
       
   489 
       
   490     // Init the button
       
   491     new SimpleButton("tristateBtn",
       
   492                      "urlsearch.snippet/icons/goto.png",
       
   493                      "urlsearch.snippet/icons/goto_pushed.png",
       
   494                      "urlsearch.snippet/icons/goto.png",
       
   495                      this.pushTriState);
       
   496 
       
   497 }
       
   498