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; |
| 16 | if (classes !== undefined) { |
| 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 | /** |
| 34 | * Upgrade this object to another object, |
| 35 | * while private data stays intact. |
| 36 | * |
| 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 |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 49 | * |
| 50 | * Returns the button element |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 51 | */ |
| 52 | add : function (title, classes, cb) { |
| 53 | var b = this._element.addE('span'); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 54 | b.setAttribute('title',title); |
| 55 | if (classes !== undefined) { |
| 56 | b.classList.add.apply(b.classList, classes); |
| 57 | }; |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 58 | b.addE('span').addT(title); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 59 | |
| 60 | var that = this; |
| 61 | b.addEventListener('click', function (e) { |
| 62 | |
| 63 | // Do not bubble |
| 64 | e.halt(); |
| 65 | |
| 66 | // Call callback |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 67 | var obj = that._bind || this; |
| 68 | obj.button = b; |
| 69 | cb.apply(obj, e) |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 70 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 71 | |
| 72 | return b; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 73 | }, |
| 74 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 75 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 76 | /** |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 77 | * Add button that spawns a list in order. |
| 78 | * |
| 79 | * Returns the list object. |
| 80 | */ |
| 81 | addList : function (title, classes, itemClass) { |
| 82 | var list = treeMenuClass.create([], itemClass || defaultItemClass); |
| 83 | this.add(title, classes, function (e) { |
| 84 | list.show(); |
| 85 | list.button(this.button); |
| 86 | list.focus(); |
| 87 | }); |
| 88 | |
| 89 | return list; |
| 90 | }, |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Bind an object to all callbacks of the button group. |
| 95 | * To get the button element inside the callback, |
| 96 | * use this.button |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 97 | */ |
| 98 | bind : function (obj) { |
| 99 | if (obj !== undefined) { |
| 100 | this._bind = obj; |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 101 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 102 | }; |
| 103 | return this._bind || this; |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 104 | }, |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Remove all defined buttons |
| 109 | */ |
| 110 | clear : function () { |
| 111 | _removeChildren(this._element); |
| 112 | return this; |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | }); |