blob: b45c485e038ccbbb0b2a146f35495e4f3a7db65f [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
Akron0f6552f2020-10-20 10:06:46 +02006"use strict";
7
Akron537bc522018-07-13 19:06:27 +02008define(['buttongroup', 'util'], function (buttonGroupClass) {
9
Akron56d42072018-07-24 11:17:24 +020010 const loc = KorAP.Locale;
Akron362c11a2018-08-29 20:01:30 +020011 loc.CLOSE = loc.CLOSE || 'Close';
Akronbfe912c2018-07-17 19:30:52 +020012
Akron537bc522018-07-13 19:06:27 +020013 return {
Akron4d926f12018-07-16 15:30:25 +020014 create : function (classes) {
15 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020016 },
17
Akron4d926f12018-07-16 15:30:25 +020018 // Override by inheriting object
19 _init : function (classes) {
Akron537bc522018-07-13 19:06:27 +020020 this.panel = undefined;
Akron4d926f12018-07-16 15:30:25 +020021 this._classes = classes;
22 this._shown = false;
Akron537bc522018-07-13 19:06:27 +020023
24 // The buttonclass is bind to the view
Akron0f6552f2020-10-20 10:06:46 +020025 const c = ['action', 'button-view'];
Akron4d926f12018-07-16 15:30:25 +020026 if (classes)
Akronbfe912c2018-07-17 19:30:52 +020027 c.push.apply(c,classes);
Akron4d926f12018-07-16 15:30:25 +020028
Akron37ea1192021-07-28 10:40:14 +020029 this._actions = buttonGroupClass.create(c).bind(this);
Akron4d926f12018-07-16 15:30:25 +020030
Akron37ea1192021-07-28 10:40:14 +020031 this._actions.add(loc.CLOSE, {'cls':['button-icon','close']}, function (e) {
Akron537bc522018-07-13 19:06:27 +020032 this.close();
33 });
34
Akron4d926f12018-07-16 15:30:25 +020035 // Warning: This is circular
Akron37ea1192021-07-28 10:40:14 +020036 this._actions.view = this;
Akron4d926f12018-07-16 15:30:25 +020037
Akron537bc522018-07-13 19:06:27 +020038 return this;
39 },
40
Akron0f6552f2020-10-20 10:06:46 +020041
Akron537bc522018-07-13 19:06:27 +020042 /**
43 * Element of the view
44 */
45 element : function () {
Akron24aa0052020-11-10 11:00:34 +010046 if (this._el) {
47 this._el.classList.add('show');
48 return this._el;
Akron3d9ce5e2020-10-01 15:18:36 +020049 };
Akron537bc522018-07-13 19:06:27 +020050
51 // Create panel element
Akron0f6552f2020-10-20 10:06:46 +020052 const e = document.createElement('div');
53 const cl = e.classList;
Akron4d926f12018-07-16 15:30:25 +020054
Akron3d9ce5e2020-10-01 15:18:36 +020055 cl.add('view', 'show');
Akron4d926f12018-07-16 15:30:25 +020056 if (this._classes)
57 cl.add.apply(cl, this._classes);
58
Akron56d42072018-07-24 11:17:24 +020059 // TODO: The show may need to be wrapped in another DIV!
Akron24f48ea2020-07-01 09:37:19 +020060 if (this.show !== undefined) {
Akron0f6552f2020-10-20 10:06:46 +020061 const s = this.show();
Akron24f48ea2020-07-01 09:37:19 +020062 if (s) {
63 e.appendChild(s);
64 } else {
65 return e
Akron0f6552f2020-10-20 10:06:46 +020066 };
Akron24f48ea2020-07-01 09:37:19 +020067 }
Akron4d926f12018-07-16 15:30:25 +020068
69 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020070
Akron37ea1192021-07-28 10:40:14 +020071 e.appendChild(this.actions().element());
Akron537bc522018-07-13 19:06:27 +020072
Akron24aa0052020-11-10 11:00:34 +010073 return this._el = e;
Akron537bc522018-07-13 19:06:27 +020074 },
75
Akron4d926f12018-07-16 15:30:25 +020076
77 /**
Akron37ea1192021-07-28 10:40:14 +020078 * The actions of the view.
79 * Can be represented as a buttongroup,
80 * a list, ..., requires an "add()" minimum at least.
81 */
82 actions : function () {
83 return this._actions;
84 },
85
86 /**
Akron4d926f12018-07-16 15:30:25 +020087 * Is the object shown?
88 */
89 shown : function () {
90 return this._shown;
91 },
Akrone6538cd2018-07-16 17:52:33 +020092
Akron3d9ce5e2020-10-01 15:18:36 +020093
94 /**
95 * Hide the widget if shown.
96 */
97 minimize : function () {
Akron24aa0052020-11-10 11:00:34 +010098 if (this._el) {
99 this._el.classList.remove("show");
Akron3d9ce5e2020-10-01 15:18:36 +0200100 }
101 },
102
Akron0f6552f2020-10-20 10:06:46 +0200103
Akrone6538cd2018-07-16 17:52:33 +0200104 // onClose : function () {},
105
Akron0f6552f2020-10-20 10:06:46 +0200106
Akron537bc522018-07-13 19:06:27 +0200107 /**
108 * Close the view.
109 */
110 close : function () {
Akron22598cd2019-12-09 14:59:03 +0100111
112 // Close embedded things before
113 if (this.onClose)
114 this.onClose();
115
Akron0f6552f2020-10-20 10:06:46 +0200116 const e = this.element();
Akron4a703872018-07-26 10:59:41 +0200117 if (e.parentNode) {
118 e.parentNode.removeChild(e);
119 };
Akron537bc522018-07-13 19:06:27 +0200120 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +0200121 this._shown = false;
122 },
123
Akron0f6552f2020-10-20 10:06:46 +0200124
Akron4d926f12018-07-16 15:30:25 +0200125 /**
126 * Upgrade this object to another object,
127 * while private data stays intact.
128 *
129 * @param {Object] An object with properties.
130 */
131 upgradeTo : function (props) {
Akron0f6552f2020-10-20 10:06:46 +0200132 for (let prop in props) {
Akron4d926f12018-07-16 15:30:25 +0200133 this[prop] = props[prop];
134 };
135 return this;
Akron537bc522018-07-13 19:06:27 +0200136 }
137 };
138});