2010-03-16 01:24:20 +01:00
|
|
|
function ExtendDataPreviewDialog(column, columnIndex, rowIndices, onDone) {
|
2010-03-13 09:08:25 +01:00
|
|
|
this._column = column;
|
2010-03-16 01:24:20 +01:00
|
|
|
this._columnIndex = columnIndex;
|
2010-03-13 09:08:25 +01:00
|
|
|
this._rowIndices = rowIndices;
|
2010-03-13 08:13:18 +01:00
|
|
|
this._onDone = onDone;
|
2010-03-13 09:08:25 +01:00
|
|
|
this._extension = { properties: [] };
|
2010-03-13 08:13:18 +01:00
|
|
|
|
|
|
|
var self = this;
|
2010-03-14 21:55:57 +01:00
|
|
|
var frame = this._frame = DialogSystem.createDialog();
|
2010-03-13 08:13:18 +01:00
|
|
|
frame.width("900px").addClass("extend-data-preview-dialog");
|
|
|
|
|
2010-03-13 09:08:25 +01:00
|
|
|
var header = $('<div></div>').addClass("dialog-header").text("Add Columns from Freebase Based on Column " + column.name).appendTo(frame);
|
2010-03-13 08:13:18 +01:00
|
|
|
var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
|
|
|
|
var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
|
|
|
|
var html = $(
|
|
|
|
'<div class="grid-layout layout-normal layout-full"><table style="height: 600px">' +
|
|
|
|
'<tr>' +
|
|
|
|
'<td width="150" height="1">Add Property</td>' +
|
2010-03-13 09:08:25 +01:00
|
|
|
'<td height="1">Preview</td>' +
|
2010-03-13 08:13:18 +01:00
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
2010-03-13 09:08:25 +01:00
|
|
|
'<td height="1"><div class="input-container"><input bind="addPropertyInput" /></div></td>' +
|
|
|
|
'<td height="100%" rowspan="3"><div class="preview-container" bind="previewContainer"></div></td>' +
|
2010-03-13 08:13:18 +01:00
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
|
|
|
'<td height="1">Suggested Properties</td>' +
|
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
2010-03-14 21:55:57 +01:00
|
|
|
'<td height="99%"><div class="suggested-property-container" bind="suggestedPropertyContainer"></div></td>' +
|
2010-03-13 08:13:18 +01:00
|
|
|
'</tr>' +
|
|
|
|
'</table></div>'
|
|
|
|
).appendTo(body);
|
|
|
|
|
|
|
|
this._elmts = DOM.bind(html);
|
|
|
|
|
|
|
|
$('<button></button>').html(" OK ").click(function() {
|
2010-03-16 01:24:20 +01:00
|
|
|
if (self._extension.properties.length === 0) {
|
|
|
|
alert("Please add some properties first.");
|
|
|
|
} else {
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
self._onDone(self._extension);
|
|
|
|
}
|
2010-03-13 08:13:18 +01:00
|
|
|
}).appendTo(footer);
|
|
|
|
|
|
|
|
$('<button></button>').text("Cancel").click(function() {
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
}).appendTo(footer);
|
|
|
|
|
2010-03-16 01:24:20 +01:00
|
|
|
var dismissBusy = DialogSystem.showBusy();
|
2010-03-14 21:55:57 +01:00
|
|
|
ExtendDataPreviewDialog.getAllProperties(column.reconConfig.type.id, function(properties) {
|
2010-03-16 01:24:20 +01:00
|
|
|
dismissBusy();
|
2010-03-14 21:55:57 +01:00
|
|
|
self._show(properties);
|
|
|
|
});
|
2010-04-08 21:52:23 +02:00
|
|
|
}
|
2010-03-14 21:55:57 +01:00
|
|
|
|
|
|
|
ExtendDataPreviewDialog.getAllProperties = function(typeID, onDone) {
|
|
|
|
var query = {
|
2010-03-15 21:41:08 +01:00
|
|
|
"id" : typeID,
|
|
|
|
"type" : "/type/type",
|
2010-03-14 21:55:57 +01:00
|
|
|
"/freebase/type_hints/included_types" : [{
|
2010-03-15 21:41:08 +01:00
|
|
|
"optional" : true,
|
|
|
|
"properties" : [{
|
|
|
|
"id" : null,
|
|
|
|
"name" : null,
|
2010-03-14 21:55:57 +01:00
|
|
|
"/type/property/expected_type" : {
|
2010-03-15 21:41:08 +01:00
|
|
|
"id" : null,
|
2010-03-16 07:18:00 +01:00
|
|
|
"name" : null,
|
2010-03-14 21:55:57 +01:00
|
|
|
"/freebase/type_hints/mediator" : []
|
|
|
|
},
|
2010-03-15 21:41:08 +01:00
|
|
|
"sort" : "name"
|
2010-03-14 21:55:57 +01:00
|
|
|
}]
|
|
|
|
}],
|
2010-03-15 21:41:08 +01:00
|
|
|
"properties" : [{
|
|
|
|
"id" : null,
|
|
|
|
"name" : null,
|
2010-03-14 21:55:57 +01:00
|
|
|
"/type/property/expected_type" : {
|
2010-03-15 21:41:08 +01:00
|
|
|
"id" : null,
|
2010-03-16 07:18:00 +01:00
|
|
|
"name" : null,
|
2010-03-14 21:55:57 +01:00
|
|
|
"/freebase/type_hints/mediator" : []
|
|
|
|
},
|
2010-03-15 21:41:08 +01:00
|
|
|
"sort" : "name"
|
2010-03-14 21:55:57 +01:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var allProperties = [];
|
2010-03-15 21:41:08 +01:00
|
|
|
var cvtProperties = [];
|
|
|
|
var processProperty = function(property, parent) {
|
2010-03-14 21:55:57 +01:00
|
|
|
var expectedType = property["/type/property/expected_type"];
|
|
|
|
if (expectedType["/freebase/type_hints/mediator"].length > 0 && expectedType["/freebase/type_hints/mediator"][0]) {
|
2010-03-15 21:41:08 +01:00
|
|
|
cvtProperties.push(property);
|
2010-03-14 21:55:57 +01:00
|
|
|
} else {
|
|
|
|
allProperties.push({
|
|
|
|
id : property.id,
|
|
|
|
name : property.name,
|
2010-03-16 07:18:00 +01:00
|
|
|
expected : {
|
|
|
|
id : expectedType.id,
|
|
|
|
name : expectedType.name
|
|
|
|
}
|
2010-03-14 21:55:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2010-03-15 21:41:08 +01:00
|
|
|
var processProperties = function(properties, parent) {
|
|
|
|
$.each(properties, function() { processProperty(this, parent); });
|
2010-03-14 21:55:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$.getJSON(
|
|
|
|
"http://api.freebase.com/api/service/mqlread?query=" + escape(JSON.stringify({ query : query })) + "&callback=?",
|
|
|
|
null,
|
|
|
|
function(o) {
|
2010-03-15 21:41:08 +01:00
|
|
|
if (!("result" in o)) {
|
2010-03-14 21:55:57 +01:00
|
|
|
onDone([]);
|
2010-03-15 21:41:08 +01:00
|
|
|
return;
|
2010-03-14 21:55:57 +01:00
|
|
|
}
|
2010-03-15 21:41:08 +01:00
|
|
|
|
|
|
|
processProperties(o.result.properties);
|
|
|
|
$.each(o.result["/freebase/type_hints/included_types"], function() {
|
|
|
|
processProperties(this.properties, null);
|
2010-04-08 21:52:23 +02:00
|
|
|
});
|
2010-03-15 21:41:08 +01:00
|
|
|
|
2010-04-08 22:16:08 +02:00
|
|
|
if (!cvtProperties.length) {
|
2010-03-15 21:41:08 +01:00
|
|
|
onDone(allProperties);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var expectedTypeToProperties = [];
|
|
|
|
var expectedTypeIDs = [];
|
|
|
|
$.each(cvtProperties, function() {
|
|
|
|
var expected = this["/type/property/expected_type"];
|
|
|
|
if (expected.id in expectedTypeToProperties) {
|
|
|
|
expectedTypeToProperties[expected.id].push(this);
|
|
|
|
} else {
|
|
|
|
expectedTypeToProperties[expected.id] = [ this ];
|
|
|
|
expectedTypeIDs.push(expected.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var query2 = [{
|
|
|
|
"id|=" : expectedTypeIDs,
|
|
|
|
"id" : null,
|
|
|
|
"type" : "/type/type",
|
|
|
|
"properties" : [{
|
|
|
|
"id" : null,
|
|
|
|
"name" : null,
|
|
|
|
"/type/property/expected_type" : {
|
2010-03-16 07:18:00 +01:00
|
|
|
"id" : null,
|
|
|
|
"name" : null,
|
2010-03-15 21:41:08 +01:00
|
|
|
"/freebase/type_hints/mediator" : []
|
|
|
|
},
|
|
|
|
"sort" : "name"
|
|
|
|
}]
|
|
|
|
}];
|
|
|
|
|
|
|
|
$.getJSON(
|
|
|
|
"http://api.freebase.com/api/service/mqlread?query=" + escape(JSON.stringify({ query : query2 })) + "&callback=?",
|
|
|
|
null,
|
|
|
|
function(o2) {
|
|
|
|
if ("result" in o2) {
|
|
|
|
var processCVTProperty = function(parentProperty, properties) {
|
2010-03-16 07:18:00 +01:00
|
|
|
var parentExpected = parentProperty["/type/property/expected_type"];
|
|
|
|
|
2010-03-15 21:41:08 +01:00
|
|
|
$.each(properties, function() {
|
2010-03-16 07:18:00 +01:00
|
|
|
var expected = this["/type/property/expected_type"];
|
2010-03-15 21:41:08 +01:00
|
|
|
allProperties.push({
|
|
|
|
id : parentProperty.id,
|
|
|
|
name : parentProperty.name,
|
2010-03-16 07:18:00 +01:00
|
|
|
expected : {
|
|
|
|
id : parentExpected.id,
|
|
|
|
name : parentExpected.name
|
|
|
|
},
|
2010-03-15 21:41:08 +01:00
|
|
|
properties: [{
|
|
|
|
id : this.id,
|
|
|
|
name : this.name,
|
2010-03-16 07:18:00 +01:00
|
|
|
expected : {
|
|
|
|
id : expected.id,
|
|
|
|
name : expected.name
|
|
|
|
}
|
2010-03-15 21:41:08 +01:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var processCVTProperties = function(parentProperties, properties) {
|
|
|
|
$.each(parentProperties, function() { processCVTProperty(this, properties); });
|
|
|
|
};
|
|
|
|
|
|
|
|
$.each(o2.result, function() {
|
|
|
|
processCVTProperties(expectedTypeToProperties[this.id], this.properties);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onDone(allProperties);
|
|
|
|
},
|
|
|
|
"jsonp"
|
|
|
|
);
|
2010-03-14 21:55:57 +01:00
|
|
|
},
|
|
|
|
"jsonp"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ExtendDataPreviewDialog.prototype._show = function(properties) {
|
|
|
|
this._level = DialogSystem.showDialog(this._frame);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var container = this._elmts.suggestedPropertyContainer;
|
|
|
|
var renderSuggestedProperty = function(property) {
|
2010-03-15 21:41:08 +01:00
|
|
|
var label = ("properties" in property) ? (property.name + " » " + property.properties[0].name) : property.name;
|
2010-03-14 21:55:57 +01:00
|
|
|
var div = $('<div>').addClass("suggested-property").appendTo(container);
|
2010-03-15 21:41:08 +01:00
|
|
|
|
|
|
|
$('<a>').attr("href", "javascript:{}").html(label).appendTo(div).click(function() {
|
2010-03-14 21:55:57 +01:00
|
|
|
self._addProperty(property);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
for (var i = 0; i < properties.length; i++) {
|
|
|
|
renderSuggestedProperty(properties[i]);
|
|
|
|
}
|
2010-03-13 09:08:25 +01:00
|
|
|
|
|
|
|
var suggestConfig = {
|
2010-03-14 21:55:57 +01:00
|
|
|
type: '/type/property',
|
|
|
|
schema: this._column.reconConfig.type.id
|
2010-03-13 09:08:25 +01:00
|
|
|
};
|
2010-03-14 21:55:57 +01:00
|
|
|
|
2010-03-13 09:08:25 +01:00
|
|
|
this._elmts.addPropertyInput.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
|
2010-03-16 07:18:00 +01:00
|
|
|
var expected = data["/type/property/expected_type"];
|
2010-03-14 21:55:57 +01:00
|
|
|
self._addProperty({
|
|
|
|
id : data.id,
|
|
|
|
name: data.name,
|
2010-03-16 07:18:00 +01:00
|
|
|
expected: {
|
|
|
|
id: expected.id,
|
|
|
|
name: expected.name
|
|
|
|
}
|
2010-03-14 21:55:57 +01:00
|
|
|
});
|
2010-03-13 09:08:25 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ExtendDataPreviewDialog.prototype._update = function() {
|
|
|
|
this._elmts.previewContainer.empty().text("Querying Freebase ...");
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var params = {
|
|
|
|
project: theProject.id,
|
|
|
|
columnName: this._column.name
|
|
|
|
};
|
|
|
|
|
|
|
|
$.post(
|
|
|
|
"/command/preview-extend-data?" + $.param(params),
|
|
|
|
{
|
|
|
|
rowIndices: JSON.stringify(this._rowIndices),
|
|
|
|
extension: JSON.stringify(this._extension)
|
|
|
|
},
|
|
|
|
function(data) {
|
2010-04-08 21:52:23 +02:00
|
|
|
self._renderPreview(data);
|
2010-03-13 09:08:25 +01:00
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ExtendDataPreviewDialog.prototype._addProperty = function(p) {
|
2010-03-15 20:44:33 +01:00
|
|
|
var addSeveralToList = function(properties, oldProperties) {
|
|
|
|
for (var i = 0; i < properties.length; i++) {
|
|
|
|
addToList(properties[i], oldProperties);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var addToList = function(property, oldProperties) {
|
|
|
|
for (var i = 0; i < oldProperties.length; i++) {
|
|
|
|
var oldProperty = oldProperties[i];
|
|
|
|
if (oldProperty.id == property.id) {
|
|
|
|
if ("included" in property) {
|
|
|
|
oldProperty.included = "included" in oldProperty ?
|
|
|
|
(oldProperty.included || property.included) :
|
|
|
|
property.included;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("properties" in property) {
|
|
|
|
if ("properties" in oldProperty) {
|
|
|
|
addSeveralToList(property.properties, oldProperty.properties);
|
|
|
|
} else {
|
|
|
|
oldProperty.properties = property.properties;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oldProperties.push(property);
|
|
|
|
};
|
|
|
|
|
|
|
|
addToList(p, this._extension.properties);
|
|
|
|
|
|
|
|
this._update();
|
|
|
|
};
|
|
|
|
|
|
|
|
ExtendDataPreviewDialog.prototype._removeProperty = function(path) {
|
|
|
|
var removeFromList = function(path, index, properties) {
|
|
|
|
var id = path[index];
|
|
|
|
|
|
|
|
for (var i = properties.length - 1; i >= 0; i--) {
|
|
|
|
var property = properties[i];
|
|
|
|
if (property.id == id) {
|
|
|
|
if (index === path.length - 1) {
|
|
|
|
if ("included" in property) {
|
|
|
|
delete property.included;
|
|
|
|
}
|
|
|
|
} else if ("properties" in property && property.properties.length > 0) {
|
|
|
|
removeFromList(path, index + 1, property.properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!("properties" in property) || property.properties.length === 0) {
|
|
|
|
properties.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
removeFromList(path, 0, this._extension.properties);
|
|
|
|
|
2010-03-13 09:08:25 +01:00
|
|
|
this._update();
|
2010-03-13 08:13:18 +01:00
|
|
|
};
|
|
|
|
|
2010-03-13 09:08:25 +01:00
|
|
|
ExtendDataPreviewDialog.prototype._renderPreview = function(data) {
|
2010-03-15 20:44:33 +01:00
|
|
|
var self = this;
|
2010-03-13 09:08:25 +01:00
|
|
|
var container = this._elmts.previewContainer.empty();
|
|
|
|
if (data.code == "error") {
|
|
|
|
container.text("Error.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var table = $('<table>')[0];
|
2010-03-14 21:55:57 +01:00
|
|
|
var trHead = table.insertRow(table.rows.length);
|
2010-03-15 20:44:33 +01:00
|
|
|
$('<th>').appendTo(trHead).text(this._column.name);
|
2010-03-14 21:55:57 +01:00
|
|
|
|
2010-03-15 21:41:08 +01:00
|
|
|
var renderColumnHeader = function(column) {
|
2010-03-15 20:44:33 +01:00
|
|
|
var th = $('<th>').appendTo(trHead);
|
|
|
|
|
|
|
|
$('<span>').html(column.names.join(" » ")).appendTo(th);
|
|
|
|
$('<img>')
|
|
|
|
.attr("src", "images/close.png")
|
|
|
|
.attr("title", "Remove this column")
|
|
|
|
.click(function() {
|
|
|
|
self._removeProperty(column.path);
|
|
|
|
}).appendTo(th);
|
2010-03-15 21:41:08 +01:00
|
|
|
};
|
|
|
|
for (var c = 0; c < data.columns.length; c++) {
|
|
|
|
renderColumnHeader(data.columns[c]);
|
2010-03-14 21:55:57 +01:00
|
|
|
}
|
2010-03-13 09:08:25 +01:00
|
|
|
|
|
|
|
for (var r = 0; r < data.rows.length; r++) {
|
|
|
|
var tr = table.insertRow(table.rows.length);
|
|
|
|
var row = data.rows[r];
|
|
|
|
|
|
|
|
for (var c = 0; c < row.length; c++) {
|
|
|
|
var td = tr.insertCell(tr.cells.length);
|
|
|
|
var cell = row[c];
|
2010-04-09 03:00:44 +02:00
|
|
|
if (cell !== null) {
|
2010-03-13 09:08:25 +01:00
|
|
|
if ($.isPlainObject(cell)) {
|
|
|
|
$('<a>').attr("href", "http://www.freebase.com/view" + cell.id).text(cell.name).appendTo(td);
|
|
|
|
} else {
|
|
|
|
$('<span>').text(cell).appendTo(td);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-13 08:13:18 +01:00
|
|
|
|
2010-03-13 09:08:25 +01:00
|
|
|
container.append(table);
|
2010-04-08 21:52:23 +02:00
|
|
|
};
|