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( |
| 47 | loc.SHOW_META, ['metatable'], function (e) { |
| 48 | this.addMeta(); |
| 49 | } |
| 50 | ); |
| 51 | |
| 52 | // Add token annotation button |
| 53 | a.add( |
| 54 | loc.SHOWANNO, ['info'], function (e) { |
| 55 | this.addTable(); |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | // Add relations button |
| 60 | a.add( |
| 61 | loc.ADDTREE, ['tree'], function (e) { |
| 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 |
| 90 | for (i in matchButtons) { |
| 91 | a.add.apply(a, matchButtons[i]); |
| 92 | } |
| 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 () { |
| 104 | if (this._metaView && this._metaView.shown()) |
| 105 | return; |
| 106 | this._metaView = metaView.create(this._match); |
| 107 | this.add(this._metaView); |
| 108 | }, |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Add table view to panel |
| 113 | */ |
| 114 | addTable : function () { |
| 115 | if (this._tableView && this._tableView.shown()) |
| 116 | return; |
| 117 | this._tableView = tableView.create(this._match); |
| 118 | this.add(this._tableView); |
| 119 | }, |
| 120 | |
| 121 | /** |
| 122 | * Add Tree view to panel |
| 123 | */ |
Akron | 7f9a6a3 | 2018-07-18 15:05:23 +0200 | [diff] [blame] | 124 | addTree : function (foundry, layer, type) { |
Akron | bfe912c | 2018-07-17 19:30:52 +0200 | [diff] [blame] | 125 | this.add( |
| 126 | relationsView.create(this._match, foundry, layer, type) |
| 127 | ); |
| 128 | |
| 129 | }, |
| 130 | |
| 131 | |
| 132 | // Return tree menu list |
| 133 | _treeMenuList : function () { |
| 134 | |
| 135 | if (this._menuList) |
| 136 | return this._menuList; |
| 137 | |
| 138 | var match = this._match; |
| 139 | |
| 140 | // Join spans and relations |
| 141 | var treeLayers = [] |
| 142 | var spans = match.getSpans(); |
| 143 | var rels = match.getRels(); |
| 144 | |
| 145 | var i; |
| 146 | for (i in spans) { |
| 147 | treeLayers.push(spans[i]); |
| 148 | }; |
| 149 | for (i in rels) { |
| 150 | treeLayers.push(rels[i]); |
| 151 | }; |
| 152 | |
| 153 | // Get spans |
| 154 | treeLayers = treeLayers.sort( |
| 155 | function (a, b) { |
| 156 | if (a.foundry < b.foundry) { |
| 157 | return -1; |
| 158 | } |
| 159 | else if (a.foundry > b.foundry) { |
| 160 | return 1; |
| 161 | } |
| 162 | else if (a.layer < b.layer) { |
| 163 | return -1; |
| 164 | } |
| 165 | else if (a.layer > b.layer) { |
| 166 | return 1; |
| 167 | }; |
| 168 | return 0; |
| 169 | }); |
| 170 | |
| 171 | var menuList = []; |
| 172 | |
| 173 | // Show tree views |
| 174 | for (var i = 0; i < treeLayers.length; i++) { |
| 175 | var span = treeLayers[i]; |
| 176 | |
| 177 | // Add foundry/layer to menu list |
| 178 | menuList.push([ |
| 179 | span.foundry + '/' + span.layer, |
| 180 | span.foundry, |
| 181 | span.layer, |
| 182 | span.type |
| 183 | ]); |
| 184 | }; |
| 185 | |
| 186 | // Create tree menu |
| 187 | this._menuList = menuList; |
| 188 | return menuList; |
| 189 | } |
| 190 | } |
| 191 | }); |