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