RandomSec/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js

552 lines
18 KiB
JavaScript
Raw Normal View History

/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var SchemaAlignment = {};
SchemaAlignment.autoAlign = function() {
var protograph = {};
var columns = theProject.columnModel.columns;
var typedCandidates = [];
var candidates = [];
for (var c = 0; c < columns.length; c++) {
var column = columns[c];
var typed = (column.reconConfig) &&
ReconciliationManager.isFreebaseIdOrMid(column.reconConfig.identifierSpace) &&
ReconciliationManager.isFreebaseId(column.reconConfig.schemaSpace);
var candidate = {
status: "unbound",
typed: typed,
index: c,
column: column
};
candidates.push(candidate);
if (typed) {
typedCandidates.push(candidate);
}
}
if (typedCandidates.length > 0) {
} else {
var queries = {};
for (var i = 0; i < candidates.length; i++) {
var candidate = candidates[i];
var name = SchemaAlignment._cleanName(candidate.column.name);
var key = "t" + i + ":search";
queries[key] = {
"query" : name,
"limit" : 10,
"type" : "/type/type,/type/property",
"type_strict" : "any"
};
}
SchemaAlignment._batchSearch(queries, function(result) {
console.log(result);
});
}
};
SchemaAlignment._cleanName = function(s) {
return s.replace(/\W/g, " ").replace(/\s+/g, " ").toLowerCase();
};
var SchemaAlignmentDialog = {};
SchemaAlignmentDialog.launch = function(onDone) {
this._onDone = onDone;
this._hasUnsavedChanges = false;
this._createDialog();
this._reset(theProject.overlayModels.wikibaseSchema, true);
}
SchemaAlignmentDialog._reset = function(schema, initial) {
2017-09-06 12:58:30 +02:00
this._originalSchema = schema || { itemDocuments: [] };
this._schema = cloneDeep(this._originalSchema); // this is what can be munched on
$('#schema-alignment-statements-container').empty();
2017-09-06 12:58:30 +02:00
if (this._schema && this._schema.itemDocuments) {
for(var i = 0; i != this._schema.itemDocuments.length; i++) {
this._addItem(this._schema.itemDocuments[i]);
2017-09-04 10:40:47 +02:00
}
}
2017-09-06 12:58:30 +02:00
if (!this._schema.itemDocuments.length) {
// this._addItem();
}
2017-09-04 22:00:58 +02:00
this.preview(initial);
};
SchemaAlignmentDialog._save = function(onDone) {
var self = this;
2017-08-31 10:11:00 +02:00
var schema = this.getJSON();
Refine.postProcess(
2017-08-31 10:11:00 +02:00
"wikidata",
"save-wikibase-schema",
{},
2017-08-31 10:11:00 +02:00
{ schema: JSON.stringify(schema) },
{},
{
onDone: function() {
2017-08-31 10:11:00 +02:00
theProject.overlayModels.wikibaseSchema = schema;
self._elmts.statusIndicator.hide();
2017-09-04 22:00:58 +02:00
$('.invalid-schema-warning').hide();
self._hasUnsavedChanges = false;
if (onDone) onDone();
}
}
);
};
2017-09-04 10:40:47 +02:00
SchemaAlignmentDialog._createDraggableColumn = function(name, reconciled) {
var cell = $("<div></div>").addClass('wbs-draggable-column').text(name);
if (reconciled) {
cell.addClass('wbs-reconciled-column');
} else {
cell.addClass('wbs-unreconciled-column');
}
return cell;
}
SchemaAlignmentDialog._createDialog = function() {
var self = this;
var frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/schema-alignment-dialog.html"));
var elmts = this._elmts = DOM.bind(frame);
this._level = DialogSystem.showDialog(frame);
2017-08-31 10:11:00 +02:00
this._wikibasePrefix = "http://www.wikidata.org/entity/"; // hardcoded for now
// Init the column area
var columns = theProject.columnModel.columns;
this._columnArea = $(".schema-alignment-dialog-columns-area");
for (var i = 0; i < columns.length; i++) {
2017-08-31 10:11:00 +02:00
var column = columns[i];
var reconConfig = column.reconConfig;
2017-09-04 10:40:47 +02:00
var cell = SchemaAlignmentDialog._createDraggableColumn(column.name,
reconConfig && reconConfig.identifierSpace === this._wikibasePrefix && column.reconStats);
this._columnArea.append(cell);
}
2017-08-31 10:11:00 +02:00
$('.wbs-reconciled-column').draggable({
helper: "clone",
cursor: "crosshair",
snap: ".wbs-item-input input, .wbs-target-input input",
});
$('.wbs-unreconciled-column').draggable({
helper: "clone",
2017-08-31 10:11:00 +02:00
cursor: "crosshair",
snap: ".wbs-target-input input",
});
2017-08-31 10:11:00 +02:00
var dismiss = function() {
DialogSystem.dismissUntil(self._level - 1);
};
elmts.saveButton.click(function() {
self._save();
});
elmts.resetButton.click(function() {
self._reset(null);
});
elmts.closeButton.click(function() {
if (!self._hasUnsavedChanges || window.confirm("There are unsaved changes. Close anyway?")) {
dismiss();
}
});
elmts.addItemButton.click(function() {
self._addItem();
});
$("#schema-alignment-tabs").tabs();
this._previewPanes = $(".schema-alignment-dialog-preview");
this._canvas = $(".schema-alignment-dialog-canvas");
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
2017-08-31 10:11:00 +02:00
var url = ReconciliationManager.ensureDefaultServicePresent();
SchemaAlignmentDialog._reconService = ReconciliationManager.getServiceFromUrl(url);
2017-09-04 22:00:58 +02:00
this.preview();
};
2017-09-04 10:40:47 +02:00
SchemaAlignmentDialog._addItem = function(json) {
var subject = null;
var statementGroups = null;
if (json) {
subject = json.subject;
statementGroups = json.statementGroups;
}
var item = $('<div></div>').addClass('wbs-item');
2017-08-31 10:11:00 +02:00
var inputContainer = $('<div></div>').addClass('wbs-item-input').appendTo(item);
SchemaAlignmentDialog._initField(inputContainer, "wikibase-item", subject);
var right = $('<div></div>').addClass('wbs-right').appendTo(item);
$('<div></div>').addClass('wbs-statement-group-container').appendTo(right);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-statement-group').text('add statement').click(function() {
2017-09-04 10:40:47 +02:00
SchemaAlignmentDialog._addStatementGroup(item, null);
}).appendTo(toolbar);
2017-08-31 10:11:00 +02:00
2017-09-04 10:40:47 +02:00
if (statementGroups) {
for(var i = 0; i != statementGroups.length; i++) {
SchemaAlignmentDialog._addStatementGroup(item, statementGroups[i]);
}
} else {
SchemaAlignmentDialog._addStatementGroup(item);
}
$('#schema-alignment-statements-container').append(item);
}
2017-08-31 10:11:00 +02:00
SchemaAlignmentDialog._itemToJSON = function (item) {
var lst = new Array();
item.find('.wbs-statement-group').each(function () {
lst.push(SchemaAlignmentDialog._statementGroupToJSON($(this)));
});
var inputContainer = item.find(".wbs-item-input").first();
return {subject: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
statementGroups: lst};
2017-08-31 10:11:00 +02:00
};
2017-09-04 10:40:47 +02:00
SchemaAlignmentDialog._addStatementGroup = function(item, json) {
var property = null;
var statements = null;
if (json) {
property = json.property;
statements = json.statements;
}
var container = item.find('.wbs-statement-group-container').first();
var statementGroup = $('<div></div>').addClass('wbs-statement-group');
2017-08-31 10:11:00 +02:00
var inputContainer = $('<div></div>').addClass('wbs-prop-input').appendTo(statementGroup);
var right = $('<div></div>').addClass('wbs-right').appendTo(statementGroup);
var statementContainer = $('<div></div>').addClass('wbs-statement-container').appendTo(right);
SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
var addValueButton = $('<a></a>').addClass('wbs-add-statement').text('add value').click(function() {
var datatype = inputContainer.data("jsonValue").datatype;
SchemaAlignmentDialog._addStatement(statementContainer, datatype, null);
}).appendTo(toolbar).hide();
container.append(statementGroup);
2017-09-04 10:40:47 +02:00
if (statements) {
for (var i = 0; i != statements.length; i++) {
SchemaAlignmentDialog._addStatement(statementContainer, property.datatype, statements[i]);
addValueButton.show();
2017-09-04 10:40:47 +02:00
}
}
}
2017-08-31 10:11:00 +02:00
SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
var lst = new Array();
statementGroup.find('.wbs-statement').each(function () {
lst.push(SchemaAlignmentDialog._statementToJSON($(this)));
});
var inputContainer = statementGroup.find(".wbs-prop-input").first();
return {property: SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
statements: lst};
2017-08-31 10:11:00 +02:00
};
SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
console.log('addStatement');
console.log(datatype);
2017-09-04 10:40:47 +02:00
var qualifiers = null;
var value = null;
if (json) {
qualifiers = json.qualifiers;
value = json.value;
}
var statement = $('<div></div>').addClass('wbs-statement');
var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(statement);
2017-08-31 10:11:00 +02:00
$('<img src="images/close.png" />').attr('alt', 'remove statement').click(function() {
SchemaAlignmentDialog._removeStatement(statement);
}).appendTo(toolbar1);
2017-08-31 10:11:00 +02:00
var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
SchemaAlignmentDialog._initField(inputContainer, datatype, value);
var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
$('<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 qualifier').appendTo(toolbar2);
container.append(statement);
}
2017-08-31 10:11:00 +02:00
SchemaAlignmentDialog._statementToJSON = function (statement) {
var inputContainer = statement.find(".wbs-target-input").first();
return {
value:SchemaAlignmentDialog._inputContainerToJSON(inputContainer),
qualifiers:[],
2017-09-04 10:40:47 +02:00
references:[],
};
2017-08-31 10:11:00 +02:00
};
SchemaAlignmentDialog._getPropertyType = function(pid, callback) {
$.ajax({
url:'https://www.wikidata.org/w/api.php',
data: {
action: "wbgetentities",
format: "json",
ids: pid,
props: "datatype",
},
dataType: "jsonp",
success: function(data) {
callback(data.entities[pid].datatype);
}});
}
SchemaAlignmentDialog._initPropertyField = function(inputContainer, targetContainer, initialValue) {
2017-08-31 10:11:00 +02:00
var input = $('<input></input>').appendTo(inputContainer);
if (this._reconService !== null) {
endpoint = this._reconService.suggest.property;
var suggestConfig = $.extend({}, endpoint);
suggestConfig.key = null;
suggestConfig.query_param_name = "prefix";
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
// Fetch the type of this property and add the appropriate target value type
var statementGroup = inputContainer.parents(".wbs-statement-group").first();
SchemaAlignmentDialog._getPropertyType(data.id, function(datatype) {
inputContainer.data("jsonValue", {
type : "wbpropconstant",
pid : data.id,
label: data.name,
datatype: datatype,
});
SchemaAlignmentDialog._addStatement(targetContainer, datatype, null);
var addValueButtons = targetContainer.parent().find('.wbs-add-statement');
addValueButtons.show();
});
SchemaAlignmentDialog._hasChanged();
}).bind("fb-textchange", function(evt, data) {
inputContainer.data("jsonValue", null);
targetContainer.find('.wbs-statement').remove();
var addValueButtons = targetContainer.parent().find('.wbs-add-statement');
addValueButtons.hide();
});
}
// Init with the provided initial value.
if (initialValue) {
if (initialValue.type === "wbpropconstant") {
input.val(initialValue.label);
}
inputContainer.data("jsonValue", initialValue);
}
}
SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) {
var input = $('<input></input>').appendTo(inputContainer);
if (this._reconService !== null && mode === "wikibase-item") {
2017-08-31 10:11:00 +02:00
var endpoint = null;
endpoint = this._reconService.suggest.entity;
2017-08-31 10:11:00 +02:00
var suggestConfig = $.extend({}, endpoint);
suggestConfig.key = null;
suggestConfig.query_param_name = "prefix";
2017-08-31 10:11:00 +02:00
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
inputContainer.data("jsonValue", {
type : "wbitemconstant",
qid : data.id,
label: data.name,
});
2017-09-04 22:00:58 +02:00
SchemaAlignmentDialog._hasChanged();
2017-08-31 10:11:00 +02:00
});
} else if (mode === "external-id") {
var propagateValue = function(val) {
inputContainer.data("jsonValue", {
type: "wbstringconstant",
value: val,
});
};
propagateValue("");
input.change(function() {
propagateValue($(this).val());
SchemaAlignmentDialog._hasChanged();
});
2017-08-31 10:11:00 +02:00
}
2017-09-04 10:40:47 +02:00
var acceptDraggableColumn = function(column) {
input.hide();
var columnDiv = $('<div></div>').appendTo(inputContainer);
column.appendTo(columnDiv);
var deleteButton = $('&nbsp;<img src="images/close.png" />').addClass('wbs-delete-column-button').appendTo(column);
deleteButton.attr('alt', 'remove column');
deleteButton.click(function () {
columnDiv.remove();
input.show();
2017-09-04 22:00:58 +02:00
SchemaAlignmentDialog._hasChanged();
2017-09-04 10:40:47 +02:00
});
};
// Make it droppable
var acceptClass = ".wbs-draggable-column";
var wbVariableType = "wbstringvariable";
if (mode === "wikibase-item") {
acceptClass = ".wbs-reconciled-column";
wbVariableType = "wbitemvariable";
2017-08-31 10:11:00 +02:00
}
2017-09-04 10:40:47 +02:00
inputContainer.droppable({
accept: acceptClass,
}).on("drop", function (evt, ui) {
var column = ui.draggable.clone();
acceptDraggableColumn(column);
inputContainer.data("jsonValue", {
type : wbVariableType,
columnName: ui.draggable.text(),
});
SchemaAlignmentDialog._hasChanged();
return true;
}).on("dropactivate", function(evt, ui) {
input.addClass("wbs-accepting-input");
}).on("dropdeactivate", function(evt, ui) {
input.removeClass("wbs-accepting-input");
});
2017-09-04 10:40:47 +02:00
// Init with the provided initial value.
if (initialValue) {
if (initialValue.type === "wbitemconstant") {
2017-09-04 10:40:47 +02:00
input.val(initialValue.label);
} else if (initialValue.type == "wbitemvariable") {
var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, true);
acceptDraggableColumn(cell);
} else if (initialValue.type == "wbstringconstant") {
input.val(initialValue.value);
} else if (initialValue.type == "wbstringvariable") {
var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, false);
acceptDraggableColumn(cell);
2017-09-04 10:40:47 +02:00
}
inputContainer.data("jsonValue", initialValue);
}
}
2017-08-31 10:11:00 +02:00
SchemaAlignmentDialog._inputContainerToJSON = function (inputContainer) {
return inputContainer.data().jsonValue;
};
SchemaAlignmentDialog._removeStatement = function(statement) {
var statementGroup = statement.parents('.wbs-statement-group').first();
statement.remove();
var remainingStatements = statementGroup.find('.wbs-statement').length;
if (remainingStatements === 0) {
statementGroup.remove();
}
2017-09-04 22:00:58 +02:00
SchemaAlignmentDialog._hasChanged();
}
/*
SchemaAlignmentDialog._addStatement = function() {
var newStatement = $('<div></div>').addClass('schema-alignment-statement');
var subject = $('<div></div>').addClass('schema-alignment-subject').appendTo(newStatement);
var prop = $('<div></div>').addClass('schema-alignment-prop').appendTo(newStatement);
var target = $('<div></div>').addClass('schema-alignment-target').appendTo(newStatement);
var qualifiersArea = $('<div></div>').addClass('schema-alignment-qualifiers').appendTo(newStatement);
var addQualifier = $('<p></p>').addClass('schema-alignment-add-qualifier').text('Add qualifier').appendTo(newStatement);
$('#schema-alignment-statements-container').append(newStatement);
}
*/
SchemaAlignmentDialog.getJSON = function() {
2017-08-31 10:11:00 +02:00
var list = new Array();
$('.wbs-item').each(function () {
list.push(SchemaAlignmentDialog._itemToJSON($(this)));
});
return {
2017-09-06 12:58:30 +02:00
'itemDocuments': list,
2017-08-31 10:11:00 +02:00
'wikibasePrefix': this._wikibasePrefix,
};
};
2017-09-04 22:00:58 +02:00
SchemaAlignmentDialog._hasChanged = function() {
this._hasUnsavedChanges = true;
SchemaAlignmentDialog.preview(false);
}
SchemaAlignmentDialog.preview = function(initial) {
var self = this;
2017-09-04 22:00:58 +02:00
$('.invalid-schema-warning').hide();
this._previewPanes.empty();
2017-09-04 22:00:58 +02:00
/*
if (!(initial)) {
this._elmts.statusIndicator.show().text("There are unsaved changes.");
this._hasUnsavedChanges = true;
}
2017-09-04 22:00:58 +02:00
*/
2017-09-04 22:00:58 +02:00
var schema = this.getJSON();
$.post(
2017-09-04 22:00:58 +02:00
"command/wikidata/preview-wikibase-schema?" + $.param({ project: theProject.id }),
{ schema: JSON.stringify(schema), engine: JSON.stringify(ui.browsingEngine.getJSON()) },
function(data) {
2017-09-04 22:00:58 +02:00
if ("quickstatements" in data) {
$(self._previewPanes[0]).text(data.quickstatements);
}
2017-09-04 22:00:58 +02:00
if ("code" in data && data.code === "error") {
$('.invalid-schema-warning').show();
}
},
"json"
);
};
SchemaAlignmentDialog._findColumn = function(cellIndex) {
var columns = theProject.columnModel.columns;
2017-09-04 22:00:58 +02:00
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
if (column.cellIndex == cellIndex) {
return column;
}
}
return null;
};