Created
November 11, 2008 00:11
-
-
Save graynorton/23699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////// | |
// Support for Freebase nouns // | |
//////////////////////////////// | |
var sandbox = this; | |
jQuery.ajax( { | |
url: "http://graynorton.com/ubiquity/freebase-noun-support.js", | |
success: function( script ) { | |
eval.call( sandbox, script ); // using jQuery.ajax + eval because jQuery.getScript doesn't work in Ubiquity Command sandbox | |
}, | |
async: false, | |
dataType: "text" } ); | |
/////////////////////////////// | |
// "Freebase type" noun type // | |
/////////////////////////////// | |
var noun_type_freebase_type = freebaseNounType( "type", [ "/freebase/type_profile" ] ); | |
//////////////////////////////////////////////////////////////////////// | |
// Slightly modified version of Will Moffat's Freebase search command // | |
// http://hamstersoup.com/freebase/ubiquity/ubiquity.js // | |
// See 'GN' comments below // | |
//////////////////////////////////////////////////////////////////////// | |
var freebase = {}; | |
var $ = jQuery; | |
(function() { | |
freebase.preview = function(pblock, directObject, args){ | |
var searchTerm = directObject.text; | |
pblock.innerHTML = 'Searches Freebase for <b>'+searchTerm+'</b>'; | |
var params = {prefix: searchTerm, limit: 4}; | |
// GN: if the "of-type" modifier is present, we use it | |
// to set the type prior to calling Freebase search | |
if (args["of-type"].data !== null) { | |
params.type = args["of-type"].data.guid; | |
} | |
var url = "http://www.freebase.com/api/service/search"; | |
var search_callback = function(response) { show_preview(pblock,response); }; | |
$.get( url, params, search_callback, "json"); | |
}; | |
var style_rules = [ | |
'<style type="text/css">', | |
' ul { list-style: none; padding:0; margin:0; } ', | |
' li { padding: 0.3em; } ', | |
' li:hover { background: #C93 } ', | |
' .blurb { font-size:small; } ', | |
' .item { height:4.5em; } ', | |
' .headline { height: 1em; overflow: hidden; padding-bottom:0.2em;} ', | |
' .item_name { font-weight:bold; } ', | |
' .item_types { font-size: small; color:grey; font-style:italic; padding:1em; } ', | |
' .thumb,.missing { float:left; width:45px; height:45px; padding: 2px; border: solid thin grey; margin-right:0.5em; }', | |
'</style>\n' | |
]; | |
function name(item) { | |
return '<div class="headline"><span class="item_name">'+ item.name +'</span>'+types(item)+'</div>'; | |
} | |
function types(item) { | |
if (!item.type || !item.type.length) { return ''; } | |
var major_types = $.grep(item.type, function(t) { | |
if (t.id.indexOf('/common/')===0 || t.id.indexOf('/user/')===0) { return false; } // ignore | |
return true; | |
}); | |
var names = $.map(major_types,function(t) { return t.name || '??'; } ); | |
return '<span class="item_types">' + names.join(', ') + '</span>'; | |
} | |
function thumbnail(item) { | |
if (!item.image || !item.image.id) { return '<div class="missing"> </div>'; } | |
var params = '?maxheight=45&mode=fillcrop&maxwidth=45'; | |
var url = 'http://www.freebase.com/api/trans/image_thumb'+item.image.id+params; | |
return '<img class="thumb" src="'+url+'" />'; | |
} | |
function blurb(item) { | |
if (!item.article || !item.article.id) { return '<i>Missing description</i>'; } | |
return '<span class="blurb" bid="'+ item.article.id +'"></span>'; | |
} | |
// this is the only function which modifes the innerHTML of the preview block | |
function show_preview(pblock,response) { | |
if (response.status != '200 OK') { pblock.innerHTML = 'Error: ' + response.status; return; } | |
var h=style_rules.join('\n'); | |
h += '<ul>'; | |
for (var i=0;i<response.result.length;i++) { | |
var item = response.result[i]; | |
h += '<li><a href="http://www.freebase.com/view' + item.id +'">'; | |
h += thumbnail(item) + '<div class="item">'+name(item)+blurb(item)+'</div>'; | |
h += '</a></li>\n'; | |
} | |
h += '</ul>'; | |
pblock.innerHTML = h; | |
$('.blurb',pblock).each(function(i,blurb) { | |
var url = 'http://www.freebase.com/api/trans/blurb' + $(blurb).attr('bid') + '?maxlength=190'; | |
$.get( url, null, function(response) { $(blurb).text(response); }); | |
}); | |
} | |
})(); | |
makeSearchCommand({ | |
name: "freebase", | |
modifiers: { "of-type": noun_type_freebase_type }, // GN: now takes "of-type" modifier | |
url: "http://www.freebase.com/search?query={QUERY}", | |
icon: "http://www.freebase.com/favicon.ico", | |
description: 'Searches <a href="http://www.freebase.com>Freebase</a> for your words (shows previews)', | |
homepage: "http://blog.hamstersoup.com/", | |
author: { name: "Will Moffat", email: "will_ub@hamstersoup.com"}, | |
preview: freebase.preview | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment