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 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 8 | const loc = KorAP.Locale; |
| 9 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 10 | |
| 11 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 12 | return { |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 13 | create : function (classes) { |
| 14 | return Object.create(this)._init(classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 15 | }, |
| 16 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 17 | // Override by inheriting object |
| 18 | _init : function (classes) { |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 19 | this.panel = undefined; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 20 | this._classes = classes; |
| 21 | this._shown = false; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 22 | |
| 23 | // The buttonclass is bind to the view |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 24 | var c = ['action', 'button-view']; |
| 25 | if (classes) |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 26 | c.push.apply(c,classes); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 27 | |
| 28 | this.actions = buttonGroupClass.create(c).bind(this); |
| 29 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 30 | this.actions.add(loc.CLOSE, ['button-icon','close'], function (e) { |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 31 | this.close(); |
| 32 | }); |
| 33 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 34 | // Warning: This is circular |
| 35 | this.actions.view = this; |
| 36 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 37 | return this; |
| 38 | }, |
| 39 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 40 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 41 | /** |
| 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'); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 50 | |
| 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; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 60 | |
| 61 | e.appendChild(this.actions.element()); |
| 62 | |
| 63 | this._element = e; |
| 64 | return e; |
| 65 | }, |
| 66 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Is the object shown? |
| 70 | */ |
| 71 | shown : function () { |
| 72 | return this._shown; |
| 73 | }, |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 74 | |
| 75 | // onClose : function () {}, |
| 76 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Close the view. |
| 79 | */ |
| 80 | close : function () { |
| 81 | var e = this.element(); |
| 82 | e.parentNode.removeChild(e); |
| 83 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 84 | this._shown = false; |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 85 | if (this.onClose) |
| 86 | this.onClose(); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 87 | }, |
| 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; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | }); |