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