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 | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame] | 13 | type : 'base', |
| 14 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 15 | create : function (classes) { |
| 16 | return Object.create(this)._init(classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 17 | }, |
| 18 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 19 | |
| 20 | // Override by inheriting object |
| 21 | _init : function (classes) { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 22 | const t = this; |
| 23 | t.views = []; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 24 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 25 | /** |
| 26 | * Main action buttons for the panel, |
| 27 | * may be at the bottom (for matches) |
| 28 | * or as tabs (for the result). |
| 29 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 30 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 31 | t._classes = classes; |
| 32 | const c = ['action', 'button-panel']; |
| 33 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 34 | if (classes) |
| 35 | c.push.apply(c,classes); |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 36 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 37 | t._actions = buttonGroupClass.create(c).bind(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 38 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 39 | //prepend or append views of the panel |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 40 | t.prepend = false; |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 41 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 42 | // Warning: This is circular |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 43 | t._actions.panel = t; |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 44 | return t; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 45 | }, |
| 46 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * The actions of the panel. |
| 50 | * Can be represented as a buttongroup, |
| 51 | * a list, ..., requires an "add()" minimum at least. |
| 52 | */ |
| 53 | actions : function () { |
| 54 | return this._actions; |
| 55 | }, |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 56 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 57 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 58 | /** |
| 59 | * The element of the panel |
| 60 | */ |
| 61 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 62 | if (this._el) |
| 63 | return this._el; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 64 | |
| 65 | // Create panel element |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 66 | const e = document.createElement('div'); |
| 67 | const cl = e.classList; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 68 | cl.add('panel'); |
| 69 | |
| 70 | if (this._classes) |
| 71 | cl.add.apply(cl, this._classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 72 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 73 | this._viewE = e.addE('div'); |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 74 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 75 | // Per default the action buttons are below the view |
| 76 | // and integrated |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 77 | const aElem = this.actions().element(); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 78 | if (!aElem.parentNode) |
| 79 | e.appendChild(aElem); |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 80 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 81 | return this._el = e; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 82 | }, |
| 83 | |
| 84 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 85 | /* |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 86 | * The element of the views |
| 87 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 88 | _viewElement : function () { |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 89 | this.element(); |
| 90 | return this._viewE; |
| 91 | }, |
| 92 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 93 | |
Akron | 3967d34 | 2018-07-14 08:35:12 +0200 | [diff] [blame] | 94 | /** |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 95 | * Add a view to the panel |
| 96 | */ |
| 97 | add : function (view) { |
| 98 | |
| 99 | // Add view to views list |
| 100 | this.views.push(view); |
| 101 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 102 | // Append or prepend element to panel element |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 103 | if (this.prepend){ |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 104 | this._viewElement().prepend( |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 105 | view.element() |
| 106 | ); |
| 107 | } |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 108 | else{ |
Akron | 7f1e07e | 2020-08-24 20:12:14 +0200 | [diff] [blame] | 109 | this._viewElement().appendChild( |
| 110 | view.element() |
| 111 | ); |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 114 | if (view.afterEmbed) |
| 115 | view.afterEmbed(); |
| 116 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 117 | view.panel = this; |
| 118 | }, |
| 119 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 120 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 121 | /** |
| 122 | * Delete a closed view from panel |
| 123 | */ |
| 124 | delView : function (view) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 125 | this.views.forEach(function(e, i, a) { |
| 126 | if (e === view) |
| 127 | a[i] = undefined; |
| 128 | }); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 129 | } |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 130 | } |
| 131 | }); |