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 | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 29 | this.actions.add(loc.CLOSE, {'cls':['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 | |
| 39 | /** |
| 40 | * Element of the view |
| 41 | */ |
| 42 | element : function () { |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 43 | if (this._element) { |
| 44 | this._element.classList.add('show'); |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 45 | return this._element; |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 46 | }; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 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; |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 52 | cl.add('view', 'show'); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 53 | if (this._classes) |
| 54 | cl.add.apply(cl, this._classes); |
| 55 | |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 56 | // TODO: The show may need to be wrapped in another DIV! |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 57 | if (this.show !== undefined) { |
| 58 | let s = this.show(); |
| 59 | if (s) { |
| 60 | e.appendChild(s); |
| 61 | } else { |
| 62 | return e |
| 63 | } |
| 64 | } |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 65 | |
| 66 | this._shown = true; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 67 | |
| 68 | e.appendChild(this.actions.element()); |
| 69 | |
| 70 | this._element = e; |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 71 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 72 | return e; |
| 73 | }, |
| 74 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Is the object shown? |
| 78 | */ |
| 79 | shown : function () { |
| 80 | return this._shown; |
| 81 | }, |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 82 | |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Hide the widget if shown. |
| 86 | */ |
| 87 | minimize : function () { |
| 88 | if (this._element) { |
| 89 | this._element.classList.remove("show"); |
| 90 | } |
| 91 | }, |
| 92 | |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 93 | // onClose : function () {}, |
| 94 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 95 | /** |
| 96 | * Close the view. |
| 97 | */ |
| 98 | close : function () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 99 | |
| 100 | // Close embedded things before |
| 101 | if (this.onClose) |
| 102 | this.onClose(); |
| 103 | |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 104 | var e = this.element(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 105 | if (e.parentNode) { |
| 106 | e.parentNode.removeChild(e); |
| 107 | }; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 108 | this.panel.delView(this); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 109 | this._shown = false; |
| 110 | }, |
| 111 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 112 | /** |
| 113 | * Upgrade this object to another object, |
| 114 | * while private data stays intact. |
| 115 | * |
| 116 | * @param {Object] An object with properties. |
| 117 | */ |
| 118 | upgradeTo : function (props) { |
| 119 | for (var prop in props) { |
| 120 | this[prop] = props[prop]; |
| 121 | }; |
| 122 | return this; |
Akron | 537bc52 | 2018-07-13 19:06:27 +0200 | [diff] [blame] | 123 | } |
| 124 | }; |
| 125 | }); |