blob: fa830d60a1aa4224e36bb05fcabe63d80d8504a7 [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
Akronbfe912c2018-07-17 19:30:52 +020029 this.actions.add(loc.CLOSE, ['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
Akron4d926f12018-07-16 15:30:25 +020039
Akron537bc522018-07-13 19:06:27 +020040 /**
41 * Element of the view
42 */
43 element : function () {
44 if (this._element)
45 return this._element;
46
47 // Create panel element
48 var e = document.createElement('div');
Akron4d926f12018-07-16 15:30:25 +020049
50 var cl = e.classList;
51 cl.add('view');
52 if (this._classes)
53 cl.add.apply(cl, this._classes);
54
Akron56d42072018-07-24 11:17:24 +020055 // TODO: The show may need to be wrapped in another DIV!
Akron4d926f12018-07-16 15:30:25 +020056 if (this.show !== undefined)
57 e.appendChild(this.show());
58
59 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020060
61 e.appendChild(this.actions.element());
62
63 this._element = e;
64 return e;
65 },
66
Akron4d926f12018-07-16 15:30:25 +020067
68 /**
69 * Is the object shown?
70 */
71 shown : function () {
72 return this._shown;
73 },
Akrone6538cd2018-07-16 17:52:33 +020074
75 // onClose : function () {},
76
Akron537bc522018-07-13 19:06:27 +020077 /**
78 * Close the view.
79 */
80 close : function () {
Akron22598cd2019-12-09 14:59:03 +010081
82 // Close embedded things before
83 if (this.onClose)
84 this.onClose();
85
Akron537bc522018-07-13 19:06:27 +020086 var e = this.element();
Akron4a703872018-07-26 10:59:41 +020087 if (e.parentNode) {
88 e.parentNode.removeChild(e);
89 };
Akron537bc522018-07-13 19:06:27 +020090 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020091 this._shown = false;
92 },
93
Akron4d926f12018-07-16 15:30:25 +020094 /**
95 * Upgrade this object to another object,
96 * while private data stays intact.
97 *
98 * @param {Object] An object with properties.
99 */
100 upgradeTo : function (props) {
101 for (var prop in props) {
102 this[prop] = props[prop];
103 };
104 return this;
Akron537bc522018-07-13 19:06:27 +0200105 }
106 };
107});