2010-02-16 21:15:19 +01:00
|
|
|
SchemaAlignmentDialog.UILink = function(dialog, link, table, options, parentUINode) {
|
|
|
|
this._dialog = dialog;
|
2010-02-12 21:29:56 +01:00
|
|
|
this._link = link;
|
2010-02-13 01:11:42 +01:00
|
|
|
this._options = options;
|
|
|
|
this._parentUINode = parentUINode;
|
2010-02-12 21:29:56 +01:00
|
|
|
|
2010-05-09 21:35:42 +02:00
|
|
|
// Make sure target node is there
|
|
|
|
this._link.target = this._link.target || { nodeType: "cell-as-value" }
|
|
|
|
|
2010-02-12 21:29:56 +01:00
|
|
|
this._tr = table.insertRow(table.rows.length);
|
|
|
|
this._tdMain = this._tr.insertCell(0);
|
|
|
|
this._tdToggle = this._tr.insertCell(1);
|
|
|
|
this._tdDetails = this._tr.insertCell(2);
|
|
|
|
|
|
|
|
$(this._tdMain).addClass("schema-alignment-link-main").attr("width", "250").addClass("padded");
|
|
|
|
$(this._tdToggle).addClass("schema-alignment-link-toggle").attr("width", "1%").addClass("padded");
|
|
|
|
$(this._tdDetails).addClass("schema-alignment-link-details").attr("width", "90%");
|
|
|
|
|
|
|
|
this._collapsedDetailDiv = $('<div></div>').appendTo(this._tdDetails).addClass("padded").html("...");
|
|
|
|
this._expandedDetailDiv = $('<div></div>').appendTo(this._tdDetails).addClass("schema-alignment-detail-container");
|
|
|
|
var self = this;
|
|
|
|
var show = function() {
|
2010-02-13 01:11:42 +01:00
|
|
|
if (self._options.expanded) {
|
2010-02-12 21:29:56 +01:00
|
|
|
self._collapsedDetailDiv.hide();
|
|
|
|
self._expandedDetailDiv.show();
|
|
|
|
} else {
|
|
|
|
self._collapsedDetailDiv.show();
|
|
|
|
self._expandedDetailDiv.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
show();
|
|
|
|
|
|
|
|
$(this._tdToggle).html(" ");
|
|
|
|
$('<img />')
|
2010-02-13 01:11:42 +01:00
|
|
|
.attr("src", this._options.expanded ? "images/expanded.png" : "images/collapsed.png")
|
2010-02-12 21:29:56 +01:00
|
|
|
.appendTo(this._tdToggle)
|
|
|
|
.click(function() {
|
2010-02-13 01:11:42 +01:00
|
|
|
self._options.expanded = !self._options.expanded;
|
2010-02-12 21:29:56 +01:00
|
|
|
|
2010-02-13 01:11:42 +01:00
|
|
|
$(this).attr("src", self._options.expanded ? "images/expanded.png" : "images/collapsed.png");
|
2010-02-12 21:29:56 +01:00
|
|
|
|
|
|
|
show();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._renderMain();
|
|
|
|
this._renderDetails();
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink.prototype._renderMain = function() {
|
2010-04-08 21:52:23 +02:00
|
|
|
$(this._tdMain).empty();
|
2010-02-12 21:29:56 +01:00
|
|
|
|
2010-04-09 03:00:44 +02:00
|
|
|
var label = this._link.property !== null ? this._link.property.id : "property?";
|
2010-02-12 21:29:56 +01:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2010-02-13 01:11:42 +01:00
|
|
|
$('<img />')
|
|
|
|
.attr("title", "remove property")
|
|
|
|
.attr("src", "images/close.png")
|
|
|
|
.css("cursor", "pointer")
|
|
|
|
.prependTo(this._tdMain)
|
|
|
|
.click(function() {
|
|
|
|
window.setTimeout(function() {
|
|
|
|
self._parentUINode.removeLink(self);
|
|
|
|
self._tr.parentNode.removeChild(self._tr);
|
2010-03-17 23:55:40 +01:00
|
|
|
self._dialog.preview();
|
2010-02-13 01:11:42 +01:00
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
2010-02-12 21:29:56 +01:00
|
|
|
var a = $('<a href="javascript:{}"></a>')
|
|
|
|
.addClass("schema-alignment-link-tag")
|
|
|
|
.html(label)
|
|
|
|
.appendTo(this._tdMain)
|
|
|
|
.click(function(evt) {
|
2010-02-19 23:56:29 +01:00
|
|
|
self._startEditProperty(this);
|
2010-02-12 21:29:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$('<img />').attr("src", "images/arrow-start.png").prependTo(a);
|
|
|
|
$('<img />').attr("src", "images/arrow-end.png").appendTo(a);
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink.prototype._renderDetails = function() {
|
2010-02-19 23:56:29 +01:00
|
|
|
if (this._targetUI) {
|
|
|
|
this._targetUI.dispose();
|
|
|
|
}
|
|
|
|
if (this._tableDetails) {
|
|
|
|
this._tableDetails.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tableDetails = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._expandedDetailDiv);
|
2010-02-12 23:11:49 +01:00
|
|
|
this._targetUI = new SchemaAlignmentDialog.UINode(
|
2010-02-16 21:15:19 +01:00
|
|
|
this._dialog,
|
2010-02-12 23:11:49 +01:00
|
|
|
this._link.target,
|
2010-02-19 23:56:29 +01:00
|
|
|
this._tableDetails[0],
|
2010-02-12 23:11:49 +01:00
|
|
|
{ expanded: "links" in this._link.target && this._link.target.links.length > 0 });
|
2010-02-12 21:29:56 +01:00
|
|
|
};
|
|
|
|
|
2010-02-19 23:56:29 +01:00
|
|
|
SchemaAlignmentDialog.UILink.prototype._startEditProperty = function(elmt) {
|
|
|
|
var sourceTypeID = this._parentUINode.getExpectedType();
|
2010-04-28 20:57:19 +02:00
|
|
|
var targetNode = this._targetUI._node;
|
|
|
|
var targetTypeID = "type" in targetNode && targetNode.type !== null ? targetNode.type.id : null;
|
|
|
|
var targetTypeName = "columnName" in targetNode ? targetNode.columnName : null;
|
2010-02-19 23:56:29 +01:00
|
|
|
|
2010-04-09 03:00:44 +02:00
|
|
|
if (sourceTypeID !== null) {
|
2010-02-19 23:56:29 +01:00
|
|
|
var self = this;
|
|
|
|
var dismissBusy = DialogSystem.showBusy();
|
|
|
|
|
|
|
|
var instanceCount = 0;
|
|
|
|
var outgoing = [];
|
|
|
|
var incoming = [];
|
|
|
|
|
2010-05-05 02:39:40 +02:00
|
|
|
var onDone = function(properties) {
|
2010-02-19 23:56:29 +01:00
|
|
|
dismissBusy();
|
|
|
|
|
2010-04-19 22:18:43 +02:00
|
|
|
self._showPropertySuggestPopup(
|
|
|
|
elmt,
|
2010-04-28 20:57:19 +02:00
|
|
|
properties
|
2010-04-19 22:18:43 +02:00
|
|
|
);
|
2010-05-05 02:39:40 +02:00
|
|
|
};
|
2010-02-19 23:56:29 +01:00
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink._getPropertiesOfType(
|
|
|
|
sourceTypeID,
|
2010-04-28 20:57:19 +02:00
|
|
|
targetTypeID,
|
|
|
|
targetTypeName,
|
2010-04-19 22:18:43 +02:00
|
|
|
onDone
|
2010-02-19 23:56:29 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._showPropertySuggestPopup(elmt, []);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-04-28 20:57:19 +02:00
|
|
|
SchemaAlignmentDialog.UILink._getPropertiesOfType = function(typeID, targetTypeID, targetTypeName, onDone) {
|
|
|
|
var done = false;
|
2010-02-19 23:56:29 +01:00
|
|
|
|
2010-04-28 20:57:19 +02:00
|
|
|
var params = {
|
|
|
|
"type" : typeID
|
|
|
|
};
|
2010-05-05 02:26:26 +02:00
|
|
|
if (targetTypeID !== null) {
|
2010-04-28 20:57:19 +02:00
|
|
|
params.expects = targetTypeID;
|
2010-05-05 02:26:26 +02:00
|
|
|
} else if (targetTypeName !== null) {
|
2010-04-28 20:57:19 +02:00
|
|
|
params.expects = targetTypeName;
|
2010-02-19 23:56:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$.getJSON(
|
2010-05-08 07:52:32 +02:00
|
|
|
Gridworks.gridworksHelperService + "get_properties_of_type?" + $.param(params) + "&callback=?",
|
2010-02-19 23:56:29 +01:00
|
|
|
null,
|
|
|
|
function(data) {
|
2010-04-19 22:18:43 +02:00
|
|
|
if (done) return;
|
|
|
|
|
2010-05-27 07:14:40 +02:00
|
|
|
done = true;
|
2010-04-19 22:18:43 +02:00
|
|
|
onDone(data.properties || []);
|
|
|
|
}
|
2010-02-19 23:56:29 +01:00
|
|
|
);
|
2010-04-19 22:18:43 +02:00
|
|
|
|
|
|
|
window.setTimeout(function() {
|
|
|
|
if (done) return;
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
onDone([]);
|
|
|
|
}, 7000); // time to give up?
|
2010-02-19 23:56:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink.prototype._showPropertySuggestPopup = function(elmt, suggestions) {
|
2010-02-12 21:29:56 +01:00
|
|
|
self = this;
|
|
|
|
|
2010-02-19 23:56:29 +01:00
|
|
|
var menu = MenuSystem.createMenu().width("350px");
|
|
|
|
|
|
|
|
var commitProperty = function(p) {
|
|
|
|
window.setTimeout(function() { MenuSystem.dismissAll(); }, 100);
|
|
|
|
|
2010-04-19 22:18:43 +02:00
|
|
|
if ("id2" in p) {
|
2010-02-19 23:56:29 +01:00
|
|
|
// self._targetUI.dispose();
|
|
|
|
self._link.property = {
|
2010-04-19 22:18:43 +02:00
|
|
|
id: p.id,
|
2010-02-19 23:56:29 +01:00
|
|
|
name: p.name
|
|
|
|
};
|
|
|
|
self._link.target = {
|
|
|
|
nodeType: "anonymous",
|
|
|
|
links: [{
|
|
|
|
property: {
|
2010-04-19 22:18:43 +02:00
|
|
|
id: p.id2,
|
2010-02-19 23:56:29 +01:00
|
|
|
name: p.name2
|
|
|
|
},
|
|
|
|
target: self._link.target
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
self._renderDetails();
|
|
|
|
} else {
|
|
|
|
self._link.property = {
|
|
|
|
id: p.id,
|
|
|
|
name: p.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self._configureTarget();
|
|
|
|
};
|
2010-02-12 21:29:56 +01:00
|
|
|
|
2010-04-09 03:48:50 +02:00
|
|
|
var divSearch;
|
|
|
|
|
2010-02-19 23:56:29 +01:00
|
|
|
if (suggestions.length > 0) {
|
2010-04-09 03:48:50 +02:00
|
|
|
divSearch = $('<div>').addClass("schema-alignment-link-menu-type-search2").html('<div>Search for a property or pick one below</div>').appendTo(menu);
|
2010-02-19 23:56:29 +01:00
|
|
|
|
2010-05-05 02:39:40 +02:00
|
|
|
var createSuggestion = function(suggestion) {
|
2010-02-19 23:56:29 +01:00
|
|
|
var menuItem = MenuSystem.createMenuItem().appendTo(menu);
|
2010-05-27 07:14:40 +02:00
|
|
|
|
|
|
|
$('<span>')
|
|
|
|
.text(suggestion.name)
|
|
|
|
.attr("title", suggestion.id)
|
|
|
|
.appendTo(menuItem);
|
|
|
|
|
|
|
|
if ("name2" in suggestion) {
|
|
|
|
$('<span>').html(" » ").appendTo(menuItem);
|
|
|
|
|
|
|
|
$('<span>')
|
|
|
|
.text(suggestion.name2)
|
|
|
|
.attr("title", suggestion.id2)
|
|
|
|
.appendTo(menuItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItem.click(function() {
|
2010-02-19 23:56:29 +01:00
|
|
|
commitProperty(suggestion);
|
|
|
|
});
|
2010-05-05 02:39:40 +02:00
|
|
|
};
|
2010-02-19 23:56:29 +01:00
|
|
|
|
2010-05-08 07:52:32 +02:00
|
|
|
for (var i = 0; i < suggestions.length && i < 10; i++) {
|
2010-02-19 23:56:29 +01:00
|
|
|
createSuggestion(suggestions[i]);
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-09 03:48:50 +02:00
|
|
|
divSearch = $('<div>').addClass("schema-alignment-link-menu-type-search").html('<div>Search for a property</div>').appendTo(menu);
|
2010-02-19 23:56:29 +01:00
|
|
|
}
|
|
|
|
var input = $('<input />').appendTo($('<div>').appendTo(divSearch));
|
2010-02-12 21:29:56 +01:00
|
|
|
|
2010-02-19 23:56:29 +01:00
|
|
|
MenuSystem.showMenu(menu, function(){});
|
|
|
|
MenuSystem.positionMenuAboveBelow(menu, $(elmt));
|
2010-02-13 01:11:42 +01:00
|
|
|
|
2010-02-19 23:56:29 +01:00
|
|
|
var suggestOptions = {
|
2010-02-13 01:11:42 +01:00
|
|
|
type : '/type/property'
|
|
|
|
};
|
2010-04-09 03:00:44 +02:00
|
|
|
if (this._link.target !== null && "type" in this._link.target && this._link.target.type !== null) {
|
2010-02-23 00:04:46 +01:00
|
|
|
/*
|
2010-02-19 23:56:29 +01:00
|
|
|
suggestOptions.mql_filter = [{
|
|
|
|
"/type/property/expected_type" : {
|
|
|
|
id: this._link.target.type.id
|
|
|
|
}
|
2010-02-13 01:11:42 +01:00
|
|
|
}];
|
2010-02-23 00:04:46 +01:00
|
|
|
*/
|
2010-02-19 23:56:29 +01:00
|
|
|
} else {
|
|
|
|
var sourceTypeID = this._parentUINode.getExpectedType();
|
2010-04-09 03:00:44 +02:00
|
|
|
if (sourceTypeID !== null) {
|
2010-03-03 22:30:46 +01:00
|
|
|
suggestOptions.schema = sourceTypeID;
|
2010-02-19 23:56:29 +01:00
|
|
|
}
|
2010-02-13 01:11:42 +01:00
|
|
|
}
|
2010-03-03 00:48:21 +01:00
|
|
|
input.suggestP(suggestOptions).bind("fb-select", function(e, data) { commitProperty(data); });
|
2010-02-19 23:56:29 +01:00
|
|
|
|
2010-02-12 21:29:56 +01:00
|
|
|
input[0].focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink.prototype.getJSON = function() {
|
2010-04-09 03:00:44 +02:00
|
|
|
if ("property" in this._link && this._link.property !== null &&
|
|
|
|
"target" in this._link && this._link.target !== null) {
|
2010-02-12 21:29:56 +01:00
|
|
|
|
|
|
|
var targetJSON = this._targetUI.getJSON();
|
2010-04-09 03:00:44 +02:00
|
|
|
if (targetJSON !== null) {
|
2010-02-12 21:29:56 +01:00
|
|
|
return {
|
|
|
|
property: cloneDeep(this._link.property),
|
|
|
|
target: targetJSON
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2010-02-13 01:11:42 +01:00
|
|
|
|
|
|
|
SchemaAlignmentDialog.UILink.prototype._configureTarget = function() {
|
|
|
|
var self = this;
|
2010-02-19 23:56:29 +01:00
|
|
|
var dismissBusy = DialogSystem.showBusy();
|
|
|
|
|
2010-02-13 01:11:42 +01:00
|
|
|
$.getJSON(
|
|
|
|
"http://api.freebase.com/api/service/mqlread?query=" + JSON.stringify({
|
|
|
|
query: {
|
|
|
|
"id" : this._link.property.id,
|
|
|
|
"type" : "/type/property",
|
|
|
|
"expected_type" : {
|
|
|
|
"id" : null,
|
|
|
|
"name" : null,
|
|
|
|
"/freebase/type_hints/mediator" : null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}) + "&callback=?",
|
|
|
|
null,
|
|
|
|
function(o) {
|
2010-02-19 23:56:29 +01:00
|
|
|
dismissBusy();
|
|
|
|
|
2010-02-13 01:11:42 +01:00
|
|
|
if ("result" in o) {
|
|
|
|
var expected_type = o.result.expected_type;
|
|
|
|
self._link.target.type = {
|
|
|
|
id: expected_type.id,
|
|
|
|
name: expected_type.name
|
|
|
|
};
|
2010-04-08 22:18:38 +02:00
|
|
|
if (expected_type["/freebase/type_hints/mediator"] === true) {
|
2010-02-13 01:11:42 +01:00
|
|
|
self._link.target.nodeType = "anonymous";
|
|
|
|
} else if (expected_type.id == "/type/key") {
|
|
|
|
self._link.target.nodeType = "cell-as-key";
|
|
|
|
} else if (expected_type.id.match(/^\/type\//)) {
|
|
|
|
self._link.target.nodeType = "cell-as-value";
|
|
|
|
} else if (!("topic" in self._link.target)) {
|
|
|
|
self._link.target.nodeType = "cell-as-topic";
|
|
|
|
self._link.target.createForNoReconMatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._targetUI.render();
|
|
|
|
}
|
2010-02-19 23:56:29 +01:00
|
|
|
|
|
|
|
self._renderMain();
|
|
|
|
self._dialog.preview();
|
2010-02-13 01:11:42 +01:00
|
|
|
},
|
|
|
|
"jsonp"
|
|
|
|
);
|
|
|
|
};
|