Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * A reference to another VC. |
| 3 | * Inherits everything from jsonld |
| 4 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 5 | "use strict"; |
| 6 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 7 | define([ |
| 8 | 'vc/jsonld', |
| 9 | 'vc/rewritelist', |
| 10 | 'vc/stringval', |
| 11 | 'util' |
| 12 | ], function (jsonldClass, rewriteListClass, stringValClass) { |
| 13 | |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 14 | // TODO: |
| 15 | // Does not support rewrites currently |
| 16 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 17 | const loc = KorAP.Locale; |
| 18 | loc.EMPTY = loc.EMPTY || '⋯'; |
| 19 | |
| 20 | return { |
| 21 | |
| 22 | // The ld-type |
| 23 | _ldType : "docGroupRef", |
| 24 | |
| 25 | /** |
| 26 | * Create new unspecified criterion |
| 27 | * with a link to the parent object |
| 28 | */ |
| 29 | create : function (parent, json) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 30 | const obj = Object(jsonldClass). |
| 31 | create(). |
| 32 | upgradeTo(this). |
| 33 | fromJson(json); |
| 34 | |
| 35 | if (obj === undefined) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 36 | console.log(json); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 37 | |
| 38 | if (parent !== undefined) |
| 39 | obj._parent = parent; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 40 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 41 | obj.__changed = true; |
| 42 | return obj; |
| 43 | }, |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Update the element |
| 48 | */ |
| 49 | update : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 50 | const t = this; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 51 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 52 | if (t._element === undefined) |
| 53 | return t.element(); |
| 54 | |
| 55 | const e = t._element; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 56 | |
| 57 | // Check if there is a change in the underlying data |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 58 | if (!t.__changed) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 59 | return e; |
| 60 | |
| 61 | // Set ref - TODO: Cleanup! |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 62 | e.refTo = t; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 63 | |
| 64 | // Was rewritten |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 65 | if (t.rewrites() !== undefined) { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 66 | e.classList.add("rewritten"); |
| 67 | }; |
| 68 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 69 | const refTitle = document.createElement('span'); |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 70 | refTitle.classList.add('key','fixed', 'ref'); |
| 71 | refTitle.addT('referTo'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 72 | |
| 73 | // Added value operator |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 74 | const refE = t._refE = document.createElement('span'); |
| 75 | refE.setAttribute('data-type', "string"); |
| 76 | refE.setAttribute('class', 'value'); |
| 77 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 78 | if (this.ref()) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 79 | refE.addT(t.ref()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 80 | } |
| 81 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 82 | refE.addT(loc.EMPTY); |
| 83 | refE.classList.add('unspecified'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // Change value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 87 | refE.addEventListener( |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 88 | 'click', |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 89 | t._changeRef.bind(t) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 90 | ); |
| 91 | |
| 92 | // Remove all element children |
| 93 | _removeChildren(e); |
| 94 | |
| 95 | // Add spans |
| 96 | e.appendChild(refTitle); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 97 | e.appendChild(refE); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 98 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 99 | t.__changed = false; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 100 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 101 | if (t._rewrites !== undefined) { |
| 102 | e.appendChild(t._rewrites.element()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 105 | if (t._parent !== undefined) { |
| 106 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 107 | // Set operators |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 108 | // Append new operators |
| 109 | e.appendChild(t.operators( |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 110 | true, |
| 111 | true, |
| 112 | true |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 113 | ).element()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 114 | }; |
hebasta | de595b4 | 2019-02-05 13:53:10 +0100 | [diff] [blame] | 115 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 116 | KorAP.vc.element().dispatchEvent( |
| 117 | new CustomEvent('vcChange', { 'detail' : t }) |
| 118 | ); |
hebasta | 48842cf | 2018-12-11 12:57:38 +0100 | [diff] [blame] | 119 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 120 | return t.element(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 121 | }, |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Get the associated element |
| 126 | */ |
| 127 | element : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 128 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 129 | if (this._element !== undefined) |
| 130 | return this._element; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 131 | |
| 132 | const e = this._element = document.createElement('div'); |
| 133 | e.setAttribute('class', 'doc groupref'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 134 | this.update(); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 135 | return e; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 136 | }, |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Get or set the value |
| 141 | */ |
| 142 | ref : function (ref) { |
| 143 | if (arguments.length === 1) { |
| 144 | this._ref = ref; |
| 145 | this._changed(); |
| 146 | return this; |
| 147 | }; |
| 148 | return this._ref; |
| 149 | }, |
| 150 | |
| 151 | |
| 152 | // Click on the reference operator, show me the option |
| 153 | _changeRef : function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 154 | const that = this; |
| 155 | const str = stringValClass.create(this.ref(), false, false); |
| 156 | const strElem = str.element(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 157 | |
| 158 | str.store = function (ref, regex) { |
| 159 | that.ref(ref); |
| 160 | |
| 161 | that._element.removeChild( |
| 162 | this._element |
| 163 | ); |
| 164 | that.update(); |
| 165 | }; |
| 166 | |
| 167 | // Insert element |
| 168 | this._element.insertBefore( |
| 169 | strElem, |
| 170 | this._refE |
| 171 | ); |
| 172 | |
| 173 | str.focus(); |
| 174 | }, |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Wrap a new operation around the doc element. |
| 179 | * This is copypasta from doc.js |
| 180 | */ |
| 181 | wrap : function (op) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 182 | const parent = this.parent(); |
| 183 | const group = require('vc/docgroup').create(parent); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 184 | group.operation(op); |
| 185 | group.append(this); |
| 186 | group.append(); |
| 187 | return parent.replaceOperand(this, group).update(); |
| 188 | }, |
| 189 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 190 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 191 | /** |
| 192 | * Deserialize from json |
| 193 | */ |
| 194 | fromJson : function (json) { |
| 195 | if (json === undefined) |
| 196 | return this; |
| 197 | |
| 198 | if (json["@type"] === undefined) { |
| 199 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 200 | return; |
| 201 | }; |
| 202 | |
| 203 | if (json["ref"] === undefined || |
| 204 | typeof json["ref"] != 'string') { |
| 205 | KorAP.log(821, "Reference is missing"); |
| 206 | return; |
| 207 | }; |
| 208 | |
| 209 | this.ref(json["ref"]); |
| 210 | |
| 211 | // Rewrite coming from the server |
| 212 | if (json["rewrites"] !== undefined) { |
| 213 | this.rewrite(json["rewrites"]); |
| 214 | }; |
| 215 | |
| 216 | return this; |
| 217 | }, |
| 218 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 219 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 220 | /** |
| 221 | * Click on the unspecified object |
| 222 | */ |
| 223 | onclick : function () { |
| 224 | console.log("Do not support click on this"); |
| 225 | }, |
| 226 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 227 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 228 | // TODO: This is identical to doc.js |
| 229 | rewrites : function () { |
| 230 | return this._rewrites; |
| 231 | }, |
| 232 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 233 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 234 | // TODO: This is identical to doc.js |
| 235 | rewrite : function (value) { |
| 236 | if (typeof value === 'string') { |
| 237 | value = [{ |
| 238 | "@type" : "koral:rewrite", |
| 239 | "operation" : "operation:" + value, |
| 240 | "src" : "Kalamar" |
| 241 | }]; |
| 242 | }; |
| 243 | this._rewrites = rewriteListClass.create(value); |
| 244 | }, |
| 245 | |
| 246 | |
| 247 | // Mark the underlying data as being changed. |
| 248 | // This is important for rerendering the dom. |
| 249 | // This will also remove rewrite markers, when the data |
| 250 | // change happened by the user |
| 251 | _changed : function () { |
| 252 | this.__changed = true; |
| 253 | |
| 254 | if (this._rewrites === undefined) |
| 255 | return; |
| 256 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 257 | delete this["_rewrites"]; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 258 | |
| 259 | if (this._element === undefined) |
| 260 | return; |
| 261 | |
| 262 | this._element.classList.remove("rewritten"); |
| 263 | }, |
| 264 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 265 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 266 | toJson : function () { |
| 267 | if (!this.ref) |
| 268 | return {}; |
| 269 | |
| 270 | return { |
| 271 | "@type" : "koral:" + this.ldType(), |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 272 | "ref" : this.ref() |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 273 | }; |
| 274 | }, |
| 275 | |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 276 | |
| 277 | incomplete : function () { |
| 278 | return this.ref() ? false : true |
| 279 | }, |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 280 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 281 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 282 | toQuery : function () { |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 283 | if (this.incomplete()) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 284 | return ""; |
| 285 | |
| 286 | // Build doc string based on key |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 287 | return 'referTo ' + this.ref().quote(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 288 | } |
| 289 | }; |
| 290 | }); |