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 |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 44 | var newDoc = docClass.create(this._parent); |
| 45 | newDoc.key(v); |
| 46 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 47 | // Unspecified document on root |
| 48 | if (this._parent.ldType() === null) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 49 | this._parent.root(newDoc); |
| 50 | this.destroy(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Unspecified document in group |
| 54 | else { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 55 | this._parent.replaceOperand(this, newDoc); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 56 | }; |
| 57 | this._parent.update(); |
| 58 | return newDoc; |
| 59 | }, |
| 60 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Update the element |
| 64 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 65 | update : function () { |
| 66 | |
| 67 | if (this._element === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 68 | return this.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 69 | |
| 70 | // Remove element content |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 71 | _removeChildren(this._element); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 72 | |
| 73 | var ellipsis = document.createElement('span'); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 74 | ellipsis.addT(loc.EMPTY); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 75 | |
| 76 | // Click on empty criterion |
| 77 | ellipsis.addEventListener('click', this.onclick.bind(this)); |
| 78 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 79 | this._element.appendChild(ellipsis); |
| 80 | |
| 81 | // Set ref - TODO: Cleanup! |
| 82 | this._element.refTo = this; |
| 83 | |
| 84 | // Set operators |
| 85 | if (this._parent !== undefined && this.parent().ldType() !== null) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 86 | var op = this.operators( |
| 87 | false, |
| 88 | false, |
| 89 | true |
| 90 | ); |
| 91 | |
| 92 | this._element.appendChild( |
| 93 | op.element() |
| 94 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | return this.element(); |
| 98 | }, |
| 99 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Get the associated element |
| 103 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 104 | element : function () { |
| 105 | if (this._element !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 106 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 107 | this._element = document.createElement('div'); |
| 108 | this._element.setAttribute('class', 'doc unspecified'); |
| 109 | this.update(); |
| 110 | return this._element; |
| 111 | }, |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Click on the unspecified object |
| 115 | */ |
| 116 | onclick : function () { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 117 | |
| 118 | // Get the key menu |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 119 | var menu = KorAP._vcKeyMenu; |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 120 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 121 | // Add key menu element at the correct position |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 122 | this._element.insertBefore( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 123 | menu.element(), |
| 124 | this._element.firstChild |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 125 | ); |
| 126 | |
| 127 | var that = this; |
| 128 | |
| 129 | // Set released method |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 130 | menu.released(function (key, type) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 131 | // Set chosen key and type - will return a doc |
| 132 | that.key(key).type(type).update(); |
| 133 | this.hide(); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 134 | }); |
| 135 | |
| 136 | menu.show(); |
| 137 | menu.focus(); |
| 138 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 139 | }; |
| 140 | }); |