blob: f9fb979b8e46b4d24b9a2c66b252ab9d397d118e [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 */
8define(['buttongroup', 'util'], function (buttonGroupClass) {
9
10 return {
Akron4d926f12018-07-16 15:30:25 +020011 create : function (classes) {
12 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020013 },
14
Akron4d926f12018-07-16 15:30:25 +020015
16 // Override by inheriting object
17 _init : function (classes) {
Akron537bc522018-07-13 19:06:27 +020018 this.views = [];
19
Akron537bc522018-07-13 19:06:27 +020020 /**
21 * Main action buttons for the panel,
22 * may be at the bottom (for matches)
23 * or as tabs (for the result).
24 */
Akron4d926f12018-07-16 15:30:25 +020025
26 this._classes = classes;
27 var c = ['action', 'button-panel'];
28 if (classes)
29 c.push.apply(c,classes);
30 this.actions = buttonGroupClass.create(c).bind(this);
31
hebastad025eb92019-12-07 21:04:42 +010032 //prepend or append views of the panel
33 this.prepend = false;
34
Akron4d926f12018-07-16 15:30:25 +020035 // Warning: This is circular
36 this.actions.panel = this;
Akron537bc522018-07-13 19:06:27 +020037 return this;
38 },
39
Akron4d926f12018-07-16 15:30:25 +020040
Akron537bc522018-07-13 19:06:27 +020041 /**
42 * The element of the panel
43 */
44 element : function () {
45 if (this._element)
46 return this._element;
47
48 // Create panel element
49 var e = document.createElement('div');
Akron4d926f12018-07-16 15:30:25 +020050 var cl = e.classList;
51 cl.add('panel');
52
53 if (this._classes)
54 cl.add.apply(cl, this._classes);
Akron537bc522018-07-13 19:06:27 +020055
Akron4d926f12018-07-16 15:30:25 +020056 this._viewE = e.addE('div');
Akron3967d342018-07-14 08:35:12 +020057
Akron4d926f12018-07-16 15:30:25 +020058 // Per default the action buttons are below the view
59 // and integrated
60 var aElem = this.actions.element();
61 if (!aElem.parentNode)
62 e.appendChild(aElem);
Akron3967d342018-07-14 08:35:12 +020063
Akron537bc522018-07-13 19:06:27 +020064 this._element = e;
65 return e;
66 },
67
68
Akron4d926f12018-07-16 15:30:25 +020069 /*
Akron3967d342018-07-14 08:35:12 +020070 * The element of the views
71 */
Akron4d926f12018-07-16 15:30:25 +020072 _viewElement : function () {
Akron3967d342018-07-14 08:35:12 +020073 this.element();
74 return this._viewE;
75 },
76
Akron3967d342018-07-14 08:35:12 +020077 /**
Akron537bc522018-07-13 19:06:27 +020078 * Add a view to the panel
79 */
80 add : function (view) {
81
82 // Add view to views list
83 this.views.push(view);
84
hebastad025eb92019-12-07 21:04:42 +010085 // Append or prepend element to panel element
Akron7f1e07e2020-08-24 20:12:14 +020086 if (this.prepend){
hebastad025eb92019-12-07 21:04:42 +010087 this._viewElement().prepend(
Akron7f1e07e2020-08-24 20:12:14 +020088 view.element()
89 );
90 }
hebastad025eb92019-12-07 21:04:42 +010091 else{
Akron7f1e07e2020-08-24 20:12:14 +020092 this._viewElement().appendChild(
93 view.element()
94 );
hebastad025eb92019-12-07 21:04:42 +010095 }
96
Akronbfe912c2018-07-17 19:30:52 +020097 if (view.afterEmbed)
98 view.afterEmbed();
99
Akron537bc522018-07-13 19:06:27 +0200100 view.panel = this;
101 },
102
103 /**
104 * Delete a closed view from panel
105 */
106 delView : function (view) {
Akron678c26f2020-10-09 08:52:50 +0200107 this.views.forEach(function(e, i, a) {
108 if (e === view)
109 a[i] = undefined;
110 });
Akron537bc522018-07-13 19:06:27 +0200111 },
Akronbfe912c2018-07-17 19:30:52 +0200112
113 /**
114 * Upgrade this object to another object,
115 * while private data stays intact.
116 *
117 * @param {Object] An object with properties.
118 */
119 upgradeTo : function (props) {
120 for (var prop in props) {
121 this[prop] = props[prop];
122 };
123 return this;
124 }
Akron537bc522018-07-13 19:06:27 +0200125 }
126});