blob: 246a8612cab3854c2d82d318cf49455fddf48c40 [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 () {
50 if (this._element)
51 return this._element;
52
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
Akron537bc522018-07-13 19:06:27 +020069 this._element = e;
70 return e;
71 },
72
73
Akron4d926f12018-07-16 15:30:25 +020074 /*
Akron3967d342018-07-14 08:35:12 +020075 * The element of the views
76 */
Akron4d926f12018-07-16 15:30:25 +020077 _viewElement : function () {
Akron3967d342018-07-14 08:35:12 +020078 this.element();
79 return this._viewE;
80 },
81
Akron14f3ee92020-10-19 11:59:40 +020082
Akron3967d342018-07-14 08:35:12 +020083 /**
Akron537bc522018-07-13 19:06:27 +020084 * Add a view to the panel
85 */
86 add : function (view) {
87
88 // Add view to views list
89 this.views.push(view);
90
hebastad025eb92019-12-07 21:04:42 +010091 // Append or prepend element to panel element
Akron7f1e07e2020-08-24 20:12:14 +020092 if (this.prepend){
hebastad025eb92019-12-07 21:04:42 +010093 this._viewElement().prepend(
Akron7f1e07e2020-08-24 20:12:14 +020094 view.element()
95 );
96 }
hebastad025eb92019-12-07 21:04:42 +010097 else{
Akron7f1e07e2020-08-24 20:12:14 +020098 this._viewElement().appendChild(
99 view.element()
100 );
hebastad025eb92019-12-07 21:04:42 +0100101 }
102
Akronbfe912c2018-07-17 19:30:52 +0200103 if (view.afterEmbed)
104 view.afterEmbed();
105
Akron537bc522018-07-13 19:06:27 +0200106 view.panel = this;
107 },
108
Akron14f3ee92020-10-19 11:59:40 +0200109
Akron537bc522018-07-13 19:06:27 +0200110 /**
111 * Delete a closed view from panel
112 */
113 delView : function (view) {
Akron678c26f2020-10-09 08:52:50 +0200114 this.views.forEach(function(e, i, a) {
115 if (e === view)
116 a[i] = undefined;
117 });
Akron537bc522018-07-13 19:06:27 +0200118 },
Akronbfe912c2018-07-17 19:30:52 +0200119
Akron14f3ee92020-10-19 11:59:40 +0200120
Akronbfe912c2018-07-17 19:30:52 +0200121 /**
122 * Upgrade this object to another object,
123 * while private data stays intact.
124 *
125 * @param {Object] An object with properties.
126 */
127 upgradeTo : function (props) {
Akron14f3ee92020-10-19 11:59:40 +0200128 for (let prop in props) {
Akronbfe912c2018-07-17 19:30:52 +0200129 this[prop] = props[prop];
130 };
131 return this;
132 }
Akron537bc522018-07-13 19:06:27 +0200133 }
134});