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 | }, |
| 70 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 71 | /** |
| 72 | * Close the view. |
| 73 | */ |
| 74 | close : function () { |
| 75 | var e = this.element(); |
| 76 | e.parentNode.removeChild(e); |
| 77 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame^] | 78 | this._shown = false; |
| 79 | }, |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Upgrade this object to another object, |
| 84 | * while private data stays intact. |
| 85 | * |
| 86 | * @param {Object] An object with properties. |
| 87 | */ |
| 88 | upgradeTo : function (props) { |
| 89 | for (var prop in props) { |
| 90 | this[prop] = props[prop]; |
| 91 | }; |
| 92 | return this; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | }); |