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 | |
| 77 | group.add('Meta', undefined, function () { |
| 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); |
| 87 | }); |
| 88 | |
| 89 | it('should respect binds', function () { |
| 90 | var group = buttonGroupClass.create(); |
| 91 | fun = FunObj.create(); |
| 92 | expect(fun.count).toEqual(0); |
| 93 | |
| 94 | // Bind group to another object |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 95 | var group2 = group.bind(fun); |
| 96 | expect(group2).toEqual(group); |
| 97 | expect(group.bind()).toEqual(fun); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 98 | |
| 99 | group.add('Incr', undefined, function (e) { |
| 100 | // increment on bind object |
| 101 | this.incr() |
| 102 | }); |
| 103 | |
| 104 | // Click on the button |
| 105 | group.element().firstChild.click(); |
| 106 | expect(fun.count).toEqual(1); |
| 107 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 108 | |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 109 | it('should add icon', function () { |
| 110 | var group = buttonGroupClass.create(); |
| 111 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 112 | |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 113 | group.add('Meta', {'cls':['meta'], 'icon': 'metaicon'}, function (e) {}); |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 114 | |
| 115 | var btn = group.element().firstChild; |
| 116 | expect(btn.tagName).toEqual('SPAN'); |
| 117 | expect(btn.getAttribute('data-icon')).toEqual('metaicon'); |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 118 | expect(btn.getAttribute('title')).toEqual('Meta'); |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 119 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 120 | expect(btn.innerText).toEqual('Meta'); |
| 121 | }); |
| 122 | |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 123 | it('should add description', function () { |
| 124 | var group = buttonGroupClass.create(); |
| 125 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 126 | |
| 127 | group.add('Meta', {'cls':['meta'], 'icon': 'metaicon', 'desc': 'MyMeta'}, function (e) {}); |
| 128 | |
| 129 | var btn = group.element().firstChild; |
| 130 | expect(btn.tagName).toEqual('SPAN'); |
| 131 | expect(btn.getAttribute('data-icon')).toEqual('metaicon'); |
| 132 | expect(btn.getAttribute('title')).toEqual('MyMeta'); |
| 133 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 134 | expect(btn.innerText).toEqual('Meta'); |
| 135 | }); |
| 136 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 137 | it('should open lists', function () { |
Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 138 | var group = buttonGroupClass.create(); |
| 139 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 140 | |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 141 | var b = group.addList('More', {'cls':['more']}); |
| 142 | var list = b.list; |
Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 143 | |
| 144 | list.readItems([ |
| 145 | ['cool', 'cool', function () { }], |
| 146 | ['very cool', 'veryCool', function () { }] |
| 147 | ]); |
| 148 | |
| 149 | var btn = group.element().firstChild; |
| 150 | expect(btn.tagName).toEqual('SPAN'); |
| 151 | expect(btn.classList.contains('more')).toBeTruthy(); |
| 152 | expect(btn.innerText).toEqual('More'); |
| 153 | |
| 154 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 155 | |
Akron | 8120ac1 | 2020-09-14 18:05:33 +0200 | [diff] [blame] | 156 | // Click to show menu |
| 157 | btn.click(); |
| 158 | |
| 159 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
| 160 | |
| 161 | expect(list.element().children[1].children[0].innerText).toEqual('cool--'); |
| 162 | expect(list.element().children[1].children[1].innerText).toEqual('very cool--'); |
| 163 | |
| 164 | document.body.removeChild(list.element()); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 165 | }); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 166 | |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 167 | it('should add to lists', function () { |
| 168 | var group = buttonGroupClass.create(); |
| 169 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 170 | |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 171 | var b = group.addList('More', {'cls':['more']}); |
| 172 | var list = b.list; |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 173 | |
| 174 | let x = 'empty'; |
| 175 | list.add('Meta1', {'cls':['meta'], 'icon': 'metaicon'}, function (e) { |
| 176 | x = 'meta1'; |
| 177 | }); |
| 178 | list.add('Meta2', {'cls':['meta'], 'icon': 'metaicon'}, function (e) { |
| 179 | x = 'meta2' |
| 180 | }); |
| 181 | |
| 182 | var btn = group.element().firstChild; |
| 183 | expect(btn.tagName).toEqual('SPAN'); |
| 184 | expect(btn.classList.contains('more')).toBeTruthy(); |
| 185 | expect(btn.innerText).toEqual('More'); |
| 186 | |
| 187 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 188 | |
| 189 | // Click to show menu |
| 190 | btn.click(); |
| 191 | |
| 192 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 193 | expect(directElementChildrenByTagName(list.element(),"li")[0].innerHTML).toEqual('Meta1'); |
| 194 | expect(directElementChildrenByTagName(list.element(),"li")[1].innerHTML).toEqual('Meta2'); |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 195 | |
| 196 | expect(x).toEqual('empty'); |
Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 197 | directElementChildrenByTagName(list.element(),"li")[0].click(); |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 198 | expect(x).toEqual('meta1'); |
| 199 | |
| 200 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 201 | |
| 202 | // Click to show menu |
| 203 | btn.click(); |
| 204 | |
| 205 | expect(list.element().classList.contains('visible')).toBeTruthy(); |
Leo Repp | dedcf1a | 2021-08-18 18:57:47 +0200 | [diff] [blame] | 206 | directElementChildrenByTagName(list.element(),"li")[1].click(); |
Akron | 20b6d31 | 2021-07-26 15:14:42 +0200 | [diff] [blame] | 207 | expect(x).toEqual('meta2'); |
| 208 | |
| 209 | expect(list.element().classList.contains('visible')).toBeFalsy(); |
| 210 | |
| 211 | expect(list.element().children[1].children[0].innerText).toEqual('Meta1--'); |
| 212 | expect(list.element().children[1].children[1].innerText).toEqual('Meta2--'); |
| 213 | |
| 214 | document.body.removeChild(list.element()); |
| 215 | }); |
| 216 | |
| 217 | |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 218 | it('should support toggle buttons', function () { |
| 219 | var group = buttonGroupClass.create(); |
| 220 | |
| 221 | let s = stateClass.create(); |
| 222 | |
| 223 | expect(s.get()).toBeFalsy(); |
| 224 | |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 225 | group.addToggle('example',{'cls':["examplecls"]}, s); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 226 | |
| 227 | let e = group.element(); |
| 228 | |
| 229 | expect(e.firstChild.getAttribute("title")).toBe("example"); |
| 230 | expect(e.firstChild.classList.contains("examplecls")).toBeTruthy(); |
| 231 | |
| 232 | expect(e.firstChild.firstChild.classList.contains("check")).toBeTruthy(); |
| 233 | expect(e.firstChild.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 234 | expect(e.firstChild.lastChild.textContent).toBe("example"); |
| 235 | |
| 236 | // Check state |
| 237 | expect(s.get()).toBeFalsy(); |
| 238 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeFalsy(); |
| 239 | |
| 240 | // Click on the button |
| 241 | e.firstChild.click(); |
| 242 | |
| 243 | // Check state |
| 244 | expect(s.get()).toBeTruthy(); |
| 245 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeTruthy(); |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 246 | |
| 247 | |
| 248 | group.addToggle('example2',{'cls':["examplecls"], "desc":"Haha"}, s); |
| 249 | |
| 250 | e = group.element(); |
| 251 | |
| 252 | expect(e.children[1].getAttribute("title")).toBe("Haha"); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 253 | }); |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 254 | |
| 255 | it('should allow adoption', function () { |
| 256 | |
| 257 | const el = document.createElement('div'); |
| 258 | |
| 259 | const group = buttonGroupClass.adopt(el); |
| 260 | |
| 261 | group.add('Meta', {'cls':['meta', 'top']}, function (e) {}); |
| 262 | group.add('Mate', {'cls':['mate']}, function (e) {}); |
| 263 | |
| 264 | var btn = group.element().children[0]; |
| 265 | expect(btn.tagName).toEqual('SPAN'); |
| 266 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 267 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 268 | expect(btn.innerText).toEqual('Meta'); |
| 269 | |
| 270 | btn = group.element().children[1]; |
| 271 | expect(btn.tagName).toEqual('SPAN'); |
| 272 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 273 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 274 | expect(btn.innerText).toEqual('Mate'); |
| 275 | }); |
| 276 | |
| 277 | it('should make anchor element definable', function () { |
| 278 | |
| 279 | const el = document.createElement('div'); |
| 280 | const c1 = el.appendChild(document.createElement('c1')); |
| 281 | const c2 = el.appendChild(document.createElement('c2')); |
| 282 | const c3 = el.appendChild(document.createElement('c3')); |
Akron | c8c8bf1 | 2021-09-24 11:30:45 +0200 | [diff] [blame] | 283 | |
| 284 | let group = buttonGroupClass.adopt(el); |
| 285 | expect(group.anchor(null)).toBeFalsy(); |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 286 | |
Akron | c8c8bf1 | 2021-09-24 11:30:45 +0200 | [diff] [blame] | 287 | group = buttonGroupClass.adopt(el); |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 288 | expect(group.anchor(c3)).toBeTruthy(); |
| 289 | |
| 290 | group.add('Meta', {'cls':['meta', 'top']}, function (e) {}); |
| 291 | group.add('Mate', {'cls':['mate']}, function (e) {}); |
| 292 | |
| 293 | let btn = group.element().children[0]; |
| 294 | expect(btn.tagName).toEqual('C1'); |
| 295 | |
| 296 | btn = group.element().children[1]; |
| 297 | expect(btn.tagName).toEqual('C2'); |
| 298 | |
| 299 | btn = group.element().children[2]; |
| 300 | expect(btn.tagName).toEqual('SPAN'); |
| 301 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 302 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 303 | expect(btn.innerText).toEqual('Meta'); |
| 304 | |
| 305 | btn = group.element().children[3]; |
| 306 | expect(btn.tagName).toEqual('SPAN'); |
| 307 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 308 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 309 | expect(btn.innerText).toEqual('Mate'); |
| 310 | |
| 311 | btn = group.element().children[4]; |
| 312 | expect(btn.tagName).toEqual('C3'); |
| 313 | }); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 314 | }); |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 315 | |
| 316 | describe('KorAP.ButtonGroup.Menu', function () { |
| 317 | it('should reposition', function () { |
| 318 | const menu = buttonGroupMenuClass.create([["hallo", undefined, function () {}]], defaultItemClass); |
| 319 | const div = document.createElement('div'); |
| 320 | |
| 321 | document.body.appendChild(div); |
| 322 | |
| 323 | div.style.position = 'absolute'; |
| 324 | div.style.display = 'block'; |
| 325 | div.style.left = '14px'; |
| 326 | div.style.top = '20px'; |
| 327 | div.style.width = '40px'; |
| 328 | div.style.height = '30px'; |
| 329 | |
| 330 | menu.show(); |
| 331 | |
| 332 | // 000 |
| 333 | menu._repos(div); |
| 334 | let elem = menu.element(); |
| 335 | const fffl = elem.style.left; |
| 336 | const ffft = elem.style.top; |
| 337 | |
| 338 | // 100 |
| 339 | menu.openAt(true, false, false); |
| 340 | menu._repos(div); |
| 341 | elem = menu.element(); |
| 342 | const tffl = elem.style.left; |
| 343 | expect(tffl).not.toEqual(fffl); |
| 344 | |
| 345 | |
| 346 | // 011 |
| 347 | menu.openAt(false, true, true); |
| 348 | menu._repos(div); |
| 349 | elem = menu.element(); |
| 350 | const fttt = elem.style.top; |
| 351 | expect(fttt).not.toEqual(ffft); |
| 352 | |
| 353 | document.body.removeChild(div); |
| 354 | document.body.removeChild(elem); |
| 355 | }); |
| 356 | }); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 357 | }); |