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 | |
| 67 | /** |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 68 | * Upgrade this object to another object, |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 69 | * while private data stays intact. |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 70 | * |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 71 | * @param {Object} An object with properties. |
| 72 | */ |
| 73 | upgradeTo : function (props) { |
| 74 | for (var prop in props) { |
| 75 | this[prop] = props[prop]; |
| 76 | }; |
| 77 | return this; |
| 78 | }, |
| 79 | |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 80 | _insert : function (tag = 'span') { |
| 81 | const span = document.createElement(tag); |
| 82 | if (this._anchor) { |
| 83 | this._el.insertBefore(span, this._anchor); |
| 84 | return span; |
| 85 | } |
| 86 | return this._el.appendChild(span); |
| 87 | }, |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 88 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 89 | /** |
| 90 | * Add button in order |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 91 | * |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 92 | * Returns the button element |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 93 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 94 | add : function (title, data, cb) { |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 95 | const b = this._insert('span'); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 96 | b.setAttribute('title',title); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 97 | |
| 98 | if (data !== undefined) { |
| 99 | if (data['cls'] !== undefined) { |
| 100 | b.classList.add.apply(b.classList, data['cls']); |
| 101 | }; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 102 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 103 | if (data['icon'] !== undefined) { |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 104 | b.setAttribute('data-icon', data['icon']); |
| 105 | }; |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 106 | |
| 107 | if (data['state'] !== undefined) { |
| 108 | b['state'] = data['state']; |
| 109 | } |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 112 | b.addE('span').addT(title); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 113 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 114 | let that = this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 115 | b.addEventListener('click', function (e) { |
| 116 | |
| 117 | // Do not bubble |
| 118 | e.halt(); |
| 119 | |
| 120 | // Call callback |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 121 | let obj = that._bind || this; |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 122 | obj.button = b; |
| 123 | cb.apply(obj, e) |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 124 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 125 | |
| 126 | return b; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 127 | }, |
| 128 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 129 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 130 | /** |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 131 | * Add button that spawns a list in order. |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 132 | * |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 133 | * Returns the list object. |
| 134 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 135 | addList : function (title, data, itemClass = defaultItemClass) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 136 | const list = treeMenuClass.create([], itemClass); |
Akron | 4510a3e | 2021-09-10 15:09:56 +0200 | [diff] [blame] | 137 | |
| 138 | list.openAt( |
| 139 | this._omLeft ? true : false, |
| 140 | this._omBelow ? true : false, |
| 141 | this._omOutside ? true : false, |
| 142 | ); |
| 143 | |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 144 | this.add(title, data, function (e) { |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 145 | list.show(); |
| 146 | list.button(this.button); |
| 147 | list.focus(); |
| 148 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 149 | return list; |
| 150 | }, |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Add button that can toggle a state. |
| 154 | * The state has to be a state object. |
| 155 | */ |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 156 | /* |
| 157 | * addToggle() should be removed in favor of add(toggleObj) |
| 158 | * or similar, so the API of buttongroups and lists is similar |
| 159 | * for use as action plugins. |
| 160 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 161 | addToggle : function (title, data, state) { |
Akron | f8af3b8 | 2021-07-21 20:24:00 +0200 | [diff] [blame] | 162 | const b = this._insert('span'); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 163 | b.setAttribute('title',title); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 164 | |
| 165 | if (data != undefined) { |
| 166 | if (data['cls'] !== undefined) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 167 | b.classList.add.apply( |
| 168 | b.classList, |
| 169 | data['cls'] |
| 170 | ); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 171 | }; |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | // Set check marker |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 175 | const check = b.addE('span'); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 176 | check.classList.add("check", "button-icon"); |
| 177 | check.addE('span'); |
| 178 | |
| 179 | // Associate this object to state |
| 180 | // Add setState method to object |
| 181 | check.setState = function (value) { |
| 182 | if (value) { |
| 183 | this.classList.add("checked"); |
| 184 | } else { |
| 185 | this.classList.remove("checked"); |
| 186 | } |
| 187 | }; |
| 188 | state.associate(check); |
| 189 | |
| 190 | b.addE('span').addT(title); |
| 191 | |
| 192 | let that = this; |
| 193 | b.addEventListener('click', function (e) { |
| 194 | |
| 195 | // Do not bubble |
| 196 | e.halt(); |
| 197 | |
| 198 | // Toggle state |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 199 | state.roll(); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 200 | }); |
| 201 | |
| 202 | return b; |
| 203 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 204 | |
| 205 | /** |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 206 | * Bind an object to all callbacks of the button group. |
| 207 | * To get the button element inside the callback, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 208 | * use this.button |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 209 | */ |
| 210 | bind : function (obj) { |
| 211 | if (obj !== undefined) { |
| 212 | this._bind = obj; |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 213 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 214 | }; |
| 215 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 216 | }, |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Remove all defined buttons |
| 221 | */ |
| 222 | clear : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 223 | _removeChildren(this._el); |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 224 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | }); |