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; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 178 | if (e.key === '/' || e.key === '=') { |
| 179 | var start = el.selectionStart; |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 180 | mirror.firstChild.textContent = el.value.substring(0, start); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 181 | var sub = el.value.substring(start - 128 >= 0 ? start - 128 : 0, start); |
| 182 | |
| 183 | if (foundryRegex.exec(sub)) |
| 184 | that.show(RegExp.$1 + (RegExp.$2 ? '/' + RegExp.$2 : '')); |
| 185 | } |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 186 | else if (e.key === '>') { |
| 187 | that.hide(); |
| 188 | } |
| 189 | else if (ql === 'poliqarp' && (e.key === '[' || e.key === '<')) { |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 190 | mirror.firstChild.textContent = el.value.substring(0, el.selectionStart); |
| 191 | that.show("-foundries"); |
| 192 | } |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 193 | else if (e.key !== 'Shift' && |
| 194 | e.key !== 'Up' && |
| 195 | e.key !== 'Down' && |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 196 | e.key !== 'Enter' && |
| 197 | e.key !== 'Alt' && |
| 198 | e.key !== 'AltGraph' && |
| 199 | e.key !== 'CapsLock') { |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 200 | that.hide(); |
| 201 | }; |
| 202 | }; |
| 203 | |
| 204 | // Select item from suggestion list |
| 205 | function select (e) { |
| 206 | if (that.active === -2) |
| 207 | return; |
| 208 | if (e.key === 'Down') { |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 209 | return that.next(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 210 | } |
| 211 | else if (e.key === 'Up') { |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 212 | return that.previous(); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 213 | } |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 214 | else if (e.key === 'Enter' && that.choose()) { |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 215 | e.stopPropagation(); |
| 216 | e.preventDefault(); |
| 217 | return false; |
Nils Diewald | bfcf090 | 2014-07-15 13:36:47 +0000 | [diff] [blame] | 218 | } |
| 219 | else if (e.key !== 'Shift' && |
| 220 | e.key !== 'Alt' && |
| 221 | e.key !== 'AltGraph' && |
| 222 | e.key !== 'CapsLock') { |
| 223 | that.hide(); |
Nils Diewald | 7cad840 | 2014-07-08 17:06:56 +0000 | [diff] [blame] | 224 | }; |
Nils Diewald | bfcf090 | 2014-07-15 13:36:47 +0000 | [diff] [blame] | 225 | return true; |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Nils Diewald | 465c425 | 2014-06-20 21:51:58 +0000 | [diff] [blame] | 228 | function qlSelect () { |
| 229 | var nodes = qlField.childNodes; |
| 230 | for (var i = 0; i < nodes.length; i++) { |
| 231 | if (nodes[i].selected) { |
| 232 | ql = nodes[i].value; |
| 233 | break; |
| 234 | }; |
| 235 | }; |
| 236 | }; |
| 237 | |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 238 | // Initialize style |
| 239 | init(); |
Nils Diewald | db03fa9 | 2014-06-23 13:36:55 +0000 | [diff] [blame] | 240 | window.onresize = init; |
Nils Diewald | 002e8fb | 2014-06-22 14:27:01 +0000 | [diff] [blame] | 241 | search.addEventListener("keyup", changed, false); |
Nils Diewald | 3b5c463 | 2014-06-20 22:06:02 +0000 | [diff] [blame] | 242 | search.addEventListener("keydown", select, false); |
Nils Diewald | bfcf090 | 2014-07-15 13:36:47 +0000 | [diff] [blame] | 243 | qlField.addEventListener("change", qlSelect, false); |
Nils Diewald | 44a7278 | 2014-06-20 16:03:21 +0000 | [diff] [blame] | 244 | }; |