Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | TODO: |
| 3 | - Limit the size to a certain number of elements |
| 4 | - addEventListener("click", ... , false); |
| 5 | - addEventListener("paste", ... , false); |
Nils Diewald | 4e9fbcb | 2014-07-15 11:45:09 +0000 | [diff] [blame^] | 6 | - Make this a general purpose hint-System with left-context-suport |
| 7 | - Die Funktion, wann was angezeigt werden soll, sollte extern |
| 8 | definiert sein (der Kontext / changed) |
| 9 | - Die Werteliste sollte weitere Attribute enthalten, wie title und class |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | var Hint = function (param) { |
| 13 | var foundryRegex = new RegExp("(?:^|[^a-zA-Z0-9])([-a-zA-Z0-9]+?)\/(?:([^=]+?)=)?$"); |
| 14 | |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 15 | var search = document.getElementById(param["ref"]); |
| 16 | var qlField = document.getElementById(param["qlRef"]); |
| 17 | var mirror = document.createElement("div"); |
| 18 | var hint = document.createElement("ul"); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 19 | var hintSize = param["hintSize"] ? param["hintSize"] : 10; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 20 | var hints = param["hints"]; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 21 | var that = this; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 22 | var ql; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 23 | |
| 24 | // Build the mirror element |
| 25 | // <div id="searchMirror"><span></span><ul></ul></div> |
| 26 | mirror.setAttribute("id", "searchMirror"); |
| 27 | mirror.appendChild(document.createElement("span")); |
| 28 | mirror.appendChild(hint); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 29 | document.getElementsByTagName("body")[0].appendChild(mirror); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 30 | // Default active state |
| 31 | this.active = -2; |
| 32 | |
| 33 | // Show hint table |
| 34 | this.show = function (topic) { |
| 35 | if (!hints[topic]) |
| 36 | return; |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 37 | this.hide(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 38 | this.active = -1; |
| 39 | this.list(hints[topic]); |
| 40 | var searchRight = search.getBoundingClientRect().right; |
| 41 | var infoRight = hint.getBoundingClientRect().right; |
| 42 | if (infoRight > searchRight) { |
| 43 | hint.style.marginLeft = '-' + (infoRight - searchRight) + 'px'; |
| 44 | }; |
| 45 | hint.style.opacity = 1; |
| 46 | }; |
| 47 | |
| 48 | // Initialize the mirror element |
| 49 | function init () { |
| 50 | // Copy input style |
| 51 | var searchRect = search.getBoundingClientRect(); |
| 52 | var searchStyle = window.getComputedStyle(search, null); |
| 53 | |
| 54 | with (mirror.style) { |
| 55 | left = searchRect.left + "px"; |
| 56 | top = searchRect.bottom + "px"; |
| 57 | borderLeftColor = "transparent"; |
| 58 | paddingLeft = searchStyle.getPropertyValue("padding-left"); |
| 59 | marginLeft = searchStyle.getPropertyValue("margin-left"); |
| 60 | borderLeftWidth = searchStyle.getPropertyValue("border-left-width"); |
| 61 | borderLeftStyle = searchStyle.getPropertyValue("border-left-style"); |
| 62 | fontSize = searchStyle.getPropertyValue("font-size"); |
| 63 | fontFamily = searchStyle.getPropertyValue("font-family"); |
| 64 | }; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 65 | |
| 66 | qlSelect(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // Hide hint table |
| 70 | this.hide = function () { |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 71 | if (this.active === -2) |
| 72 | return; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 73 | this.active = -2; |
| 74 | hint.style.opacity = 0; |
| 75 | hint.style.marginLeft = 0; |
| 76 | |
| 77 | // Remove all children |
| 78 | var lis = hint.childNodes; |
| 79 | for (var li = lis.length - 1; li >= 0; li--) { |
| 80 | hint.removeChild(lis[li]) |
| 81 | }; |
| 82 | }; |
| 83 | |
| 84 | // List elements in the hint table |
| 85 | this.list = function (hintList) { |
| 86 | var li, title; |
| 87 | var arrayType = hintList instanceof Array; |
| 88 | for (var i in hintList) { |
| 89 | // Create list items |
| 90 | li = document.createElement("li"); |
| 91 | li.setAttribute("data-action", arrayType ? hintList[i] : hintList[i][0]); |
| 92 | title = document.createElement("strong"); |
| 93 | title.appendChild(document.createTextNode(arrayType ? hintList[i] : i)); |
| 94 | li.appendChild(title); |
| 95 | hint.appendChild(li); |
| 96 | |
| 97 | // Include descriptions |
| 98 | if (!arrayType && hintList[i][1]) { |
| 99 | var desc = document.createElement("span"); |
| 100 | desc.appendChild(document.createTextNode(hintList[i][1])); |
| 101 | li.appendChild(desc); |
| 102 | }; |
| 103 | }; |
| 104 | }; |
| 105 | |
| 106 | // Choose next item in list |
| 107 | this.next = function () { |
| 108 | if (this.active === -2) |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 109 | return false; |
| 110 | |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 111 | var lis = hint.getElementsByTagName("li"); |
| 112 | if (this.active === -1) { |
| 113 | lis[0].setAttribute("class", "active"); |
| 114 | this.active = 0; |
| 115 | } |
| 116 | else if (this.active === lis.length - 1) { |
| 117 | lis[this.active].removeAttribute("class"); |
| 118 | lis[0].setAttribute("class", "active"); |
| 119 | this.active = 0; |
| 120 | } |
| 121 | else { |
| 122 | lis[this.active].removeAttribute("class"); |
| 123 | lis[++this.active].setAttribute("class", "active"); |
| 124 | }; |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 125 | |
| 126 | return true; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // Choose previous item in list |
| 130 | this.previous = function () { |
| 131 | if (this.active === -2) |
| 132 | return; |
| 133 | |
| 134 | var lis = hint.getElementsByTagName("li"); |
| 135 | if (this.active === -1) { |
| 136 | this.active = lis.length - 1; |
| 137 | lis[this.active].setAttribute("class", "active"); |
| 138 | } |
| 139 | else if (this.active === 0) { |
| 140 | lis[0].removeAttribute("class"); |
| 141 | this.active = lis.length - 1; |
| 142 | lis[this.active].setAttribute("class", "active"); |
| 143 | } |
| 144 | else { |
| 145 | lis[this.active].removeAttribute("class"); |
| 146 | lis[--this.active].setAttribute("class", "active"); |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | // Choose item from list |
| 151 | this.choose = function () { |
| 152 | if (this.active < 0) |
| 153 | return; |
| 154 | |
| 155 | var action = hint.getElementsByTagName("li")[this.active].getAttribute("data-action"); |
| 156 | |
| 157 | var value = search.value; |
| 158 | var start = search.selectionStart; |
| 159 | var begin = value.substring(0, start); |
| 160 | var end = value.substring(start, value.length); |
| 161 | search.value = begin + action + end; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 162 | search.selectionStart = (begin + action).length |
| 163 | search.selectionEnd = search.selectionStart; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 164 | |
| 165 | this.hide(); |
| 166 | |
| 167 | // Check for new changes |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 168 | mirror.firstChild.textContent = begin + action; |
| 169 | |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 170 | if (foundryRegex.exec(begin + action)) |
| 171 | this.show(RegExp.$1 + (RegExp.$2 ? '/' + RegExp.$2 : '')); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 172 | |
| 173 | return true; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | function changed (e) { |
| 177 | var el = e.target; |
| 178 | |
| 179 | if (e.key === '/' || e.key === '=') { |
| 180 | var start = el.selectionStart; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 181 | mirror.firstChild.textContent = el.value.substring(0, start); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 182 | var sub = el.value.substring(start - 128 >= 0 ? start - 128 : 0, start); |
| 183 | |
| 184 | if (foundryRegex.exec(sub)) |
| 185 | that.show(RegExp.$1 + (RegExp.$2 ? '/' + RegExp.$2 : '')); |
| 186 | } |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 187 | else if (e.key === '>') { |
| 188 | that.hide(); |
| 189 | } |
| 190 | else if (ql === 'poliqarp' && (e.key === '[' || e.key === '<')) { |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 191 | mirror.firstChild.textContent = el.value.substring(0, el.selectionStart); |
| 192 | that.show("-foundries"); |
| 193 | } |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 194 | else if (e.key !== 'Shift' && |
| 195 | e.key !== 'Up' && |
| 196 | e.key !== 'Down' && |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 197 | e.key !== 'Enter' && |
| 198 | e.key !== 'Alt' && |
| 199 | e.key !== 'AltGraph' && |
| 200 | e.key !== 'CapsLock') { |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 201 | that.hide(); |
| 202 | }; |
| 203 | }; |
| 204 | |
| 205 | // Select item from suggestion list |
| 206 | function select (e) { |
| 207 | if (that.active === -2) |
| 208 | return; |
| 209 | if (e.key === 'Down') { |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 210 | return that.next(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 211 | } |
| 212 | else if (e.key === 'Up') { |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 213 | return that.previous(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 214 | } |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 215 | else if (e.key === 'Enter' && that.choose()) { |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 216 | e.stopPropagation(); |
| 217 | e.preventDefault(); |
| 218 | return false; |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 219 | }; |
| 220 | that.hide(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 223 | function qlSelect () { |
| 224 | var nodes = qlField.childNodes; |
| 225 | for (var i = 0; i < nodes.length; i++) { |
| 226 | if (nodes[i].selected) { |
| 227 | ql = nodes[i].value; |
| 228 | break; |
| 229 | }; |
| 230 | }; |
| 231 | }; |
| 232 | |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 233 | // Initialize style |
| 234 | init(); |
Nils Diewald | db03fa9 | 2014-06-23 13:36:55 +0000 | [diff] [blame] | 235 | window.onresize = init; |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 236 | search.addEventListener("keyup", changed, false); |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 237 | search.addEventListener("keydown", select, false); |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 238 | qlField.addEventListener('change', qlSelect, false); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 239 | }; |