Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 2 | * An unspecified criterion in a virtual collection. |
| 3 | * Inherits everything from jsonld |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 4 | */ |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 5 | define([ |
| 6 | 'vc/jsonld', |
| 7 | 'vc/doc', |
| 8 | 'util' |
| 9 | ], function (jsonldClass, docClass) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 10 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 11 | // Localize empty string |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 12 | var loc = KorAP.Locale; |
| 13 | loc.EMPTY = loc.EMPTY || '⋯'; |
| 14 | |
| 15 | return { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 16 | |
| 17 | // The ld-type |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | _ldType : "non", |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Create new unspecified criterion |
| 22 | * with a link to the parent object |
| 23 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | create : function (parent) { |
| 25 | var obj = Object.create(jsonldClass). |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 26 | upgradeTo(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 27 | |
| 28 | if (parent !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 29 | obj._parent = parent; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | |
| 31 | return obj; |
| 32 | }, |
| 33 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Set the key; this will spawn a new doc |
| 36 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 37 | key : function (v) { |
| 38 | |
| 39 | // Not replaceable |
| 40 | if (this._parent === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 41 | return null; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 42 | |
| 43 | // Set JSON-LD type |
| 44 | var newDoc = docClass.create(this._parent, { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 45 | "@type" : "koral:doc", |
| 46 | "value" : "", |
| 47 | "key" : v |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | }); |
| 49 | |
| 50 | // Unspecified document on root |
| 51 | if (this._parent.ldType() === null) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 52 | this._parent.root(newDoc); |
| 53 | this.destroy(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Unspecified document in group |
| 57 | else { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 58 | this._parent.replaceOperand(this, newDoc); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 59 | }; |
| 60 | this._parent.update(); |
| 61 | return newDoc; |
| 62 | }, |
| 63 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Update the element |
| 67 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 68 | update : function () { |
| 69 | |
| 70 | if (this._element === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 71 | return this.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 72 | |
| 73 | // Remove element content |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 74 | _removeChildren(this._element); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 75 | |
| 76 | var ellipsis = document.createElement('span'); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 77 | ellipsis.addT(loc.EMPTY); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 78 | |
| 79 | // Click on empty criterion |
| 80 | ellipsis.addEventListener('click', this.onclick.bind(this)); |
| 81 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 82 | this._element.appendChild(ellipsis); |
| 83 | |
| 84 | // Set ref - TODO: Cleanup! |
| 85 | this._element.refTo = this; |
| 86 | |
| 87 | // Set operators |
| 88 | if (this._parent !== undefined && this.parent().ldType() !== null) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 89 | var op = this.operators( |
| 90 | false, |
| 91 | false, |
| 92 | true |
| 93 | ); |
| 94 | |
| 95 | this._element.appendChild( |
| 96 | op.element() |
| 97 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | return this.element(); |
| 101 | }, |
| 102 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Get the associated element |
| 106 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 107 | element : function () { |
| 108 | if (this._element !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 109 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 110 | this._element = document.createElement('div'); |
| 111 | this._element.setAttribute('class', 'doc unspecified'); |
| 112 | this.update(); |
| 113 | return this._element; |
| 114 | }, |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Click on the unspecified object |
| 118 | */ |
| 119 | onclick : function () { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 120 | |
| 121 | // Get the key menu |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 122 | var menu = KorAP._vcKeyMenu; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 123 | // Add key menu element at the correct position |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 124 | this._element.insertBefore( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 125 | menu.element(), |
| 126 | this._element.firstChild |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 127 | ); |
| 128 | |
| 129 | var that = this; |
| 130 | |
| 131 | // Set released method |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 132 | menu.released(function (key, type) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 133 | // Set chosen key and type - will return a doc |
| 134 | that.key(key).type(type).update(); |
| 135 | this.hide(); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 136 | }); |
| 137 | |
| 138 | menu.show(); |
| 139 | menu.focus(); |
| 140 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 141 | }; |
| 142 | }); |