blob: 99f7366cddc09b9a4ad4098b3c6324160c979795 [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 */
Akronbfe912c2018-07-17 19:30:52 +02006define([
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
Akron7f9a6a32018-07-18 15:05:23 +020039 // TODO:
40 // Ugly hack!
Akronbfe912c2018-07-17 19:30:52 +020041 var cl= a.element().classList;
42 cl.remove('matchinfo');
43 cl.add('button-matchinfo');
44
45 // Add meta button
46 a.add(
Akron792b1a42020-09-14 18:56:38 +020047 loc.SHOW_META, {'cls':['metatable']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020048 this.addMeta();
49 }
50 );
51
52 // Add token annotation button
53 a.add(
Akron792b1a42020-09-14 18:56:38 +020054 loc.SHOWANNO, {'cls':['info']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020055 this.addTable();
56 }
57 );
58
59 // Add relations button
60 a.add(
Akron792b1a42020-09-14 18:56:38 +020061 loc.ADDTREE, {'cls':['tree']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020062
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
Akron7f9a6a32018-07-18 15:05:23 +020072 tm.panel(this);
Akronbfe912c2018-07-17 19:30:52 +020073
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 }
Akrone1c27f62018-07-20 11:42:59 +020082 );
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
Akron678c26f2020-10-09 08:52:50 +020090 matchButtons.forEach(m => a.add.apply(a, m));
Akrone1c27f62018-07-20 11:42:59 +020091 };
92 };
Akronbfe912c2018-07-17 19:30:52 +020093
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 */
Akron7f9a6a32018-07-18 15:05:23 +0200122 addTree : function (foundry, layer, type) {
Akronbfe912c2018-07-17 19:30:52 +0200123 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
Akron678c26f2020-10-09 08:52:50 +0200143 spans.forEach(i => treeLayers.push(i));
144 rels.forEach(i => treeLayers.push(i));
Akronbfe912c2018-07-17 19:30:52 +0200145
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
Akronb50964a2020-10-12 11:44:37 +0200167 treeLayers.forEach(
168 s =>
169 // Add foundry/layer to menu list
170 menuList.push([
171 s.foundry + '/' + s.layer,
172 s.foundry,
173 s.layer,
174 s.type
175 ]));
Akronbfe912c2018-07-17 19:30:52 +0200176
177 // Create tree menu
178 this._menuList = menuList;
179 return menuList;
180 }
181 }
182});