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