ginebra/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 /* The following "classes" are psudo classes since javascript doesn't have 
       
     9    true classes. The prototype object is a property of all JavaScript
       
    10    objects and can be used to impart properties to each instance of a
       
    11    class - these properties become public and can't access private class 
       
    12    members. Another way to create a public method is to add code like the
       
    13    following inside of the class constructor.
       
    14    
       
    15    // create a public method named clearPBar
       
    16    this.clearPBar = function()
       
    17    {
       
    18    }
       
    19    
       
    20    Methods created this way can access private members. Syntax-wise, 
       
    21    private member variables and methods are created in the usual fasion but 
       
    22    within the class constructor. Semantically they are simulated in JavaScript 
       
    23    using closures.
       
    24 */
       
    25 
       
    26 /*!
       
    27   Class to handle displaying and updating the URL search bar. Only 1 search 
       
    28   bar should be created for the browser. This class is not designed to be code
       
    29   space efficient for creating multiple URL search bar objects.
       
    30 */
       
    31 function UrlSearch(lastUrl)
       
    32 {   
       
    33     /*!
       
    34       Class to handle displaying and updating the URL text box.
       
    35     */
       
    36     function UrlTextBox()
       
    37     {
       
    38     }
       
    39     
       
    40     //! UrlTextBox Public Methods
       
    41     
       
    42     UrlTextBox.prototype.getTextBoxValue = function()
       
    43     {
       
    44         return document.gotoUrlForm.textBox.value;
       
    45     }
       
    46     
       
    47     UrlTextBox.prototype.setTextBoxValue = function(text)
       
    48     {
       
    49         //window.chrome.alert("setTextBoxValue" + text);
       
    50         document.gotoUrlForm.textBox.value = text;
       
    51     }
       
    52     
       
    53     // Unused Method
       
    54     UrlTextBox.prototype.textBoxFocus = function()
       
    55     {
       
    56         document.gotoUrlForm.textBox.focus();
       
    57     }
       
    58     
       
    59     UrlTextBox.prototype.hasFocus = function()
       
    60     {
       
    61         return document.getElementById("urlBox").hasFocus;
       
    62     }
       
    63     
       
    64     UrlTextBox.prototype.setFocus = function(hasFocus)
       
    65     {
       
    66         document.getElementById("urlBox").hasFocus = hasFocus;
       
    67     }
       
    68     
       
    69     /*!
       
    70       Class to handle displaying and updating the URL tri-state button.
       
    71     */
       
    72     function UrlTriStateButton()
       
    73     {
       
    74     }
       
    75     
       
    76     //! UrlTriStateButton Public Methods
       
    77     UrlTriStateButton.prototype.setButton = function()
       
    78     {
       
    79         //window.chrome.alert("setButton " + window.pageController.loadState);
       
    80         btnDom = document.getElementById("tristateBtn").button;
       
    81         switch (window.pageController.loadState) {
       
    82         case enumObj.state.Loading:
       
    83             btnDom.updateImages("urlsearch.snippet/icons/stop.png",
       
    84                                 "urlsearch.snippet/icons/stop_pushed.png",
       
    85                                 "urlsearch.snippet/icons/stop.png");
       
    86             break;
       
    87         case enumObj.state.Editing:
       
    88             btnDom.updateImages("urlsearch.snippet/icons/goto.png",
       
    89                                 "urlsearch.snippet/icons/goto_pushed.png",
       
    90                                 "urlsearch.snippet/icons/goto.png");
       
    91             break;
       
    92         case enumObj.state.Reloadable:
       
    93             btnDom.updateImages("urlsearch.snippet/icons/reload.png",
       
    94                                 "urlsearch.snippet/icons/reload_pushed.png",
       
    95                                 "urlsearch.snippet/icons/reload.png");
       
    96             break;
       
    97         default:
       
    98             window.chrome.alert("Incorrect state");
       
    99             break;
       
   100         }
       
   101         
       
   102         document.getElementById("tristateBtn").button.updateButton();
       
   103     }
       
   104 
       
   105     // Unused Method
       
   106     UrlTriStateButton.prototype.changeTriState = function(a)
       
   107     {
       
   108        if (this.getValue() != a) {
       
   109             this.setValue(a);
       
   110             /* Shouln't have to do this check. This function should be 
       
   111              * called only after chrome has completed loading which 
       
   112              * means that all snippets have also been created.
       
   113              */
       
   114             if (window.snippets.UrlSearchChromeId) {
       
   115                 window.snippets.UrlSearchChromeId.repaint();
       
   116             } 
       
   117        }
       
   118     }
       
   119     
       
   120     /*!
       
   121       Class to handle displaying and updating the URL progress bar.
       
   122     */
       
   123     function UrlProgressBar()
       
   124     {
       
   125         this.oldpercent = 0;
       
   126     }
       
   127     
       
   128     //! UrlProgressBar Public Methods
       
   129     
       
   130     UrlProgressBar.prototype.setProgress = function(widthStr)
       
   131     {
       
   132         document.getElementById("PBar").style.width = widthStr;
       
   133     }
       
   134     
       
   135     UrlProgressBar.prototype.handlePageLoadProgress = function(percent)
       
   136     {
       
   137         //window.chrome.alert("Load prog "+percent);
       
   138         if ((window.viewManager.currentView.type == "webView")  &&
       
   139             (this.oldpercent != percent)) {
       
   140             this.oldpercent = percent;
       
   141             //window.chrome.alert("Load prog "+percent);
       
   142             var parentWidth = document.getElementById("PWrap").offsetWidth;
       
   143             this.setProgress((parentWidth * percent)/100 + "px");
       
   144         }
       
   145     }
       
   146     
       
   147     UrlProgressBar.prototype.updateProgress = function(percent)
       
   148     {
       
   149         if (percent != 100) {
       
   150             //window.chrome.alert("Load prog "+percent);
       
   151             var parentWidth = document.getElementById("PWrap").offsetWidth;
       
   152             this.setProgress((parentWidth * percent)/100 + "px");
       
   153         } else {
       
   154             //clearProgressBar();
       
   155             this.setProgress("0%");
       
   156             oldpercent = 0;
       
   157         }
       
   158     }
       
   159     
       
   160     // UrlSearch Private Member Variables
       
   161     var animator;
       
   162     var animating = false;
       
   163     var defaultPos;
       
   164     var defaultHeight;
       
   165     var animateDuration = 500;
       
   166     var __blockDeselect = false;
       
   167     var urlBarTextBox = new UrlTextBox();
       
   168     var urlBarButton = new UrlTriStateButton();
       
   169     var urlBarProgressBar = new UrlProgressBar();
       
   170     var searchSuggestEngine = new SearchSuggest();
       
   171     // UrlSearch Private Methods
       
   172     
       
   173     function _urlsearch_write(lastUrl) {
       
   174           var html = ''+
       
   175             '<form name = "gotoUrlForm">' +
       
   176               '<table id="UrlSearchTable">' +
       
   177               '<tr>' +
       
   178                 '<td id="faviconCell"><img id = "favicon" src="urlsearch.snippet/icons/defaultFavicon.png"></td>'+
       
   179                 '<td>' +
       
   180                   '<div id="PWrap">' +
       
   181                   '<input type ="text" id = "urlBox" name = "textBox" value="' + lastUrl + '" '  +
       
   182                   'maxlength="256" '+
       
   183                   'OnFocus="urlsearch.focusElement(this,event)" '+
       
   184                   'OnMouseUp="return urlsearch.mouseUpElement(this,event)" '+
       
   185                   'OnMouseDown="urlsearch.mouseDownElement(this,event)" '+
       
   186                   'OnKeyUp="urlsearch.resetTriState();urlsearch.querySuggests();"/>' +
       
   187                   '<div id="PBar"></div>' +
       
   188                   '</div>' +
       
   189                 '</td>' +
       
   190                 '<td id="tristateBtnCell"' +
       
   191                   '<img id="tristateBtn">' +
       
   192                 '</td>' +
       
   193               '</tr>' +
       
   194               '</table>' +
       
   195             '</form>';
       
   196 
       
   197         document.write(html);
       
   198     }
       
   199     
       
   200     function loadToMainWindow(gotourl){
       
   201         // use a single call to guess url and goto url
       
   202         // (could do as separate calls, but we try to eliminate lag with a single call)
       
   203 
       
   204         //var correctedUrl = window.chrome.guessAndGotoUrl(gotourl);
       
   205         //urlBarTextBox.setTextBoxValue(correctedUrl);
       
   206 
       
   207         var gotourl = window.chrome.guessUrlFromString(gotourl);
       
   208         window.pageController.currentLoad(gotourl);
       
   209         urlBarTextBox.setTextBoxValue(gotourl);
       
   210         window.pageController.urlTextChanged(gotourl);
       
   211     }
       
   212     
       
   213     function loadSearchUrl(gotourl){
       
   214         var gotourl = window.chrome.searchUrl(gotourl);
       
   215         window.pageController.currentLoad(gotourl);
       
   216         urlBarTextBox.setTextBoxValue(gotourl);
       
   217         window.pageController.urlTextChanged(gotourl);
       
   218     }
       
   219 
       
   220     function chromeAlert(){
       
   221         window.chrome.alert("chrome Alert!");
       
   222     }
       
   223 
       
   224     function canSeeUrl()
       
   225     {
       
   226         return (!window.snippets.UrlSearchChromeId.isHiding && window.snippets.UrlSearchChromeId.isVisible());
       
   227     }
       
   228 
       
   229     function showUrlBar()
       
   230     {
       
   231         if (!canSeeUrl()) {
       
   232             //window.chrome.alert("showUrlbar");
       
   233             window.snippets.UrlSearchChromeId.zValue =  window.snippets.StatusBarChromeId.zValue - 1;
       
   234             window.snippets.UrlSearchChromeId.setPosition(0,defaultPos);
       
   235             window.snippets.UrlSearchChromeId.show(false);
       
   236             window.chrome.updateViewPort();
       
   237         }
       
   238     }
       
   239 
       
   240     function hideUrlBar()
       
   241     {
       
   242         //window.chrome.alert("hideUrlbar");
       
   243         if (canSeeUrl()) {
       
   244             window.snippets.UrlSearchChromeId.hide(false);
       
   245         }
       
   246     }
       
   247 
       
   248     function slideUrlOut() {
       
   249         //window.chrome.alert("slideUrlOut");
       
   250         if (canSeeUrl()) {
       
   251             window.snippets.UrlSearchChromeId.zValue =  window.snippets.StatusBarChromeId.zValue - 1;
       
   252             animating = true;
       
   253             //window.snippets.UrlSearchChromeId.isHiding = true;
       
   254             animator = window.snippets.UrlSearchChromeId.animate(animateDuration).translateBy(0, -1 * defaultHeight).start();
       
   255             animator.updated.connect(animationUpdated);
       
   256             animator.finished.connect(hideAnimationFinished);
       
   257         }
       
   258     }
       
   259 
       
   260     function slideUrlIn()
       
   261     {
       
   262         /* show it at right position and zorder first */
       
   263         window.snippets.UrlSearchChromeId.zValue =  window.snippets.StatusBarChromeId.zValue - 1;
       
   264         window.snippets.UrlSearchChromeId.setPosition(0, defaultPos-defaultHeight);
       
   265         window.snippets.UrlSearchChromeId.show(false);
       
   266         //window.snippets.UrlSearchChromeId.isHiding = false;
       
   267 
       
   268         animating = true;
       
   269         animator = window.snippets.UrlSearchChromeId.animate(animateDuration).translateBy(0, defaultHeight).start();
       
   270         animator.updated.connect(animationUpdated);
       
   271         animator.finished.connect(showAnimationFinished);
       
   272     }
       
   273     
       
   274     function setUrlSearchValues()
       
   275     {
       
   276         //window.chrome.alert("setUrlSearchValues " + window.pageController.loadText);
       
   277         urlBarTextBox.setTextBoxValue(window.pageController.loadText);
       
   278         urlBarButton.setButton();
       
   279 
       
   280         //window.chrome.alert("setProgress " + "New: " +  window.pageController.loadProgressValue +  " Old Value: " + oldpercent );
       
   281         var percent = window.pageController.loadProgressValue;
       
   282         
       
   283         urlBarProgressBar.updateProgress(percent);
       
   284         if (percent == 100) {
       
   285             __blockDeselect = false;
       
   286             urlBarTextBox.setFocus(false);
       
   287         }
       
   288         window.snippets.UrlSearchChromeId.repaint();
       
   289     }
       
   290 
       
   291     function animationUpdated(value)
       
   292     {
       
   293         window.chrome.updateViewPort();
       
   294     }
       
   295 
       
   296     function hideAnimationFinished()
       
   297     {
       
   298         //window.chrome.alert("hideAnimationFinished");
       
   299         animating = false;
       
   300         window.snippets.UrlSearchChromeId.isHiding = true;
       
   301     }
       
   302 
       
   303     function showAnimationFinished()
       
   304     {
       
   305         //window.chrome.alert("showAnimationFinished");
       
   306         animating = false;
       
   307         window.snippets.UrlSearchChromeId.isHiding = false;
       
   308     }
       
   309     
       
   310     // Public Methods
       
   311     this.focusElement = function(el, event)
       
   312     {
       
   313         el.select();
       
   314         __blockDeselect = true;
       
   315         document.getElementById("urlBox").scrollLeft = 1000;
       
   316         urlBarTextBox.setFocus(true);
       
   317     }
       
   318 
       
   319     this.mouseDownElement = function(el, event)
       
   320     {
       
   321         // Clear __justSelected in case the element gained focus through some non-mouse event
       
   322         // and still has focus.
       
   323         __blockDeselect = false;
       
   324     }
       
   325 
       
   326     this.mouseUpElement = function(el, event)
       
   327     {
       
   328         // Return false to prevent Qt from deselecting the text if we've just selected it
       
   329         // in focusElement(), return true otherwise.
       
   330 
       
   331         var result = !__blockDeselect;
       
   332         __blockDeselect = false;
       
   333         return result;
       
   334     }
       
   335 
       
   336     this.clearPBar = function()
       
   337     {
       
   338         urlBarProgressBar.setProgress("0%");
       
   339     }
       
   340 
       
   341     this.pushTriState = function()
       
   342     {
       
   343         //window.chrome.alert("pushTriState " + window.pageController.loadState);
       
   344         switch (window.pageController.loadState) {
       
   345         case enumObj.state.Loading:
       
   346             //window.chrome.alert("In loading state, stop loading");
       
   347             window.pageController.currentStop();
       
   348             setTimeout("urlsearch.clearPBar()", 50);
       
   349             break;
       
   350         case enumObj.state.Editing:
       
   351             //window.chrome.alert("In editing state, start loading");
       
   352             loadToMainWindow(urlBarTextBox.getTextBoxValue());
       
   353             break;
       
   354         case enumObj.state.Reloadable:
       
   355             //window.chrome.alert("In reloadable state, start reloading");
       
   356             window.pageController.currentReload();
       
   357             break;
       
   358         default:
       
   359             window.chrome.alert("Incorrect state");
       
   360             break;
       
   361         }
       
   362         urlBarButton.setButton();
       
   363 
       
   364     }
       
   365 
       
   366     this.resetTriState = function()
       
   367     {
       
   368         if(window.pageController.loadState == enumObj.state.Reloadable) {
       
   369             urlBarTextBox.setFocus(true);
       
   370             //window.chrome.alert("Change to editing");
       
   371             window.pageController.setLoadState(enumObj.state.Editing);
       
   372             urlBarButton.setButton();
       
   373         }
       
   374     }
       
   375 
       
   376     this.querySuggests = function()
       
   377     {
       
   378         /* Disable suggests
       
   379         var text = urlBarTextBox.getTextBoxValue();
       
   380         //window.chrome.alert("call searchSuggest " + text);
       
   381         searchSuggests.removeAllItems();
       
   382         searchSuggests.hideSuggests();
       
   383         searchSuggestEngine.searchSuggest(text, this.updateSuggests);
       
   384         */
       
   385     }
       
   386 
       
   387     this.updateSuggests = function(result)
       
   388     {
       
   389         var curText = urlBarTextBox.getTextBoxValue();
       
   390         searchSuggests.removeAllItems();
       
   391         if (result.length > 1 && curText == result[0]){
       
   392             for(var i = 1; i < result.length; i++) {
       
   393                 //window.chrome.alert(result[i]);
       
   394                 searchSuggests.addItem(result[i]);
       
   395             }
       
   396         searchSuggests.showSuggests();
       
   397         }
       
   398         
       
   399         
       
   400             
       
   401     }
       
   402 
       
   403     // UrlSearch Constructor
       
   404     
       
   405     // do setup
       
   406     _urlsearch_write(lastUrl);
       
   407 
       
   408 
       
   409     // Init the button
       
   410     new SimpleButton("tristateBtn",
       
   411                      "urlsearch.snippet/icons/goto.png",
       
   412                      "urlsearch.snippet/icons/goto_pushed.png",
       
   413                      "urlsearch.snippet/icons/goto.png",
       
   414                      this.pushTriState);
       
   415     urlBarButton.setButton();
       
   416     
       
   417     // Note that in the slots below the "this" object is never used directly.
       
   418     // This is because they don't have access to "this" as they are called
       
   419     // externaly.
       
   420     
       
   421     // Connect Chrome load complete signal to slot.
       
   422     window.chrome.loadComplete.connect(
       
   423         function () {
       
   424             defaultPos = window.snippets.UrlSearchChromeId.anchorOffset;
       
   425             defaultHeight = UrlSearchChromeId.offsetHeight;
       
   426         }
       
   427         );
       
   428 
       
   429     // Connect various Page Controller signals to slots.
       
   430     window.pageController.pageUrlChanged.connect(
       
   431         function(url) {
       
   432             //window.chrome.alert(url);
       
   433             //url is coming from loadContoller. So need not set it back
       
   434             urlBarTextBox.setTextBoxValue(url);
       
   435         }
       
   436         );
       
   437 
       
   438     window.pageController.pageLoadStarted.connect(
       
   439         function() {
       
   440             //window.chrome.alert("Load start");
       
   441             urlBarProgressBar.setProgress("5%");
       
   442             urlBarButton.setButton();
       
   443             
       
   444             /* show url bar if it's hiding in contentView */
       
   445             if (window.viewManager.currentView.type == "webView") {
       
   446                 if (window.snippets.UrlSearchChromeId) {
       
   447                     showUrlBar();
       
   448                 }
       
   449             }
       
   450         }
       
   451         );
       
   452 
       
   453     window.pageController.pageLoadProgress.connect(
       
   454         function(percent) {
       
   455             urlBarProgressBar.handlePageLoadProgress(percent);
       
   456         }
       
   457         );
       
   458 
       
   459     window.pageController.pageLoadFinished.connect(
       
   460         function(ok) {
       
   461             //window.chrome.alert("pageLoadFinished: ");
       
   462             // If the load was finished normally and not due to user stopping it,
       
   463             // simulate progress completion 
       
   464             if(!window.pageController.loadCanceled) {
       
   465                 urlBarProgressBar.setProgress("99%");
       
   466             }
       
   467             setTimeout("urlsearch.clearPBar()", 500);
       
   468             __blockDeselect = false;
       
   469             urlBarTextBox.setFocus(false);
       
   470             urlBarButton.setButton();
       
   471 
       
   472             /* start hide animation if contentYpos is not zero */
       
   473             if (window.viewManager.currentView.type == "webView") {
       
   474                if (window.snippets.UrlSearchChromeId && window.pageController.contentsYPos > 0) {
       
   475                    slideUrlOut();
       
   476                 }
       
   477             }
       
   478         }
       
   479         );
       
   480 
       
   481     window.pageController.pageIconChanged.connect(
       
   482         function() {
       
   483             window.chrome.alert("FAVICON changed");
       
   484         }
       
   485         );
       
   486 
       
   487     window.pageController.pageCreated.connect(
       
   488         function() {
       
   489             //window.chrome.alert("add new window");
       
   490             showUrlBar();
       
   491         }
       
   492         );
       
   493 
       
   494     window.pageController.pageChanged.connect(
       
   495         function() {
       
   496 
       
   497          //window.chrome.alert("Page  changed");
       
   498          // Set the values of the new page 
       
   499          setUrlSearchValues();
       
   500         }
       
   501         );
       
   502 
       
   503     window.pageController.pageScrollPositionZero.connect(
       
   504         function() {
       
   505             var contentYPos = window.pageController.contentsYPos;
       
   506             //window.chrome.alert("pageScrollRequested contentYPos = " + contentYPos);
       
   507             if (contentYPos > 0) {
       
   508                 if(canSeeUrl() && !animating && !pageController.isPageLoading) {
       
   509                     slideUrlOut();
       
   510                     //window.chrome.updateViewPort();
       
   511                 }
       
   512             }
       
   513             else {
       
   514                 if(!canSeeUrl() && !animating) {
       
   515                     slideUrlIn();
       
   516                 }
       
   517             }
       
   518         }
       
   519         );
       
   520     
       
   521     // Connect snippets CR signal to slot.
       
   522     window.snippets.symbianCarriageReturn.connect(
       
   523         function() {
       
   524             if (urlBarTextBox.hasFocus()) {
       
   525                 // When we get load started, the button will be changed
       
   526                 loadToMainWindow(urlBarTextBox.getTextBoxValue());
       
   527             }
       
   528         }
       
   529         );
       
   530 
       
   531     // Connect View Manager current view changed signal to slot.
       
   532     window.viewManager.currentViewChanged.connect(
       
   533         function() {
       
   534              if (window.viewManager.currentView.type ==  "webView") {
       
   535                  /*window.chrome.alert("currentViewChanged  window.viewManager.currentView.type =" + window.viewManager.currentView.type + "Pos " + window.pageController.contentsYPos == 0  + "Loading still: " + pageController.isPageLoading);*/
       
   536                  // When we change back to content view, show the urlsearch bar if either we are
       
   537                  // at the top or we are still loading the page (in this case, we will hide the urlsearch bar
       
   538                  // if needed on getting loadFinished
       
   539                  if (window.pageController.contentsYPos == 0 || pageController.isPageLoading) {
       
   540                      // show url bar with no animation
       
   541                      showUrlBar();
       
   542                  }
       
   543                  //set the correct values based on the window 
       
   544                  //window.chrome.alert("currentViewChanged " + window.pageController.loadText);
       
   545                  setUrlSearchValues();
       
   546                  
       
   547                  // place focus in urlsearch bar when returning from adding a new window in windows view
       
   548                  if (window.pageController.loadText == "") {
       
   549                      __blockDeselect = false;
       
   550                      document.getElementById("urlBox").focus();
       
   551                      return;
       
   552                  }
       
   553              } else {
       
   554                  //Save url text box value
       
   555                  window.pageController.urlTextChanged(urlBarTextBox.getTextBoxValue());
       
   556              
       
   557                  // Remove progress bar and url text field value so that 
       
   558                  // incorrect values are not seen before we can update when we come back 
       
   559                  urlBarTextBox.setTextBoxValue("");
       
   560                  urlBarProgressBar.setProgress("0%");
       
   561                  hideUrlBar();
       
   562              }
       
   563      
       
   564         }
       
   565         );
       
   566 }
       
   567