blob: 946cdd34e82d08386e89d9eb35b58e554175dbb8 [file] [log] [blame]
Akron537bc522018-07-13 19:06:27 +02001/**
2 * Create a view that can be added to a panel,
3 * like a tree view or the metadata view.
4 */
5
6define(['buttongroup', 'util'], function (buttonGroupClass) {
7
8 return {
Akron4d926f12018-07-16 15:30:25 +02009 create : function (classes) {
10 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020011 },
12
Akron4d926f12018-07-16 15:30:25 +020013 // Override by inheriting object
14 _init : function (classes) {
Akron537bc522018-07-13 19:06:27 +020015 this.panel = undefined;
Akron4d926f12018-07-16 15:30:25 +020016 this._classes = classes;
17 this._shown = false;
Akron537bc522018-07-13 19:06:27 +020018
19 // The buttonclass is bind to the view
Akron4d926f12018-07-16 15:30:25 +020020 var c = ['action', 'button-view'];
21 if (classes)
22 c.push.apply(null,classes);
23
24 this.actions = buttonGroupClass.create(c).bind(this);
25
Akron537bc522018-07-13 19:06:27 +020026 this.actions.add('close', ['button-icon','close'], function (e) {
27 this.close();
28 });
29
Akron4d926f12018-07-16 15:30:25 +020030 // Warning: This is circular
31 this.actions.view = this;
32
Akron537bc522018-07-13 19:06:27 +020033 return this;
34 },
35
Akron4d926f12018-07-16 15:30:25 +020036
Akron537bc522018-07-13 19:06:27 +020037 /**
38 * Element of the view
39 */
40 element : function () {
41 if (this._element)
42 return this._element;
43
44 // Create panel element
45 var e = document.createElement('div');
Akron4d926f12018-07-16 15:30:25 +020046
47 var cl = e.classList;
48 cl.add('view');
49 if (this._classes)
50 cl.add.apply(cl, this._classes);
51
52 if (this.show !== undefined)
53 e.appendChild(this.show());
54
55 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020056
57 e.appendChild(this.actions.element());
58
59 this._element = e;
60 return e;
61 },
62
Akron4d926f12018-07-16 15:30:25 +020063
64 /**
65 * Is the object shown?
66 */
67 shown : function () {
68 return this._shown;
69 },
Akrone6538cd2018-07-16 17:52:33 +020070
71 // onClose : function () {},
72
Akron537bc522018-07-13 19:06:27 +020073 /**
74 * Close the view.
75 */
76 close : function () {
77 var e = this.element();
78 e.parentNode.removeChild(e);
79 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020080 this._shown = false;
Akrone6538cd2018-07-16 17:52:33 +020081 if (this.onClose)
82 this.onClose();
Akron4d926f12018-07-16 15:30:25 +020083 },
84
85
86 /**
87 * Upgrade this object to another object,
88 * while private data stays intact.
89 *
90 * @param {Object] An object with properties.
91 */
92 upgradeTo : function (props) {
93 for (var prop in props) {
94 this[prop] = props[prop];
95 };
96 return this;
Akron537bc522018-07-13 19:06:27 +020097 }
98 };
99});