2010-04-21 23:08:34 +02:00
function FreebaseLoadingDialog ( ) {
this . _createDialog ( ) ;
2010-04-27 09:13:11 +02:00
this . _signedin = false ;
2010-04-21 23:08:34 +02:00
}
FreebaseLoadingDialog . prototype . _createDialog = function ( ) {
var self = this ;
2010-06-21 23:59:53 +02:00
var dialog = $ ( DOM . loadHTML ( "core" , "scripts/dialogs/freebase-loading-dialog.html" ) ) ;
this . _elmts = DOM . bind ( dialog ) ;
this . _elmts . cancelButton . click ( function ( ) { self . _dismiss ( ) ; } ) ;
this . _elmts . selector . buttonset ( ) ;
2010-08-24 08:06:07 +02:00
2010-04-23 10:25:52 +02:00
var provider = "www.freebase.com" ;
2010-06-21 23:59:53 +02:00
var authorization = this . _elmts . authorization ;
var loadButton = this . _elmts . loadButton ;
2010-04-27 09:13:11 +02:00
var check _authorization = function ( cont ) {
2010-08-22 08:16:13 +02:00
$ . get ( "/command/core/check-authorization/" + provider , function ( data ) {
2010-04-27 09:13:11 +02:00
if ( "status" in data && data . code == "/api/status/ok" ) {
2010-04-23 10:25:52 +02:00
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 ( ) {
2010-04-27 09:13:11 +02:00
self . _signedin = false ;
2010-06-21 23:59:53 +02:00
loadButton . attr ( "disabled" , "disabled" ) ;
2010-04-27 10:13:45 +02:00
$ ( "#freebase-loading-graph-selector-freebase" ) . attr ( "disabled" , "disabled" ) . button ( "refresh" ) ;
2010-04-23 10:25:52 +02:00
Sign . signout ( check _authorization , provider ) ;
} ) ;
2010-06-21 23:59:53 +02:00
loadButton . unbind ( ) . click ( function ( ) {
2010-04-24 10:02:32 +02:00
self . _load ( ) ;
2010-04-27 09:13:11 +02:00
} ) ;
self . _signedin = true ;
$ ( "#freebase-loading-source-name" ) . keyup ( ) ;
if ( typeof cont == "function" ) cont ( data ) ;
2010-04-23 10:25:52 +02:00
} else {
2010-04-27 09:13:11 +02:00
authorization . html ( '<a href="javascript:{}" bind="signin">Sign into Freebase</a> to enable loading' ) . show ( ) ;
DOM . bind ( authorization ) . signin . click ( function ( ) {
2010-04-24 10:02:32 +02:00
Sign . signin ( function ( ) {
2010-04-27 09:13:11 +02:00
check _authorization ( cont ) ;
2010-04-24 10:02:32 +02:00
} , provider ) ;
2010-04-23 10:25:52 +02:00
} ) ;
}
} , "json" ) ;
} ;
2010-04-27 09:13:11 +02:00
var check _allowed = function ( user _id , cont ) {
2010-08-22 08:16:13 +02:00
$ . get ( "/command/core/user-badges/" + provider ,
2010-04-27 21:47:12 +02:00
{ "user_id" : user _id } ,
2010-04-27 09:13:11 +02:00
function ( data ) {
if ( "status" in data && data . code == "/api/status/ok" ) {
2010-04-27 21:47:12 +02:00
var badges = data . result [ '!/type/usergroup/member' ] ;
var allowed = false ;
for ( var i = 0 ; i < badges . length ; i ++ ) {
var id = badges [ i ] . id ;
if ( id == "/en/metaweb_staff" ) {
allowed = true ;
break ;
}
}
if ( typeof cont == "function" ) cont ( allowed ) ;
2010-04-27 09:13:11 +02:00
} else {
self . _show _error ( "Error checking if user is a staff member" , data ) ;
}
} ,
"json"
) ;
} ;
var make _topic = function ( new _topic _id , topic _type , cont ) {
2010-04-27 10:13:45 +02:00
var mql _query = [ {
2010-04-27 09:13:11 +02:00
"create" : "unless_exists" ,
"name" : new _topic _id ,
2010-04-27 10:13:45 +02:00
"a:type" : topic _type ,
2010-04-27 21:47:12 +02:00
"b:type" : "/common/topic" ,
2010-04-27 09:13:11 +02:00
"id" : null ,
"guid" : null
2010-04-27 10:13:45 +02:00
} ] ;
2010-04-27 09:13:11 +02:00
2010-08-22 08:16:13 +02:00
$ . post ( "/command/core/mqlwrite/" + provider ,
2010-04-27 09:13:11 +02:00
{ "query" : JSON . stringify ( mql _query ) } ,
function ( data ) {
if ( "status" in data && data . code == "/api/status/ok" ) {
2010-04-27 10:13:45 +02:00
self . _elmts . source _id . val ( data . result [ 0 ] . id ) ;
2010-04-27 09:13:11 +02:00
if ( typeof cont == "function" ) cont ( ) ;
} else {
self . _show _error ( "Error creating new topic" , data ) ;
}
} ,
"json"
) ;
} ;
var show _triples = function ( cont ) {
2010-05-01 02:11:44 +02:00
$ . post (
2010-08-22 08:16:13 +02:00
"/command/core/preview-protograph?" + $ . param ( { project : theProject . id } ) ,
2010-07-07 00:24:16 +02:00
{
protograph : JSON . stringify ( theProject . overlayModels . freebaseProtograph || { } ) ,
engine : JSON . stringify ( ui . browsingEngine . getJSON ( ) )
} ,
2010-04-27 09:13:11 +02:00
function ( data ) {
2010-07-07 00:24:16 +02:00
var body = self . _elmts . dialogBody ;
2010-05-01 02:11:44 +02:00
if ( "tripleloader" in data ) {
2010-04-27 09:13:11 +02:00
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>' +
2010-05-01 02:11:44 +02:00
'<div class="freebase-loading-tripleloader-data">' + data . tripleloader + '</div>'
2010-04-27 09:13:11 +02:00
) ;
2010-06-21 23:59:53 +02:00
self . _elmts = DOM . bind ( dialog ) ;
2010-04-27 09:13:11 +02:00
self . _elmts . source _name . keyup ( function ( ) {
if ( self . _signedin && $ ( this ) . val ( ) != "" ) {
2010-06-21 23:59:53 +02:00
loadButton . removeAttr ( "disabled" ) ;
2010-04-27 09:13:11 +02:00
} else {
2010-06-21 23:59:53 +02:00
loadButton . attr ( "disabled" , "disabled" ) ;
2010-04-27 09:13:11 +02:00
}
} ) ;
self . _elmts . source _id . suggest ( {
"type" : "/dataworld/information_source" ,
"suggest_new" : "Click here to add a new information source"
2010-04-27 10:13:45 +02:00
} ) . bind ( "fb-select" , function ( e , data ) {
self . _elmts . source _id . val ( data . id ) ;
2010-04-27 09:13:11 +02:00
} ) . bind ( "fb-select-new" , function ( e , val ) {
make _topic ( val , "/dataworld/information_source" ) ;
} ) ;
2010-08-24 08:06:07 +02:00
$ . 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 ;
}
}
) ;
2010-04-27 09:13:11 +02:00
if ( typeof cont == "function" ) cont ( ) ;
2010-05-01 02:11:44 +02:00
} 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>'
) ;
2010-06-21 23:59:53 +02:00
self . _elmts = DOM . bind ( dialog ) ;
2010-05-01 02:11:44 +02:00
self . _end ( ) ;
2010-04-27 09:13:11 +02:00
}
2010-06-21 23:59:53 +02:00
self . _level = DialogSystem . showDialog ( dialog ) ;
2010-05-01 02:11:44 +02:00
} ,
"json"
2010-04-27 09:13:11 +02:00
) ;
} ;
show _triples ( function ( ) {
check _authorization ( function ( data ) {
check _allowed ( data . id , function ( is _allowed ) {
if ( is _allowed ) {
$ ( "#freebase-loading-graph-selector-freebase" ) . removeAttr ( "disabled" ) . button ( "refresh" ) ;
}
} ) ;
} ) ;
} ) ;
2010-04-24 10:02:32 +02:00
} ;
FreebaseLoadingDialog . prototype . _load = function ( ) {
var self = this ;
var freebase = self . _elmts . freebase . attr ( "checked" ) ;
2010-04-24 10:40:29 +02:00
2010-04-27 09:13:11 +02:00
var get _peacock _url = function ( url ) {
2010-08-24 08:06:07 +02:00
return "http://gridworks-loads.freebaseapps.com/load/" + url . split ( "/" ) . slice ( - 1 ) [ 0 ] ;
2010-05-05 02:26:26 +02:00
} ;
2010-04-27 09:13:11 +02:00
2010-04-24 10:40:29 +02:00
var doLoad = function ( ) {
2010-05-01 02:11:44 +02:00
var dismissBusy = DialogSystem . showBusy ( ) ;
2010-08-22 08:16:13 +02:00
$ . post ( "/command/core/upload-data" ,
2010-04-24 10:02:32 +02:00
{
project : theProject . id ,
"graph" : ( freebase ) ? "otg" : "sandbox" ,
2010-05-25 01:01:34 +02:00
"engine" : JSON . stringify ( ui . browsingEngine . getJSON ( ) ) ,
2010-04-27 09:13:11 +02:00
"source_name" : self . _elmts . source _name . val ( ) ,
"source_id" : self . _elmts . source _id . val ( )
2010-04-24 10:02:32 +02:00
} ,
function ( data ) {
2010-05-01 02:11:44 +02:00
dismissBusy ( ) ;
2010-06-21 23:59:53 +02:00
var body = self . _elmts . dialogBody ;
2010-04-30 08:12:42 +02:00
if ( "status" in data && typeof data . status == "object" && "code" in data . status && data . status . code == 200 ) {
2010-04-24 10:02:32 +02:00
body . html (
'<div class="freebase-loading-tripleloader-message">' +
2010-04-27 09:13:11 +02:00
'<h2>' + data . result . added + ' triples successfully scheduled for loading</h2>' +
2010-05-09 22:20:56 +02:00
'<h4>Follow the loading progress <a href="' + get _peacock _url ( data . result . status _url ) + '" target="_new">here</a>.</h4>' +
2010-04-24 10:02:32 +02:00
'</div>'
) ;
2010-04-27 09:13:11 +02:00
self . _end ( ) ;
2010-04-24 10:02:32 +02:00
} else {
2010-04-27 21:47:12 +02:00
self . _show _error ( "Error loading data" , data ) ;
2010-04-24 10:02:32 +02:00
}
} ,
"json"
) ;
2010-04-24 10:40:29 +02:00
} ;
if ( freebase ) {
var dialog = $ (
'<div id="freebase-confirmation-dialog" title="Are you sure?">' +
2010-04-27 09:13:11 +02:00
'<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 uploaded into <b>Freebase</b>?</td></tr></table>' +
2010-04-24 10:40:29 +02:00
'</div>'
) . dialog ( {
resizable : false ,
width : 400 ,
height : 230 ,
modal : true ,
buttons : {
2010-04-27 09:13:11 +02:00
'yes, I know what I\'m doing' : function ( ) {
2010-04-24 10:40:29 +02:00
$ ( this ) . dialog ( 'close' ) ;
doLoad ( ) ;
} ,
'cancel' : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
}
}
} ) ;
} else {
doLoad ( ) ;
2010-04-24 10:02:32 +02:00
}
2010-05-05 02:26:26 +02:00
} ;
2010-04-21 23:08:34 +02:00
FreebaseLoadingDialog . prototype . _dismiss = function ( ) {
DialogSystem . dismissUntil ( this . _level - 1 ) ;
} ;
2010-04-27 09:13:11 +02:00
FreebaseLoadingDialog . prototype . _show _error = function ( msg , error ) {
2010-06-21 23:59:53 +02:00
var body = self . _elmts . dialogBody ;
2010-04-27 09:13:11 +02:00
body . html (
'<div class="freebase-loading-tripleloader-message">' +
'<h2>' + msg + '</h2>' +
'<p>' + error . message + '</p>' +
( ( 'stack' in error ) ? '<pre>' + error . stack . replace ( /\\n/g , '\n' ) . replace ( /\\t/g , '\t' ) + '</p>' : "" ) +
'</div>'
) ;
this . _end ( ) ;
} ;
FreebaseLoadingDialog . prototype . _end = function ( ) {
var self = this ;
2010-06-21 23:59:53 +02:00
self . _elmts . loadButton . text ( "Close" ) . removeAttr ( "disabled" ) . unbind ( ) . click ( function ( ) {
2010-04-27 09:13:11 +02:00
self . _dismiss ( ) ;
} ) ;
2010-06-21 23:59:53 +02:00
self . _elmts . cancelButton . hide ( ) ;
2010-04-27 09:13:11 +02:00
self . _elmts . authorization . hide ( ) ;
self . _elmts . selector . hide ( ) ;
} ;