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) { |
| 14 | var e = document.createElement('div'); |
| 15 | var 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 | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 54 | var 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 | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 62 | if (data['icon'] !== undefined){ |
| 63 | b.setAttribute('data-icon', data['icon']); |
| 64 | }; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 67 | b.addE('span').addT(title); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 68 | |
| 69 | var that = this; |
| 70 | b.addEventListener('click', function (e) { |
| 71 | |
| 72 | // Do not bubble |
| 73 | e.halt(); |
| 74 | |
| 75 | // Call callback |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 76 | var obj = that._bind || this; |
| 77 | obj.button = b; |
| 78 | cb.apply(obj, e) |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 79 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 80 | |
| 81 | return b; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 82 | }, |
| 83 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 84 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 85 | /** |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 86 | * Add button that spawns a list in order. |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 87 | * |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 88 | * Returns the list object. |
| 89 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 90 | addList : function (title, data, itemClass = defaultItemClass) { |
Akron | 3b253d3 | 2018-07-15 10:16:06 +0200 | [diff] [blame] | 91 | var list = treeMenuClass.create([], itemClass); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 92 | this.add(title, data, function (e) { |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 93 | list.show(); |
| 94 | list.button(this.button); |
| 95 | list.focus(); |
| 96 | }); |
| 97 | |
| 98 | return list; |
| 99 | }, |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Add button that can toggle a state. |
| 103 | * The state has to be a state object. |
| 104 | */ |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 105 | addToggle : function (title, data, state) { |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 106 | let b = this._element.addE('span'); |
| 107 | b.setAttribute('title',title); |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 108 | |
| 109 | if (data != undefined) { |
| 110 | if (data['cls'] !== undefined) { |
| 111 | b.classList.add.apply(b.classList, data['cls']); |
| 112 | }; |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // Set check marker |
| 116 | let check = b.addE('span'); |
| 117 | check.classList.add("check", "button-icon"); |
| 118 | check.addE('span'); |
| 119 | |
| 120 | // Associate this object to state |
| 121 | // Add setState method to object |
| 122 | check.setState = function (value) { |
| 123 | if (value) { |
| 124 | this.classList.add("checked"); |
| 125 | } else { |
| 126 | this.classList.remove("checked"); |
| 127 | } |
| 128 | }; |
| 129 | state.associate(check); |
| 130 | |
| 131 | b.addE('span').addT(title); |
| 132 | |
| 133 | let that = this; |
| 134 | b.addEventListener('click', function (e) { |
| 135 | |
| 136 | // Do not bubble |
| 137 | e.halt(); |
| 138 | |
| 139 | // Toggle state |
| 140 | if (state.get()) { |
| 141 | state.set(false) |
| 142 | } else { |
| 143 | state.set(true); |
| 144 | } |
| 145 | }); |
| 146 | |
| 147 | return b; |
| 148 | }, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 149 | |
| 150 | /** |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 151 | * Bind an object to all callbacks of the button group. |
| 152 | * To get the button element inside the callback, |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 153 | * use this.button |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 154 | */ |
| 155 | bind : function (obj) { |
| 156 | if (obj !== undefined) { |
| 157 | this._bind = obj; |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 158 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 159 | }; |
| 160 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 161 | }, |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Remove all defined buttons |
| 166 | */ |
| 167 | clear : function () { |
| 168 | _removeChildren(this._element); |
| 169 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | }); |