Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Create a view that can be added to a panel, |
| 3 | * like a tree view or the metadata view. |
| 4 | */ |
| 5 | |
| 6 | define(['buttongroup', 'util'], function (buttonGroupClass) { |
| 7 | |
| 8 | return { |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 9 | create : function (classes) { |
| 10 | return Object.create(this)._init(classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 11 | }, |
| 12 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 13 | // Override by inheriting object |
| 14 | _init : function (classes) { |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 15 | this.panel = undefined; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 16 | this._classes = classes; |
| 17 | this._shown = false; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 18 | |
| 19 | // The buttonclass is bind to the view |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 20 | var c = ['action', 'button-view']; |
| 21 | if (classes) |
| 22 | c.push.apply(null,classes); |
| 23 | |
| 24 | this.actions = buttonGroupClass.create(c).bind(this); |
| 25 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 26 | this.actions.add('close', ['button-icon','close'], function (e) { |
| 27 | this.close(); |
| 28 | }); |
| 29 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 30 | // Warning: This is circular |
| 31 | this.actions.view = this; |
| 32 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 33 | return this; |
| 34 | }, |
| 35 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 36 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 37 | /** |
| 38 | * Element of the view |
| 39 | */ |
| 40 | element : function () { |
| 41 | if (this._element) |
| 42 | return this._element; |
| 43 | |
| 44 | // Create panel element |
| 45 | var e = document.createElement('div'); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 46 | |
| 47 | var cl = e.classList; |
| 48 | cl.add('view'); |
| 49 | if (this._classes) |
| 50 | cl.add.apply(cl, this._classes); |
| 51 | |
| 52 | if (this.show !== undefined) |
| 53 | e.appendChild(this.show()); |
| 54 | |
| 55 | this._shown = true; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 56 | |
| 57 | e.appendChild(this.actions.element()); |
| 58 | |
| 59 | this._element = e; |
| 60 | return e; |
| 61 | }, |
| 62 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Is the object shown? |
| 66 | */ |
| 67 | shown : function () { |
| 68 | return this._shown; |
| 69 | }, |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame^] | 70 | |
| 71 | // onClose : function () {}, |
| 72 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 73 | /** |
| 74 | * Close the view. |
| 75 | */ |
| 76 | close : function () { |
| 77 | var e = this.element(); |
| 78 | e.parentNode.removeChild(e); |
| 79 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 80 | this._shown = false; |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame^] | 81 | if (this.onClose) |
| 82 | this.onClose(); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 83 | }, |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Upgrade this object to another object, |
| 88 | * while private data stays intact. |
| 89 | * |
| 90 | * @param {Object] An object with properties. |
| 91 | */ |
| 92 | upgradeTo : function (props) { |
| 93 | for (var prop in props) { |
| 94 | this[prop] = props[prop]; |
| 95 | }; |
| 96 | return this; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 97 | } |
| 98 | }; |
| 99 | }); |