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