blob: 53ea7579be7964f026e15e28670f08fbfc326050 [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
Akron37ea1192021-07-28 10:40:14 +020035 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
Akron37ea1192021-07-28 10:40:14 +020041 t._actions.panel = t;
Akron14f3ee92020-10-19 11:59:40 +020042 return t;
Akron537bc522018-07-13 19:06:27 +020043 },
44
Akron37ea1192021-07-28 10:40:14 +020045
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 },
Akron4d926f12018-07-16 15:30:25 +020054
Akron37ea1192021-07-28 10:40:14 +020055
Akron537bc522018-07-13 19:06:27 +020056 /**
57 * The element of the panel
58 */
59 element : function () {
Akron24aa0052020-11-10 11:00:34 +010060 if (this._el)
61 return this._el;
Akron537bc522018-07-13 19:06:27 +020062
63 // Create panel element
Akron14f3ee92020-10-19 11:59:40 +020064 const e = document.createElement('div');
65 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020066 cl.add('panel');
67
68 if (this._classes)
69 cl.add.apply(cl, this._classes);
Akron537bc522018-07-13 19:06:27 +020070
Akron4d926f12018-07-16 15:30:25 +020071 this._viewE = e.addE('div');
Akron3967d342018-07-14 08:35:12 +020072
Akron4d926f12018-07-16 15:30:25 +020073 // Per default the action buttons are below the view
74 // and integrated
Akron37ea1192021-07-28 10:40:14 +020075 const aElem = this.actions().element();
Akron4d926f12018-07-16 15:30:25 +020076 if (!aElem.parentNode)
77 e.appendChild(aElem);
Akron3967d342018-07-14 08:35:12 +020078
Akron24aa0052020-11-10 11:00:34 +010079 return this._el = e;
Akron537bc522018-07-13 19:06:27 +020080 },
81
82
Akron4d926f12018-07-16 15:30:25 +020083 /*
Akron3967d342018-07-14 08:35:12 +020084 * The element of the views
85 */
Akron4d926f12018-07-16 15:30:25 +020086 _viewElement : function () {
Akron3967d342018-07-14 08:35:12 +020087 this.element();
88 return this._viewE;
89 },
90
Akron14f3ee92020-10-19 11:59:40 +020091
Akron3967d342018-07-14 08:35:12 +020092 /**
Akron537bc522018-07-13 19:06:27 +020093 * Add a view to the panel
94 */
95 add : function (view) {
96
97 // Add view to views list
98 this.views.push(view);
99
hebastad025eb92019-12-07 21:04:42 +0100100 // Append or prepend element to panel element
Akron7f1e07e2020-08-24 20:12:14 +0200101 if (this.prepend){
hebastad025eb92019-12-07 21:04:42 +0100102 this._viewElement().prepend(
Akron7f1e07e2020-08-24 20:12:14 +0200103 view.element()
104 );
105 }
hebastad025eb92019-12-07 21:04:42 +0100106 else{
Akron7f1e07e2020-08-24 20:12:14 +0200107 this._viewElement().appendChild(
108 view.element()
109 );
hebastad025eb92019-12-07 21:04:42 +0100110 }
111
Akronbfe912c2018-07-17 19:30:52 +0200112 if (view.afterEmbed)
113 view.afterEmbed();
114
Akron537bc522018-07-13 19:06:27 +0200115 view.panel = this;
116 },
117
Akron14f3ee92020-10-19 11:59:40 +0200118
Akron537bc522018-07-13 19:06:27 +0200119 /**
120 * Delete a closed view from panel
121 */
122 delView : function (view) {
Akron678c26f2020-10-09 08:52:50 +0200123 this.views.forEach(function(e, i, a) {
124 if (e === view)
125 a[i] = undefined;
126 });
Akron537bc522018-07-13 19:06:27 +0200127 },
Akronbfe912c2018-07-17 19:30:52 +0200128
Akron14f3ee92020-10-19 11:59:40 +0200129
Akronbfe912c2018-07-17 19:30:52 +0200130 /**
131 * Upgrade this object to another object,
132 * while private data stays intact.
133 *
134 * @param {Object] An object with properties.
135 */
136 upgradeTo : function (props) {
Akron14f3ee92020-10-19 11:59:40 +0200137 for (let prop in props) {
Akronbfe912c2018-07-17 19:30:52 +0200138 this[prop] = props[prop];
139 };
140 return this;
141 }
Akron537bc522018-07-13 19:06:27 +0200142 }
143});