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