Home | History | Annotate | Download | only in front-end
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 WebInspector.GoToLineDialog = function(view)
     32 {
     33     this._element = document.createElement("div");
     34     this._element.className = "go-to-line-dialog";
     35     this._element.addEventListener("keydown", this._onKeyDown.bind(this), false);
     36     this._closeKeys = [
     37         WebInspector.KeyboardShortcut.Keys.Enter.code,
     38         WebInspector.KeyboardShortcut.Keys.Esc.code,
     39     ];
     40 
     41     var dialogWindow = this._element;
     42 
     43     dialogWindow.createChild("label").textContent = WebInspector.UIString("Go to line: ");
     44 
     45     this._input = dialogWindow.createChild("input");
     46     this._input.setAttribute("type", "text");
     47     this._input.setAttribute("size", 6);
     48     var linesCount = view.textModel.linesCount;
     49     if (linesCount)
     50         this._input.setAttribute("title", WebInspector.UIString("1 - %d", linesCount));
     51     var blurHandler = this._onBlur.bind(this);
     52     this._input.addEventListener("blur", blurHandler, false);
     53 
     54 
     55     var go = dialogWindow.createChild("button");
     56     go.textContent = WebInspector.UIString("Go");
     57     go.addEventListener("click", this._onClick.bind(this), false);
     58     go.addEventListener("mousedown", function(e) {
     59         // Ok button click will close the dialog, removing onBlur listener
     60         // to let click event be handled.
     61         this._input.removeEventListener("blur", blurHandler, false);
     62     }.bind(this), false);
     63 
     64     this._view = view;
     65     view.element.appendChild(this._element);
     66 
     67     this._previousFocusElement = WebInspector.currentFocusElement;
     68     WebInspector.currentFocusElement = this._input;
     69     this._input.select();
     70 }
     71 
     72 WebInspector.GoToLineDialog.show = function(sourceView)
     73 {
     74     if (this._instance)
     75         return;
     76     this._instance = new WebInspector.GoToLineDialog(sourceView);
     77 }
     78 
     79 WebInspector.GoToLineDialog.prototype = {
     80     _hide: function()
     81     {
     82         if (this._isHiding)
     83             return;
     84         this._isHiding = true;
     85 
     86         WebInspector.currentFocusElement = this._previousFocusElement;
     87         WebInspector.GoToLineDialog._instance = null;
     88         this._element.parentElement.removeChild(this._element);
     89     },
     90 
     91     _onBlur: function(event)
     92     {
     93         this._hide();
     94     },
     95 
     96     _onKeyDown: function(event)
     97     {
     98         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
     99             event.preventDefault();
    100             return;
    101         }
    102 
    103         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
    104             this._highlightSelectedLine();
    105 
    106         if (this._closeKeys.indexOf(event.keyCode) >= 0) {
    107             this._hide();
    108             event.stopPropagation();
    109         }
    110     },
    111 
    112     _onClick: function(event)
    113     {
    114         this._highlightSelectedLine();
    115         this._hide();
    116     },
    117 
    118     _highlightSelectedLine: function()
    119     {
    120         var value = this._input.value;
    121         var lineNumber = parseInt(value, 10) - 1;
    122         if (!isNaN(lineNumber) && lineNumber >= 0) {
    123             lineNumber = Math.min(lineNumber, this._view.textModel.linesCount - 1);
    124             this._view.highlightLine(lineNumber);
    125         }
    126     }
    127 };
    128