blob: 2f35c9e033b2aa0378d6ecde9f30b6fd77de5c1e [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;
9 loc.CLOSE = loc.CLOSE || 'Close';
Akronbfe912c2018-07-17 19:30:52 +020010
11
Akron537bc522018-07-13 19:06:27 +020012 return {
Akron4d926f12018-07-16 15:30:25 +020013 create : function (classes) {
14 return Object.create(this)._init(classes);
Akron537bc522018-07-13 19:06:27 +020015 },
16
Akron4d926f12018-07-16 15:30:25 +020017 // Override by inheriting object
18 _init : function (classes) {
Akron537bc522018-07-13 19:06:27 +020019 this.panel = undefined;
Akron4d926f12018-07-16 15:30:25 +020020 this._classes = classes;
21 this._shown = false;
Akron537bc522018-07-13 19:06:27 +020022
23 // The buttonclass is bind to the view
Akron4d926f12018-07-16 15:30:25 +020024 var c = ['action', 'button-view'];
25 if (classes)
Akronbfe912c2018-07-17 19:30:52 +020026 c.push.apply(c,classes);
Akron4d926f12018-07-16 15:30:25 +020027
28 this.actions = buttonGroupClass.create(c).bind(this);
29
Akronbfe912c2018-07-17 19:30:52 +020030 this.actions.add(loc.CLOSE, ['button-icon','close'], function (e) {
Akron537bc522018-07-13 19:06:27 +020031 this.close();
32 });
33
Akron4d926f12018-07-16 15:30:25 +020034 // Warning: This is circular
35 this.actions.view = this;
36
Akron537bc522018-07-13 19:06:27 +020037 return this;
38 },
39
Akron4d926f12018-07-16 15:30:25 +020040
Akron537bc522018-07-13 19:06:27 +020041 /**
42 * Element of the view
43 */
44 element : function () {
45 if (this._element)
46 return this._element;
47
48 // Create panel element
49 var e = document.createElement('div');
Akron4d926f12018-07-16 15:30:25 +020050
51 var cl = e.classList;
52 cl.add('view');
53 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!
Akron4d926f12018-07-16 15:30:25 +020057 if (this.show !== undefined)
58 e.appendChild(this.show());
59
60 this._shown = true;
Akron537bc522018-07-13 19:06:27 +020061
62 e.appendChild(this.actions.element());
63
64 this._element = e;
65 return e;
66 },
67
Akron4d926f12018-07-16 15:30:25 +020068
69 /**
70 * Is the object shown?
71 */
72 shown : function () {
73 return this._shown;
74 },
Akrone6538cd2018-07-16 17:52:33 +020075
76 // onClose : function () {},
77
Akron537bc522018-07-13 19:06:27 +020078 /**
79 * Close the view.
80 */
81 close : function () {
82 var e = this.element();
Akron4a703872018-07-26 10:59:41 +020083 if (e.parentNode) {
84 e.parentNode.removeChild(e);
85 };
Akron537bc522018-07-13 19:06:27 +020086 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020087 this._shown = false;
Akrone6538cd2018-07-16 17:52:33 +020088 if (this.onClose)
89 this.onClose();
Akron4d926f12018-07-16 15:30:25 +020090 },
91
Akron4d926f12018-07-16 15:30:25 +020092 /**
93 * Upgrade this object to another object,
94 * while private data stays intact.
95 *
96 * @param {Object] An object with properties.
97 */
98 upgradeTo : function (props) {
99 for (var prop in props) {
100 this[prop] = props[prop];
101 };
102 return this;
Akron537bc522018-07-13 19:06:27 +0200103 }
104 };
105});