blob: 4885afab49b020eee651e61a29ca5daaa95873fd [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 () {
81 var e = this.element();
Akron4a703872018-07-26 10:59:41 +020082 if (e.parentNode) {
83 e.parentNode.removeChild(e);
84 };
Akron537bc522018-07-13 19:06:27 +020085 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020086 this._shown = false;
Akrone6538cd2018-07-16 17:52:33 +020087 if (this.onClose)
88 this.onClose();
Akron4d926f12018-07-16 15:30:25 +020089 },
90
Akron4d926f12018-07-16 15:30:25 +020091 /**
92 * Upgrade this object to another object,
93 * while private data stays intact.
94 *
95 * @param {Object] An object with properties.
96 */
97 upgradeTo : function (props) {
98 for (var prop in props) {
99 this[prop] = props[prop];
100 };
101 return this;
Akron537bc522018-07-13 19:06:27 +0200102 }
103 };
104});