blob: bfc95c59c3190a4de1f2c851a325bc52366b7ab9 [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 {
Akron4d926f12018-07-16 15:30:25 +020013 create : function (classes) {
14 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020015 },
16
Akron4d926f12018-07-16 15:30:25 +020017
18 // Override by inheriting object
19 _init : function (classes) {
Akron14f3ee92020-10-19 11:59:40 +020020 const t = this;
21 t.views = [];
Akron537bc522018-07-13 19:06:27 +020022
Akron537bc522018-07-13 19:06:27 +020023 /**
24 * Main action buttons for the panel,
25 * may be at the bottom (for matches)
26 * or as tabs (for the result).
27 */
Akron4d926f12018-07-16 15:30:25 +020028
Akron14f3ee92020-10-19 11:59:40 +020029 t._classes = classes;
30 const c = ['action', 'button-panel'];
31
Akron4d926f12018-07-16 15:30:25 +020032 if (classes)
33 c.push.apply(c,classes);
Akron14f3ee92020-10-19 11:59:40 +020034
35 t.actions = buttonGroupClass.create(c).bind(this);
Akron4d926f12018-07-16 15:30:25 +020036
hebastad025eb92019-12-07 21:04:42 +010037 //prepend or append views of the panel
Akron14f3ee92020-10-19 11:59:40 +020038 t.prepend = false;
hebastad025eb92019-12-07 21:04:42 +010039
Akron4d926f12018-07-16 15:30:25 +020040 // Warning: This is circular
Akron14f3ee92020-10-19 11:59:40 +020041 t.actions.panel = t;
42 return t;
Akron537bc522018-07-13 19:06:27 +020043 },
44
Akron4d926f12018-07-16 15:30:25 +020045
Akron537bc522018-07-13 19:06:27 +020046 /**
47 * The element of the panel
48 */
49 element : function () {
Akron24aa0052020-11-10 11:00:34 +010050 if (this._el)
51 return this._el;
Akron537bc522018-07-13 19:06:27 +020052
53 // Create panel element
Akron14f3ee92020-10-19 11:59:40 +020054 const e = document.createElement('div');
55 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020056 cl.add('panel');
57
58 if (this._classes)
59 cl.add.apply(cl, this._classes);
Akron537bc522018-07-13 19:06:27 +020060
Akron4d926f12018-07-16 15:30:25 +020061 this._viewE = e.addE('div');
Akron3967d342018-07-14 08:35:12 +020062
Akron4d926f12018-07-16 15:30:25 +020063 // Per default the action buttons are below the view
64 // and integrated
Akron14f3ee92020-10-19 11:59:40 +020065 const aElem = this.actions.element();
Akron4d926f12018-07-16 15:30:25 +020066 if (!aElem.parentNode)
67 e.appendChild(aElem);
Akron3967d342018-07-14 08:35:12 +020068
Akron24aa0052020-11-10 11:00:34 +010069 return this._el = e;
Akron537bc522018-07-13 19:06:27 +020070 },
71
72
Akron4d926f12018-07-16 15:30:25 +020073 /*
Akron3967d342018-07-14 08:35:12 +020074 * The element of the views
75 */
Akron4d926f12018-07-16 15:30:25 +020076 _viewElement : function () {
Akron3967d342018-07-14 08:35:12 +020077 this.element();
78 return this._viewE;
79 },
80
Akron14f3ee92020-10-19 11:59:40 +020081
Akron3967d342018-07-14 08:35:12 +020082 /**
Akron537bc522018-07-13 19:06:27 +020083 * Add a view to the panel
84 */
85 add : function (view) {
86
87 // Add view to views list
88 this.views.push(view);
89
hebastad025eb92019-12-07 21:04:42 +010090 // Append or prepend element to panel element
Akron7f1e07e2020-08-24 20:12:14 +020091 if (this.prepend){
hebastad025eb92019-12-07 21:04:42 +010092 this._viewElement().prepend(
Akron7f1e07e2020-08-24 20:12:14 +020093 view.element()
94 );
95 }
hebastad025eb92019-12-07 21:04:42 +010096 else{
Akron7f1e07e2020-08-24 20:12:14 +020097 this._viewElement().appendChild(
98 view.element()
99 );
hebastad025eb92019-12-07 21:04:42 +0100100 }
101
Akronbfe912c2018-07-17 19:30:52 +0200102 if (view.afterEmbed)
103 view.afterEmbed();
104
Akron537bc522018-07-13 19:06:27 +0200105 view.panel = this;
106 },
107
Akron14f3ee92020-10-19 11:59:40 +0200108
Akron537bc522018-07-13 19:06:27 +0200109 /**
110 * Delete a closed view from panel
111 */
112 delView : function (view) {
Akron678c26f2020-10-09 08:52:50 +0200113 this.views.forEach(function(e, i, a) {
114 if (e === view)
115 a[i] = undefined;
116 });
Akron537bc522018-07-13 19:06:27 +0200117 },
Akronbfe912c2018-07-17 19:30:52 +0200118
Akron14f3ee92020-10-19 11:59:40 +0200119
Akronbfe912c2018-07-17 19:30:52 +0200120 /**
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) {
Akron14f3ee92020-10-19 11:59:40 +0200127 for (let prop in props) {
Akronbfe912c2018-07-17 19:30:52 +0200128 this[prop] = props[prop];
129 };
130 return this;
131 }
Akron537bc522018-07-13 19:06:27 +0200132 }
133});