blob: 33e7dc9e14658c24196b2d662dd7a56006826dd5 [file] [log] [blame]
Akronbfe912c2018-07-17 19:30:52 +02001/**
Akron5cb9b2b2018-07-24 17:01:09 +02002 * The match panel.
3 *
4 * @author Nils Diewald
Akronbfe912c2018-07-17 19:30:52 +02005 */
Akron14f3ee92020-10-19 11:59:40 +02006"use strict";
7
Akronbfe912c2018-07-17 19:30:52 +02008define([
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 {
Akron386f1222022-12-21 16:26:11 +010030 type : 'match',
31
Akronbfe912c2018-07-17 19:30:52 +020032 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
Akron37ea1192021-07-28 10:40:14 +020041 const a = this.actions();
Akronbfe912c2018-07-17 19:30:52 +020042
Akron7f9a6a32018-07-18 15:05:23 +020043 // TODO:
44 // Ugly hack!
Akron14f3ee92020-10-19 11:59:40 +020045 const cl = a.element().classList;
Akronbfe912c2018-07-17 19:30:52 +020046 cl.remove('matchinfo');
47 cl.add('button-matchinfo');
48
49 // Add meta button
50 a.add(
Akron792b1a42020-09-14 18:56:38 +020051 loc.SHOW_META, {'cls':['metatable']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020052 this.addMeta();
53 }
54 );
55
56 // Add token annotation button
57 a.add(
Akron792b1a42020-09-14 18:56:38 +020058 loc.SHOWANNO, {'cls':['info']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020059 this.addTable();
60 }
61 );
62
63 // Add relations button
64 a.add(
Akron792b1a42020-09-14 18:56:38 +020065 loc.ADDTREE, {'cls':['tree']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020066
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
Akron14f3ee92020-10-19 11:59:40 +020073 const tm = KorAP.TreeMenu;
Akronbfe912c2018-07-17 19:30:52 +020074
75 // Set panel
Akron7f9a6a32018-07-18 15:05:23 +020076 tm.panel(this);
Akronbfe912c2018-07-17 19:30:52 +020077
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 }
Akrone1c27f62018-07-20 11:42:59 +020086 );
87
88 // If plugins are enabled, add all buttons for the match panel
89 if (KorAP.Plugin) {
Akron14f3ee92020-10-19 11:59:40 +020090 const matchButtons = KorAP.Plugin.buttonGroup("match");
Akrone1c27f62018-07-20 11:42:59 +020091 if (matchButtons) {
92
93 // Add all matchbuttons in order
Akron678c26f2020-10-09 08:52:50 +020094 matchButtons.forEach(m => a.add.apply(a, m));
Akrone1c27f62018-07-20 11:42:59 +020095 };
96 };
Akronbfe912c2018-07-17 19:30:52 +020097
98 return this;
99 },
100
101
102 /**
103 * Add meta view to panel
104 */
105 addMeta : function () {
Akron14f3ee92020-10-19 11:59:40 +0200106 const t = this;
107 if (t._metaView && t._metaView.shown())
Akronbfe912c2018-07-17 19:30:52 +0200108 return;
Akron14f3ee92020-10-19 11:59:40 +0200109
110 t._metaView = metaView.create(t._match);
111 t.add(t._metaView);
Akronbfe912c2018-07-17 19:30:52 +0200112 },
113
114
115 /**
116 * Add table view to panel
117 */
118 addTable : function () {
Akron14f3ee92020-10-19 11:59:40 +0200119 const t = this;
120 if (t._tableView && t._tableView.shown())
Akronbfe912c2018-07-17 19:30:52 +0200121 return;
Akron14f3ee92020-10-19 11:59:40 +0200122 t._tableView = tableView.create(t._match);
123 t.add(t._tableView);
Akronbfe912c2018-07-17 19:30:52 +0200124 },
125
Akron14f3ee92020-10-19 11:59:40 +0200126
Akronbfe912c2018-07-17 19:30:52 +0200127 /**
128 * Add Tree view to panel
129 */
Akron7f9a6a32018-07-18 15:05:23 +0200130 addTree : function (foundry, layer, type) {
Akronbfe912c2018-07-17 19:30:52 +0200131 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
Akronbfe912c2018-07-17 19:30:52 +0200144 // Join spans and relations
Akron14f3ee92020-10-19 11:59:40 +0200145 let treeLayers = []
146
147 const match = this._match;
148 match.getSpans().forEach(i => treeLayers.push(i));
149 match.getRels().forEach(i => treeLayers.push(i));
Akronbfe912c2018-07-17 19:30:52 +0200150
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
Akron14f3ee92020-10-19 11:59:40 +0200169 let menuList = [];
Akronbfe912c2018-07-17 19:30:52 +0200170
171 // Show tree views
Akronb50964a2020-10-12 11:44:37 +0200172 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 ]));
Akronbfe912c2018-07-17 19:30:52 +0200181
182 // Create tree menu
183 this._menuList = menuList;
184 return menuList;
185 }
186 }
187});