2010-10-20 20:11:15 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
* Redistributions of source code must retain the above copyright
|
2010-10-20 20:11:15 +02:00
|
|
|
notice, this list of conditions and the following disclaimer.
|
2011-08-04 22:37:14 +02:00
|
|
|
* Redistributions in binary form must reproduce the above
|
2010-10-20 20:11:15 +02:00
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
2011-08-04 22:37:14 +02:00
|
|
|
* Neither the name of Google Inc. nor the names of its
|
2010-10-20 20:11:15 +02:00
|
|
|
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.
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
*/
|
2010-10-20 20:11:15 +02:00
|
|
|
|
2010-10-08 03:39:38 +02:00
|
|
|
function FreebaseLoadingDialog() {
|
2011-08-04 22:37:14 +02:00
|
|
|
this._createDialog();
|
|
|
|
this._signedin = false;
|
2010-10-08 03:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FreebaseLoadingDialog.prototype._createDialog = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
var dialog = $(DOM.loadHTML("freebase", "scripts/dialogs/freebase-loading-dialog.html"));
|
|
|
|
this._elmts = DOM.bind(dialog);
|
|
|
|
this._elmts.cancelButton.click(function() { self._dismiss(); });
|
|
|
|
|
2012-08-01 23:46:53 +02:00
|
|
|
var provider = "freebase.com";
|
2011-08-04 22:37:14 +02:00
|
|
|
var authorization = this._elmts.authorization;
|
|
|
|
var loadButton = this._elmts.loadButton;
|
|
|
|
|
2013-03-12 22:46:18 +01:00
|
|
|
// TODO: Does this need to be redone for the new API / freebase.com site?
|
2011-08-04 22:37:14 +02:00
|
|
|
var check_authorization = function(cont) {
|
2012-10-13 19:47:08 +02:00
|
|
|
var check_authorization_url = "command/freebase/check-authorization/" + provider;
|
2012-07-12 03:52:15 +02:00
|
|
|
$.get(check_authorization_url, function(data) {
|
2011-08-04 22:37:14 +02:00
|
|
|
if ("status" in data && data.code == "/api/status/ok") {
|
|
|
|
authorization.html('Signed in as: <a target="_new" href="http://www.freebase.com/view/user/' + data.username + '">' + data.username + '</a> | <a href="javascript:{}" bind="signout">Sign Out</a>').show();
|
|
|
|
DOM.bind(authorization).signout.click(function() {
|
|
|
|
self._signedin = false;
|
|
|
|
loadButton.attr("disabled","disabled");
|
|
|
|
$("#freebase-loading-graph-selector-freebase").attr("disabled","disabled").button("refresh");
|
2012-07-12 03:54:24 +02:00
|
|
|
Sign.signout(function() {
|
|
|
|
$('.signedin').hide();
|
|
|
|
$('.signedout').show();
|
|
|
|
check_authorization();
|
|
|
|
},provider);
|
2011-08-04 22:37:14 +02:00
|
|
|
});
|
|
|
|
loadButton.unbind().click(function() {
|
|
|
|
self._load();
|
|
|
|
});
|
|
|
|
|
|
|
|
self._signedin = true;
|
|
|
|
$("#freebase-loading-source-name").keyup();
|
|
|
|
|
|
|
|
if (typeof cont == "function") cont(data);
|
|
|
|
} else {
|
|
|
|
authorization.html('<a href="javascript:{}" bind="signin">Sign into Freebase</a> to enable loading').show();
|
|
|
|
|
|
|
|
self._signedin = false;
|
|
|
|
$("#freebase-loading-source-name").keyup();
|
|
|
|
|
|
|
|
DOM.bind(authorization).signin.click(function() {
|
|
|
|
Sign.signin(function() {
|
2012-07-12 03:54:24 +02:00
|
|
|
$('.signedout').hide();
|
|
|
|
$('.signedin').show();
|
2011-08-04 22:37:14 +02:00
|
|
|
check_authorization(cont);
|
2012-07-12 03:52:15 +02:00
|
|
|
},provider,check_authorization_url);
|
|
|
|
});
|
2011-08-04 22:37:14 +02:00
|
|
|
}
|
|
|
|
},"json");
|
|
|
|
};
|
|
|
|
|
|
|
|
var make_topic = function(new_topic_id, topic_type, cont) {
|
|
|
|
var mql_query = [{
|
|
|
|
"create": "unless_exists",
|
|
|
|
"name": new_topic_id,
|
|
|
|
"a:type": topic_type,
|
|
|
|
"b:type": "/common/topic",
|
|
|
|
"id": null,
|
|
|
|
"guid": null
|
|
|
|
}];
|
|
|
|
|
2012-10-13 19:47:08 +02:00
|
|
|
$.post("command/freebase/mqlwrite/" + provider,
|
2011-08-04 22:37:14 +02:00
|
|
|
{ "query" : JSON.stringify(mql_query) },
|
|
|
|
function(data) {
|
|
|
|
if ("status" in data && data.code == "/api/status/ok") {
|
|
|
|
self._elmts.source_id.val(data.result[0].id);
|
|
|
|
if (typeof cont == "function") cont();
|
|
|
|
} else {
|
|
|
|
self._show_error("Error creating new topic", data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var show_triples = function(cont) {
|
|
|
|
$.post(
|
2012-10-13 19:47:08 +02:00
|
|
|
"command/freebase/preview-protograph?" + $.param({ project: theProject.id }),
|
2011-08-04 22:37:14 +02:00
|
|
|
{
|
|
|
|
protograph: JSON.stringify(theProject.overlayModels.freebaseProtograph || {}),
|
|
|
|
engine: JSON.stringify(ui.browsingEngine.getJSON())
|
|
|
|
},
|
|
|
|
function(data) {
|
|
|
|
if ("tripleloader" in data) {
|
|
|
|
self._elmts.functionalCase.show();
|
|
|
|
self._level = DialogSystem.showDialog(dialog);
|
|
|
|
|
|
|
|
self._elmts.functionalTabs.tabs();
|
|
|
|
|
|
|
|
self._elmts.previewContainer.text(data.tripleloader).show();
|
|
|
|
|
|
|
|
self._elmts.source_name.keyup(function() {
|
|
|
|
if (self._signedin && $(this).val() != "") {
|
|
|
|
loadButton.removeAttr("disabled").removeClass("button-disabled");
|
2010-10-08 03:39:38 +02:00
|
|
|
} else {
|
2011-08-04 22:37:14 +02:00
|
|
|
loadButton.attr("disabled","disabled").addClass("button-disabled");
|
2010-10-08 03:39:38 +02:00
|
|
|
}
|
2011-08-04 22:37:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
self._elmts.source_id.suggest({
|
2012-10-12 20:39:05 +02:00
|
|
|
filter: "(all type:/dataworld/information_source)",
|
|
|
|
suggest_new: "Click here to add a new information source"
|
2011-08-04 22:37:14 +02:00
|
|
|
}).bind("fb-select", function(e, data) {
|
|
|
|
self._elmts.source_id.val(data.id);
|
|
|
|
}).bind("fb-select-new", function(e, val) {
|
|
|
|
make_topic(val, "/dataworld/information_source");
|
|
|
|
});
|
|
|
|
|
|
|
|
$.getJSON(
|
2012-10-13 19:47:08 +02:00
|
|
|
"command/core/get-preference?" + $.param({ project: theProject.id, name: "freebase.load.jobName" }),
|
2011-08-04 22:37:14 +02:00
|
|
|
null,
|
2010-10-08 03:39:38 +02:00
|
|
|
function(data) {
|
2011-08-04 22:37:14 +02:00
|
|
|
self._elmts.source_name[0].value = (data.value) ? data.value : theProject.metadata.name;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (typeof cont == "function") cont();
|
|
|
|
} else {
|
|
|
|
self._elmts.unalignedCase.show();
|
|
|
|
self._level = DialogSystem.showDialog(dialog);
|
|
|
|
|
|
|
|
self._elmts.alignButton.click(function() {
|
|
|
|
self._dismiss();
|
|
|
|
FreebaseExtension.handlers.editSchemaAlignment(false);
|
|
|
|
});
|
|
|
|
self._end();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
2010-10-08 03:39:38 +02:00
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
show_triples(check_authorization);
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FreebaseLoadingDialog.prototype._load = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
var qa = self._elmts.qaCheckbox.is(':checked');
|
|
|
|
|
|
|
|
var get_refinery_url = function(url) {
|
|
|
|
return "http://refinery.freebaseapps.com/load/" + url.split("/").slice(-1)[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
var doLoad = function() {
|
|
|
|
var dismissBusy = DialogSystem.showBusy();
|
|
|
|
|
|
|
|
$.post(
|
2012-10-13 19:47:08 +02:00
|
|
|
"command/freebase/upload-data",
|
2011-08-04 22:37:14 +02:00
|
|
|
{
|
|
|
|
"project" : theProject.id,
|
|
|
|
"qa" : qa,
|
|
|
|
"engine" : JSON.stringify(ui.browsingEngine.getJSON()),
|
|
|
|
"source_name" : self._elmts.source_name.val(),
|
|
|
|
"source_id" : self._elmts.source_id.val()
|
|
|
|
},
|
|
|
|
function(data) {
|
|
|
|
dismissBusy();
|
|
|
|
|
|
|
|
var body = self._elmts.dialogBody;
|
|
|
|
if ("status" in data && typeof data.status == "object" && "code" in data.status && data.status.code == 200) {
|
|
|
|
self._elmts.tripleCountSpan.text(data.result.added);
|
|
|
|
self._elmts.refineryLink.attr("href", get_refinery_url(data.result.status_url));
|
|
|
|
self._elmts.functionalCase.hide();
|
|
|
|
self._elmts.loadedCase.show();
|
|
|
|
self._end();
|
|
|
|
} else {
|
|
|
|
self._show_error("Error loading data",data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (qa) {
|
|
|
|
var dialog = $(DOM.loadHTML("freebase", "scripts/dialogs/confirm-qa-dialog.html"));
|
|
|
|
var elmts = DOM.bind(dialog);
|
|
|
|
var level = DialogSystem.showDialog(dialog);
|
|
|
|
var dismiss = function() {
|
|
|
|
DialogSystem.dismissUntil(level - 1);
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|
2011-08-04 22:37:14 +02:00
|
|
|
|
|
|
|
elmts.okButton.click(function() {
|
|
|
|
doLoad();
|
|
|
|
dismiss();
|
|
|
|
});
|
|
|
|
elmts.cancelButton.click(function() {
|
|
|
|
dismiss();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
doLoad();
|
|
|
|
}
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FreebaseLoadingDialog.prototype._dismiss = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
DialogSystem.dismissUntil(this._level - 1);
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FreebaseLoadingDialog.prototype._show_error = function(msg, error) {
|
2011-08-04 22:37:14 +02:00
|
|
|
this._elmts.dialogBody.children().hide();
|
|
|
|
this._elmts.errorCase.show();
|
|
|
|
this._elmts.errorMessage.text(msg);
|
|
|
|
this._elmts.errorDetails.html(
|
|
|
|
(('message' in error) ? '<p>' + error.message + '</p>' : '<pre>' + JSON.stringify(error, null, 2) + '</pre>') +
|
|
|
|
(('stack' in error) ? '<pre>' + error.stack.replace(/\\n/g,'\n').replace(/\\t/g,'\t') + '</pre>' : "")
|
|
|
|
);
|
|
|
|
this._end();
|
|
|
|
console.log(error);
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FreebaseLoadingDialog.prototype._end = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
this._elmts.loadButton.text("Close").removeAttr("disabled").removeClass("button-disabled").unbind().click(function() {
|
|
|
|
self._dismiss();
|
|
|
|
});
|
|
|
|
this._elmts.cancelButton.hide();
|
|
|
|
this._elmts.authorization.hide();
|
2010-10-08 03:39:38 +02:00
|
|
|
};
|