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). |
| 26 | upgradeTo(this); |
| 27 | |
| 28 | if (parent !== undefined) |
| 29 | obj._parent = parent; |
| 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) |
| 41 | return null; |
| 42 | |
| 43 | // Set JSON-LD type |
| 44 | var newDoc = docClass.create(this._parent, { |
| 45 | "@type" : "koral:doc", |
| 46 | "value" : "", |
| 47 | "key" : v |
| 48 | }); |
| 49 | |
| 50 | // Unspecified document on root |
| 51 | if (this._parent.ldType() === null) { |
| 52 | this._parent.root(newDoc); |
| 53 | this.destroy(); |
| 54 | } |
| 55 | |
| 56 | // Unspecified document in group |
| 57 | else { |
| 58 | this._parent.replaceOperand(this, newDoc); |
| 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) |
| 71 | return this.element(); |
| 72 | |
| 73 | // Remove element content |
| 74 | _removeChildren(this._element); |
| 75 | |
| 76 | var ellipsis = document.createElement('span'); |
| 77 | ellipsis.appendChild(document.createTextNode(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) { |
| 89 | var op = this.operators( |
| 90 | false, |
| 91 | false, |
| 92 | true |
| 93 | ); |
| 94 | |
| 95 | this._element.appendChild( |
| 96 | op.element() |
| 97 | ); |
| 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) |
| 109 | return this._element; |
| 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; |
| 123 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame^] | 124 | // Add key menu element at the correct position |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 125 | this._element.insertBefore( |
| 126 | menu.element(), |
| 127 | this._element.firstChild |
| 128 | ); |
| 129 | |
| 130 | var that = this; |
| 131 | |
| 132 | // Set released method |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame^] | 133 | menu.released(function (key, type) { |
| 134 | // Set chosen key and type - will return a doc |
| 135 | that.key(key).type(type).update(); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 136 | this.hide(); |
| 137 | }); |
| 138 | |
| 139 | menu.show(); |
| 140 | menu.focus(); |
| 141 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 142 | }; |
| 143 | }); |