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 | |
| 22 | /** |
| 23 | * Return main element |
| 24 | */ |
| 25 | element : function () { |
| 26 | return this._element; |
| 27 | }, |
| 28 | |
| 29 | /** |
| 30 | * Add button in order |
| 31 | */ |
| 32 | add : function (title, classes, cb) { |
| 33 | var b = this._element.addE('span'); |
| 34 | b.addT(title); |
| 35 | b.setAttribute('title',title); |
| 36 | if (classes !== undefined) { |
| 37 | b.classList.add.apply(b.classList, classes); |
| 38 | }; |
| 39 | |
| 40 | var that = this; |
| 41 | b.addEventListener('click', function (e) { |
| 42 | |
| 43 | // Do not bubble |
| 44 | e.halt(); |
| 45 | |
| 46 | // Call callback |
| 47 | cb.apply(that._bind || this, e) |
| 48 | }); |
| 49 | }, |
| 50 | |
| 51 | /** |
| 52 | * Bind an object to all callbacks of the button group |
| 53 | */ |
| 54 | bind : function (obj) { |
| 55 | if (obj !== undefined) { |
| 56 | this._bind = obj; |
| 57 | }; |
| 58 | return this._bind || this; |
| 59 | } |
| 60 | } |
| 61 | }); |