Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | // Input field for queries |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 2 | /* |
| 3 | * TODO: Support allert for query problems. |
| 4 | */ |
| 5 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 6 | define({ |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * Create a new input field. |
| 10 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 11 | create : function (element) { |
| 12 | return Object.create(this)._init(element); |
| 13 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Get the mirrored input field. |
| 18 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | mirror : function () { |
| 20 | return this._mirror; |
| 21 | }, |
| 22 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Get the container element. |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 26 | * This contains the hint helper / menus |
| 27 | * and probably an |
| 28 | * error message. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 29 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | container : function () { |
| 31 | return this._container; |
| 32 | }, |
| 33 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Get the input element the |
| 37 | * hint helper is attached to. |
| 38 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 39 | element : function () { |
| 40 | return this._element; |
| 41 | }, |
| 42 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 43 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Get the value of the input field |
| 46 | * the hint helper is attached to. |
| 47 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | value : function () { |
| 49 | return this._element.value; |
| 50 | }, |
| 51 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 52 | /** |
| 53 | * Get the value of the input field mirror. |
| 54 | */ |
| 55 | mirrorValue : function () { |
| 56 | return this._mirror.firstChild.textContent; |
| 57 | }, |
| 58 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Update the mirror content. |
| 62 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 63 | update : function () { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 64 | this._mirror.firstChild.textContent = this._split()[0]; |
| 65 | this._container.style.left = this._rightPos() + 'px'; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 66 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | }, |
| 68 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 69 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Insert text into the mirror. |
| 72 | * This is a prefix of the input field's |
| 73 | * value. |
| 74 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 75 | insert : function (text) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 76 | var splittedText = this._split(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 77 | var s = this._element; |
| 78 | s.value = splittedText[0] + text + splittedText[1]; |
| 79 | s.selectionStart = (splittedText[0] + text).length; |
| 80 | s.selectionEnd = s.selectionStart; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 81 | |
| 82 | // Maybe update? |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | this._mirror.firstChild.textContent = splittedText[0] + text; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 84 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 85 | }, |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Move hinthelper to given character position |
| 89 | */ |
| 90 | moveto : function (charpos) { |
| 91 | this._element.selectionStart = charpos; |
| 92 | this._element.selectionEnd = charpos; |
| 93 | this._element.focus(); |
| 94 | return this.update(); |
| 95 | }, |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 96 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 97 | /** |
| 98 | * Reposition the input mirror directly |
| 99 | * below the input box. |
| 100 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 101 | reposition : function () { |
| 102 | var inputClientRect = this._element.getBoundingClientRect(); |
| 103 | var inputStyle = window.getComputedStyle(this._element, null); |
| 104 | |
| 105 | var bodyClientRect = |
| 106 | document.getElementsByTagName('body')[0].getBoundingClientRect(); |
| 107 | |
| 108 | // Reset position |
| 109 | var mirrorStyle = this._mirror.style; |
| 110 | mirrorStyle.left = inputClientRect.left + "px"; |
| 111 | mirrorStyle.top = (inputClientRect.bottom - bodyClientRect.top) + "px"; |
| 112 | mirrorStyle.width = inputStyle.getPropertyValue("width"); |
| 113 | |
| 114 | // These may be relevant in case of media depending css |
| 115 | mirrorStyle.paddingLeft = inputStyle.getPropertyValue("padding-left"); |
| 116 | mirrorStyle.marginLeft = inputStyle.getPropertyValue("margin-left"); |
| 117 | mirrorStyle.borderLeftWidth = inputStyle.getPropertyValue("border-left-width"); |
| 118 | mirrorStyle.borderLeftStyle = inputStyle.getPropertyValue("border-left-style"); |
| 119 | mirrorStyle.fontSize = inputStyle.getPropertyValue("font-size"); |
| 120 | mirrorStyle.fontFamily = inputStyle.getPropertyValue("font-family"); |
| 121 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 122 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 123 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Get the context, which is the input |
| 126 | * field's value bounded to the |
| 127 | * cursor position. |
| 128 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 129 | context : function () { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 130 | return this._split()[0]; |
| 131 | }, |
| 132 | |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 133 | |
| 134 | // Initialize new input field |
| 135 | _init : function (element) { |
| 136 | this._element = element; |
| 137 | |
| 138 | // Create mirror for searchField |
| 139 | // This is important for positioning |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 140 | // if ((this._mirror = document.getElementById("searchMirror")) === null) { |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 141 | this._mirror = document.createElement("div"); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 142 | this._mirror.classList.add('hint', 'mirror'); |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 143 | this._mirror.appendChild(document.createElement("span")); |
| 144 | this._container = this._mirror.appendChild(document.createElement("div")); |
| 145 | this._mirror.style.height = "0px"; |
| 146 | document.getElementsByTagName("body")[0].appendChild(this._mirror); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame^] | 147 | // }; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 148 | |
| 149 | // Update position of the mirror |
| 150 | window.addEventListener('resize', this.reposition.bind(this)); |
| 151 | this._element.addEventListener('onfocus', this.reposition.bind(this)); |
| 152 | this.reposition(); |
| 153 | return this; |
| 154 | }, |
| 155 | |
| 156 | // Get the right position |
| 157 | _rightPos : function () { |
| 158 | var box = this._mirror.firstChild.getBoundingClientRect(); |
| 159 | return box.right - box.left; |
| 160 | }, |
| 161 | |
| 162 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Return two substrings, |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 165 | * splitted at a given position |
| 166 | * or at the current cursor position. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 167 | */ |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 168 | _split : function (start) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 169 | var s = this._element; |
| 170 | var value = s.value; |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 171 | |
| 172 | // Get start from cursor position |
| 173 | if (arguments.length === 0) |
| 174 | start = s.selectionStart; |
| 175 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 176 | return new Array( |
| 177 | value.substring(0, start), |
| 178 | value.substring(start, value.length) |
| 179 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 180 | } |
| 181 | }); |