Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 1 | define(['util'], function () { |
| 2 | return { |
| 3 | /** |
| 4 | * Create button group |
| 5 | */ |
| 6 | create : function (classes) { |
| 7 | return Object.create(this)._init(classes); |
| 8 | }, |
| 9 | |
| 10 | // Initialize button group |
| 11 | _init : function (classes) { |
| 12 | var e = document.createElement('div'); |
| 13 | var cl = e.classList; |
| 14 | if (classes !== undefined) { |
| 15 | cl.add.apply(cl,classes); |
| 16 | }; |
| 17 | cl.add('button-group'); |
| 18 | this._element = e; |
| 19 | return this; |
| 20 | }, |
| 21 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame^] | 22 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 23 | /** |
| 24 | * Return main element |
| 25 | */ |
| 26 | element : function () { |
| 27 | return this._element; |
| 28 | }, |
| 29 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame^] | 30 | |
| 31 | /** |
| 32 | * Upgrade this object to another object, |
| 33 | * while private data stays intact. |
| 34 | * |
| 35 | * @param {Object} An object with properties. |
| 36 | */ |
| 37 | upgradeTo : function (props) { |
| 38 | for (var prop in props) { |
| 39 | this[prop] = props[prop]; |
| 40 | }; |
| 41 | return this; |
| 42 | }, |
| 43 | |
| 44 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Add button in order |
| 47 | */ |
| 48 | add : function (title, classes, cb) { |
| 49 | var b = this._element.addE('span'); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 50 | b.setAttribute('title',title); |
| 51 | if (classes !== undefined) { |
| 52 | b.classList.add.apply(b.classList, classes); |
| 53 | }; |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 54 | b.addE('span').addT(title); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 55 | |
| 56 | var that = this; |
| 57 | b.addEventListener('click', function (e) { |
| 58 | |
| 59 | // Do not bubble |
| 60 | e.halt(); |
| 61 | |
| 62 | // Call callback |
| 63 | cb.apply(that._bind || this, e) |
| 64 | }); |
| 65 | }, |
| 66 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame^] | 67 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 68 | /** |
| 69 | * Bind an object to all callbacks of the button group |
| 70 | */ |
| 71 | bind : function (obj) { |
| 72 | if (obj !== undefined) { |
| 73 | this._bind = obj; |
| 74 | }; |
| 75 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame^] | 76 | }, |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Remove all defined buttons |
| 81 | */ |
| 82 | clear : function () { |
| 83 | _removeChildren(this._element); |
| 84 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | }); |