Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 1 | /** |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 2 | * The match panel. |
| 3 | * |
| 4 | * @author Nils Diewald |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 5 | */ |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 6 | "use strict"; |
| 7 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 8 | define([ |
| 9 | 'panel', |
| 10 | 'match/treeitem', |
| 11 | 'view/match/tokentable', |
| 12 | 'view/match/meta', |
| 13 | 'view/match/relations', |
| 14 | 'buttongroup/menu', |
| 15 | ], function (panelClass,treeItemClass,tableView,metaView,relationsView,buttonGroupMenuClass) { |
| 16 | |
| 17 | // Override |
| 18 | KorAP.API.getMatchInfo = KorAP.API.getMatchInfo || function () { |
| 19 | KorAP.log(0, 'KorAP.API.getMatchInfo() not implemented') |
| 20 | return {}; |
| 21 | }; |
| 22 | |
| 23 | const loc = KorAP.Locale; |
| 24 | |
| 25 | loc.SHOWANNO = loc.SHOWANNO || 'Tokens'; |
| 26 | loc.SHOW_META = loc.SHOW_META || 'Metadata'; |
| 27 | loc.ADDTREE = loc.ADDTREE || 'Relations'; |
| 28 | |
| 29 | return { |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame] | 30 | type : 'match', |
| 31 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 32 | create : function (match) { |
| 33 | return Object.create(panelClass)._init(['matchinfo']).upgradeTo(this)._init(match); |
| 34 | }, |
| 35 | |
| 36 | // Initialize match panel |
| 37 | _init : function (match) { |
| 38 | |
| 39 | this._match = match; |
| 40 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 41 | const a = this.actions(); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 42 | |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 43 | // TODO: |
| 44 | // Ugly hack! |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 45 | const cl = a.element().classList; |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 46 | cl.remove('matchinfo'); |
| 47 | cl.add('button-matchinfo'); |
| 48 | |
| 49 | // Add meta button |
| 50 | a.add( |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 51 | loc.SHOW_META, {'cls':['metatable']}, function (e) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 52 | this.addMeta(); |
| 53 | } |
| 54 | ); |
| 55 | |
| 56 | // Add token annotation button |
| 57 | a.add( |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 58 | loc.SHOWANNO, {'cls':['info']}, function (e) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 59 | this.addTable(); |
| 60 | } |
| 61 | ); |
| 62 | |
| 63 | // Add relations button |
| 64 | a.add( |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 65 | loc.ADDTREE, {'cls':['tree']}, function (e) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 66 | |
| 67 | // Get global tree menu |
| 68 | if (KorAP.TreeMenu === undefined) { |
| 69 | KorAP.TreeMenu = buttonGroupMenuClass.create([], treeItemClass); |
| 70 | KorAP.TreeMenu.element().setAttribute('id', 'treeMenu'); |
| 71 | }; |
| 72 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 73 | const tm = KorAP.TreeMenu; |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 74 | |
| 75 | // Set panel |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 76 | tm.panel(this); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 77 | |
| 78 | // Reread list |
| 79 | tm.readItems(this._treeMenuList()); |
| 80 | |
| 81 | // Reposition and show menu |
| 82 | tm.show(); |
| 83 | tm.button(this.button); |
| 84 | tm.focus(); |
| 85 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 86 | ); |
| 87 | |
| 88 | // If plugins are enabled, add all buttons for the match panel |
| 89 | if (KorAP.Plugin) { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 90 | const matchButtons = KorAP.Plugin.buttonGroup("match"); |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 91 | if (matchButtons) { |
| 92 | |
| 93 | // Add all matchbuttons in order |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 94 | matchButtons.forEach(m => a.add.apply(a, m)); |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 95 | }; |
| 96 | }; |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 97 | |
| 98 | return this; |
| 99 | }, |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Add meta view to panel |
| 104 | */ |
| 105 | addMeta : function () { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 106 | const t = this; |
| 107 | if (t._metaView && t._metaView.shown()) |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 108 | return; |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 109 | |
| 110 | t._metaView = metaView.create(t._match); |
| 111 | t.add(t._metaView); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 112 | }, |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Add table view to panel |
| 117 | */ |
| 118 | addTable : function () { |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 119 | const t = this; |
| 120 | if (t._tableView && t._tableView.shown()) |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 121 | return; |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 122 | t._tableView = tableView.create(t._match); |
| 123 | t.add(t._tableView); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 124 | }, |
| 125 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 126 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 127 | /** |
| 128 | * Add Tree view to panel |
| 129 | */ |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 130 | addTree : function (foundry, layer, type) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 131 | this.add( |
| 132 | relationsView.create(this._match, foundry, layer, type) |
| 133 | ); |
| 134 | |
| 135 | }, |
| 136 | |
| 137 | |
| 138 | // Return tree menu list |
| 139 | _treeMenuList : function () { |
| 140 | |
| 141 | if (this._menuList) |
| 142 | return this._menuList; |
| 143 | |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 144 | // Join spans and relations |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 145 | let treeLayers = [] |
| 146 | |
| 147 | const match = this._match; |
| 148 | match.getSpans().forEach(i => treeLayers.push(i)); |
| 149 | match.getRels().forEach(i => treeLayers.push(i)); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 150 | |
| 151 | // Get spans |
| 152 | treeLayers = treeLayers.sort( |
| 153 | function (a, b) { |
| 154 | if (a.foundry < b.foundry) { |
| 155 | return -1; |
| 156 | } |
| 157 | else if (a.foundry > b.foundry) { |
| 158 | return 1; |
| 159 | } |
| 160 | else if (a.layer < b.layer) { |
| 161 | return -1; |
| 162 | } |
| 163 | else if (a.layer > b.layer) { |
| 164 | return 1; |
| 165 | }; |
| 166 | return 0; |
| 167 | }); |
| 168 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 169 | let menuList = []; |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 170 | |
| 171 | // Show tree views |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 172 | treeLayers.forEach( |
| 173 | s => |
| 174 | // Add foundry/layer to menu list |
| 175 | menuList.push([ |
| 176 | s.foundry + '/' + s.layer, |
| 177 | s.foundry, |
| 178 | s.layer, |
| 179 | s.type |
| 180 | ])); |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 181 | |
| 182 | // Create tree menu |
| 183 | this._menuList = menuList; |
| 184 | return menuList; |
| 185 | } |
| 186 | } |
| 187 | }); |