Skip to content

Commit

Permalink
feat: formatNumber in Stats widget
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Sep 15, 2015
1 parent 4cc2a5b commit cf6a83c
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 21 deletions.
5 changes: 4 additions & 1 deletion components/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Template = require('./Template');
class Stats extends React.Component {
render() {
var template = this.props.template;
var templateHelpers = this.props.templateHelpers;
var data = {
nbHits: this.props.nbHits,
hasNoResults: this.props.nbHits === 0,
Expand All @@ -17,6 +18,7 @@ class Stats extends React.Component {
<Template
data={data}
template={template}
templateHelpers={templateHelpers}
/>
);
}
Expand All @@ -28,7 +30,8 @@ Stats.propTypes = {
template: React.PropTypes.oneOfType([
React.PropTypes.func,
React.PropTypes.string
]).isRequired
]).isRequired,
templateHelpers: React.PropTypes.object
};

module.exports = Stats;
7 changes: 6 additions & 1 deletion components/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ var renderTemplate = require('../lib/utils').renderTemplate;

class Template {
render() {
var content = renderTemplate(this.props.template, this.props.data);
var content = renderTemplate({
template: this.props.template,
templateHelpers: this.props.templateHelpers,
data: this.props.data
});

return <div dangerouslySetInnerHTML={{__html: content}} />;
}
Expand All @@ -15,6 +19,7 @@ Template.propTypes = {
React.PropTypes.string,
React.PropTypes.func
]).isRequired,
templateHelpers: React.PropTypes.object,
data: React.PropTypes.object
};

Expand Down
3 changes: 2 additions & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var instantsearch = require('../');
var search = instantsearch({
appId: 'latency',
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
indexName: 'instant_search'
indexName: 'instant_search',
numberLocale: 'fr-FR'
});

search.addWidget(
Expand Down
19 changes: 15 additions & 4 deletions lib/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InstantSearch {
appId = null,
apiKey = null,
indexName = null,
numberLocale = 'en-EN',
searchParameters = {}
}) {
if (appId === null || apiKey === null || indexName === null) {
Expand All @@ -22,14 +23,18 @@ Usage: instantsearch({
throw new Error(usage);
}


var client = algoliasearch(appId, apiKey);

this.client = client;
this.helper = null;
this.indexName = indexName;
this.searchParameters = searchParameters || {};
this.widgets = [];
this.templateHelpers = {
formatNumber(number) {
return Number(number).toLocaleString(numberLocale);
}
};
}

addWidget(widgetDefinition) {
Expand Down Expand Up @@ -67,10 +72,16 @@ Usage: instantsearch({

_render(helper, results, state) {
forEach(this.widgets, function(widget) {
if (widget.render) {
widget.render(results, state, helper);
if (!widget.render) {
return;
}
});
widget.render({
templateHelpers: this.templateHelpers,
results,
state,
helper
});
}, this);
}

_init(state, helper) {
Expand Down
29 changes: 23 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,35 @@ function isDomElement(o) {
return o instanceof HTMLElement || o && o.nodeType > 0;
}

function renderTemplate(template, data) {
function renderTemplate({template, templateHelpers, data}) {
var hogan = require('hogan.js');
var forEach = require('lodash/collection/forEach');
var content;

if (typeof template === 'string') {
content = hogan.compile(template).render(data);
} else if (typeof template === 'function') {
content = template(data);
} else {
if (typeof template !== 'string' && typeof template !== 'function') {
throw new Error('Template must be `string` or `function`');
}

if (typeof template === 'function') {
content = template(data);
}

if (typeof template === 'string') {
// We add all our template helper methods to the template as lambdas. Note
// that lambdas in Mustache are supposed to accept a second argument of
// `render` to get the rendered value, not the literal `{{value}}`. But
// this is currently broken (see
// https://github.com/twitter/hogan.js/issues/222).
forEach(templateHelpers, function(method, name) {
data['_' + name] = function() {
return function(value) {
return method(hogan.compile(value).render(data));
};
};
});
content = hogan.compile(this.props.template).render(data);
}

return content;
}

Expand Down
2 changes: 1 addition & 1 deletion widgets/hits.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function hits({container = null, templates = {}, hitsPerPage = 20}) {

return {
getConfiguration: () => ({hitsPerPage}),
render: function(results, state, helper) {
render: function({results, helper}) {
React.render(
<Hits
hits={results.hits}
Expand Down
2 changes: 1 addition & 1 deletion widgets/index-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function indexSelector({
}
},

render: function(results, state, helper) {
render: function({helper}) {
var containerId = containerNode.id;
React.render(
<IndexSelector
Expand Down
2 changes: 1 addition & 1 deletion widgets/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function menu({
attributes: [facetName]
}]
}),
render: function(results, state, helper) {
render: function({results, helper}) {
React.render(
<RefinementList
rootClass={cx(rootClass)}
Expand Down
2 changes: 1 addition & 1 deletion widgets/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function pagination({container, cssClass, labels, maxPages} = {}) {
var containerNode = utils.getContainerNode(container);

return {
render: function(results, state, helper) {
render: function({results, helper}) {
var nbPages = results.nbPages;
if (maxPages !== undefined) {
nbPages = Math.min(maxPages, results.nbPages);
Expand Down
2 changes: 1 addition & 1 deletion widgets/refinement-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function refinementList({
getConfiguration: () => ({
[operator === 'and' ? 'facets' : 'disjunctiveFacets']: [facetName]
}),
render: function(results, state, helper) {
render: function({results, helper}) {
React.render(
<RefinementList
rootClass={cx(rootClass)}
Expand Down
3 changes: 2 additions & 1 deletion widgets/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ function stats({container = null, template = defaultTemplate}) {
}

return {
render: function(results) {
render: function({results, templateHelpers}) {
React.render(
<Stats
nbHits={results.nbHits}
processingTimeMS={results.processingTimeMS}
template={template}
templateHelpers={templateHelpers}
/>,
containerNode
);
Expand Down
2 changes: 1 addition & 1 deletion widgets/stats/template.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
{{#hasNoResults}}No results{{/hasNoResults}}
{{#hasOneResult}}1 result{{/hasOneResult}}
{{#hasManyResults}}{{nbHits}} results{{/hasManyResults}}
{{#hasManyResults}}{{#_formatNumber}}{{nbHits}}{{/_formatNumber}} results{{/hasManyResults}}
<small>found in {{processingTimeMS}}ms</small>
</div>
2 changes: 1 addition & 1 deletion widgets/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function toggle({
getConfiguration: () => ({
facets: [facetName]
}),
render: function(results, state, helper) {
render: function({helper}) {
var isRefined = helper.hasRefinements(facetName);

function toggleFilter() {
Expand Down

0 comments on commit cf6a83c

Please sign in to comment.