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