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 | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 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 () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 81 | |
| 82 | // Close embedded things before |
| 83 | if (this.onClose) |
| 84 | this.onClose(); |
| 85 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 86 | var e = this.element(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 87 | if (e.parentNode) { |
| 88 | e.parentNode.removeChild(e); |
| 89 | }; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 90 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 91 | this._shown = false; |
| 92 | }, |
| 93 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 94 | /** |
| 95 | * Upgrade this object to another object, |
| 96 | * while private data stays intact. |
| 97 | * |
| 98 | * @param {Object] An object with properties. |
| 99 | */ |
| 100 | upgradeTo : function (props) { |
| 101 | for (var prop in props) { |
| 102 | this[prop] = props[prop]; |
| 103 | }; |
| 104 | return this; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | }); |