2010-10-08 03:39:38 +02:00
function FreebaseLoadingDialog ( ) {
this . _createDialog ( ) ;
this . _signedin = false ;
}
FreebaseLoadingDialog . prototype . _createDialog = function ( ) {
var self = this ;
2010-10-15 09:33:34 +02:00
var dialog = $ ( DOM . loadHTML ( "freebase" , "scripts/dialogs/freebase-loading-dialog.html" ) ) ;
2010-10-08 03:39:38 +02:00
this . _elmts = DOM . bind ( dialog ) ;
this . _elmts . cancelButton . click ( function ( ) { self . _dismiss ( ) ; } ) ;
var provider = "www.freebase.com" ;
var authorization = this . _elmts . authorization ;
var loadButton = this . _elmts . loadButton ;
var check _authorization = function ( cont ) {
2010-10-15 09:33:34 +02:00
$ . get ( "/command/freebase/check-authorization/" + provider , function ( data ) {
2010-10-08 03:39:38 +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" ) ;
Sign . signout ( check _authorization , provider ) ;
} ) ;
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 ( ) ;
DOM . bind ( authorization ) . signin . click ( function ( ) {
Sign . signin ( function ( ) {
check _authorization ( cont ) ;
} , provider ) ;
} ) ;
}
} , "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
} ] ;
2010-10-15 09:33:34 +02:00
$ . post ( "/command/freebase/mqlwrite/" + provider ,
2010-10-08 03:39:38 +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 (
2010-10-15 09:33:34 +02:00
"/command/freebase/preview-protograph?" + $ . param ( { project : theProject . id } ) ,
2010-10-08 03:39:38 +02:00
{
protograph : JSON . stringify ( theProject . overlayModels . freebaseProtograph || { } ) ,
engine : JSON . stringify ( ui . browsingEngine . getJSON ( ) )
} ,
function ( data ) {
var body = self . _elmts . dialogBody ;
if ( "tripleloader" in data ) {
body . html (
'<div class="freebase-loading-tripleloader-info"><table><tr>' +
'<td><div>Name this data load ¬ <sup style="color: red">required</sup></div>' +
'<input type="text" size="40" id="freebase-loading-source-name" bind="source_name"></td>' +
'<td><div>Source ID ¬ <sup style="color: #888">optional</sup></div>' +
'<input type="text" size="60" id="freebase-loading-source-id" bind="source_id"></td>' +
'</tr></table></div>' +
'<div class="freebase-loading-tripleloader-data">' + data . tripleloader + '</div>'
) ;
self . _elmts = DOM . bind ( dialog ) ;
self . _elmts . source _name . keyup ( function ( ) {
if ( self . _signedin && $ ( this ) . val ( ) != "" ) {
loadButton . removeAttr ( "disabled" ) ;
} else {
loadButton . attr ( "disabled" , "disabled" ) ;
}
} ) ;
self . _elmts . source _id . suggest ( {
"type" : "/dataworld/information_source" ,
"suggest_new" : "Click here to add a new information source"
} ) . 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 (
"/command/core/get-preference?" + $ . param ( { project : theProject . id , name : "freebase.load.jobName" } ) ,
null ,
function ( data ) {
if ( data . value != null ) {
self . _elmts . source _name [ 0 ] . value = data . value ;
}
}
) ;
if ( typeof cont == "function" ) cont ( ) ;
} else {
body . html (
'<div class="freebase-loading-tripleloader-message">' +
'<h2>This dataset has no triples</h2>' +
'<p>Have you aligned it with the Freebase schemas yet?</p>' +
'</div>'
) ;
self . _elmts = DOM . bind ( dialog ) ;
self . _end ( ) ;
}
self . _level = DialogSystem . showDialog ( dialog ) ;
} ,
"json"
) ;
} ;
show _triples ( check _authorization ) ;
} ;
FreebaseLoadingDialog . prototype . _load = function ( ) {
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 ( ) ;
2010-10-15 09:33:34 +02:00
$ . post ( "/command/freebase/upload-data" ,
2010-10-08 03:39:38 +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 ) {
body . html (
'<div class="freebase-loading-tripleloader-message">' +
'<h2><span>' + data . result . added + '</span> triples successfully scheduled for loading</h2>' +
'<h4>Follow the loading progress in the <a href="' + get _refinery _url ( data . result . status _url ) + '" target="_new">Freebase Refinery</a></h4>' +
'</div>'
) ;
self . _end ( ) ;
} else {
self . _show _error ( "Error loading data" , data ) ;
}
} ,
"json"
) ;
} ;
if ( qa ) {
var dialog = $ (
'<div id="freebase-confirmation-dialog" title="Are you sure?">' +
'<table><tr><td width="30%"><img src="/images/cop.png" width="140px"></td><td width="70%" style="text-align: center; vertical-align: middle; font-size: 120%">Are you sure this data is ready to be tested for upload into <b>Freebase</b>?</td></tr></table>' +
'</div>'
) . dialog ( {
resizable : false ,
width : 400 ,
height : 230 ,
modal : true ,
buttons : {
'yes, I\'m sure' : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
doLoad ( ) ;
} ,
'hmm, not really' : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
}
}
} ) ;
} else {
doLoad ( ) ;
}
} ;
FreebaseLoadingDialog . prototype . _dismiss = function ( ) {
DialogSystem . dismissUntil ( this . _level - 1 ) ;
} ;
FreebaseLoadingDialog . prototype . _show _error = function ( msg , error ) {
console . log ( error ) ;
var self = this ;
var body = self . _elmts . dialogBody ;
body . html (
'<div class="freebase-loading-tripleloader-message">' +
'<h2>' + msg + '</h2>' +
( ( '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>' : "" ) +
'</div>'
) ;
this . _end ( ) ;
} ;
FreebaseLoadingDialog . prototype . _end = function ( ) {
var self = this ;
self . _elmts . loadButton . text ( "Close" ) . removeAttr ( "disabled" ) . unbind ( ) . click ( function ( ) {
self . _dismiss ( ) ;
} ) ;
self . _elmts . cancelButton . hide ( ) ;
self . _elmts . qaCheckboxContainer . hide ( ) ;
self . _elmts . authorization . hide ( ) ;
} ;