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