blob: 0a47f0b4f748194f268177bf9c9c5648133ee76f [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
Akronbfe912c2018-07-17 19:30:52 +02008 const loc = KorAP.Locale;
9 loc.CLOSE = loc.CLOSE || 'Close';
10
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
56 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();
82 e.parentNode.removeChild(e);
83 this.panel.delView(this);
Akron4d926f12018-07-16 15:30:25 +020084 this._shown = false;
Akrone6538cd2018-07-16 17:52:33 +020085 if (this.onClose)
86 this.onClose();
Akron4d926f12018-07-16 15:30:25 +020087 },
88
89
90 /**
91 * Upgrade this object to another object,
92 * while private data stays intact.
93 *
94 * @param {Object] An object with properties.
95 */
96 upgradeTo : function (props) {
97 for (var prop in props) {
98 this[prop] = props[prop];
99 };
100 return this;
Akron537bc522018-07-13 19:06:27 +0200101 }
102 };
103});