Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * MenuItems may define: |
| 3 | * |
| 4 | * onclick: action happen on click and enter. |
| 5 | * further: action happen on right arrow |
| 6 | */ |
| 7 | |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 8 | "use strict"; |
| 9 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 10 | /** |
| 11 | * Item in the Dropdown menu |
| 12 | */ |
| 13 | define({ |
| 14 | /** |
| 15 | * Create a new MenuItem object. |
| 16 | * |
| 17 | * @constructor |
| 18 | * @this {MenuItem} |
| 19 | * @param {Array.<string>} An array object of name, action and |
| 20 | * optionally a description |
| 21 | */ |
| 22 | create : function (params) { |
| 23 | return Object.create(this)._init(params); |
| 24 | }, |
| 25 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 26 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 27 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 28 | * Get or set the content of the meun item. |
| 29 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | content : function (content) { |
| 31 | if (arguments.length === 1) |
| 32 | this._content = document.createTextNode(content); |
| 33 | return this._content; |
| 34 | }, |
| 35 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 36 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 37 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 38 | * Get or set the information for action of this item. |
| 39 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 40 | action : function (action) { |
| 41 | if (arguments.length === 1) |
| 42 | this._action = action; |
| 43 | return this._action; |
| 44 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 45 | |
| 46 | |
| 47 | /** |
| 48 | * Get the lower cased field of the item |
| 49 | * (used for analyses). |
| 50 | */ |
| 51 | lcField : function () { |
| 52 | return this._lcField; |
| 53 | }, |
| 54 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Check or set if the item is active |
| 58 | * |
| 59 | * @param {boolean|null} State of activity |
| 60 | */ |
| 61 | active : function (bool) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 62 | const cl = this.element().classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 63 | if (bool === undefined) |
| 64 | return cl.contains("active"); |
| 65 | else if (bool) |
| 66 | cl.add("active"); |
| 67 | else |
| 68 | cl.remove("active"); |
| 69 | }, |
| 70 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 71 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Check or set if the item is |
| 74 | * at the boundary of the menu |
| 75 | * list |
| 76 | * |
| 77 | * @param {boolean|null} State of activity |
| 78 | */ |
| 79 | noMore : function (bool) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 80 | const cl = this.element().classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 81 | if (bool === undefined) |
| 82 | return cl.contains("no-more"); |
| 83 | else if (bool) |
| 84 | cl.add("no-more"); |
| 85 | else |
| 86 | cl.remove("no-more"); |
| 87 | }, |
| 88 | |
| 89 | /** |
| 90 | * Get the document element of the menu item |
| 91 | */ |
| 92 | element : function () { |
| 93 | // already defined |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 94 | if (this._el !== undefined) |
| 95 | return this._el; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 96 | |
| 97 | // Create list item |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 98 | const li = document.createElement("li"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 99 | |
| 100 | // Connect action |
| 101 | if (this["onclick"] !== undefined) { |
| 102 | li["onclick"] = this.onclick.bind(this); |
| 103 | }; |
| 104 | |
| 105 | // Append template |
| 106 | li.appendChild(this.content()); |
| 107 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 108 | return this._el = li; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 109 | }, |
| 110 | |
| 111 | /** |
| 112 | * Highlight parts of the item |
| 113 | * |
| 114 | * @param {string} Prefix string for highlights |
| 115 | */ |
| 116 | highlight : function (prefix) { |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 117 | |
| 118 | // The prefix already matches |
| 119 | if (this._prefix === prefix) |
| 120 | return; |
| 121 | |
| 122 | // There is a prefix but it doesn't match |
| 123 | if (this._prefix !== null) { |
| 124 | this.lowlight(); |
| 125 | } |
| 126 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 127 | const children = this.element().childNodes; |
| 128 | for (let i = children.length -1; i >= 0; i--) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 129 | this._highlight(children[i], prefix); |
| 130 | }; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 131 | |
| 132 | this._prefix = prefix; |
| 133 | }, |
| 134 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 135 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 136 | /** |
| 137 | * Remove highlight of the menu item |
| 138 | */ |
| 139 | lowlight : function () { |
| 140 | if (this._prefix === null) |
| 141 | return; |
| 142 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 143 | const e = this.element(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 144 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 145 | const marks = e.getElementsByTagName("mark"); |
| 146 | |
| 147 | for (let i = marks.length - 1; i >= 0; i--) { |
| 148 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 149 | // Replace with content |
| 150 | marks[i].parentNode.replaceChild( |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 151 | // Create text node clone |
| 152 | document.createTextNode( |
| 153 | marks[i].firstChild.nodeValue |
| 154 | ), |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 155 | marks[i] |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 156 | ); |
| 157 | }; |
| 158 | |
| 159 | // Remove consecutive textnodes |
| 160 | e.normalize(); |
| 161 | this._prefix = null; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 162 | }, |
| 163 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 164 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 165 | // Highlight a certain substring of the menu item |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 166 | _highlight : function (elem, prefixString) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 167 | if (elem.nodeType === 3) { |
| 168 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 169 | const text = elem.nodeValue; |
| 170 | const textlc = text.toLowerCase(); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 171 | |
| 172 | // Split prefixes |
| 173 | if (prefixString) { |
Akron | 359c7e1 | 2017-12-19 12:06:55 +0100 | [diff] [blame] | 174 | |
| 175 | // ND: |
| 176 | // Doing this in a single line can trigger |
| 177 | // a deep-recursion in Firefox 57.01, though I don't know why. |
| 178 | prefixString = prefixString.trim(); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 179 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 180 | const prefixes = prefixString.split(" "); |
| 181 | |
| 182 | let testPos, |
| 183 | pos = -1, |
| 184 | len = 0; |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 185 | |
| 186 | // Iterate over all prefixes and get the best one |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 187 | // for (var i = 0; i < prefixes.length; i++) { |
| 188 | prefixes.forEach(function(i) { |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 189 | |
| 190 | // Get first pos of a matching prefix |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 191 | testPos = textlc.indexOf(i); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 192 | if (testPos < 0) |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 193 | return; |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 194 | |
| 195 | if (pos === -1 || testPos < pos) { |
| 196 | pos = testPos; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 197 | len = i.length; |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 198 | } |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 199 | else if (testPos === pos && i.length > len) { |
| 200 | len = i.length; |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 201 | }; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 202 | }); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 203 | |
| 204 | // Matches! |
| 205 | if (pos >= 0) { |
| 206 | |
| 207 | // First element |
| 208 | if (pos > 0) { |
| 209 | elem.parentNode.insertBefore( |
| 210 | document.createTextNode(text.substr(0, pos)), |
| 211 | elem |
| 212 | ); |
| 213 | }; |
| 214 | |
| 215 | // Second element |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 216 | const hl = document.createElement("mark"); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 217 | hl.appendChild( |
| 218 | document.createTextNode(text.substr(pos, len)) |
| 219 | ); |
| 220 | elem.parentNode.insertBefore(hl, elem); |
| 221 | |
| 222 | // Third element |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 223 | const third = text.substr(pos + len); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 224 | |
| 225 | if (third.length > 0) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 226 | const thirdE = document.createTextNode(third); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 227 | elem.parentNode.insertBefore( |
| 228 | thirdE, |
| 229 | elem |
| 230 | ); |
| 231 | this._highlight(thirdE, prefixString); |
| 232 | }; |
| 233 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 234 | elem.parentNode.removeChild(elem); |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 235 | }; |
| 236 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 237 | } |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 238 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 239 | else { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 240 | const children = elem.childNodes; |
| 241 | for (let i = children.length -1; i >= 0; i--) { |
Akron | acffc65 | 2017-12-18 21:21:25 +0100 | [diff] [blame] | 242 | this._highlight(children[i], prefixString); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 243 | }; |
| 244 | }; |
| 245 | }, |
| 246 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 247 | // Initialize menu item |
| 248 | _init : function (params) { |
| 249 | |
Akron | c82513b | 2016-06-11 11:22:36 +0200 | [diff] [blame] | 250 | if (params[0] === undefined) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 251 | throw new Error("Missing parameters"); |
Akron | c82513b | 2016-06-11 11:22:36 +0200 | [diff] [blame] | 252 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 253 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 254 | const t = this; |
| 255 | |
| 256 | t.content(params[0]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 257 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 258 | if (params.length > 1) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 259 | t._action = params[1]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 260 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 261 | if (params.length > 2) |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 262 | t._onclick = params[2]; |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 263 | }; |
| 264 | |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 265 | t._lcField = ' ' + t.content().textContent.toLowerCase(); |
| 266 | t._prefix = null; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 267 | |
| 268 | return this; |
| 269 | }, |
| 270 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * The click action of the menu item. |
| 274 | */ |
| 275 | onclick : function (e) { |
Akron | c53cfc8 | 2020-10-19 11:00:58 +0200 | [diff] [blame] | 276 | const m = this.menu(); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 277 | |
| 278 | // Reset prefix |
| 279 | m.prefix(""); |
| 280 | |
| 281 | if (this._onclick) |
| 282 | this._onclick.apply(this, e); |
| 283 | |
| 284 | m.hide(); |
| 285 | }, |
| 286 | |
| 287 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 288 | /** |
| 289 | * Return menu list. |
| 290 | */ |
| 291 | menu : function () { |
| 292 | return this._menu; |
| 293 | } |
| 294 | }); |