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