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