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