blob: 7058a9ae3393dd342eb1795a6b8ba0ba18ad48d1 [file] [log] [blame]
Akronbfe912c2018-07-17 19:30:52 +02001/**
2 * Define a panel for matches
3 */
4
5define([
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
Akron7f9a6a32018-07-18 15:05:23 +020038 // TODO:
39 // Ugly hack!
Akronbfe912c2018-07-17 19:30:52 +020040 var cl= a.element().classList;
41 cl.remove('matchinfo');
42 cl.add('button-matchinfo');
43
44 // Add meta button
45 a.add(
46 loc.SHOW_META, ['metatable'], function (e) {
47 this.addMeta();
48 }
49 );
50
51 // Add token annotation button
52 a.add(
53 loc.SHOWANNO, ['info'], function (e) {
54 this.addTable();
55 }
56 );
57
58 // Add relations button
59 a.add(
60 loc.ADDTREE, ['tree'], function (e) {
61
62 // Get global tree menu
63 if (KorAP.TreeMenu === undefined) {
64 KorAP.TreeMenu = buttonGroupMenuClass.create([], treeItemClass);
65 KorAP.TreeMenu.element().setAttribute('id', 'treeMenu');
66 };
67
68 var tm = KorAP.TreeMenu;
69
70 // Set panel
Akron7f9a6a32018-07-18 15:05:23 +020071 tm.panel(this);
Akronbfe912c2018-07-17 19:30:52 +020072
73 // Reread list
74 tm.readItems(this._treeMenuList());
75
76 // Reposition and show menu
77 tm.show();
78 tm.button(this.button);
79 tm.focus();
80 }
Akrone1c27f62018-07-20 11:42:59 +020081 );
82
83 // If plugins are enabled, add all buttons for the match panel
84 if (KorAP.Plugin) {
85 var matchButtons = KorAP.Plugin.buttonGroup("match");
86 if (matchButtons) {
87
88 // Add all matchbuttons in order
89 for (i in matchButtons) {
90 a.add.apply(a, matchButtons[i]);
91 }
92 };
93 };
Akronbfe912c2018-07-17 19:30:52 +020094
95 return this;
96 },
97
98
99 /**
100 * Add meta view to panel
101 */
102 addMeta : function () {
103 if (this._metaView && this._metaView.shown())
104 return;
105 this._metaView = metaView.create(this._match);
106 this.add(this._metaView);
107 },
108
109
110 /**
111 * Add table view to panel
112 */
113 addTable : function () {
114 if (this._tableView && this._tableView.shown())
115 return;
116 this._tableView = tableView.create(this._match);
117 this.add(this._tableView);
118 },
119
120 /**
121 * Add Tree view to panel
122 */
Akron7f9a6a32018-07-18 15:05:23 +0200123 addTree : function (foundry, layer, type) {
Akronbfe912c2018-07-17 19:30:52 +0200124 this.add(
125 relationsView.create(this._match, foundry, layer, type)
126 );
127
128 },
129
130
131 // Return tree menu list
132 _treeMenuList : function () {
133
134 if (this._menuList)
135 return this._menuList;
136
137 var match = this._match;
138
139 // Join spans and relations
140 var treeLayers = []
141 var spans = match.getSpans();
142 var rels = match.getRels();
143
144 var i;
145 for (i in spans) {
146 treeLayers.push(spans[i]);
147 };
148 for (i in rels) {
149 treeLayers.push(rels[i]);
150 };
151
152 // Get spans
153 treeLayers = treeLayers.sort(
154 function (a, b) {
155 if (a.foundry < b.foundry) {
156 return -1;
157 }
158 else if (a.foundry > b.foundry) {
159 return 1;
160 }
161 else if (a.layer < b.layer) {
162 return -1;
163 }
164 else if (a.layer > b.layer) {
165 return 1;
166 };
167 return 0;
168 });
169
170 var menuList = [];
171
172 // Show tree views
173 for (var i = 0; i < treeLayers.length; i++) {
174 var span = treeLayers[i];
175
176 // Add foundry/layer to menu list
177 menuList.push([
178 span.foundry + '/' + span.layer,
179 span.foundry,
180 span.layer,
181 span.type
182 ]);
183 };
184
185 // Create tree menu
186 this._menuList = menuList;
187 return menuList;
188 }
189 }
190});