Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Create a panel for a certain aspect of the system, like |
| 3 | * the result, a match, or the VC. |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 4 | * |
| 5 | * The buttons are associated with the panel's views, |
| 6 | * though they are integrated independently |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 7 | */ |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 8 | "use strict"; |
| 9 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 10 | define(['buttongroup', 'util'], function (buttonGroupClass) { |
| 11 | |
| 12 | return { |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 13 | create : function (classes) { |
| 14 | return Object.create(this)._init(classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 15 | }, |
| 16 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 17 | |
| 18 | // Override by inheriting object |
| 19 | _init : function (classes) { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 20 | const t = this; |
| 21 | t.views = []; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 22 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 23 | /** |
| 24 | * Main action buttons for the panel, |
| 25 | * may be at the bottom (for matches) |
| 26 | * or as tabs (for the result). |
| 27 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 28 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 29 | t._classes = classes; |
| 30 | const c = ['action', 'button-panel']; |
| 31 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 32 | if (classes) |
| 33 | c.push.apply(c,classes); |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 34 | |
| 35 | t.actions = buttonGroupClass.create(c).bind(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 36 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 37 | //prepend or append views of the panel |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 38 | t.prepend = false; |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 39 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 40 | // Warning: This is circular |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 41 | t.actions.panel = t; |
| 42 | return t; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 43 | }, |
| 44 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 45 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 46 | /** |
| 47 | * The element of the panel |
| 48 | */ |
| 49 | element : function () { |
| 50 | if (this._element) |
| 51 | return this._element; |
| 52 | |
| 53 | // Create panel element |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 54 | const e = document.createElement('div'); |
| 55 | const cl = e.classList; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 56 | cl.add('panel'); |
| 57 | |
| 58 | if (this._classes) |
| 59 | cl.add.apply(cl, this._classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 60 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 61 | this._viewE = e.addE('div'); |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 62 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 63 | // Per default the action buttons are below the view |
| 64 | // and integrated |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 65 | const aElem = this.actions.element(); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 66 | if (!aElem.parentNode) |
| 67 | e.appendChild(aElem); |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 68 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 69 | this._element = e; |
| 70 | return e; |
| 71 | }, |
| 72 | |
| 73 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 74 | /* |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 75 | * The element of the views |
| 76 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 77 | _viewElement : function () { |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 78 | this.element(); |
| 79 | return this._viewE; |
| 80 | }, |
| 81 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 82 | |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 83 | /** |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 84 | * Add a view to the panel |
| 85 | */ |
| 86 | add : function (view) { |
| 87 | |
| 88 | // Add view to views list |
| 89 | this.views.push(view); |
| 90 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 91 | // Append or prepend element to panel element |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 92 | if (this.prepend){ |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 93 | this._viewElement().prepend( |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 94 | view.element() |
| 95 | ); |
| 96 | } |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 97 | else{ |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 98 | this._viewElement().appendChild( |
| 99 | view.element() |
| 100 | ); |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 103 | if (view.afterEmbed) |
| 104 | view.afterEmbed(); |
| 105 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 106 | view.panel = this; |
| 107 | }, |
| 108 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 109 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 110 | /** |
| 111 | * Delete a closed view from panel |
| 112 | */ |
| 113 | delView : function (view) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 114 | this.views.forEach(function(e, i, a) { |
| 115 | if (e === view) |
| 116 | a[i] = undefined; |
| 117 | }); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 118 | }, |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 119 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 120 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 121 | /** |
| 122 | * Upgrade this object to another object, |
| 123 | * while private data stays intact. |
| 124 | * |
| 125 | * @param {Object] An object with properties. |
| 126 | */ |
| 127 | upgradeTo : function (props) { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 128 | for (let prop in props) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 129 | this[prop] = props[prop]; |
| 130 | }; |
| 131 | return this; |
| 132 | } |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 133 | } |
| 134 | }); |