Add reference support in Wikibase schema editor

This commit is contained in:
Antonin Delpeuch 2017-09-08 17:18:22 +01:00
parent 68122b2b27
commit 23355eb472
4 changed files with 158 additions and 10 deletions

View File

@ -211,6 +211,10 @@ SchemaAlignmentDialog._createDialog = function() {
this.preview(); this.preview();
}; };
/**************/
/*** ITEMS ****/
/**************/
SchemaAlignmentDialog._addItem = function(json) { SchemaAlignmentDialog._addItem = function(json) {
var subject = null; var subject = null;
var statementGroups = null; var statementGroups = null;
@ -249,6 +253,10 @@ SchemaAlignmentDialog._itemToJSON = function (item) {
statementGroups: lst}; statementGroups: lst};
}; };
/********************
* STATEMENT GROUPS *
********************/
SchemaAlignmentDialog._addStatementGroup = function(item, json) { SchemaAlignmentDialog._addStatementGroup = function(item, json) {
var property = null; var property = null;
var statements = null; var statements = null;
@ -290,12 +298,17 @@ SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
statements: lst}; statements: lst};
}; };
/**************
* STATEMENTS *
**************/
SchemaAlignmentDialog._addStatement = function(container, datatype, json) { SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
var qualifiers = null; var qualifiers = null;
var references = null;
var value = null; var value = null;
if (json) { if (json) {
qualifiers = json.qualifiers; qualifiers = json.qualifiers;
references = json.references;
value = json.value; value = json.value;
} }
@ -306,10 +319,12 @@ SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
}).appendTo(toolbar1); }).appendTo(toolbar1);
var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement); var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
SchemaAlignmentDialog._initField(inputContainer, datatype, value); SchemaAlignmentDialog._initField(inputContainer, datatype, value);
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
// If we are in a mainsnak, add qualifiers and references // If we are in a mainsnak...
if (container.parents('.wbs-statement').length == 0) { if (container.parents('.wbs-statement').length == 0) {
// add qualifiers...
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
var qualifierContainer = $('<div></div>').addClass('wbs-qualifier-container').appendTo(right); var qualifierContainer = $('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right); var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-qualifier').text('add qualifier').click(function() { $('<a></a>').addClass('wbs-add-qualifier').text('add qualifier').click(function() {
@ -320,6 +335,27 @@ SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
SchemaAlignmentDialog._addQualifier(qualifierContainer, qualifiers[i]); SchemaAlignmentDialog._addQualifier(qualifierContainer, qualifiers[i]);
} }
} }
// and references
var referencesToggleContainer = $('<div></div>').addClass('wbs-references-toggle').appendTo(statement);
var referencesToggle = $('<a></a>').appendTo(referencesToggleContainer);
right = $('<div></div>').addClass('wbs-right').appendTo(statement);
var referenceContainer = $('<div></div>').addClass('wbs-reference-container').appendTo(right);
referencesToggle.click(function () {
referenceContainer.toggle(100);
});
var right2 = $('<div></div>').addClass('wbs-right').appendTo(right);
var toolbar3 = $('<div></div>').addClass('wbs-toolbar').appendTo(right2);
$('<a></a>').addClass('wbs-add-reference').text('add reference').click(function() {
SchemaAlignmentDialog._addReference(referenceContainer, null);
SchemaAlignmentDialog._updateReferencesNumber(referenceContainer);
}).appendTo(toolbar3);
if (references) {
for (var i = 0; i != references.length; i++) {
SchemaAlignmentDialog._addReference(referenceContainer, references[i]);
}
}
SchemaAlignmentDialog._updateReferencesNumber(referenceContainer);
} }
container.append(statement); container.append(statement);
} }
@ -327,16 +363,24 @@ SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
SchemaAlignmentDialog._statementToJSON = function (statement) { SchemaAlignmentDialog._statementToJSON = function (statement) {
var inputContainer = statement.find(".wbs-target-input").first(); var inputContainer = statement.find(".wbs-target-input").first();
var qualifiersList = new Array(); var qualifiersList = new Array();
statement.find('.wbs-qualifier').each(function () { var referencesList = new Array();
statement.find('.wbs-qualifier-container').first().children().each(function () {
qualifiersList.push(SchemaAlignmentDialog._qualifierToJSON($(this))); qualifiersList.push(SchemaAlignmentDialog._qualifierToJSON($(this)));
}); });
statement.find('.wbs-reference-container').first().children().each(function () {
referencesList.push(SchemaAlignmentDialog._referenceToJSON($(this)));
});
return { return {
value:SchemaAlignmentDialog._inputContainerToJSON(inputContainer), value:SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
qualifiers: qualifiersList, qualifiers: qualifiersList,
references:[], references: referencesList,
}; };
}; };
/**************
* QUALIFIERS *
**************/
SchemaAlignmentDialog._addQualifier = function(container, json) { SchemaAlignmentDialog._addQualifier = function(container, json) {
var property = null; var property = null;
var value = null; var value = null;
@ -351,7 +395,6 @@ SchemaAlignmentDialog._addQualifier = function(container, json) {
var right = $('<div></div>').addClass('wbs-right').appendTo(qualifier); var right = $('<div></div>').addClass('wbs-right').appendTo(qualifier);
var statementContainer = $('<div></div>').addClass('wbs-statement-container').appendTo(right); var statementContainer = $('<div></div>').addClass('wbs-statement-container').appendTo(right);
SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property); SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property);
console.log(json);
if (value && property) { if (value && property) {
SchemaAlignmentDialog._addStatement(statementContainer, property.datatype, {value:value}); SchemaAlignmentDialog._addStatement(statementContainer, property.datatype, {value:value});
} }
@ -370,6 +413,61 @@ SchemaAlignmentDialog._qualifierToJSON = function(elem) {
}; };
} }
/**************
* REFERENCES *
**************/
SchemaAlignmentDialog._addReference = function(container, json) {
var snaks = null;
if (json) {
snaks = json.snaks;
}
var reference = $('<div></div>').addClass('wbs-reference').appendTo(container);
var referenceHeader = $('<div></div>').addClass('wbs-reference-header').appendTo(reference);
var toolbarRef = $('<div></div>').addClass('wbs-toolbar').appendTo(referenceHeader);
$('<img src="images/close.png" />').attr('alt', 'remove reference').click(function() {
reference.remove();
SchemaAlignmentDialog._updateReferencesNumber(container);
}).appendTo(toolbarRef);
var right = $('<div></div>').addClass('wbs-right').appendTo(reference);
var qualifierContainer = $('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-qualifier').text('add').click(function() {
SchemaAlignmentDialog._addQualifier(qualifierContainer, null);
}).appendTo(toolbar2);
if (snaks) {
for (var i = 0; i != snaks.length; i++) {
SchemaAlignmentDialog._addQualifier(qualifierContainer, snaks[i]);
}
} else {
SchemaAlignmentDialog._addQualifier(qualifierContainer, null);
}
}
SchemaAlignmentDialog._referenceToJSON = function(reference) {
var snaks = reference.find('.wbs-qualifier-container').first().children();
var snaksList = new Array();
snaks.each(function () {
snaksList.push(SchemaAlignmentDialog._qualifierToJSON($(this)));
});
return {snaks:snaksList};
}
SchemaAlignmentDialog._updateReferencesNumber = function(container) {
var childrenCount = container.children().length;
var statement = container.parents('.wbs-statement');
console.log(statement);
var a = statement.find('.wbs-references-toggle a').first();
console.log(a);
a.html(childrenCount+'&nbsp;references');
}
/************************
* FIELD INITIALIZATION *
************************/
SchemaAlignmentDialog._getPropertyType = function(pid, callback) { SchemaAlignmentDialog._getPropertyType = function(pid, callback) {
$.ajax({ $.ajax({
url:'https://www.wikidata.org/w/api.php', url:'https://www.wikidata.org/w/api.php',

View File

@ -82,7 +82,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
width: 120px; width: 120px;
} }
.wbs-qualifier .wbs-prop-input, .wbs-reference .wbs-prop-input { .wbs-qualifier .wbs-prop-input {
width: 85px; width: 85px;
} }
@ -100,18 +100,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
padding: 2px; padding: 2px;
} }
.wbs-add-item, .wbs-add-statement-group, .wbs-add-statement, .wbs-add-qualifier { .wbs-add-item, .wbs-add-statement-group, .wbs-add-statement, .wbs-add-qualifier, .wbs-add-reference {
color: #0645ad !important; color: #0645ad !important;
font-size: 0.9em; font-size: 0.9em;
} }
.wbs-statement-group-container, .wbs-statement-container, .wbs-qualifier-container { .wbs-statement-group-container, .wbs-statement-container, .wbs-qualifier-container, .wbs-reference-container {
width: 100%; width: 100%;
display: block; display: block;
overflow: auto; overflow: auto;
} }
.wbs-item, .wbs-statement-group, .wbs-statement, .wbs-qualifier { .wbs-item, .wbs-statement-group, .wbs-statement, .wbs-qualifier, .wbs-reference {
display: block; display: block;
overflow: auto; overflow: auto;
} }
@ -142,13 +142,26 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
position: relative; position: relative;
} }
.wbs-qualifier .wbs-right, .wbs-reference .wbs-right { .wbs-qualifier .wbs-right {
width: 70%; width: 70%;
position: absolute; position: absolute;
top: 0px; top: 0px;
margin-left: 100px; margin-left: 100px;
} }
.wbs-reference > .wbs-right {
width: 100%;
}
.wbs-references-toggle {
width: 12em;
margin: 5px;
}
.wbs-references-toggle a {
color: #0645ad !important;
}
.wbs-qualifier .wbs-target-input { .wbs-qualifier .wbs-target-input {
width: 90px; width: 90px;
} }
@ -157,6 +170,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
overflow: hidden; overflow: hidden;
} }
.wbs-reference-header {
height: 20px;
overflow: auto;
display: block;
background-color: #b6c8ec;
}
.wbs-reference {
background-color: #eaf3ff;
margin-bottom: 5px;
}
.wbs-reference .wbs-statement {
background-color: #eaf3ff;
}
.schema-alignment-columns-header { .schema-alignment-columns-header {
margin-bottom: 0.3em; margin-bottom: 0.3em;
} }

View File

@ -20,6 +20,7 @@ import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue; import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
@ -89,6 +90,12 @@ public class QuickStatementsExporter implements WriterExporter {
for(SnakGroup q : claim.getQualifiers()) { for(SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer); translateSnakGroup(q, false, writer);
} }
for(Reference r : statement.getReferences()) {
for(SnakGroup g : r.getSnakGroups()) {
translateSnakGroup(g, true, writer);
}
break; // QS only supports one reference
}
writer.write("\n"); writer.write("\n");
} }
} }

View File

@ -50,6 +50,12 @@ public class WbStatementExpr extends BiJsonizable {
expr.write(writer, options); expr.write(writer, options);
} }
writer.endArray(); writer.endArray();
writer.key("references");
writer.array();
for (WbReferenceExpr expr : referenceExprs) {
expr.write(writer, options);
}
writer.endArray();
} }
public static WbStatementExpr fromJSON(JSONObject obj) throws JSONException { public static WbStatementExpr fromJSON(JSONObject obj) throws JSONException {
@ -95,13 +101,21 @@ public class WbStatementExpr extends BiJsonizable {
public Statement evaluate(ExpressionContext ctxt, ItemIdValue subject, PropertyIdValue propertyId) throws SkipStatementException { public Statement evaluate(ExpressionContext ctxt, ItemIdValue subject, PropertyIdValue propertyId) throws SkipStatementException {
Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt); Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt);
Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue); Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
// evaluate qualifiers
List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size()); List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size());
for (WbSnakExpr qExpr : qualifierExprs) { for (WbSnakExpr qExpr : qualifierExprs) {
qualifiers.add(qExpr.evaluate(ctxt)); qualifiers.add(qExpr.evaluate(ctxt));
} }
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers); List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
Claim claim = Datamodel.makeClaim(subject, mainSnak, groupedQualifiers); Claim claim = Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
// evaluate references
List<Reference> references = new ArrayList<Reference>(); List<Reference> references = new ArrayList<Reference>();
for (WbReferenceExpr rExpr : referenceExprs) {
references.add(rExpr.evaluate(ctxt));
}
StatementRank rank = StatementRank.NORMAL; StatementRank rank = StatementRank.NORMAL;
return Datamodel.makeStatement(claim, references, rank, ""); return Datamodel.makeStatement(claim, references, rank, "");
} }