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