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 | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 8 | const loc = KorAP.Locale; |
Akron | 362c11a | 2018-08-29 20:01:30 +0200 | [diff] [blame] | 9 | loc.CLOSE = loc.CLOSE || 'Close'; |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 10 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 11 | return { |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 12 | create : function (classes) { |
| 13 | return Object.create(this)._init(classes); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 14 | }, |
| 15 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 16 | // Override by inheriting object |
| 17 | _init : function (classes) { |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 18 | this.panel = undefined; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 19 | this._classes = classes; |
| 20 | this._shown = false; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 21 | |
| 22 | // The buttonclass is bind to the view |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 23 | var c = ['action', 'button-view']; |
| 24 | if (classes) |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 25 | c.push.apply(c,classes); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 26 | |
| 27 | this.actions = buttonGroupClass.create(c).bind(this); |
| 28 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 29 | this.actions.add(loc.CLOSE, ['button-icon','close'], function (e) { |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 30 | this.close(); |
| 31 | }); |
| 32 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 33 | // Warning: This is circular |
| 34 | this.actions.view = this; |
| 35 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 36 | return this; |
| 37 | }, |
| 38 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 39 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Element of the view |
| 42 | */ |
| 43 | element : function () { |
| 44 | if (this._element) |
| 45 | return this._element; |
| 46 | |
| 47 | // Create panel element |
| 48 | var e = document.createElement('div'); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 49 | |
| 50 | var cl = e.classList; |
| 51 | cl.add('view'); |
| 52 | if (this._classes) |
| 53 | cl.add.apply(cl, this._classes); |
| 54 | |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 55 | // TODO: The show may need to be wrapped in another DIV! |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 56 | if (this.show !== undefined) { |
| 57 | let s = this.show(); |
| 58 | if (s) { |
| 59 | e.appendChild(s); |
| 60 | } else { |
| 61 | return e |
| 62 | } |
| 63 | } |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 64 | |
| 65 | this._shown = true; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 66 | |
| 67 | e.appendChild(this.actions.element()); |
| 68 | |
| 69 | this._element = e; |
| 70 | return e; |
| 71 | }, |
| 72 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Is the object shown? |
| 76 | */ |
| 77 | shown : function () { |
| 78 | return this._shown; |
| 79 | }, |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 80 | |
| 81 | // onClose : function () {}, |
| 82 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Close the view. |
| 85 | */ |
| 86 | close : function () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 87 | |
| 88 | // Close embedded things before |
| 89 | if (this.onClose) |
| 90 | this.onClose(); |
| 91 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 92 | var e = this.element(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 93 | if (e.parentNode) { |
| 94 | e.parentNode.removeChild(e); |
| 95 | }; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 96 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 97 | this._shown = false; |
| 98 | }, |
| 99 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 100 | /** |
| 101 | * Upgrade this object to another object, |
| 102 | * while private data stays intact. |
| 103 | * |
| 104 | * @param {Object] An object with properties. |
| 105 | */ |
| 106 | upgradeTo : function (props) { |
| 107 | for (var prop in props) { |
| 108 | this[prop] = props[prop]; |
| 109 | }; |
| 110 | return this; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | }); |