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