blob: 603a230f6f9b046f1bfe82523eff759ece67c89c [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 {
30 create : function (match) {
31 return Object.create(panelClass)._init(['matchinfo']).upgradeTo(this)._init(match);
32 },
33
34 // Initialize match panel
35 _init : function (match) {
36
37 this._match = match;
38
Akron14f3ee92020-10-19 11:59:40 +020039 const a = this.actions;
Akronbfe912c2018-07-17 19:30:52 +020040
Akron7f9a6a32018-07-18 15:05:23 +020041 // TODO:
42 // Ugly hack!
Akron14f3ee92020-10-19 11:59:40 +020043 const cl = a.element().classList;
Akronbfe912c2018-07-17 19:30:52 +020044 cl.remove('matchinfo');
45 cl.add('button-matchinfo');
46
47 // Add meta button
48 a.add(
Akron792b1a42020-09-14 18:56:38 +020049 loc.SHOW_META, {'cls':['metatable']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020050 this.addMeta();
51 }
52 );
53
54 // Add token annotation button
55 a.add(
Akron792b1a42020-09-14 18:56:38 +020056 loc.SHOWANNO, {'cls':['info']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020057 this.addTable();
58 }
59 );
60
61 // Add relations button
62 a.add(
Akron792b1a42020-09-14 18:56:38 +020063 loc.ADDTREE, {'cls':['tree']}, function (e) {
Akronbfe912c2018-07-17 19:30:52 +020064
65 // Get global tree menu
66 if (KorAP.TreeMenu === undefined) {
67 KorAP.TreeMenu = buttonGroupMenuClass.create([], treeItemClass);
68 KorAP.TreeMenu.element().setAttribute('id', 'treeMenu');
69 };
70
Akron14f3ee92020-10-19 11:59:40 +020071 const tm = KorAP.TreeMenu;
Akronbfe912c2018-07-17 19:30:52 +020072
73 // Set panel
Akron7f9a6a32018-07-18 15:05:23 +020074 tm.panel(this);
Akronbfe912c2018-07-17 19:30:52 +020075
76 // Reread list
77 tm.readItems(this._treeMenuList());
78
79 // Reposition and show menu
80 tm.show();
81 tm.button(this.button);
82 tm.focus();
83 }
Akrone1c27f62018-07-20 11:42:59 +020084 );
85
86 // If plugins are enabled, add all buttons for the match panel
87 if (KorAP.Plugin) {
Akron14f3ee92020-10-19 11:59:40 +020088 const matchButtons = KorAP.Plugin.buttonGroup("match");
Akrone1c27f62018-07-20 11:42:59 +020089 if (matchButtons) {
90
91 // Add all matchbuttons in order
Akron678c26f2020-10-09 08:52:50 +020092 matchButtons.forEach(m => a.add.apply(a, m));
Akrone1c27f62018-07-20 11:42:59 +020093 };
94 };
Akronbfe912c2018-07-17 19:30:52 +020095
96 return this;
97 },
98
99
100 /**
101 * Add meta view to panel
102 */
103 addMeta : function () {
Akron14f3ee92020-10-19 11:59:40 +0200104 const t = this;
105 if (t._metaView && t._metaView.shown())
Akronbfe912c2018-07-17 19:30:52 +0200106 return;
Akron14f3ee92020-10-19 11:59:40 +0200107
108 t._metaView = metaView.create(t._match);
109 t.add(t._metaView);
Akronbfe912c2018-07-17 19:30:52 +0200110 },
111
112
113 /**
114 * Add table view to panel
115 */
116 addTable : function () {
Akron14f3ee92020-10-19 11:59:40 +0200117 const t = this;
118 if (t._tableView && t._tableView.shown())
Akronbfe912c2018-07-17 19:30:52 +0200119 return;
Akron14f3ee92020-10-19 11:59:40 +0200120 t._tableView = tableView.create(t._match);
121 t.add(t._tableView);
Akronbfe912c2018-07-17 19:30:52 +0200122 },
123
Akron14f3ee92020-10-19 11:59:40 +0200124
Akronbfe912c2018-07-17 19:30:52 +0200125 /**
126 * Add Tree view to panel
127 */
Akron7f9a6a32018-07-18 15:05:23 +0200128 addTree : function (foundry, layer, type) {
Akronbfe912c2018-07-17 19:30:52 +0200129 this.add(
130 relationsView.create(this._match, foundry, layer, type)
131 );
132
133 },
134
135
136 // Return tree menu list
137 _treeMenuList : function () {
138
139 if (this._menuList)
140 return this._menuList;
141
Akronbfe912c2018-07-17 19:30:52 +0200142 // Join spans and relations
Akron14f3ee92020-10-19 11:59:40 +0200143 let treeLayers = []
144
145 const match = this._match;
146 match.getSpans().forEach(i => treeLayers.push(i));
147 match.getRels().forEach(i => treeLayers.push(i));
Akronbfe912c2018-07-17 19:30:52 +0200148
149 // Get spans
150 treeLayers = treeLayers.sort(
151 function (a, b) {
152 if (a.foundry < b.foundry) {
153 return -1;
154 }
155 else if (a.foundry > b.foundry) {
156 return 1;
157 }
158 else if (a.layer < b.layer) {
159 return -1;
160 }
161 else if (a.layer > b.layer) {
162 return 1;
163 };
164 return 0;
165 });
166
Akron14f3ee92020-10-19 11:59:40 +0200167 let menuList = [];
Akronbfe912c2018-07-17 19:30:52 +0200168
169 // Show tree views
Akronb50964a2020-10-12 11:44:37 +0200170 treeLayers.forEach(
171 s =>
172 // Add foundry/layer to menu list
173 menuList.push([
174 s.foundry + '/' + s.layer,
175 s.foundry,
176 s.layer,
177 s.type
178 ]));
Akronbfe912c2018-07-17 19:30:52 +0200179
180 // Create tree menu
181 this._menuList = menuList;
182 return menuList;
183 }
184 }
185});