| Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 1 | define(['buttongroup','buttongroup/menu','menu/item','state'], function (buttonGroupClass, buttonGroupMenuClass, defaultItemClass, stateClass) { |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 2 | |
| 3 | var FunObj = { |
| 4 | count : 0, |
| 5 | create : function () { |
| 6 | return Object.create(this); |
| 7 | }, |
| 8 | incr : function () { |
| 9 | this.count++; |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | describe('KorAP.ButtonGroup', function () { |
| 14 | |
| 15 | it('should be initializable', function () { |
| 16 | var group = buttonGroupClass.create(['action', 'bottom']); |
| 17 | var el = group.element(); |
| 18 | expect(el.tagName).toEqual('DIV'); |
| 19 | expect(el.firstChild).toBeNull(); |
| 20 | expect(el.classList.contains('action')).toBeTruthy(); |
| 21 | expect(el.classList.contains('bottom')).toBeTruthy(); |
| 22 | expect(el.classList.contains('button-group')).toBeTruthy(); |
| 23 | }); |
| 24 | |
| 25 | it('should be expandable', function () { |
| 26 | var group = buttonGroupClass.create(); |
| 27 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 28 | |
| Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 29 | group.add('Meta', { 'cls':['meta', 'top']}, function (e) {}); |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 30 | |
| 31 | var btn = group.element().firstChild; |
| 32 | expect(btn.tagName).toEqual('SPAN'); |
| 33 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 34 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 35 | expect(btn.innerText).toEqual('Meta'); |
| 36 | }); |
| 37 | |
| Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 38 | it('should be clearable', function () { |
| 39 | var group = buttonGroupClass.create(); |
| 40 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 41 | |
| Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 42 | group.add('Meta', {'cls':['meta', 'top']}, function (e) {}); |
| 43 | group.add('Mate', {'cls':['mate']}, function (e) {}); |
| Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 44 | |
| 45 | var btn = group.element().children[0]; |
| 46 | expect(btn.tagName).toEqual('SPAN'); |
| 47 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 48 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 49 | expect(btn.innerText).toEqual('Meta'); |
| 50 | |
| 51 | btn = group.element().children[1]; |
| 52 | expect(btn.tagName).toEqual('SPAN'); |
| 53 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 54 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 55 | expect(btn.innerText).toEqual('Mate'); |
| 56 | |
| 57 | // clear button |
| 58 | group.clear(); |
| 59 | |
| 60 | expect(group.element().children.length).toEqual(0); |
| 61 | |
| Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 62 | group.add('New', {'cls':['new']}, function (e) {}); |
| Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 63 | |
| 64 | btn = group.element().children[0]; |
| 65 | expect(btn.tagName).toEqual('SPAN'); |
| 66 | expect(btn.classList.contains('new')).toBeTruthy(); |
| 67 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 68 | expect(btn.innerText).toEqual('New'); |
| 69 | }); |
| 70 | |
| 71 | |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 72 | it('should listen to button clicks', function () { |
| 73 | var group = buttonGroupClass.create(); |
| 74 | |
| 75 | var count = 0; |
| 76 | |
| Akron | 9306b25 | 2026-02-18 15:29:41 +0100 | [diff] [blame^] | 77 | let btn = group.add('Meta', undefined, function () { |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 78 | count++; |
| 79 | }); |
| 80 | |
| 81 | expect(count).toEqual(0); |
| 82 | |
| 83 | // Click on the button |
| 84 | group.element().firstChild.click(); |
| 85 | |
| 86 | expect(count).toEqual(1); |
| Akron | 9306b25 | 2026-02-18 15:29:41 +0100 | [diff] [blame^] | 87 | |
| 88 | expect(btn.textContent).toEqual("Meta"); |
| 89 | btn.changeTitle("Cool"); |
| 90 | expect(btn.textContent).toEqual("Cool"); |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 91 | }); |
| 92 | |
| 93 | it('should respect binds', function () { |
| 94 | var group = buttonGroupClass.create(); |
| 95 | fun = FunObj.create(); |
| 96 | expect(fun.count).toEqual(0); |
| 97 | |
| 98 | // Bind group to another object |
| Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 99 | var group2 = group.bind(fun); |
| 100 | expect(group2).toEqual(group); |
| 101 | expect(group.bind()).toEqual(fun); |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 102 | |
| 103 | group.add('Incr', undefined, function (e) { |
| 104 | // increment on bind object |
| 105 | this.incr() |
| 106 | }); |
| 107 | |
| 108 | // Click on the button |
| 109 | group.element().firstChild.click(); |
| 110 | expect(fun.count).toEqual(1); |
| 111 | }); |
| Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 112 | |
| hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 113 | it('should add icon', function () { |
| 114 | var group = buttonGroupClass.create(); |
| 115 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 116 | |
| Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 117 | group.add('Meta', {'cls':['meta'], 'icon': 'metaicon'}, function (e) {}); |
| hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 118 | |
| 119 | var btn = group.element().firstChild; |
| 120 | expect(btn.tagName).toEqual('SPAN'); |
| 121 | expect(btn.getAttribute('data-icon')).toEqual('metaicon'); |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 122 | expect(btn.getAttribute('title')).toEqual('Meta'); |
| hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 123 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 124 | expect(btn.innerText).toEqual('Meta'); |
| 125 | }); |
| 126 | |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 127 | it('should add description', function () { |
| 128 | var group = buttonGroupClass.create(); |
| 129 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 130 | |
| 131 | group.add('Meta', {'cls':['meta'], 'icon': 'metaicon', 'desc': 'MyMeta'}, function (e) {}); |
| 132 | |
| 133 | var btn = group.element().firstChild; |
| 134 | expect(btn.tagName).toEqual('SPAN'); |
| 135 | expect(btn.getAttribute('data-icon')).toEqual('metaicon'); |
| 136 | expect(btn.getAttribute('title')).toEqual('MyMeta'); |
| 137 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 138 | expect(btn.innerText).toEqual('Meta'); |
| 139 | }); |
| 140 | |
| Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 141 | it('should open lists', function () { |
| Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 142 | var group = buttonGroupClass.create(); |
| 143 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 144 | |
| Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 145 | var b = group.addList('More', {'cls':['more']}); |
| 146 | var list = b.list; |
| Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 147 | |
| 148 | list.readItems([ |
| 149 | ['cool', 'cool', function () { }], |
| 150 | ['very cool', 'veryCool', function () { }] |
| 151 | ]); |
| 152 | |
| 153 | var btn = group.element().firstChild; |
| 154 | expect(btn.tagName).toEqual('SPAN'); |
| 155 | expect(btn.classList.contains('more')).toBeTruthy(); |
| 156 | expect(btn.innerText).toEqual('More'); |
| 157 | |
| 158 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 159 | |
| Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 160 | // Click to show menu |
| 161 | btn.click(); |
| 162 | |
| 163 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
| 164 | |
| 165 | expect(list.element().children[1].children[0].innerText).toEqual('cool--'); |
| 166 | expect(list.element().children[1].children[1].innerText).toEqual('very cool--'); |
| 167 | |
| 168 | document.body.removeChild(list.element()); |
| Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 169 | }); |
| Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 170 | |
| Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 171 | it('should add to lists', function () { |
| 172 | var group = buttonGroupClass.create(); |
| 173 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 174 | |
| Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 175 | var b = group.addList('More', {'cls':['more']}); |
| 176 | var list = b.list; |
| Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 177 | |
| 178 | let x = 'empty'; |
| 179 | list.add('Meta1', {'cls':['meta'], 'icon': 'metaicon'}, function (e) { |
| 180 | x = 'meta1'; |
| 181 | }); |
| 182 | list.add('Meta2', {'cls':['meta'], 'icon': 'metaicon'}, function (e) { |
| 183 | x = 'meta2' |
| 184 | }); |
| 185 | |
| 186 | var btn = group.element().firstChild; |
| 187 | expect(btn.tagName).toEqual('SPAN'); |
| 188 | expect(btn.classList.contains('more')).toBeTruthy(); |
| 189 | expect(btn.innerText).toEqual('More'); |
| 190 | |
| 191 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 192 | |
| 193 | // Click to show menu |
| 194 | btn.click(); |
| 195 | |
| 196 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
| Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 197 | expect(directElementChildrenByTagName(list.element(),"li")[0].innerHTML).toEqual('Meta1'); |
| 198 | expect(directElementChildrenByTagName(list.element(),"li")[1].innerHTML).toEqual('Meta2'); |
| Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 199 | |
| 200 | expect(x).toEqual('empty'); |
| Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 201 | directElementChildrenByTagName(list.element(),"li")[0].click(); |
| Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 202 | expect(x).toEqual('meta1'); |
| 203 | |
| 204 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 205 | |
| 206 | // Click to show menu |
| 207 | btn.click(); |
| 208 | |
| 209 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
| Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 210 | directElementChildrenByTagName(list.element(),"li")[1].click(); |
| Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 211 | expect(x).toEqual('meta2'); |
| 212 | |
| 213 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 214 | |
| 215 | expect(list.element().children[1].children[0].innerText).toEqual('Meta1--'); |
| 216 | expect(list.element().children[1].children[1].innerText).toEqual('Meta2--'); |
| 217 | |
| 218 | document.body.removeChild(list.element()); |
| 219 | }); |
| 220 | |
| 221 | |
| Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 222 | it('should support toggle buttons', function () { |
| 223 | var group = buttonGroupClass.create(); |
| 224 | |
| 225 | let s = stateClass.create(); |
| 226 | |
| 227 | expect(s.get()).toBeFalsy(); |
| 228 | |
| Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 229 | group.addToggle('example',{'cls':["examplecls"]}, s); |
| Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 230 | |
| 231 | let e = group.element(); |
| 232 | |
| 233 | expect(e.firstChild.getAttribute("title")).toBe("example"); |
| 234 | expect(e.firstChild.classList.contains("examplecls")).toBeTruthy(); |
| 235 | |
| 236 | expect(e.firstChild.firstChild.classList.contains("check")).toBeTruthy(); |
| 237 | expect(e.firstChild.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 238 | expect(e.firstChild.lastChild.textContent).toBe("example"); |
| 239 | |
| 240 | // Check state |
| 241 | expect(s.get()).toBeFalsy(); |
| 242 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeFalsy(); |
| 243 | |
| 244 | // Click on the button |
| 245 | e.firstChild.click(); |
| 246 | |
| 247 | // Check state |
| 248 | expect(s.get()).toBeTruthy(); |
| 249 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeTruthy(); |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 250 | |
| 251 | |
| 252 | group.addToggle('example2',{'cls':["examplecls"], "desc":"Haha"}, s); |
| 253 | |
| 254 | e = group.element(); |
| 255 | |
| 256 | expect(e.children[1].getAttribute("title")).toBe("Haha"); |
| Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 257 | }); |
| Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 258 | |
| Akron | 5881be1 | 2026-02-19 11:52:01 +0100 | [diff] [blame] | 259 | it('should support active state on regular buttons', function () { |
| 260 | var group = buttonGroupClass.create(); |
| 261 | let active = stateClass.create(); |
| 262 | let count = 0; |
| 263 | |
| 264 | group.add('Meta', {'active': active}, function () { |
| 265 | count++; |
| 266 | }); |
| 267 | |
| 268 | let e = group.element(); |
| 269 | let button = e.firstChild; |
| 270 | let check = button.firstChild; |
| 271 | |
| 272 | expect(check.classList.contains("check")).toBeTruthy(); |
| 273 | expect(check.classList.contains("button-icon")).toBeTruthy(); |
| 274 | expect(active.get()).toBeFalsy(); |
| 275 | expect(check.classList.contains("checked")).toBeFalsy(); |
| 276 | expect(count).toEqual(0); |
| 277 | |
| 278 | // Clicking the check toggles active state, but does not trigger callback. |
| 279 | check.click(); |
| 280 | expect(active.get()).toBeTruthy(); |
| 281 | expect(check.classList.contains("checked")).toBeTruthy(); |
| 282 | expect(count).toEqual(0); |
| 283 | |
| 284 | // Clicking the button still triggers the callback. |
| 285 | button.click(); |
| 286 | expect(count).toEqual(1); |
| 287 | }); |
| 288 | |
| Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 289 | it('should allow adoption', function () { |
| 290 | |
| 291 | const el = document.createElement('div'); |
| 292 | |
| 293 | const group = buttonGroupClass.adopt(el); |
| 294 | |
| 295 | group.add('Meta', {'cls':['meta', 'top']}, function (e) {}); |
| 296 | group.add('Mate', {'cls':['mate']}, function (e) {}); |
| 297 | |
| 298 | var btn = group.element().children[0]; |
| 299 | expect(btn.tagName).toEqual('SPAN'); |
| 300 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 301 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 302 | expect(btn.innerText).toEqual('Meta'); |
| 303 | |
| 304 | btn = group.element().children[1]; |
| 305 | expect(btn.tagName).toEqual('SPAN'); |
| 306 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 307 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 308 | expect(btn.innerText).toEqual('Mate'); |
| 309 | }); |
| 310 | |
| 311 | it('should make anchor element definable', function () { |
| 312 | |
| 313 | const el = document.createElement('div'); |
| 314 | const c1 = el.appendChild(document.createElement('c1')); |
| 315 | const c2 = el.appendChild(document.createElement('c2')); |
| 316 | const c3 = el.appendChild(document.createElement('c3')); |
| Akron | c8c8bf1 | 2021-09-24 11:30:45 +0200 | [diff] [blame] | 317 | |
| 318 | let group = buttonGroupClass.adopt(el); |
| 319 | expect(group.anchor(null)).toBeFalsy(); |
| Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 320 | |
| Akron | c8c8bf1 | 2021-09-24 11:30:45 +0200 | [diff] [blame] | 321 | group = buttonGroupClass.adopt(el); |
| Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 322 | expect(group.anchor(c3)).toBeTruthy(); |
| 323 | |
| 324 | group.add('Meta', {'cls':['meta', 'top']}, function (e) {}); |
| 325 | group.add('Mate', {'cls':['mate']}, function (e) {}); |
| 326 | |
| 327 | let btn = group.element().children[0]; |
| 328 | expect(btn.tagName).toEqual('C1'); |
| 329 | |
| 330 | btn = group.element().children[1]; |
| 331 | expect(btn.tagName).toEqual('C2'); |
| 332 | |
| 333 | btn = group.element().children[2]; |
| 334 | expect(btn.tagName).toEqual('SPAN'); |
| 335 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 336 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 337 | expect(btn.innerText).toEqual('Meta'); |
| 338 | |
| 339 | btn = group.element().children[3]; |
| 340 | expect(btn.tagName).toEqual('SPAN'); |
| 341 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 342 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 343 | expect(btn.innerText).toEqual('Mate'); |
| 344 | |
| 345 | btn = group.element().children[4]; |
| 346 | expect(btn.tagName).toEqual('C3'); |
| 347 | }); |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 348 | }); |
| Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 349 | |
| 350 | describe('KorAP.ButtonGroup.Menu', function () { |
| 351 | it('should reposition', function () { |
| 352 | const menu = buttonGroupMenuClass.create([["hallo", undefined, function () {}]], defaultItemClass); |
| 353 | const div = document.createElement('div'); |
| 354 | |
| 355 | document.body.appendChild(div); |
| 356 | |
| 357 | div.style.position = 'absolute'; |
| 358 | div.style.display = 'block'; |
| 359 | div.style.left = '14px'; |
| 360 | div.style.top = '20px'; |
| 361 | div.style.width = '40px'; |
| 362 | div.style.height = '30px'; |
| 363 | |
| 364 | menu.show(); |
| 365 | |
| 366 | // 000 |
| 367 | menu._repos(div); |
| 368 | let elem = menu.element(); |
| 369 | const fffl = elem.style.left; |
| 370 | const ffft = elem.style.top; |
| 371 | |
| 372 | // 100 |
| 373 | menu.openAt(true, false, false); |
| 374 | menu._repos(div); |
| 375 | elem = menu.element(); |
| 376 | const tffl = elem.style.left; |
| 377 | expect(tffl).not.toEqual(fffl); |
| 378 | |
| 379 | |
| 380 | // 011 |
| 381 | menu.openAt(false, true, true); |
| 382 | menu._repos(div); |
| 383 | elem = menu.element(); |
| 384 | const fttt = elem.style.top; |
| 385 | expect(fttt).not.toEqual(ffft); |
| 386 | |
| 387 | document.body.removeChild(div); |
| 388 | document.body.removeChild(elem); |
| 389 | }); |
| 390 | }); |
| Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 391 | }); |