blob: a79fad72e04afd99f7d59f6b8676ccabd01d3956 [file] [log] [blame]
Akron537bc522018-07-13 19:06:27 +02001/**
2 * Create a panel for a certain aspect of the system, like
3 * the result, a match, or the VC.
Akron4d926f12018-07-16 15:30:25 +02004 *
5 * The buttons are associated with the panel's views,
6 * though they are integrated independently
Akron537bc522018-07-13 19:06:27 +02007 */
Akron14f3ee92020-10-19 11:59:40 +02008"use strict";
9
Akron537bc522018-07-13 19:06:27 +020010define(['buttongroup', 'util'], function (buttonGroupClass) {
11
12 return {
Akron386f1222022-12-21 16:26:11 +010013 type : 'base',
14
Akron4d926f12018-07-16 15:30:25 +020015 create : function (classes) {
16 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020017 },
18
Akron4d926f12018-07-16 15:30:25 +020019
20 // Override by inheriting object
21 _init : function (classes) {
Akron14f3ee92020-10-19 11:59:40 +020022 const t = this;
23 t.views = [];
Akron537bc522018-07-13 19:06:27 +020024
Akron537bc522018-07-13 19:06:27 +020025 /**
26 * Main action buttons for the panel,
27 * may be at the bottom (for matches)
28 * or as tabs (for the result).
29 */
Akron4d926f12018-07-16 15:30:25 +020030
Akron14f3ee92020-10-19 11:59:40 +020031 t._classes = classes;
32 const c = ['action', 'button-panel'];
33
Akron4d926f12018-07-16 15:30:25 +020034 if (classes)
35 c.push.apply(c,classes);
Akron14f3ee92020-10-19 11:59:40 +020036
Akron37ea1192021-07-28 10:40:14 +020037 t._actions = buttonGroupClass.create(c).bind(this);
Akron4d926f12018-07-16 15:30:25 +020038
hebastad025eb92019-12-07 21:04:42 +010039 //prepend or append views of the panel
Akron14f3ee92020-10-19 11:59:40 +020040 t.prepend = false;
hebastad025eb92019-12-07 21:04:42 +010041
Akron4d926f12018-07-16 15:30:25 +020042 // Warning: This is circular
Akron37ea1192021-07-28 10:40:14 +020043 t._actions.panel = t;
Akron14f3ee92020-10-19 11:59:40 +020044 return t;
Akron537bc522018-07-13 19:06:27 +020045 },
46
Akron37ea1192021-07-28 10:40:14 +020047
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 },
Akron4d926f12018-07-16 15:30:25 +020056
Akron37ea1192021-07-28 10:40:14 +020057
Akron537bc522018-07-13 19:06:27 +020058 /**
59 * The element of the panel
60 */
61 element : function () {
Akron24aa0052020-11-10 11:00:34 +010062 if (this._el)
63 return this._el;
Akron537bc522018-07-13 19:06:27 +020064
65 // Create panel element
Akron14f3ee92020-10-19 11:59:40 +020066 const e = document.createElement('div');
67 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020068 cl.add('panel');
69
70 if (this._classes)
71 cl.add.apply(cl, this._classes);
Akron537bc522018-07-13 19:06:27 +020072
Akron4d926f12018-07-16 15:30:25 +020073 this._viewE = e.addE('div');
Akron3967d342018-07-14 08:35:12 +020074
Akron4d926f12018-07-16 15:30:25 +020075 // Per default the action buttons are below the view
76 // and integrated
Akron37ea1192021-07-28 10:40:14 +020077 const aElem = this.actions().element();
Akron4d926f12018-07-16 15:30:25 +020078 if (!aElem.parentNode)
79 e.appendChild(aElem);
Akron3967d342018-07-14 08:35:12 +020080
Akron24aa0052020-11-10 11:00:34 +010081 return this._el = e;
Akron537bc522018-07-13 19:06:27 +020082 },
83
84
Akron4d926f12018-07-16 15:30:25 +020085 /*
Akron3967d342018-07-14 08:35:12 +020086 * The element of the views
87 */
Akron4d926f12018-07-16 15:30:25 +020088 _viewElement : function () {
Akron3967d342018-07-14 08:35:12 +020089 this.element();
90 return this._viewE;
91 },
92
Akron14f3ee92020-10-19 11:59:40 +020093
Akron3967d342018-07-14 08:35:12 +020094 /**
Akron537bc522018-07-13 19:06:27 +020095 * Add a view to the panel
96 */
97 add : function (view) {
98
99 // Add view to views list
100 this.views.push(view);
101
hebastad025eb92019-12-07 21:04:42 +0100102 // Append or prepend element to panel element
Akron7f1e07e2020-08-24 20:12:14 +0200103 if (this.prepend){
hebastad025eb92019-12-07 21:04:42 +0100104 this._viewElement().prepend(
Akron7f1e07e2020-08-24 20:12:14 +0200105 view.element()
106 );
107 }
hebastad025eb92019-12-07 21:04:42 +0100108 else{
Akron7f1e07e2020-08-24 20:12:14 +0200109 this._viewElement().appendChild(
110 view.element()
111 );
hebastad025eb92019-12-07 21:04:42 +0100112 }
113
Akronbfe912c2018-07-17 19:30:52 +0200114 if (view.afterEmbed)
115 view.afterEmbed();
116
Akron537bc522018-07-13 19:06:27 +0200117 view.panel = this;
118 },
119
Akron14f3ee92020-10-19 11:59:40 +0200120
Akron537bc522018-07-13 19:06:27 +0200121 /**
122 * Delete a closed view from panel
123 */
124 delView : function (view) {
Akron678c26f2020-10-09 08:52:50 +0200125 this.views.forEach(function(e, i, a) {
126 if (e === view)
127 a[i] = undefined;
128 });
Akronbfe912c2018-07-17 19:30:52 +0200129 }
Akron537bc522018-07-13 19:06:27 +0200130 }
131});