1/* 2 * Copyright (C) 2013 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26WebInspector.GoToLineDialog = function() 27{ 28 WebInspector.Object.call(this); 29 30 this._element = document.createElement("div"); 31 this._element.className = WebInspector.GoToLineDialog.StyleClassName; 32 33 var field = this._element.appendChild(document.createElement("div")); 34 35 this._input = field.appendChild(document.createElement("input")); 36 this._input.type = "text"; 37 this._input.placeholder = WebInspector.UIString("Line Number"); 38 this._input.spellcheck = false; 39 40 this._clearIcon = field.appendChild(document.createElement("img")); 41 42 this._input.addEventListener("input", this); 43 this._input.addEventListener("keydown", this); 44 this._input.addEventListener("blur", this); 45 this._clearIcon.addEventListener("mousedown", this); 46 this._clearIcon.addEventListener("click", this); 47} 48 49WebInspector.GoToLineDialog.StyleClassName = "go-to-line-dialog"; 50WebInspector.GoToLineDialog.NonEmptyClassName = "non-empty"; 51 52WebInspector.GoToLineDialog.prototype = { 53 constructor: WebInspector.GoToLineDialog, 54 55 __proto__: WebInspector.Object.prototype, 56 57 // Public 58 59 present: function(parent) 60 { 61 parent.appendChild(this._element); 62 this._input.focus(); 63 this._clear(); 64 }, 65 66 dismiss: function() 67 { 68 var parent = this._element.parentNode; 69 if (!parent) 70 return; 71 72 parent.removeChild(this._element); 73 74 if (this.delegate && typeof this.delegate.goToLineDialogWasDismissed === "function") 75 this.delegate.goToLineDialogWasDismissed(this); 76 }, 77 78 // Protected 79 80 handleEvent: function(event) 81 { 82 switch (event.type) { 83 case "input": 84 this._handleInputEvent(event); 85 break; 86 case "keydown": 87 this._handleKeydownEvent(event); 88 break; 89 case "blur": 90 this._handleBlurEvent(event); 91 break; 92 case "mousedown": 93 this._handleMousedownEvent(event); 94 break; 95 case "click": 96 this._handleClickEvent(event); 97 break; 98 } 99 }, 100 101 // Private 102 103 _handleInputEvent: function(event) 104 { 105 if (this._input.value === "") 106 this._element.classList.remove(WebInspector.GoToLineDialog.NonEmptyClassName); 107 else 108 this._element.classList.add(WebInspector.GoToLineDialog.NonEmptyClassName); 109 }, 110 111 _handleKeydownEvent: function(event) 112 { 113 if (event.keyCode === WebInspector.KeyboardShortcut.Key.Escape.keyCode) { 114 if (this._input.value === "") 115 this.dismiss(); 116 else 117 this._clear(); 118 } else if (event.keyCode === WebInspector.KeyboardShortcut.Key.Enter.keyCode) { 119 var value = parseInt(this._input.value, 10); 120 121 var valueIsValid = false; 122 if (this.delegate && typeof this.delegate.isGoToLineDialogValueValid === "function") 123 valueIsValid = this.delegate.isGoToLineDialogValueValid(this, value); 124 125 if (valueIsValid && this.delegate && typeof this.delegate.goToLineDialogValueWasValidated === "function") { 126 this.delegate.goToLineDialogValueWasValidated(this, value); 127 this.dismiss(); 128 return; 129 } 130 131 this._input.select(); 132 InspectorFrontendHost.beep(); 133 } 134 }, 135 136 _handleBlurEvent: function(event) 137 { 138 this.dismiss(); 139 }, 140 141 _handleMousedownEvent: function(event) 142 { 143 this._input.select(); 144 // This ensures we don't get a "blur" event triggered for the text field 145 // which would end up dimissing the dialog, which is not the intent. 146 event.preventDefault(); 147 }, 148 149 _handleClickEvent: function(event) 150 { 151 this._clear(); 152 }, 153 154 _clear: function() 155 { 156 this._input.value = ""; 157 this._element.classList.remove(WebInspector.GoToLineDialog.NonEmptyClassName); 158 } 159} 160