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