blob: 2b220d3ed4b8bea65c7668e094324b781bd69eb2 [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 }
81 )
82
83 return this;
84 },
85
86
87 /**
88 * Add meta view to panel
89 */
90 addMeta : function () {
91 if (this._metaView && this._metaView.shown())
92 return;
93 this._metaView = metaView.create(this._match);
94 this.add(this._metaView);
95 },
96
97
98 /**
99 * Add table view to panel
100 */
101 addTable : function () {
102 if (this._tableView && this._tableView.shown())
103 return;
104 this._tableView = tableView.create(this._match);
105 this.add(this._tableView);
106 },
107
108 /**
109 * Add Tree view to panel
110 */
Akron7f9a6a32018-07-18 15:05:23 +0200111 addTree : function (foundry, layer, type) {
Akronbfe912c2018-07-17 19:30:52 +0200112 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});