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