Merge branch 'master' into gh-pages

This commit is contained in:
Rufus Pollock
2012-10-20 02:33:08 +01:00
4 changed files with 90 additions and 52 deletions

View File

@@ -74,6 +74,7 @@ var SearchView = Backbone.View.extend({
<div class="controls"> \ <div class="controls"> \
<div class="query-here"></div> \ <div class="query-here"></div> \
</div> \ </div> \
<div class="total"><h2><span></span> records found</h2></div> \
<div class="body"> \ <div class="body"> \
<div class="sidebar"></div> \ <div class="sidebar"></div> \
<div class="results"> \ <div class="results"> \
@@ -99,6 +100,8 @@ var SearchView = Backbone.View.extend({
}); });
this.el.html(html); this.el.html(html);
this.el.find('.total span').text(this.model.recordCount);
var view = new recline.View.FacetViewer({ var view = new recline.View.FacetViewer({
model: this.model model: this.model
}); });
@@ -125,14 +128,36 @@ function setupMoreComplexExample(config) {
var dataset = new recline.Model.Dataset(config); var dataset = new recline.Model.Dataset(config);
// async as may be fetching remote // async as may be fetching remote
dataset.fetch().done(function() { dataset.fetch().done(function() {
if (dataset.get('url').indexOf('openspending') === -1) { var template = templates[dataset.get('url')] || templates['generic'];
var searchView = new SearchView({
el: $el,
model: dataset,
template: template
});
searchView.render();
dataset.queryState.set({
size: 5
},
{silent: true}
);
if (dataset.get('url') in templates) {
// for gdocs example
dataset.queryState.addFacet('cause');
}
dataset.query();
});
};
var templates = {
// generic template function // generic template function
var template = function(record) { 'generic': function(record) {
var template = '<div class="record"> \ var template = '<div class="record"> \
<ul> \ <ul> \
{{#data}} \ {{#data}} \
<li>{{key}}: {{value}}</li> \ <li>{{key}}: {{value}}</li> \
{{/data}} \ {{/data}} \
</ul> \
</div> \ </div> \
'; ';
var data = _.map(_.keys(record), function(key) { var data = _.map(_.keys(record), function(key) {
@@ -141,10 +166,8 @@ function setupMoreComplexExample(config) {
return Mustache.render(template, { return Mustache.render(template, {
data: data data: data
}); });
} },
} else { 'http://openspending.org/api/search': function(record) {
// generic template function
var template = function(record) {
record['time'] = record['time.label_facet'] record['time'] = record['time.label_facet']
var template = '<div class="record"> \ var template = '<div class="record"> \
<h3> \ <h3> \
@@ -155,6 +178,7 @@ function setupMoreComplexExample(config) {
{{#data}} \ {{#data}} \
<li>{{key}}: {{value}}</li> \ <li>{{key}}: {{value}}</li> \
{{/data}} \ {{/data}} \
</ul> \
</div> \ </div> \
'; ';
var data = []; var data = [];
@@ -168,27 +192,29 @@ function setupMoreComplexExample(config) {
amount_formatted: formatAmount(record['amount']), amount_formatted: formatAmount(record['amount']),
data: data data: data
}); });
}
}
var searchView = new SearchView({
el: $el,
model: dataset,
template: template
});
searchView.render();
dataset.queryState.set({
size: 10
}, },
{silent: true} 'https://docs.google.com/spreadsheet/ccc?key=0Aon3JiuouxLUdExXSTl2Y01xZEszOTBFZjVzcGtzVVE': function(record) {
); var template = '<div class="record"> \
if (dataset.get('url').indexOf('openspending') != -1) { <h3> \
dataset.queryState.addFacet('dataset'); {{record.incidentsite}} &ndash; {{record.datereported}} &ndash; {{record.estimatedspillvolumebbl}} barrels \
} </h3> \
dataset.query(); <ul> \
{{#data}} \
<li>{{key}}: {{value}}</li> \
{{/data}} \
</ul> \
</div> \
';
var data = [];
_.each(_.keys(record), function(key) {
data.push({ key: key, value: record[key] });
}); });
}; return Mustache.render(template, {
record: record,
data: data
});
}
}
var sampleData = [ var sampleData = [
{ {

View File

@@ -73,6 +73,11 @@ ul.facet-items {
list-style-type: none; list-style-type: none;
margin-left: 0; margin-left: 0;
} }
.record ul {
list-style-type: none;
margin-left: 0;
}
</style> </style>
<div class="page-header"> <div class="page-header">
@@ -81,7 +86,7 @@ ul.facet-items {
<div class="info"> <div class="info">
<p>This demo shows how Recline can be used to build a search app. It includes faceting as well as search. You can find the <a href="app.js">source javascript here</a> &ndash; please feel free to reuse!</p> <p>This demo shows how Recline can be used to build a search app. It includes faceting as well as search. You can find the <a href="app.js">source javascript here</a> &ndash; please feel free to reuse!</p>
<p>The default version uses some local example data but you can also connect directly to any other backend supported by Recline, in particular SOLR or ElasticSearch. For example, here's an <a href="?backend=solr&amp;url=http://openspending.org/api/search">example of this app running</a> off the OpenSpending SOLR-style search API.</p> <p>The default setup uses local example data but you can also connect directly to any other <a href="{{page.root}}/docs/backends.html">backend supported by Recline</a>, for example SOLR, ElasticSearch or even a google docs spreadsheet. As an example: here's an <a href="?backend=solr&amp;url=http://openspending.org/api/search">example running against the SOLR-style OpenSpending API</a> and here's an example <a href="?backend=gdocs&amp;url=https://docs.google.com/spreadsheet/ccc?key=0Aon3JiuouxLUdExXSTl2Y01xZEszOTBFZjVzcGtzVVE">running against a GDocs spreadsheet (Oil spills in the Niger Delta)</a>.</p>
</div> </div>
<hr /> <hr />

View File

@@ -130,7 +130,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
// Convenience function to get GDocs JSON API Url from standard URL // Convenience function to get GDocs JSON API Url from standard URL
my.getGDocsAPIUrls = function(url) { my.getGDocsAPIUrls = function(url) {
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=YYY // https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=YYY
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*gid=([\d]+).*/; var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+)[^#]*(#gid=([\d]+).*)?/;
var matches = url.match(regex); var matches = url.match(regex);
var key; var key;
var worksheet; var worksheet;
@@ -139,7 +139,10 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
if(!!matches) { if(!!matches) {
key = matches[1]; key = matches[1];
// the gid in url is 0-based and feed url is 1-based // the gid in url is 0-based and feed url is 1-based
worksheet = parseInt(matches[2]) + 1; worksheet = parseInt(matches[3]) + 1;
if (isNaN(worksheet)) {
worksheet = 1;
}
urls = { urls = {
worksheet : 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json', worksheet : 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json',
spreadsheet: 'https://spreadsheets.google.com/feeds/worksheets/'+ key +'/public/basic?alt=json' spreadsheet: 'https://spreadsheets.google.com/feeds/worksheets/'+ key +'/public/basic?alt=json'

View File

@@ -310,6 +310,10 @@ test("GDocs Backend.getUrl", function() {
var exp2 = 'https://spreadsheets.google.com/feeds/worksheets/' + key + '/public/basic?alt=json' var exp2 = 'https://spreadsheets.google.com/feeds/worksheets/' + key + '/public/basic?alt=json'
equal(exp1, out.worksheet); equal(exp1, out.worksheet);
equal(exp2, out.spreadsheet); equal(exp2, out.spreadsheet);
var url = 'https://docs.google.com/spreadsheet/ccc?key=' + key;
var out = recline.Backend.GDocs.getGDocsAPIUrls(url);
equal(out.worksheet, exp1);
}); });
})(this.jQuery); })(this.jQuery);