doofinder
Advanced tools
Comparing version 1.0.8 to 2.0.0
{ | ||
"name": "doofinder", | ||
"description": "Javascript Library for Doofinder Search API", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"main": "dist/doofinder.js", | ||
@@ -6,0 +6,0 @@ "authors": [ |
(function() { | ||
module.exports = { | ||
version: "1.0.8", | ||
version: "2.0.0", | ||
Client: require("./client"), | ||
Handlebars: require("handlebars"), | ||
Mustache: require("mustache"), | ||
Widget: require("./widget"), | ||
@@ -7,0 +7,0 @@ Controller: require("./controller"), |
/* | ||
* author: @ecoslado | ||
* 2015 06 30 | ||
* 2015 09 30 | ||
*/ | ||
(function() { | ||
var addHelpers; | ||
var $, addHelpers; | ||
addHelpers = function(Handlebars, parameters, currency, translations, extraHelpers) { | ||
var helpers, key, results; | ||
$ = require("./jquery"); | ||
addHelpers = function(context, parameters, currency, translations, extraHelpers) { | ||
var helpers; | ||
if (!currency) { | ||
@@ -28,104 +30,66 @@ currency = { | ||
helpers = { | ||
'url-params': function(url, query) { | ||
var i, paramsArray, paramsFinal, params_str; | ||
paramsFinal = url; | ||
if (paramsFinal) { | ||
paramsArray = []; | ||
if (parameters.queryParam) { | ||
paramsArray.push(parameters.queryParam + '=' + query); | ||
} | ||
if (parameters.extraParams) { | ||
for (i in parameters.extraParams) { | ||
paramsArray.push(i + '=' + parameters.extraParams[i]); | ||
'url-params': function() { | ||
return function(text, render) { | ||
var i, paramsArray, paramsFinal, params_str; | ||
paramsFinal = render(text); | ||
if (paramsFinal) { | ||
paramsArray = []; | ||
if (parameters.queryParam) { | ||
paramsArray.push(parameters.queryParam + '=' + context.query); | ||
} | ||
} | ||
if (paramsArray.length) { | ||
params_str = paramsArray.join('&'); | ||
if (paramsFinal.match(/\?/)) { | ||
paramsFinal = paramsFinal + '&' + params_str; | ||
} else { | ||
paramsFinal = paramsFinal + '?' + params_str; | ||
if (parameters.extraParams) { | ||
for (i in parameters.extraParams) { | ||
paramsArray.push(i + '=' + parameters.extraParams[i]); | ||
} | ||
} | ||
if (paramsArray.length) { | ||
params_str = paramsArray.join('&'); | ||
if (paramsFinal.match(/\?/)) { | ||
paramsFinal = paramsFinal + '&' + params_str; | ||
} else { | ||
paramsFinal = paramsFinal + '?' + params_str; | ||
} | ||
} | ||
} | ||
} | ||
return paramsFinal; | ||
return paramsFinal; | ||
}; | ||
}, | ||
'remove-protocol': function(options) { | ||
return options.fn(this).replace(/^https?:/, ''); | ||
'remove-protocol': function() { | ||
return function(text, render) { | ||
var url; | ||
url = render(text); | ||
return url.replace(/^https?:/, ''); | ||
}; | ||
}, | ||
'format-currency': function(options) { | ||
var base, mod, neg, number, power, value; | ||
value = parseFloat(options.fn(this)); | ||
if (!value && value !== 0) { | ||
return ''; | ||
} | ||
power = Math.pow(10, currency.precision); | ||
number = (Math.round(value * power) / power).toFixed(currency.precision); | ||
neg = number < 0 ? '-' : ''; | ||
base = '' + parseInt(Math.abs(number || 0).toFixed(currency.precision), 10); | ||
mod = base.length > 3 ? base.length % 3 : 0; | ||
number = neg + (mod ? base.substr(0, mod) + currency.thousand : '') + base.substr(mod).replace(/(\d{3})(?=\d)/g, '$1' + currency.thousand) + (currency.precision ? currency.decimal + Math.abs(number).toFixed(currency.precision).split('.')[1] : ''); | ||
return currency.format.replace(/%s/g, currency.symbol).replace(/%v/g, number); | ||
'format-currency': function() { | ||
return function(text, render) { | ||
var base, mod, neg, number, power, price, value; | ||
price = render(text); | ||
value = parseFloat(price); | ||
if (!value && value !== 0) { | ||
return ''; | ||
} | ||
power = Math.pow(10, currency.precision); | ||
number = (Math.round(value * power) / power).toFixed(currency.precision); | ||
neg = number < 0 ? '-' : ''; | ||
base = '' + parseInt(Math.abs(number || 0).toFixed(currency.precision), 10); | ||
mod = base.length > 3 ? base.length % 3 : 0; | ||
number = neg + (mod ? base.substr(0, mod) + currency.thousand : '') + base.substr(mod).replace(/(\d{3})(?=\d)/g, '$1' + currency.thousand) + (currency.precision ? currency.decimal + Math.abs(number).toFixed(currency.precision).split('.')[1] : ''); | ||
return currency.format.replace(/%s/g, currency.symbol).replace(/%v/g, number); | ||
}; | ||
}, | ||
'translate': function(options) { | ||
var key; | ||
key = options.fn(this); | ||
if (translations && key in translations) { | ||
return translations[key]; | ||
} else { | ||
return key; | ||
} | ||
}, | ||
'eq': function(lvalue, rvalue, options) { | ||
if (arguments.length < 3) { | ||
throw new Error('2 parameters are required for helper.'); | ||
} | ||
if (lvalue !== rvalue) { | ||
return options.inverse(this); | ||
} else { | ||
return options.fn(this); | ||
} | ||
}, | ||
'lt': function(lvalue, rvalue, options) { | ||
if (arguments.length < 3) { | ||
throw new Error('2 parameters are required for helper.'); | ||
} | ||
if (lvalue < rvalue) { | ||
return options.fn(this); | ||
} else { | ||
return options.inverse(this); | ||
} | ||
}, | ||
'gt': function(lvalue, rvalue, options) { | ||
if (arguments.length < 3) { | ||
throw new Error('2 parameters are required for helper.'); | ||
} | ||
if (lvalue > rvalue) { | ||
return options.fn(this); | ||
} else { | ||
return options.inverse(this); | ||
} | ||
}, | ||
'times': function(n, block) { | ||
var accum, i, j, ref; | ||
accum = ''; | ||
for (i = j = 1, ref = n; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) { | ||
accum += block.fn(i); | ||
} | ||
return accum; | ||
}, | ||
'module': function(a, b, options) { | ||
return parseInt(a) % parseInt(b); | ||
'translate': function() { | ||
return function(text, render) { | ||
var key; | ||
key = render(text); | ||
if (translations && key in translations) { | ||
return translations[key]; | ||
} else { | ||
return key; | ||
} | ||
}; | ||
} | ||
}; | ||
for (key in helpers) { | ||
Handlebars.registerHelper(key, helpers[key]); | ||
} | ||
if (extraHelpers) { | ||
results = []; | ||
for (key in extraHelpers) { | ||
results.push(Handlebars.registerHelper(key, extraHelpers[key])); | ||
} | ||
return results; | ||
} | ||
$.extend(helpers, extraHelpers); | ||
return $.extend(context, helpers); | ||
}; | ||
@@ -132,0 +96,0 @@ |
@@ -42,2 +42,3 @@ | ||
function Display(container, template, options) { | ||
this.template = template; | ||
if (options == null) { | ||
@@ -47,12 +48,7 @@ options = {}; | ||
this.container = container; | ||
this.handlebars = require("handlebars"); | ||
this.mustache = require("mustache"); | ||
this.extraContext = options.templateVars; | ||
addHelpers(this.handlebars, options.urlParams, options.currency, options.translations, options.templateFunctions); | ||
if (template.constructor === String) { | ||
this.template = this.handlebars.compile(template); | ||
} else if (template instanceof Function) { | ||
this.template = template; | ||
} else { | ||
throw Error("The provided template is not the right type." + " String or rendered handlebars expected."); | ||
} | ||
this.addHelpers = function(context) { | ||
return addHelpers(context, options.urlParams, options.currency, options.translations, options.templateFunctions); | ||
}; | ||
Display.__super__.constructor.call(this, container); | ||
@@ -75,9 +71,7 @@ } | ||
context = $.extend(true, res, this.extraContext || {}); | ||
html = this.template(context); | ||
try { | ||
$(this.container).html(html); | ||
return this.trigger("df:rendered", [res]); | ||
} catch (_error) { | ||
throw Error("widget.Display: Error while rendering." + " The container you are trying to access does not already exist."); | ||
} | ||
context.is_first = true; | ||
this.addHelpers(context); | ||
html = this.mustache.render(this.template, context); | ||
$(this.container).html(html); | ||
return this.trigger("df:rendered", [res]); | ||
}; | ||
@@ -95,3 +89,8 @@ | ||
Display.prototype.renderNext = function(res) { | ||
this.render(res); | ||
var context, html; | ||
context = $.extend(true, res, this.extraContext || {}); | ||
context.is_first = false; | ||
this.addHelpers(context); | ||
html = this.mustache.render(this.template, context); | ||
$(this.container).html(html); | ||
return this.trigger("df:rendered", [res]); | ||
@@ -98,0 +97,0 @@ }; |
@@ -54,3 +54,3 @@ | ||
}, this.extraContext || {}); | ||
html = this.template(context); | ||
html = this.mustache.render(this.template, context); | ||
$(this.container).html(html); | ||
@@ -57,0 +57,0 @@ range = { |
@@ -35,3 +35,3 @@ | ||
if (!options.template) { | ||
template = '{{#if @index}}' + '<hr class="df-separator">' + '{{/if}}' + '<div class="df-facets">' + '<a href="#" class="df-panel__title" data-toggle="panel">{{label}}</a>' + '<div class="df-facets__content">' + '<ul>' + '{{#each terms}}' + '<li>' + '<a href="#" class="df-facet {{#if selected}}df-facet--active{{/if}}" data-facet="{{../name}}"' + 'data-value="{{ term }}">{{ term }} <span' + 'class="df-facet__count">{{ count }}</span></a>' + '</li>' + '{{/each}}'; | ||
template = '{{#if @index}}' + '<hr class="df-separator">' + '{{/if}}' + '<div class="df-facets">' + '<a href="#" class="df-panel__title" data-toggle="panel">{{label}}</a>' + '<div class="df-facets__content">' + '<ul>' + '{{#each terms}}' + '<li>' + '<a href="#" class="df-facet {{#if selected}}df-facet--active{{/if}}" data-facet="{{name}}"' + 'data-value="{{ term }}">{{ term }} <span' + 'class="df-facet__count">{{ count }}</span></a>' + '</li>' + '{{/each}}'; | ||
} else { | ||
@@ -93,3 +93,3 @@ template = options.template; | ||
TermFacet.prototype.render = function(res) { | ||
var context, html, i, j, len, len1, ref, ref1, term, totalSelected; | ||
var anySelected, context, html, i, key, len, ref, ref1, term, totalSelected; | ||
if (!res.facets || !res.facets[this.name]) { | ||
@@ -102,2 +102,3 @@ throw Error("Error in TermFacet: " + this.name + " facet is not configured."); | ||
totalSelected = 0; | ||
anySelected = false; | ||
if (res.filter && res.filter.terms && res.filter.terms[this.name]) { | ||
@@ -108,2 +109,3 @@ ref = res.filter.terms[this.name]; | ||
this.selected[term] = 1; | ||
anySelected = true; | ||
totalSelected += 1; | ||
@@ -114,4 +116,6 @@ } | ||
ref1 = res.facets[this.name].terms; | ||
for (j = 0, len1 = ref1.length; j < len1; j++) { | ||
term = ref1[j]; | ||
for (key in ref1) { | ||
term = ref1[key]; | ||
term.key = key; | ||
term.name = this.name; | ||
if (this.selected[term.term]) { | ||
@@ -124,2 +128,3 @@ term.selected = 1; | ||
context = $.extend(true, { | ||
any_selected: anySelected, | ||
total_selected: totalSelected, | ||
@@ -129,3 +134,4 @@ name: this.name, | ||
}, this.extraContext || {}); | ||
html = this.template(context); | ||
this.addHelpers(context); | ||
html = this.mustache.render(this.template, context); | ||
$(this.container).html(html); | ||
@@ -132,0 +138,0 @@ return this.trigger('df:rendered', [res]); |
@@ -43,3 +43,3 @@ | ||
if (!options.template) { | ||
template = '<ul>{{#each results}}' + ' <li>{{#each this}}' + ' <b>{{@key}}</b>:{{this}}<br></li>' + ' {{/each}}</div>' + ' {{/each}}' + ' </ul>'; | ||
template = '<ul>{{#results}}' + ' <li>' + ' <b>{{title}}</b>:{{description}}<br></li>' + ' {{/results}}' + ' </ul>'; | ||
} else { | ||
@@ -46,0 +46,0 @@ template = options.template; |
@@ -45,3 +45,3 @@ | ||
if (!options.template) { | ||
template = '<ul>{{#each results}}' + ' <li>{{#each this}}' + ' <b>{{@key}}</b>:{{this}}<br>' + ' {{/each}}' + ' </li>' + ' {{/each}}' + ' </ul>'; | ||
template = '<ul>{{#results}}{{@index}}<li><b>{{title}}</b>:{{description}}<br></li>{{/results}}</ul>'; | ||
} else { | ||
@@ -81,4 +81,2 @@ template = options.template; | ||
ScrollResults.prototype.render = function(res) { | ||
var context; | ||
context = $.extend(true, res, this.extraContext || {}); | ||
ScrollResults.__super__.render.call(this, res); | ||
@@ -100,4 +98,2 @@ return this.trigger("df:rendered", res); | ||
ScrollResults.prototype.renderNext = function(res) { | ||
var context; | ||
context = $.extend(true, res, this.extraContext || {}); | ||
ScrollResults.__super__.renderNext.call(this, res); | ||
@@ -104,0 +100,0 @@ return this.trigger("df:rendered", res); |
@@ -104,4 +104,7 @@ | ||
ScrollDisplay.prototype.renderNext = function(res) { | ||
var html; | ||
html = this.template(res); | ||
var context, html; | ||
context = $.extend(true, res, this.extraContext || {}); | ||
context.is_first = false; | ||
this.addHelpers(context); | ||
html = this.mustache.render(this.template, context); | ||
return $(this.container).append(html); | ||
@@ -108,0 +111,0 @@ }; |
{ | ||
"name": "doofinder", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"description": "Javascript Library for Doofinder Search API", | ||
@@ -10,6 +10,6 @@ "main": "lib/doofinder.js", | ||
"dependencies": { | ||
"handlebars": "^4.0.4", | ||
"http": "^0.0.0", | ||
"ion-rangeslider": "git://github.com/doofinder/ion.rangeSlider.git", | ||
"jquery": "^2.1.4", | ||
"mustache": "^2.2.1", | ||
"qs": "^4.0.0" | ||
@@ -27,6 +27,6 @@ }, | ||
"coffee-script": "^1.9.1", | ||
"grunt-browserify": "^4.0.1", | ||
"grunt-version": "^1.0.0", | ||
"mocha": "^2.2.1", | ||
"nock": "^5.2.1" | ||
"nock": "^5.2.1", | ||
"browserify": "8.1.3" | ||
}, | ||
@@ -33,0 +33,0 @@ "scripts": { |
@@ -261,3 +261,3 @@ # jsDoofinder (In progress) | ||
------ | ---- | -------------- | ||
template | `String` | [Handlebars](http://handlebarsjs.com) template to shape the results. | ||
template | `String` | [Mustache](http://mustache.github.io/) template to shape the results. | ||
templateVars | `Object` | Extra info you want to render in the template. Look at [Example 2](#example-2-adding-extra-info-to-our-template). | ||
@@ -304,3 +304,3 @@ templateFunctions | `Object` | Custom helpers to use in your template. Look at [Example 3](#example-3-create-a-custom-template-function). | ||
------ | ---- | -------------- | ||
template | `String` | [Handlebars](http://handlebarsjs.com) template to shape the results. | ||
template | `String` | [Mustache](http://mustache.github.io/) template to shape the results. | ||
templateVars | `Object` | Extra info you want to render in the template. Look at [Example 2](#example-2-adding-extra-info-to-our-template). | ||
@@ -361,3 +361,3 @@ templateFunctions | `Object` | Custom helpers to use in your template. Look at [Example 3](#example-3-create-a-custom-template-function). | ||
------ | ---- | -------------- | ||
template | `String` | [Handlebars](http://handlebarsjs.com) template to shape the results. | ||
template | `String` | [Mustache](http://mustache.github.io/) template to shape the results. | ||
templateVars | `Object` | Extra info you want to render in the template. Look at [Example 2](#example-2-adding-extra-info-to-our-template). | ||
@@ -408,3 +408,3 @@ templateFunctions | `Object` | Custom helpers to use in your template. Look at [Example 3](#example-3-create-a-custom-template-function). | ||
------ | ---- | -------------- | ||
template | `String` | [Handlebars](http://handlebarsjs.com) template to shape the results. | ||
template | `String` | [Mustache](http://mustache.github.io/) template to shape the results. | ||
templateVars | `Object` | Extra info you want to render in the template. Look at [Example 2](#example-2-adding-extra-info-to-our-template). | ||
@@ -464,7 +464,7 @@ templateFunctions | `Object` | Custom helpers to use in your template. Look at [Example 3](#example-3-create-a-custom-template-function). | ||
In the [Quick Start](#quick-start) example we composed a simple view using the Doofinder Library. Let's see how to shape the results by a Handlebars template. | ||
In the [Quick Start](#quick-start) example we composed a simple view using the Doofinder Library. Let's see how to shape the results by a Mustache template. | ||
```javascript | ||
var resultsTemplate = '{{#each results}}' + | ||
var resultsTemplate = '{{#results}}' + | ||
' <div>'+ | ||
@@ -486,3 +486,3 @@ ' <div>' + | ||
'</div>' + | ||
'{{/each}}'; | ||
'{{/results}}'; | ||
@@ -495,5 +495,5 @@ var resultsWidget = new doofinder.widgets.ScrollResults('#scroll', {template: resultsTemplate}); | ||
- We used `{{#each results}}` to iterate through the items. | ||
- We used `{{#results}}` to iterate through the items. | ||
- We used `{{field_name}}` tags to print the content of a field. | ||
- We used `{{#if filed_name}}` to check the presence of a field. | ||
- We used `{{#field_name}}` to check the presence of a field. | ||
- We used `{{#format-currency}}` helper to print the price with the coin symbol and formatted. | ||
@@ -504,4 +504,3 @@ - Note that we use a data attribute (data-df-hitcounter) to show the dfid. You can use this to send it with the [Controller.hit](#hit) method. | ||
Maybe you want to add more information to your template dynamically. Imagine you want to show prices lower that a given number, i.e. 100. | ||
You can add this variable to the template scope by the option templateVars. | ||
Maybe you have some items with no header and you want to show a standard one, just in case. | ||
@@ -512,3 +511,3 @@ ```javascript | ||
templateVars: { | ||
maxPrice: 100 | ||
standardTitle: "My title" | ||
} | ||
@@ -518,7 +517,7 @@ }); | ||
So you can modify your template in order to show just the prices lower than `maxPrice`. | ||
So you can modify your template in order to show a standard title if there's no header supplied by the item. | ||
```javascript | ||
var resultsTemplate = '{{#each results}}' + | ||
var resultsTemplate = '{{#results}}' + | ||
' <div>'+ | ||
@@ -531,9 +530,9 @@ ' <div>' + | ||
' <a target="_blank" data-df-hitcounter="{{dfid}}" href="{{href}}">'+ | ||
' {{#header}}' + | ||
' <div>{{header}}</div>' + | ||
' {{/header}}' + | ||
' {{^header}}' + | ||
' <div>{{standardTitle}}</div>' + | ||
' {{/header}}' + | ||
' <div>{{body}}</div>' + | ||
' {{#lt price ../maxPrice}}' | ||
' <div>' + | ||
' <span>{{#format-currency}}{{price}}{{/format-currency}}</span>' + | ||
' </div>' + | ||
' {{/lt}}' + | ||
' </a>' + | ||
@@ -543,3 +542,3 @@ ' </div>' + | ||
'</div>' + | ||
'{{/each}}'; | ||
'{{/results}}'; | ||
@@ -549,11 +548,10 @@ var resultsWidget = new doofinder.widgets.ScrollResults('#scroll', { | ||
templateVars: { | ||
maxPrice: 100 | ||
standardTitle: "My title" | ||
} | ||
}); | ||
``` | ||
And we have used the `{{#lt}}` helper to compare the `price` with the `maxPrice`. Note that we have written `../maxPrice` instead of `maxPrice`. This is because we are in the loop and we must access to a variable out of `results` array. For more information about Handlebars templates, [click here](http://handlebarsjs.com/). | ||
### Example 3: Create a custom template function. | ||
Template functions are called helpers in [Handlebars](http://handlebarsjs.com/). You can create and add custom helpers to your template by using the `templateFunctions` option. We'll create a helper that convert a text in bold. | ||
Template functions are called helpers in [Mustache](http://mustache.github.io/). You can create and add custom helpers to your template by using the `templateFunctions` option. We'll create a helper that convert a text in bold. | ||
@@ -564,4 +562,6 @@ ```javascript | ||
templateFunctions: { | ||
bold: function(options){ | ||
return "<b>" + options.fn(this) + "</b>" | ||
bold: function(){ | ||
function(text, render){ | ||
return "<b>" + render(text) + "</b>" | ||
} | ||
} | ||
@@ -571,3 +571,3 @@ } | ||
``` | ||
This helper will take the text in the tag and will wrapper it with the `<b>` tag. Note that `options.fn(this)` returns the text wrapped by the helper. | ||
This helper will take the text in the tag and will wrapper it with the `<b>` tag. Note that `render(text)` returns the text wrapped by the helper. | ||
@@ -577,3 +577,3 @@ We'll use the helper to show the header and instantiate the widget. | ||
```javascript | ||
var resultsTemplate = '{{#each results}}' + | ||
var resultsTemplate = '{{#results}}' + | ||
' <div>'+ | ||
@@ -595,3 +595,3 @@ ' <div>' + | ||
'</div>' + | ||
'{{/each}}'; | ||
'{{/results}}'; | ||
@@ -601,7 +601,9 @@ var resultsWidget = new doofinder.widgets.ScrollResults('#scroll', { | ||
templateFunctions: { | ||
bold: function(options){ | ||
return "<b>" + options.fn(this) + "</b>" | ||
bold: function(){ | ||
return function(text, render){ | ||
return "<b>" + render(text) + "</b>" | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
@@ -608,0 +610,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
599
86265
13
1880
+ Addedmustache@^2.2.1
+ Addedmustache@2.3.2(transitive)
- Removedhandlebars@^4.0.4
- Removedhandlebars@4.7.8(transitive)
- Removedminimist@1.2.8(transitive)
- Removedneo-async@2.6.2(transitive)
- Removedsource-map@0.6.1(transitive)
- Removeduglify-js@3.19.3(transitive)
- Removedwordwrap@1.0.0(transitive)