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