From 6b421c2c75405b84c6469f4bea5ba2ec460bc657 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 8 Mar 2010 20:56:04 +0000 Subject: [PATCH] In property suggest, bubble up properties of included types as well. git-svn-id: http://google-refine.googlecode.com/svn/trunk@241 7d457c2a-affb-35e4-300a-418c747d4874 --- .../webapp/scripts/util/custom-suggest.js | 146 +++++++++++++++--- 1 file changed, 125 insertions(+), 21 deletions(-) diff --git a/src/main/webapp/scripts/util/custom-suggest.js b/src/main/webapp/scripts/util/custom-suggest.js index 6a8fcbb70..ff424f30d 100644 --- a/src/main/webapp/scripts/util/custom-suggest.js +++ b/src/main/webapp/scripts/util/custom-suggest.js @@ -1,6 +1,38 @@ (function() { - var base = { - response: $.suggest.suggest.prototype.response + var oldResponse = $.suggest.suggest.prototype.response; + var typeToIncludedTypes = {}; + var resortByType = function(data, type) { + var schemaPrefixes = [ type + "/" ]; + + var includedTypes = typeToIncludedTypes[type]; + for (var i = 0; i < includedTypes.length; i++) { + schemaPrefixes.push(includedTypes[i] + "/"); + } + + var results = data.result; + var entries1 = []; + var entries2 = []; + + for (var i = 0; i < results.length; i++) { + var result = results[i]; + + var matched = false; + for (var j = 0; j < schemaPrefixes.length; j++) { + var schemaPrefix = schemaPrefixes[j]; + if (result.id.substring(0, schemaPrefix.length) == schemaPrefix) { + matched = true; + break; + } + } + + if (matched) { + entries1.push(result); + } else { + entries2.push(result); + } + } + + data.result = entries1.concat(entries2); }; /* @@ -13,26 +45,98 @@ {}, $.suggest.suggest.prototype, { - response: function(data) { - if ("schema" in this.options) { - var schema = this.options.schema + "/"; - - var results = data.result; - var entries1 = []; - var entries2 = []; - - for (var i = 0; i < results.length; i++) { - var result = results[i]; - if (result.id.substring(0, schema.length) == schema) { - entries1.push(result); - } else { - entries2.push(result); - } - } - - data.result = entries1.concat(entries2); + request: function(val, start) { + var self = this, + o = this.options; + + if (this.ac_xhr) { + this.ac_xhr.abort(); + this.ac_xhr = null; } - base.response.apply(this, [ data ]); + var data = { + query: val + }; + if (start) { + data.start = start; + } + + $.extend(data, o.ac_param); + $.extend(data, { limit: 50 }); + + var baseUrl = "http://api.freebase.com/api/service/search"; + var url = baseUrl + "?" + $.param(data), + cached = $.suggest.cache[url]; + if (cached) { + this.response(cached, start ? start : -1, true); + return; + } + + clearTimeout(this.request.timeout); + this.request.timeout = + setTimeout(function() { + self.ac_xhr = $.ajax({ + url: baseUrl, + data: data, + beforeSend: function() { + var calls = self.input.data("request.count.suggest") || 0; + if (!calls) { + self.trackEvent(self.name, "start_session"); + } + calls += 1; + self.trackEvent(self.name, "request", "count", calls); + self.input.data("request.count.suggest", calls); + }, + success: function(data) { + data.prefix = val; // we need this so that the rest of suggest wouldn't error out + + if ("schema" in o) { + var type = o.schema; + var apply = function() { + resortByType(data, type); + + $.suggest.cache[url] = data; + self.response(data, start ? start : -1); + }; + if (type in typeToIncludedTypes) { + apply(); + } else { + var query = { + query: { + "id" : type, + "/freebase/type_hints/included_types": [] + } + }; + $.getJSON( + "http://api.freebase.com/api/service/mqlread?" + $.param({ query: JSON.stringify(query) }) + "&callback=?", + null, + function(d) { + var types = []; + if ("result" in d) { + types = d.result["/freebase/type_hints/included_types"]; + } + typeToIncludedTypes[type] = types; + + apply(); + }, + "jsonp" + ); + } + } else { + self.response(data, start ? start : -1); + } + }, + error: function(xhr) { + self.trackEvent(self.name, "request", "error", {url:this.url, response: xhr ? xhr.responseText : ''}); + }, + complete: function(xhr) { + if (xhr) { + self.trackEvent(self.name, "request", "tid", xhr.getResponseHeader("X-Metaweb-TID")); + } + }, + dataType: "jsonp", + cache: true + }); + }, o.xhr_delay); }, create_item: function(data, response_data) { var css = this.options.css;