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 | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 109 | addToggle : function (title, data, state) { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 110 | const b = this._el.addE('span'); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 111 | b.setAttribute('title',title); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 112 | |
| 113 | if (data != undefined) { |
| 114 | if (data['cls'] !== undefined) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 115 | b.classList.add.apply( |
| 116 | b.classList, |
| 117 | data['cls'] |
| 118 | ); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 119 | }; |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // Set check marker |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 123 | const check = b.addE('span'); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 124 | check.classList.add("check", "button-icon"); |
| 125 | check.addE('span'); |
| 126 | |
| 127 | // Associate this object to state |
| 128 | // Add setState method to object |
| 129 | check.setState = function (value) { |
| 130 | if (value) { |
| 131 | this.classList.add("checked"); |
| 132 | } else { |
| 133 | this.classList.remove("checked"); |
| 134 | } |
| 135 | }; |
| 136 | state.associate(check); |
| 137 | |
| 138 | b.addE('span').addT(title); |
| 139 | |
| 140 | let that = this; |
| 141 | b.addEventListener('click', function (e) { |
| 142 | |
| 143 | // Do not bubble |
| 144 | e.halt(); |
| 145 | |
| 146 | // Toggle state |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 147 | state.roll(); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 148 | }); |
| 149 | |
| 150 | return b; |
| 151 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 152 | |
| 153 | /** |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 154 | * Bind an object to all callbacks of the button group. |
| 155 | * To get the button element inside the callback, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 156 | * use this.button |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 157 | */ |
| 158 | bind : function (obj) { |
| 159 | if (obj !== undefined) { |
| 160 | this._bind = obj; |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 161 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 162 | }; |
| 163 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 164 | }, |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Remove all defined buttons |
| 169 | */ |
| 170 | clear : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 171 | _removeChildren(this._el); |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 172 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | }); |