Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 3 | define(['buttongroup/menu','menu/item','util'], function (treeMenuClass, defaultItemClass) { |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 4 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 5 | return { |
| 6 | /** |
| 7 | * Create button group |
| 8 | */ |
| 9 | create : function (classes) { |
| 10 | return Object.create(this)._init(classes); |
| 11 | }, |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * Adopt existing button group element |
| 15 | */ |
| 16 | adopt : function (element) { |
| 17 | const obj = Object.create(this); |
| 18 | obj._el = element; |
| 19 | return obj; |
| 20 | }, |
| 21 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 22 | // Initialize button group |
| 23 | _init : function (classes) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 24 | const e = document.createElement('div'); |
| 25 | const cl = e.classList; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 26 | if (classes) { |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 27 | cl.add.apply(cl,classes); |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 28 | classes.forEach(i => { |
| 29 | switch (i) { |
| 30 | case "open-menu-below" : { |
| 31 | this._omBelow = true; |
| 32 | break; |
| 33 | } |
| 34 | case "open-menu-outside" : { |
| 35 | this._omOutside = true; |
| 36 | break; |
| 37 | } |
| 38 | case "open-menu-left" : { |
| 39 | this._omLeft = true; |
| 40 | } |
| 41 | } |
| 42 | }) |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 43 | }; |
| 44 | cl.add('button-group'); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 45 | this._el = e; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 46 | return this; |
| 47 | }, |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 48 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Return main element |
| 51 | */ |
| 52 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 53 | return this._el; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 54 | }, |
| 55 | |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 56 | /** |
| 57 | * Define element following newly added buttons. |
| 58 | */ |
| 59 | anchor : function (anchor) { |
Akron | c8c8bf1 | 2021-09-24 11:30:45 +0200 | [diff] [blame] | 60 | if (anchor !== null && anchor.parentNode == this._el) { |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 61 | this._anchor = anchor; |
| 62 | return true; |
| 63 | }; |
| 64 | return false; |
| 65 | }, |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 66 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 67 | |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 68 | _insert : function (tag = 'span') { |
| 69 | const span = document.createElement(tag); |
| 70 | if (this._anchor) { |
| 71 | this._el.insertBefore(span, this._anchor); |
| 72 | return span; |
| 73 | } |
| 74 | return this._el.appendChild(span); |
| 75 | }, |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 76 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Add button in order |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 79 | * |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 80 | * Returns the button element |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 81 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 82 | add : function (title, data, cb) { |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 83 | const b = this._insert('span'); |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 84 | let desc = title; |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 85 | |
| 86 | if (data !== undefined) { |
| 87 | if (data['cls'] !== undefined) { |
| 88 | b.classList.add.apply(b.classList, data['cls']); |
| 89 | }; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 90 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 91 | if (data['icon'] !== undefined) { |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 92 | b.setAttribute('data-icon', data['icon']); |
| 93 | }; |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 94 | |
| 95 | if (data['state'] !== undefined) { |
| 96 | b['state'] = data['state']; |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | if (data['desc'] !== undefined) { |
| 100 | desc = data['desc']; |
| 101 | }; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 102 | }; |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 103 | |
| 104 | b.setAttribute('title', desc); |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 105 | b.addE('span').addT(title); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 106 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 107 | let that = this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 108 | b.addEventListener('click', function (e) { |
| 109 | |
| 110 | // Do not bubble |
| 111 | e.halt(); |
| 112 | |
| 113 | // Call callback |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 114 | let obj = that._bind || this; |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 115 | obj.button = b; |
| 116 | cb.apply(obj, e) |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 117 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 118 | |
| 119 | return b; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 120 | }, |
| 121 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 122 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 123 | /** |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 124 | * Add button that spawns a list in order. |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 125 | * |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 126 | * Returns the list object. |
| 127 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 128 | addList : function (title, data, itemClass = defaultItemClass) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 129 | const list = treeMenuClass.create([], itemClass); |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 130 | |
| 131 | list.openAt( |
| 132 | this._omLeft ? true : false, |
| 133 | this._omBelow ? true : false, |
| 134 | this._omOutside ? true : false, |
| 135 | ); |
| 136 | |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 137 | let b = this.add(title, data, function (e) { |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 138 | list.show(); |
| 139 | list.button(this.button); |
| 140 | list.focus(); |
| 141 | }); |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 142 | b.list = list; |
| 143 | return b; |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 144 | }, |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Add button that can toggle a state. |
| 148 | * The state has to be a state object. |
| 149 | */ |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 150 | /* |
| 151 | * addToggle() should be removed in favor of add(toggleObj) |
| 152 | * or similar, so the API of buttongroups and lists is similar |
| 153 | * for use as action plugins. |
| 154 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 155 | addToggle : function (title, data, state) { |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 156 | const b = this._insert('span'); |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 157 | let desc = title; |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 158 | |
| 159 | if (data != undefined) { |
| 160 | if (data['cls'] !== undefined) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 161 | b.classList.add.apply( |
| 162 | b.classList, |
| 163 | data['cls'] |
| 164 | ); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 165 | }; |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 166 | |
| 167 | if (data['icon'] !== undefined) { |
| 168 | b.setAttribute('data-icon', data['icon']); |
| 169 | }; |
| 170 | |
| 171 | if (data['desc'] !== undefined) { |
| 172 | desc = data['desc']; |
| 173 | }; |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 176 | b.setAttribute('title',desc); |
| 177 | |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 178 | // Set check marker |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 179 | const check = b.addE('span'); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 180 | check.classList.add("check", "button-icon"); |
| 181 | check.addE('span'); |
| 182 | |
| 183 | // Associate this object to state |
| 184 | // Add setState method to object |
| 185 | check.setState = function (value) { |
| 186 | if (value) { |
| 187 | this.classList.add("checked"); |
| 188 | } else { |
| 189 | this.classList.remove("checked"); |
| 190 | } |
| 191 | }; |
| 192 | state.associate(check); |
| 193 | |
| 194 | b.addE('span').addT(title); |
| 195 | |
| 196 | let that = this; |
| 197 | b.addEventListener('click', function (e) { |
| 198 | |
| 199 | // Do not bubble |
| 200 | e.halt(); |
| 201 | |
| 202 | // Toggle state |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 203 | state.roll(); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 204 | }); |
| 205 | |
| 206 | return b; |
| 207 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 208 | |
| 209 | /** |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 210 | * Bind an object to all callbacks of the button group. |
| 211 | * To get the button element inside the callback, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 212 | * use this.button |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 213 | */ |
| 214 | bind : function (obj) { |
| 215 | if (obj !== undefined) { |
| 216 | this._bind = obj; |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 217 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 218 | }; |
| 219 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 220 | }, |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * Remove all defined buttons |
| 225 | */ |
| 226 | clear : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 227 | _removeChildren(this._el); |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 228 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | }); |