blob: d0a517c098863e8b3bad84876b242a0a18172982 [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!
Akron24f48ea2020-07-01 09:37:19 +020056 if (this.show !== undefined) {
57 let s = this.show();
58 if (s) {
59 e.appendChild(s);
60 } else {
61 return e
62 }
63 }
Akron4d926f12018-07-16 15:30:25 +020064
65 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020066
67 e.appendChild(this.actions.element());
68
69 this._element = e;
70 return e;
71 },
72
Akron4d926f12018-07-16 15:30:25 +020073
74 /**
75 * Is the object shown?
76 */
77 shown : function () {
78 return this._shown;
79 },
Akrone6538cd2018-07-16 17:52:33 +020080
81 // onClose : function () {},
82
Akron537bc522018-07-13 19:06:27 +020083 /**
84 * Close the view.
85 */
86 close : function () {
Akron22598cd2019-12-09 14:59:03 +010087
88 // Close embedded things before
89 if (this.onClose)
90 this.onClose();
91
Akron537bc522018-07-13 19:06:27 +020092 var e = this.element();
Akron4a703872018-07-26 10:59:41 +020093 if (e.parentNode) {
94 e.parentNode.removeChild(e);
95 };
Akron537bc522018-07-13 19:06:27 +020096 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020097 this._shown = false;
98 },
99
Akron4d926f12018-07-16 15:30:25 +0200100 /**
101 * Upgrade this object to another object,
102 * while private data stays intact.
103 *
104 * @param {Object] An object with properties.
105 */
106 upgradeTo : function (props) {
107 for (var prop in props) {
108 this[prop] = props[prop];
109 };
110 return this;
Akron537bc522018-07-13 19:06:27 +0200111 }
112 };
113});