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