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 () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 50 | if (this._el) |
| 51 | return this._el; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 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 | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 69 | return this._el = e; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 70 | }, |
| 71 | |
| 72 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 73 | /* |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 74 | * The element of the views |
| 75 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 76 | _viewElement : function () { |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 77 | this.element(); |
| 78 | return this._viewE; |
| 79 | }, |
| 80 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 81 | |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 82 | /** |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 83 | * Add a view to the panel |
| 84 | */ |
| 85 | add : function (view) { |
| 86 | |
| 87 | // Add view to views list |
| 88 | this.views.push(view); |
| 89 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 90 | // Append or prepend element to panel element |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 91 | if (this.prepend){ |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 92 | this._viewElement().prepend( |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 93 | view.element() |
| 94 | ); |
| 95 | } |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 96 | else{ |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 97 | this._viewElement().appendChild( |
| 98 | view.element() |
| 99 | ); |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 102 | if (view.afterEmbed) |
| 103 | view.afterEmbed(); |
| 104 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 105 | view.panel = this; |
| 106 | }, |
| 107 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 108 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 109 | /** |
| 110 | * Delete a closed view from panel |
| 111 | */ |
| 112 | delView : function (view) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 113 | this.views.forEach(function(e, i, a) { |
| 114 | if (e === view) |
| 115 | a[i] = undefined; |
| 116 | }); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 117 | }, |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 118 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 119 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 120 | /** |
| 121 | * Upgrade this object to another object, |
| 122 | * while private data stays intact. |
| 123 | * |
| 124 | * @param {Object] An object with properties. |
| 125 | */ |
| 126 | upgradeTo : function (props) { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 127 | for (let prop in props) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 128 | this[prop] = props[prop]; |
| 129 | }; |
| 130 | return this; |
| 131 | } |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 132 | } |
| 133 | }); |