blob: 02dfa53b301b21ed679229ff227079f813cb0e2c [file] [log] [blame]
Akrone51eaa32020-11-10 09:35:53 +01001"use strict";
Akrond4b000b2018-01-28 18:33:14 +01002
Akrone51eaa32020-11-10 09:35:53 +01003define(['match/corpusByMatch','match/attachement','util'], function (cbmClass, attClass) {
Akronff1b1f32020-10-18 11:41:29 +02004
Akronbd342982018-01-25 18:01:46 +01005 // Localization values
Akron0b489ad2018-02-02 16:49:32 +01006 const loc = KorAP.Locale;
Akron0ad7cd22018-02-08 18:03:06 +01007 loc.METADATA = loc.METADATA || 'Metadata';
Akronbd342982018-01-25 18:01:46 +01008
Akron24866cf2018-01-23 20:22:01 +01009 return {
10
11 /**
12 * Create new match object
13 */
Akron0ad7cd22018-02-08 18:03:06 +010014 create : function (match, fields) {
15 return Object.create(this)._init(match, fields);
Akron24866cf2018-01-23 20:22:01 +010016 },
17
Akronff1b1f32020-10-18 11:41:29 +020018
Akron24866cf2018-01-23 20:22:01 +010019 /**
20 * Initialize object
21 */
Akron0ad7cd22018-02-08 18:03:06 +010022 _init : function (match, fields) {
Akron24866cf2018-01-23 20:22:01 +010023 this._match = match;
Akron0ad7cd22018-02-08 18:03:06 +010024 this._fields = fields;
Akron24866cf2018-01-23 20:22:01 +010025 return this;
26 },
27
Akronff1b1f32020-10-18 11:41:29 +020028
Akron24866cf2018-01-23 20:22:01 +010029 /**
30 * Get match object
31 */
32 match : function () {
33 return this._match;
34 },
35
Akronff1b1f32020-10-18 11:41:29 +020036
Akron24866cf2018-01-23 20:22:01 +010037 /**
38 * Create match reference view.
39 */
Akron0ad7cd22018-02-08 18:03:06 +010040 element : function () {
Akron24aa0052020-11-10 11:00:34 +010041 if (this._el !== undefined)
42 return this._el;
Akronbd342982018-01-25 18:01:46 +010043
Akron0ad7cd22018-02-08 18:03:06 +010044 if (this._fields === null)
45 return;
Akronbd342982018-01-25 18:01:46 +010046
Akronff1b1f32020-10-18 11:41:29 +020047 const metaDL = document.createElement('dl');
Akron2f979122018-07-25 17:00:23 +020048 metaDL.classList.add("flex");
Akronbd342982018-01-25 18:01:46 +010049
Akron24aa0052020-11-10 11:00:34 +010050 this._el = metaDL;
Akron24866cf2018-01-23 20:22:01 +010051
Akronff1b1f32020-10-18 11:41:29 +020052 const fields = this._fields;
Akron0ad7cd22018-02-08 18:03:06 +010053
Akronabd6e352018-11-19 15:36:42 +010054 // Copy original array position to object
55 // before sorting by key title
Akronff1b1f32020-10-18 11:41:29 +020056 const posInMetaArray = {};
Akronb50964a2020-10-12 11:44:37 +020057 fields.forEach((f,i) => posInMetaArray[f["key"]] = i);
Akronabd6e352018-11-19 15:36:42 +010058
Akron24866cf2018-01-23 20:22:01 +010059 // TODO: Meta fields should be separated
Akronff1b1f32020-10-18 11:41:29 +020060
Akronabd6e352018-11-19 15:36:42 +010061 // Sort all meta keys alphabetically
Akron678c26f2020-10-09 08:52:50 +020062 Object.keys(posInMetaArray).sort().forEach(function(k) {
63 let field = fields[posInMetaArray[k]]; // This is the object
Akron7dd32822018-10-01 19:38:31 +020064
Akronff1b1f32020-10-18 11:41:29 +020065 let metaL, dt, metaDescr, metaDD, att;
66
Akron7dd32822018-10-01 19:38:31 +020067 // Ignore internal IDs
Akron24866cf2018-01-23 20:22:01 +010068 if (k !== "UID" &&
69 k !== "corpusID" &&
70 k !== "docID" &&
71 k !== "textID" &&
Akron24866cf2018-01-23 20:22:01 +010072 k !== "layerInfos") {
73
Akronff1b1f32020-10-18 11:41:29 +020074 metaL = document.createElement('div');
hebasta24fbc9e2018-04-12 13:47:50 +020075
Akronff1b1f32020-10-18 11:41:29 +020076 dt = metaL.addE('dt');
Akronc4568382018-03-06 20:16:12 +010077 dt.addT(k);
78 dt.setAttribute("title", k);
hebasta24fbc9e2018-04-12 13:47:50 +020079
Akronff1b1f32020-10-18 11:41:29 +020080 metaDescr = field["value"];
Akrond45a1702018-11-19 18:15:17 +010081 metaDD = metaL.addE('dd');
82 metaDD.setAttribute('data-type', field["type"]);
Akrona0ea3c32018-12-14 18:33:48 +010083
hebasta24fbc9e2018-04-12 13:47:50 +020084 if(metaDescr instanceof Array){
Akronb50964a2020-10-12 11:44:37 +020085 metaDD.classList.add("metakeyvalues");
86 metaDescr.forEach(function(md) {
Akrona0ea3c32018-12-14 18:33:48 +010087 if (field["type"] === 'type:attachement') {
Akronff1b1f32020-10-18 11:41:29 +020088 att = attClass.create(md);
Akrona0ea3c32018-12-14 18:33:48 +010089 if (att)
90 metaDD.addE('div').appendChild(att.inline());
91 }
92 else {
Akronb50964a2020-10-12 11:44:37 +020093 metaDD.addE('div').addT(md);
Akrona0ea3c32018-12-14 18:33:48 +010094 }
Akronb50964a2020-10-12 11:44:37 +020095 });
hebasta24fbc9e2018-04-12 13:47:50 +020096 }
97 else{
Akrona0ea3c32018-12-14 18:33:48 +010098 if (field["type"] === 'type:attachement') {
Akronff1b1f32020-10-18 11:41:29 +020099 att = attClass.create(field["value"]);
Akrona0ea3c32018-12-14 18:33:48 +0100100 if (att)
Akronff1b1f32020-10-18 11:41:29 +0200101 metaDD.appendChild(att.inline());
Akrona0ea3c32018-12-14 18:33:48 +0100102 }
103 else {
104 metaDD.addT(field["value"]);
105 };
hebasta24fbc9e2018-04-12 13:47:50 +0200106 }
Akron7dd32822018-10-01 19:38:31 +0200107
Akronbd342982018-01-25 18:01:46 +0100108 metaDL.appendChild(metaL);
Akron24866cf2018-01-23 20:22:01 +0100109 };
Akron678c26f2020-10-09 08:52:50 +0200110 });
Akron7dd32822018-10-01 19:38:31 +0200111
Akronabd6e352018-11-19 15:36:42 +0100112 // Add corpusByMatch assistant
Akron24aa0052020-11-10 11:00:34 +0100113 this._corpusByMatch = cbmClass.create(this._el);
Akronabd6e352018-11-19 15:36:42 +0100114
Akron24aa0052020-11-10 11:00:34 +0100115 return this._el;
Akronaeceda72018-02-02 20:44:06 +0100116 }
Akron24866cf2018-01-23 20:22:01 +0100117 };
Akron30ee5142015-06-26 01:50:14 +0200118});