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