blob: 4ebef1e2746572b3475db8422e77f1f0d4e713dc [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
86 if(this.prepend){
87 this._viewElement().prepend(
88 view.element()
89 );
90 }
91 else{
Akron4d926f12018-07-16 15:30:25 +020092 this._viewElement().appendChild(
Akron537bc522018-07-13 19:06:27 +020093 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) {
107 for (i in this.views) {
108 if (this.views[i] === view) {
109 this.views[i] = undefined;
110 }
111 }
112 },
Akronbfe912c2018-07-17 19:30:52 +0200113
114 /**
115 * Upgrade this object to another object,
116 * while private data stays intact.
117 *
118 * @param {Object] An object with properties.
119 */
120 upgradeTo : function (props) {
121 for (var prop in props) {
122 this[prop] = props[prop];
123 };
124 return this;
125 }
Akron537bc522018-07-13 19:06:27 +0200126 }
127});