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 | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 52 | if (t._el === undefined) |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 53 | return t.element(); |
| 54 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 55 | const e = t._el; |
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 | 0f00b77 | 2020-12-02 17:32:13 +0100 | [diff] [blame^] | 63 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 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 | }; |
Akron | 0f00b77 | 2020-12-02 17:32:13 +0100 | [diff] [blame^] | 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 | }; |
Akron | 0f00b77 | 2020-12-02 17:32:13 +0100 | [diff] [blame^] | 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 | }; |
Akron | 0f00b77 | 2020-12-02 17:32:13 +0100 | [diff] [blame^] | 115 | |
| 116 | if (KorAP.vc !== undefined) { |
| 117 | KorAP.vc.element().dispatchEvent( |
| 118 | new CustomEvent('vcChange', { 'detail' : t }) |
| 119 | ); |
| 120 | }; |
hebasta | 48842cf | 2018-12-11 12:57:38 +0100 | [diff] [blame] | 121 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 122 | return t.element(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 123 | }, |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Get the associated element |
| 128 | */ |
| 129 | element : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 130 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 131 | if (this._el !== undefined) |
| 132 | return this._el; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 133 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 134 | const e = this._el = document.createElement('div'); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 135 | e.setAttribute('class', 'doc groupref'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 136 | this.update(); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 137 | return e; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 138 | }, |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Get or set the value |
| 143 | */ |
| 144 | ref : function (ref) { |
| 145 | if (arguments.length === 1) { |
| 146 | this._ref = ref; |
| 147 | this._changed(); |
| 148 | return this; |
| 149 | }; |
| 150 | return this._ref; |
| 151 | }, |
| 152 | |
| 153 | |
| 154 | // Click on the reference operator, show me the option |
| 155 | _changeRef : function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 156 | const that = this; |
| 157 | const str = stringValClass.create(this.ref(), false, false); |
| 158 | const strElem = str.element(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 159 | |
| 160 | str.store = function (ref, regex) { |
| 161 | that.ref(ref); |
| 162 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 163 | that._el.removeChild( |
| 164 | this._el |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 165 | ); |
| 166 | that.update(); |
| 167 | }; |
| 168 | |
| 169 | // Insert element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 170 | this._el.insertBefore( |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 171 | strElem, |
| 172 | this._refE |
| 173 | ); |
| 174 | |
| 175 | str.focus(); |
| 176 | }, |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Wrap a new operation around the doc element. |
| 181 | * This is copypasta from doc.js |
| 182 | */ |
| 183 | wrap : function (op) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 184 | const parent = this.parent(); |
| 185 | const group = require('vc/docgroup').create(parent); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 186 | group.operation(op); |
| 187 | group.append(this); |
| 188 | group.append(); |
| 189 | return parent.replaceOperand(this, group).update(); |
| 190 | }, |
| 191 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 192 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 193 | /** |
| 194 | * Deserialize from json |
| 195 | */ |
| 196 | fromJson : function (json) { |
| 197 | if (json === undefined) |
| 198 | return this; |
| 199 | |
| 200 | if (json["@type"] === undefined) { |
| 201 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 202 | return; |
| 203 | }; |
| 204 | |
| 205 | if (json["ref"] === undefined || |
| 206 | typeof json["ref"] != 'string') { |
| 207 | KorAP.log(821, "Reference is missing"); |
| 208 | return; |
| 209 | }; |
| 210 | |
| 211 | this.ref(json["ref"]); |
| 212 | |
| 213 | // Rewrite coming from the server |
| 214 | if (json["rewrites"] !== undefined) { |
| 215 | this.rewrite(json["rewrites"]); |
| 216 | }; |
| 217 | |
| 218 | return this; |
| 219 | }, |
| 220 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 221 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 222 | /** |
| 223 | * Click on the unspecified object |
| 224 | */ |
| 225 | onclick : function () { |
| 226 | console.log("Do not support click on this"); |
| 227 | }, |
| 228 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 229 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 230 | // TODO: This is identical to doc.js |
| 231 | rewrites : function () { |
| 232 | return this._rewrites; |
| 233 | }, |
| 234 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 235 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 236 | // TODO: This is identical to doc.js |
| 237 | rewrite : function (value) { |
| 238 | if (typeof value === 'string') { |
| 239 | value = [{ |
| 240 | "@type" : "koral:rewrite", |
| 241 | "operation" : "operation:" + value, |
| 242 | "src" : "Kalamar" |
| 243 | }]; |
| 244 | }; |
| 245 | this._rewrites = rewriteListClass.create(value); |
| 246 | }, |
| 247 | |
| 248 | |
| 249 | // Mark the underlying data as being changed. |
| 250 | // This is important for rerendering the dom. |
| 251 | // This will also remove rewrite markers, when the data |
| 252 | // change happened by the user |
| 253 | _changed : function () { |
| 254 | this.__changed = true; |
| 255 | |
| 256 | if (this._rewrites === undefined) |
| 257 | return; |
| 258 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 259 | delete this["_rewrites"]; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 260 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 261 | if (this._el === undefined) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 262 | return; |
| 263 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 264 | this._el.classList.remove("rewritten"); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 265 | }, |
| 266 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 267 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 268 | toJson : function () { |
| 269 | if (!this.ref) |
| 270 | return {}; |
| 271 | |
| 272 | return { |
| 273 | "@type" : "koral:" + this.ldType(), |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 274 | "ref" : this.ref() |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 275 | }; |
| 276 | }, |
| 277 | |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 278 | |
| 279 | incomplete : function () { |
| 280 | return this.ref() ? false : true |
| 281 | }, |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 282 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 283 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 284 | toQuery : function () { |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 285 | if (this.incomplete()) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 286 | return ""; |
| 287 | |
| 288 | // Build doc string based on key |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 289 | return 'referTo ' + this.ref().quote(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 290 | } |
| 291 | }; |
| 292 | }); |