blob: 27d1ab2e63cd92312350c4fe37890fb36ad8525f [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
Akron56d42072018-07-24 11:17:24 +02008 const loc = KorAP.Locale;
Akron362c11a2018-08-29 20:01:30 +02009 loc.CLOSE = loc.CLOSE || 'Close';
Akronbfe912c2018-07-17 19:30:52 +020010
Akron537bc522018-07-13 19:06:27 +020011 return {
Akron4d926f12018-07-16 15:30:25 +020012 create : function (classes) {
13 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020014 },
15
Akron4d926f12018-07-16 15:30:25 +020016 // Override by inheriting object
17 _init : function (classes) {
Akron537bc522018-07-13 19:06:27 +020018 this.panel = undefined;
Akron4d926f12018-07-16 15:30:25 +020019 this._classes = classes;
20 this._shown = false;
Akron537bc522018-07-13 19:06:27 +020021
22 // The buttonclass is bind to the view
Akron4d926f12018-07-16 15:30:25 +020023 var c = ['action', 'button-view'];
24 if (classes)
Akronbfe912c2018-07-17 19:30:52 +020025 c.push.apply(c,classes);
Akron4d926f12018-07-16 15:30:25 +020026
27 this.actions = buttonGroupClass.create(c).bind(this);
28
Akron792b1a42020-09-14 18:56:38 +020029 this.actions.add(loc.CLOSE, {'cls':['button-icon','close']}, function (e) {
Akron537bc522018-07-13 19:06:27 +020030 this.close();
31 });
32
Akron4d926f12018-07-16 15:30:25 +020033 // Warning: This is circular
34 this.actions.view = this;
35
Akron537bc522018-07-13 19:06:27 +020036 return this;
37 },
38
39 /**
40 * Element of the view
41 */
42 element : function () {
Akron3d9ce5e2020-10-01 15:18:36 +020043 if (this._element) {
44 this._element.classList.add('show');
Akron537bc522018-07-13 19:06:27 +020045 return this._element;
Akron3d9ce5e2020-10-01 15:18:36 +020046 };
Akron537bc522018-07-13 19:06:27 +020047
48 // Create panel element
49 var e = document.createElement('div');
Akron4d926f12018-07-16 15:30:25 +020050
51 var cl = e.classList;
Akron3d9ce5e2020-10-01 15:18:36 +020052 cl.add('view', 'show');
Akron4d926f12018-07-16 15:30:25 +020053 if (this._classes)
54 cl.add.apply(cl, this._classes);
55
Akron56d42072018-07-24 11:17:24 +020056 // TODO: The show may need to be wrapped in another DIV!
Akron24f48ea2020-07-01 09:37:19 +020057 if (this.show !== undefined) {
58 let s = this.show();
59 if (s) {
60 e.appendChild(s);
61 } else {
62 return e
63 }
64 }
Akron4d926f12018-07-16 15:30:25 +020065
66 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020067
68 e.appendChild(this.actions.element());
69
70 this._element = e;
Akron3d9ce5e2020-10-01 15:18:36 +020071
Akron537bc522018-07-13 19:06:27 +020072 return e;
73 },
74
Akron4d926f12018-07-16 15:30:25 +020075
76 /**
77 * Is the object shown?
78 */
79 shown : function () {
80 return this._shown;
81 },
Akrone6538cd2018-07-16 17:52:33 +020082
Akron3d9ce5e2020-10-01 15:18:36 +020083
84 /**
85 * Hide the widget if shown.
86 */
87 minimize : function () {
88 if (this._element) {
89 this._element.classList.remove("show");
90 }
91 },
92
Akrone6538cd2018-07-16 17:52:33 +020093 // onClose : function () {},
94
Akron537bc522018-07-13 19:06:27 +020095 /**
96 * Close the view.
97 */
98 close : function () {
Akron22598cd2019-12-09 14:59:03 +010099
100 // Close embedded things before
101 if (this.onClose)
102 this.onClose();
103
Akron537bc522018-07-13 19:06:27 +0200104 var e = this.element();
Akron4a703872018-07-26 10:59:41 +0200105 if (e.parentNode) {
106 e.parentNode.removeChild(e);
107 };
Akron537bc522018-07-13 19:06:27 +0200108 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +0200109 this._shown = false;
110 },
111
Akron4d926f12018-07-16 15:30:25 +0200112 /**
113 * Upgrade this object to another object,
114 * while private data stays intact.
115 *
116 * @param {Object] An object with properties.
117 */
118 upgradeTo : function (props) {
119 for (var prop in props) {
120 this[prop] = props[prop];
121 };
122 return this;
Akron537bc522018-07-13 19:06:27 +0200123 }
124 };
125});