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